Module_PsuComm.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  1. #include "Module_PsuComm.h"
  2. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  3. #define PASS 1
  4. #define FAIL -1
  5. #define YES 1
  6. #define NO 0
  7. #define DERATING_COUNT 30
  8. #define DERATING_GAP 30
  9. #define ELEMENT_NOT_FIND 255
  10. #define CHK_VOL_RANGE 50
  11. #define CHK_CUR_RANGE 10
  12. #define DERATING_RANGE 100
  13. #define ZERO_CURRENT 10 // 該值須保持最小為 1A
  14. #define ZERO_VOLTAGE 50
  15. #define STOP_CURRENT 30
  16. #define PSU_MIN_CUR 1000
  17. #define PSU_MIN_VOL 1500
  18. #define PRE_CHARG_STEP_CUR 30
  19. #define PRE_CHARG_RANGE 50
  20. #define EQUAL 0
  21. #define CMD_DELAY_TIME 25000
  22. #define LOG_VOL_GAP 50
  23. #define LOG_CUR_GAP 5
  24. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  25. struct StatusCodeData *ShmStatusCodeData;
  26. struct PsuData *ShmPsuData;
  27. bool libInitialize = false;
  28. byte getAvailableCapOffset = 5;
  29. byte deratingKeepCount = 0;
  30. byte psuCmdSeq = _PSU_CMD_CAP;
  31. float chargingOutputLogInfo[2][4];
  32. void PRINTF_FUNC(char *string, ...);
  33. int StoreLogMsg(const char *fmt, ...);
  34. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  35. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  36. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  37. unsigned long GetTimeoutValue(struct timeval _sour_time);
  38. unsigned long GetTimeoutValue(struct timeval _sour_time)
  39. {
  40. struct timeval _end_time;
  41. gettimeofday(&_end_time, NULL);
  42. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  43. }
  44. int StoreLogMsg(const char *fmt, ...)
  45. {
  46. char Buf[4096+256];
  47. char buffer[4096];
  48. va_list args;
  49. struct timeb SeqEndTime;
  50. struct tm *tm;
  51. va_start(args, fmt);
  52. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  53. va_end(args);
  54. memset(Buf,0,sizeof(Buf));
  55. ftime(&SeqEndTime);
  56. SeqEndTime.time = time(NULL);
  57. tm=localtime(&SeqEndTime.time);
  58. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  59. {
  60. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  61. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  62. printf("%s \n", Buf);
  63. }
  64. else
  65. {
  66. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  67. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  68. buffer,
  69. tm->tm_year+1900,tm->tm_mon+1);
  70. system(Buf);
  71. }
  72. return rc;
  73. }
  74. void PRINTF_FUNC(char *string, ...)
  75. {
  76. va_list args;
  77. char buffer[4096];
  78. va_start(args, string);
  79. vsnprintf(buffer, sizeof(buffer), string, args);
  80. va_end(args);
  81. DEBUG_INFO("%s \n", buffer);
  82. }
  83. //=================================
  84. // Common routine
  85. //=================================
  86. size_t FindIndex(const int a[], size_t size, int value, byte group)
  87. {
  88. size_t index = 0;
  89. while ( index < size && a[index] != value ) ++index;
  90. return (index == size ? ELEMENT_NOT_FIND : group);
  91. }
  92. byte FindTargetGroup(byte address)
  93. {
  94. byte _group = ELEMENT_NOT_FIND;
  95. if (ShmPsuData->GroupCount == 1)
  96. _group = 0;
  97. else
  98. {
  99. _group = FindIndex(connector_1, ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity, address, 0);
  100. if (_group == ELEMENT_NOT_FIND)
  101. _group = FindIndex(connector_2, ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity, address, 1);
  102. }
  103. return _group;
  104. }
  105. bool IsOverModuleCount(byte count)
  106. {
  107. bool result = false;
  108. if (count >= ShmPsuData->SystemPresentPsuQuantity)
  109. result = true;
  110. return result;
  111. }
  112. //=================================
  113. // Save data to share memory Function
  114. //=================================
  115. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  116. {
  117. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  118. {
  119. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  120. {
  121. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  122. return true;
  123. }
  124. }
  125. for (byte index = 0; index < CCS_QUANTITY; index++)
  126. {
  127. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  128. {
  129. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  130. return true;
  131. }
  132. }
  133. for (byte index = 0; index < GB_QUANTITY; index++)
  134. {
  135. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  136. {
  137. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. //=================================
  144. // Alarm code mapping to share memory Function
  145. //=================================
  146. // 檢查 Byte 中某個 Bit 的值
  147. // _byte : 欲改變的 byte
  148. // _bit : 該 byte 的第幾個 bit
  149. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  150. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  151. {
  152. return ( _byte & mask_table[_bit] ) != 0x00;
  153. }
  154. void AbnormalStopAnalysis(byte gun_index, int errCode)
  155. {
  156. for (char i = 0; i < 3; i++)
  157. {
  158. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  159. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  160. {
  161. if(DetectBitValue(byteIndex , bitIndex) == 1)
  162. {
  163. switch(byteIndex)
  164. {
  165. case 0:
  166. {
  167. if (bitIndex == 0)
  168. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  169. else if (bitIndex == 5)
  170. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  171. }
  172. break;
  173. case 1:
  174. {
  175. if (bitIndex == 1)
  176. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  177. else if (bitIndex == 2)
  178. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = YES;
  179. else if (bitIndex == 3)
  180. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  181. else if (bitIndex == 4)
  182. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  183. else if (bitIndex == 5)
  184. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  185. }
  186. break;
  187. case 2:
  188. {
  189. if (bitIndex == 0)
  190. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  191. else if (bitIndex == 1)
  192. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  193. else if (bitIndex == 2)
  194. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = YES;
  195. else if (bitIndex == 3)
  196. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  197. else if (bitIndex == 4)
  198. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  199. else if (bitIndex == 5)
  200. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = YES;
  201. else if (bitIndex == 6)
  202. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  203. }
  204. break;
  205. }
  206. }
  207. // else
  208. // {
  209. // switch (byteIndex) {
  210. // case 0: {
  211. // if (bitIndex == 0)
  212. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  213. // else if (bitIndex == 5)
  214. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  215. // }
  216. // break;
  217. // case 1: {
  218. // if (bitIndex == 1)
  219. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  220. // else if (bitIndex == 2)
  221. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  222. // else if (bitIndex == 3)
  223. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  224. // else if (bitIndex == 4)
  225. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  226. // else if (bitIndex == 5)
  227. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  228. // }
  229. // break;
  230. // case 2: {
  231. // if (bitIndex == 1)
  232. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  233. // if (bitIndex == 2)
  234. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  235. // else if (bitIndex == 3)
  236. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  237. // else if (bitIndex == 4)
  238. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  239. // else if (bitIndex == 5)
  240. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  241. // else if (bitIndex == 6)
  242. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  243. // }
  244. // break;
  245. // }
  246. // }
  247. }
  248. }
  249. }
  250. //=================================
  251. // Callback Function
  252. //=================================
  253. // no using -- GetOutputAndTempCallback
  254. void GetStatusCallback(byte group, byte SN, byte temp, int alarm)
  255. {
  256. bool isFind = false;
  257. if ((conn_1_count + conn_2_count) != ShmPsuData->SystemPresentPsuQuantity)
  258. {
  259. if (group == 0)
  260. {
  261. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  262. {
  263. if (connector_1[psuIndex] == SN)
  264. {
  265. isFind = true;
  266. break;
  267. }
  268. }
  269. if(!isFind)
  270. {
  271. connector_1[conn_1_count] = SN;
  272. conn_1_count++;
  273. }
  274. }
  275. else if (group == 1)
  276. {
  277. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  278. {
  279. if (connector_2[psuIndex] == SN)
  280. {
  281. isFind = true;
  282. break;
  283. }
  284. }
  285. if(!isFind)
  286. {
  287. connector_2[conn_2_count] = SN;
  288. conn_2_count++;
  289. }
  290. }
  291. }
  292. if ((conn_1_count + conn_2_count) == ShmPsuData->SystemPresentPsuQuantity)
  293. {
  294. // Arrangment
  295. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  296. {
  297. connector_1[psuIndex] = psuIndex;
  298. }
  299. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  300. {
  301. connector_2[psuIndex] = conn_1_count + psuIndex;
  302. }
  303. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  304. PRINTF_FUNC("Connector 1 - PSU Number = %d \n", connector_1[psuIndex]);
  305. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  306. PRINTF_FUNC("Connector 2 - PSU Number = %d \n", connector_2[psuIndex]);
  307. }
  308. }
  309. // no using -- GetOutputAndTempCallback End
  310. void GetModuleCountCallback(byte group, byte count)
  311. {
  312. if (group == SYSTEM_CMD)
  313. ShmPsuData->SystemPresentPsuQuantity = count;
  314. else
  315. {
  316. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  317. }
  318. }
  319. void GetMaxPowerAndCur(unsigned char mode, int ratingCur, int *pow, int *cur)
  320. {
  321. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  322. return;
  323. //unsigned short maxCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  324. unsigned short maxCurrent = ShmPsuData->SystemAvailableCurrent;
  325. //unsigned short maxPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  326. unsigned short maxPower = ShmPsuData->SystemAvailablePower;
  327. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10 != 0 &&
  328. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10 < maxCurrent)
  329. maxCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  330. if (ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10 != 0 &&
  331. ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10 < maxPower)
  332. maxPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  333. if (mode == _MAIN_CHARGING_MODE_AVER)
  334. {
  335. maxCurrent /= 2;
  336. maxPower /= 2;
  337. }
  338. if (maxPower != 0 && maxPower <= *pow)
  339. *pow = maxPower;
  340. if (maxCurrent != 0 && maxCurrent <= *cur)
  341. *cur = maxCurrent;
  342. if (ratingCur != 0 && ratingCur <= *cur)
  343. *cur = ratingCur;
  344. }
  345. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  346. {
  347. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  348. return;
  349. int _groupPower = 0, _groupCurrent = 0;
  350. byte group = FindTargetGroup(address);
  351. float _chargingVol = 0, _targetVol = 0;
  352. if (group == 1)
  353. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  354. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  355. {
  356. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  357. {
  358. if (chargingInfo[groupIndex]->EvBatteryMaxVoltage > 0)
  359. {
  360. _chargingVol = chargingInfo[groupIndex]->EvBatteryMaxVoltage;
  361. _targetVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  362. break;
  363. }
  364. }
  365. }
  366. // PRINTF_FUNC("group = %d, DeratingChargingCurrent = %f, RealRatingPower = %d \n",
  367. // group, chargingInfo[group]->DeratingChargingCurrent, chargingInfo[group]->RealRatingPower);
  368. if (chargingInfo[group]->DeratingChargingCurrent == 0)
  369. {
  370. // 在還沒取得真正可輸出的電流前,依照 GFD 階段得到的真正 POWER / 模塊個數 / 車子電池最大電壓
  371. if (ShmPsuData->PsuGroup[group].GroupRealOutputPower > 0 && _chargingVol > 0)
  372. {
  373. // printf("GroupRealOutputPower = %d, GroupPresentPsuQuantity = %d\n",
  374. // ShmPsuData->PsuGroup[group].GroupRealOutputPower,
  375. // ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity);
  376. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent =
  377. ((ShmPsuData->PsuGroup[group].GroupRealOutputPower / ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity) * 1000 / (int)_chargingVol) * 10;
  378. // PRINTF_FUNC(" *1* AvailableCurrent = %d \n",
  379. // ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent);
  380. }
  381. else
  382. {
  383. // 注一 : 獲取模塊最大輸出能力 (忽視 Derating 狀態),所以這邊需要限制實際可輸出的電流
  384. if (ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent <= 0)
  385. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = PSU_MIN_CUR;
  386. // PRINTF_FUNC(" *2* group = %d, AvailableCurrent = %d \n",
  387. // group, ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent);
  388. }
  389. // PRINTF_FUNC("group = %d, address = %d, AvailableCurrent = %d \n",
  390. // group, address, ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent);
  391. }
  392. else
  393. {
  394. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = maxCur;
  395. }
  396. ShmPsuData->PsuGroup[group].PsuModule[address].AvailablePower = totalPow;
  397. // 總和該 Group 的可輸出電流
  398. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  399. {
  400. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[index].AvailableCurrent;
  401. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[index].AvailablePower;
  402. }
  403. // 各群得到最大輸出能力 (電流、Power)
  404. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  405. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  406. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  407. int _power = 0, _current = 0, _ratingcurrent = 0, _sysRealPower = 0;
  408. bool isGetAllDeratingCurrent = true;
  409. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  410. {
  411. _power += ShmPsuData->PsuGroup[index].GroupAvailablePower;
  412. _current += ShmPsuData->PsuGroup[index].GroupAvailableCurrent;
  413. _ratingcurrent += chargingInfo[index]->DeratingChargingCurrent;
  414. _sysRealPower += ShmPsuData->PsuGroup[index].GroupRealOutputPower;
  415. if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage >= PSU_MIN_VOL &&
  416. chargingInfo[index]->DeratingChargingCurrent == 0)
  417. isGetAllDeratingCurrent = false;
  418. }
  419. // 如果不是所有群都得到 Derating current,則先不採樣該次的 ratingCurrent
  420. if (!isGetAllDeratingCurrent) _ratingcurrent = 0;
  421. // 因應注一,為避免一值改變通知車子電樁最大可輸出電流所做的限制
  422. // 而因為 rating value 一般都會小於模塊的最大可輸出電流
  423. // 所以如果該值大於在注一所限制的輸出電流,則以此值為主
  424. if (_ratingcurrent != 0) _current = _ratingcurrent;
  425. //printf("=============== _current ==================== %d \n", _current);
  426. //printf("=============== _ratingcurrent ==================== %d \n", _ratingcurrent);
  427. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  428. {
  429. ShmPsuData->SystemAvailableCurrent = _current;
  430. ShmPsuData->SystemAvailablePower = _power;
  431. }
  432. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER ||
  433. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  434. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  435. {
  436. int halfPow = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  437. int halfCur = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  438. int ratingCur = chargingInfo[group]->DeratingChargingCurrent;
  439. int gpRealPow = ShmPsuData->PsuGroup[group].GroupRealOutputPower;
  440. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  441. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  442. {
  443. if (chargingInfo[group]->DividChargingCurrent == 0)
  444. return;
  445. else
  446. {
  447. halfCur = chargingInfo[group]->DividChargingCurrent;
  448. ratingCur = 0;
  449. }
  450. }
  451. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_AVER, ratingCur, &halfPow, &halfCur);
  452. // if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  453. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  454. // {
  455. // chargingInfo[group]->AvailableChargingCurrent = DERATING_RANGE;
  456. // chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  457. // }
  458. // else
  459. {
  460. // 以下狀況 -> 槍資訊中的最大輸出能力,為該群的輸出能力
  461. // 1. 如不是最大充
  462. // 2. 智能切換成均充過程
  463. chargingInfo[group]->AvailableChargingCurrent = halfCur;
  464. chargingInfo[group]->AvailableChargingPower = halfPow;
  465. chargingInfo[group]->RealRatingPower = gpRealPow;
  466. if(chargingInfo[group]->DeratingChargingCurrent > 0)
  467. {
  468. unsigned short _powBuf = 0;
  469. _powBuf = ((chargingInfo[group]->DeratingChargingCurrent / 10) * ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10) / 1000; // 單位是 KW
  470. if (_powBuf > ShmPsuData->PsuGroup[group].GroupRealOutputPower ||
  471. chargingInfo[group]->EvBatterytargetVoltage > 0)
  472. {
  473. ShmPsuData->PsuGroup[group].GroupRealOutputPower = _powBuf;
  474. // PRINTF_FUNC("group = %d, DeratingChargingCurrent = %f, RealRatingPower = %d \n",
  475. // group, chargingInfo[group]->DeratingChargingCurrent, chargingInfo[group]->RealRatingPower);
  476. }
  477. }
  478. //printf("(Aver.) RealRatingPower = %d \n", chargingInfo[group]->RealRatingPower);
  479. }
  480. }
  481. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  482. {
  483. //PRINTF_FUNC("group = %d, Final = %d \n", group, _current);
  484. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_MAX, _ratingcurrent, &_power, &_current);
  485. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  486. {
  487. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  488. {
  489. chargingInfo[count]->MaximumChargingVoltage = maxVol;
  490. chargingInfo[count]->AvailableChargingCurrent = _current;
  491. chargingInfo[count]->AvailableChargingPower = _power;
  492. chargingInfo[count]->RealRatingPower = _sysRealPower;
  493. }
  494. }
  495. else
  496. {
  497. // 如果是最大充,該槍資訊中的輸出能力為各群輸出能力的和
  498. chargingInfo[group]->AvailableChargingCurrent = _current;
  499. chargingInfo[group]->AvailableChargingPower = _power;
  500. chargingInfo[group]->RealRatingPower = _sysRealPower;
  501. }
  502. if(chargingInfo[group]->DeratingChargingCurrent > 0)
  503. {
  504. unsigned short _powBuf = 0;
  505. _powBuf = ((chargingInfo[group]->DeratingChargingCurrent / 10) * ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10) / 1000; // 單位是 KW
  506. if (_powBuf > ShmPsuData->PsuGroup[group].GroupRealOutputPower ||
  507. chargingInfo[group]->EvBatterytargetVoltage > 0 ||
  508. _targetVol > 0)
  509. {
  510. ShmPsuData->PsuGroup[group].GroupRealOutputPower = _powBuf;
  511. }
  512. }
  513. }
  514. }
  515. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  516. {
  517. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  518. return;
  519. if (IsOverModuleCount(address))
  520. return;
  521. byte group = FindTargetGroup(address);
  522. sprintf((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  523. sprintf((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, "PFC %d.%02d", (pfcSwVer & 0xFF00) >> 8, pfcSwVer & 0xFF);
  524. if (group == 1)
  525. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  526. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  527. //DEBUG_INFO("fw Ver. = %s \n", ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion);
  528. }
  529. // no using -- GetInputVoltageCallback
  530. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  531. {
  532. // if (ShmPsuData->Work_Step < GET_SYS_CAP)
  533. // return;
  534. //
  535. // if (IsOverModuleCount(address))
  536. // return;
  537. //
  538. // byte group = FindTargetGroup(address);
  539. //
  540. // if (group == 1)
  541. // address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  542. //
  543. // ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = vol1;
  544. // ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = vol2;
  545. // ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = vol3;
  546. //
  547. // PRINTF_FUNC("***Input*** address = %d, R = %d, S = %d, T = %d, gp = %d \n",
  548. // address, vol1, vol2, vol3, group);
  549. }
  550. // no using -- GetInputVoltageCallback End
  551. // no using -- GetOutputAndTempCallback
  552. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  553. {
  554. // if (ShmPsuData->Work_Step < GET_SYS_CAP)
  555. // return;
  556. //if (outCur != ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent)
  557. //{
  558. // PRINTF_FUNC("Gp_%d, gp output cur = %d \n", group, outCur);
  559. //}
  560. // // PSU Group - 電壓
  561. // ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outVol;
  562. // // PSU Group - 電流
  563. // ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outCur;
  564. //
  565. // if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  566. // (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  567. // (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  568. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  569. // )
  570. // {
  571. // unsigned short outputVol = 0;
  572. // unsigned short outputCur = 0;
  573. //
  574. // for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  575. // {
  576. // bool needtoAdd = true;
  577. //
  578. // if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  579. // outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  580. //
  581. // if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  582. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  583. // {
  584. //// PRINTF_FUNC("Gp_%d, DividChargingCurrent = %d \n", index,
  585. //// chargingInfo[index]->DividChargingCurrent);
  586. // if (chargingInfo[index]->DividChargingCurrent == 0)
  587. // needtoAdd = false;
  588. // }
  589. //
  590. // if (needtoAdd)
  591. // outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  592. // }
  593. //
  594. // // 黑白機
  595. // if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  596. // {
  597. // for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  598. // {
  599. // float _vol_buf = outputVol;
  600. // float _cur_buf = outputCur;
  601. //
  602. // // EVSE - 電壓
  603. // _vol_buf /= 10;
  604. // chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  605. // // EVSE - 電流
  606. // _cur_buf /= 10;
  607. // chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  608. // }
  609. // }
  610. //
  611. // if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  612. // (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  613. // {
  614. // float _vol_buf = outputVol;
  615. // float _cur_buf = outputCur;
  616. //
  617. // // EVSE - 電壓
  618. // _vol_buf /= 10;
  619. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  620. // // EVSE - 電流
  621. // _cur_buf /= 10;
  622. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  623. // }
  624. // }
  625. // else
  626. // {
  627. // float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  628. // float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  629. //
  630. // // EVSE - 電壓
  631. // _vol_buf /= 10;
  632. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  633. // // EVSE - 電流
  634. // _cur_buf /= 10;
  635. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  636. // }
  637. //
  638. // PRINTF_FUNC("Gun_%d, PresentChargingVoltage = %f, PresentChargingCurrent = %f \n", group,
  639. // chargingInfo[group]->PresentChargingVoltage,
  640. // chargingInfo[group]->PresentChargingCurrent);
  641. }
  642. // no using -- GetOutputAndTempCallback End
  643. void GetMisCallback(byte address, unsigned int value, byte type)
  644. {
  645. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  646. return;
  647. if (IsOverModuleCount(address))
  648. return;
  649. byte group = FindTargetGroup(address);
  650. if (group == 1)
  651. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  652. if (type == 1)
  653. {
  654. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_1 = value;
  655. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_2 = value;
  656. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_3 = value;
  657. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_4 = value;
  658. }
  659. else if (type == 2)
  660. {
  661. //printf("DC - group = %d, index = %d, value = %d \n", group, address, value);
  662. // ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp1 = value;
  663. // ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp2 = value;
  664. // ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp3 = value;
  665. ShmPsuData->PsuGroup[group].PsuModule[address].ExletTemp = value;
  666. }
  667. else if (type == 3)
  668. {
  669. printf("PFC - group = %d, index = %d, value = %d \n", group, address, value);
  670. }
  671. }
  672. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  673. {
  674. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  675. return;
  676. if (IsOverModuleCount(address))
  677. return;
  678. // 經度 0.1
  679. //ShmPsuData->PsuGroup[address].GroupTargetOutputVoltage = Vext;
  680. //PRINTF_FUNC("address = %d, Iavail = %d \n", address, Iavail);
  681. byte group = FindTargetGroup(address);
  682. if (group == 1)
  683. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  684. //PRINTF_FUNC("group = %d, address_%d, Iavail = %d \n", group, address, Iavail);
  685. ShmPsuData->PsuGroup[group].PsuModule[address].IAvailableCurrent = Iavail;
  686. bool isPass = true;
  687. int totalCur = 0;
  688. byte sampleCount = 8;
  689. if (Iavail == 0)
  690. {
  691. for (byte count = 0; count < sampleCount; count++)
  692. {
  693. chargingInfo[group]->SampleChargingCur[count] = Iavail;
  694. }
  695. }
  696. else
  697. {
  698. // 該群的可輸出電流
  699. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  700. {
  701. totalCur += ShmPsuData->PsuGroup[group].PsuModule[index].IAvailableCurrent;
  702. }
  703. for (byte count = 0; count < sampleCount; count++)
  704. {
  705. if (chargingInfo[group]->SampleChargingCur[count] == 0)
  706. {
  707. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  708. return;
  709. }
  710. else
  711. {
  712. if (chargingInfo[group]->SampleChargingCur[count] != totalCur)
  713. {
  714. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  715. isPass = false;
  716. break;
  717. }
  718. }
  719. }
  720. }
  721. if (isPass)
  722. {
  723. // if (totalCur != 0)
  724. // PRINTF_FUNC("group = %d, rating pass value = %d \n", group, totalCur);
  725. chargingInfo[group]->DeratingChargingCurrent = totalCur;
  726. }
  727. }
  728. void GetPresentOutputFCallback(byte group, float outVol, float outCur)
  729. {
  730. // isinf : -1 = 負無窮,1 = 正無窮,0 = 其他
  731. if (isinf(outVol) == 0)
  732. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = (unsigned short)(outVol * 10);
  733. else
  734. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = 0;
  735. if (isinf(outCur) == 0)
  736. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = (unsigned short)(outCur * 10);
  737. else
  738. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = 0;
  739. //printf("group = %d, Current = %d \n", group, ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent);
  740. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  741. (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  742. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  743. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  744. )
  745. {
  746. unsigned short outputVol = 0;
  747. unsigned short outputCur = 0;
  748. unsigned char _maxTOaver = 0;
  749. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  750. {
  751. bool needtoAdd = true;
  752. if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  753. outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  754. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  755. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  756. {
  757. if (chargingInfo[index]->DividChargingCurrent == 0)
  758. needtoAdd = false;
  759. else
  760. _maxTOaver = index;
  761. }
  762. if (needtoAdd)
  763. outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  764. }
  765. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  766. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  767. {
  768. if (chargingInfo[_maxTOaver]->DividChargingCurrent != 0)
  769. {
  770. float _cur_buf = outputCur;
  771. _cur_buf /= 10;
  772. chargingInfo[_maxTOaver]->PresentChargingCurrent = _cur_buf;
  773. }
  774. }
  775. // 黑白機
  776. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  777. {
  778. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  779. {
  780. float _vol_buf = outputVol;
  781. float _cur_buf = outputCur;
  782. // EVSE - 電壓
  783. _vol_buf /= 10;
  784. chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  785. _cur_buf /= 10;
  786. chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  787. }
  788. }
  789. if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  790. (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  791. chargingInfo[group]->SystemStatus == S_ALARM)
  792. {
  793. float _vol_buf = outputVol;
  794. float _cur_buf = outputCur;
  795. // EVSE - 電壓
  796. _vol_buf /= 10;
  797. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  798. _cur_buf /= 10;
  799. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  800. }
  801. }
  802. else
  803. {
  804. float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  805. float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  806. // EVSE - 電壓
  807. _vol_buf /= 10;
  808. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  809. _cur_buf /= 10;
  810. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  811. }
  812. }
  813. //==========================================
  814. // 特規用指令
  815. //==========================================
  816. void GetOutputAndTempCallback(byte address, unsigned short outputVol_s,
  817. unsigned short outputCur_s, unsigned short outputPower, unsigned char Temperature)
  818. {
  819. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  820. return;
  821. //unsigned short outVol = outputVol_s;
  822. //unsigned short outCur = outputCur_s;
  823. if (IsOverModuleCount(address))
  824. return;
  825. byte group = FindTargetGroup(address);
  826. if (group == 1)
  827. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  828. // // PSU Group - 電壓
  829. // ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outVol;
  830. // // PSU Group - 電流
  831. // ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outCur;
  832. // // PSU Group - 能量
  833. // ShmPsuData->PsuGroup[group].GroupPresentOutputPower = outVol * outCur;
  834. //
  835. // if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  836. // (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  837. // (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  838. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  839. // )
  840. // {
  841. // unsigned short outputVol = 0;
  842. // unsigned short outputCur = 0;
  843. // unsigned char _maxTOaver = 0;
  844. //
  845. // for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  846. // {
  847. // bool needtoAdd = true;
  848. //
  849. // if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  850. // outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  851. //
  852. //// if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  853. //// ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  854. //// {
  855. //// if (chargingInfo[index]->DividChargingCurrent == 0)
  856. //// needtoAdd = false;
  857. //// else
  858. //// _maxTOaver = index;
  859. //// }
  860. //
  861. // if (needtoAdd)
  862. // outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  863. // }
  864. //
  865. // if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  866. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  867. // {
  868. // if (chargingInfo[_maxTOaver]->DividChargingCurrent != 0)
  869. // {
  870. // float _cur_buf = outputCur;
  871. // _cur_buf /= 10;
  872. // chargingInfo[_maxTOaver]->PresentChargingCurrent = _cur_buf;
  873. // }
  874. // }
  875. //
  876. // // 黑白機
  877. // if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  878. // {
  879. // for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  880. // {
  881. // float _vol_buf = outputVol;
  882. // float _cur_buf = outputCur;
  883. //
  884. // // EVSE - 電壓
  885. // _vol_buf /= 10;
  886. // chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  887. //
  888. // _cur_buf /= 10;
  889. // chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  890. // }
  891. // }
  892. //
  893. // if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  894. // (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  895. // {
  896. // float _vol_buf = outputVol;
  897. // float _cur_buf = outputCur;
  898. //
  899. // // EVSE - 電壓
  900. // _vol_buf /= 10;
  901. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  902. //
  903. // _cur_buf /= 10;
  904. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  905. // }
  906. // }
  907. // else
  908. // {
  909. // float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  910. // float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  911. //
  912. // // EVSE - 電壓
  913. // _vol_buf /= 10;
  914. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  915. //
  916. // _cur_buf /= 10;
  917. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  918. // }
  919. ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp1 = Temperature;
  920. ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp2 = Temperature;
  921. ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp3 = Temperature;
  922. // ShmPsuData->PsuGroup[group].PsuModule[address].ExletTemp = Temperature;
  923. // PRINTF_FUNC("***Output Value and Temp*** group = %d, Vol = %d, Cur = %d \n",
  924. // group, outputVol_s, outputCur_s);
  925. }
  926. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  927. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  928. {
  929. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  930. return;
  931. if (IsOverModuleCount(address))
  932. return;
  933. byte group1 = FindTargetGroup(address);
  934. if (group1 == 1)
  935. address -= ShmPsuData->PsuGroup[group1 - 1].GroupPresentPsuQuantity;
  936. int alarm = (err2 << 24) | (err3 << 16) | (err4 << 8);
  937. ShmPsuData->PsuGroup[group1].PsuModule[address].AlarmCode = alarm;
  938. AbnormalStopAnalysis(group1, alarm);
  939. }
  940. void GetModuleInputCallback(byte address, unsigned short inputR,
  941. unsigned short inputS, unsigned short inputT)
  942. {
  943. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  944. return;
  945. if (IsOverModuleCount(address))
  946. return;
  947. byte group = FindTargetGroup(address);
  948. if (group == 1)
  949. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  950. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = inputR;
  951. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = inputS;
  952. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = inputT;
  953. //PRINTF_FUNC("***Input*** address = %d, R = %d, S = %d, T = %d \n",
  954. // address, inputR, inputS, inputT);
  955. }
  956. //==========================================
  957. // Init all share memory
  958. //==========================================
  959. int InitShareMemory()
  960. {
  961. int result = PASS;
  962. int MeterSMId;
  963. //creat ShmSysConfigAndInfo
  964. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  965. {
  966. #ifdef SystemLogMessage
  967. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG %d \n");
  968. #endif
  969. result = FAIL;
  970. }
  971. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  972. {
  973. #ifdef SystemLogMessage
  974. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG \n");
  975. #endif
  976. result = FAIL;
  977. }
  978. else
  979. {}
  980. //creat ShmStatusCodeData
  981. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  982. {
  983. #ifdef SystemLogMessage
  984. DEBUG_ERROR("shmget ShmStatusCodeData NG \n");
  985. #endif
  986. result = FAIL;
  987. }
  988. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  989. {
  990. #ifdef SystemLogMessage
  991. DEBUG_ERROR("shmat ShmStatusCodeData NG \n");
  992. #endif
  993. result = FAIL;
  994. }
  995. else
  996. {}
  997. //creat ShmPsuData
  998. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  999. {
  1000. #ifdef SystemLogMessage
  1001. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1002. #endif
  1003. result = FAIL;
  1004. }
  1005. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1006. {
  1007. #ifdef SystemLogMessage
  1008. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1009. #endif
  1010. result = FAIL;
  1011. }
  1012. memset(ShmPsuData,0,sizeof(struct PsuData));
  1013. return result;
  1014. }
  1015. //================================================
  1016. // Log function
  1017. //================================================
  1018. void OutputChargingLogFuncion(byte groupIndex)
  1019. {
  1020. // 列印時機 : 需求改變或輸出電壓與紀錄落差超過 5V 或者輸出電流與紀錄落差超過 0.5A
  1021. if (chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_VOL] != chargingInfo[groupIndex]->EvBatterytargetVoltage * 10 ||
  1022. (chargingInfo[groupIndex]->FireChargingVoltage <= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] - LOG_VOL_GAP ||
  1023. chargingInfo[groupIndex]->FireChargingVoltage >= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] + LOG_VOL_GAP))
  1024. {
  1025. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_VOL] = chargingInfo[groupIndex]->EvBatterytargetVoltage * 10;
  1026. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] = chargingInfo[groupIndex]->FireChargingVoltage;
  1027. PRINTF_FUNC("Conn %d, EV Req Voltage : %.1f, EVSE Output Voltage = %.1f \n", groupIndex,
  1028. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_VOL] / 10,
  1029. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] / 10);
  1030. }
  1031. if (chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_CUR] != chargingInfo[groupIndex]->EvBatterytargetCurrent * 10 ||
  1032. (chargingInfo[groupIndex]->PresentChargingCurrent <= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR] - LOG_CUR_GAP ||
  1033. chargingInfo[groupIndex]->PresentChargingCurrent >= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR] + LOG_CUR_GAP))
  1034. {
  1035. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_CUR] = chargingInfo[groupIndex]->EvBatterytargetCurrent * 10;
  1036. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR] = chargingInfo[groupIndex]->PresentChargingCurrent;
  1037. PRINTF_FUNC("Conn %d, EV Req Current : %.1f, EVSE Output Current = %.1f \n", groupIndex,
  1038. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_CUR] / 10,
  1039. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR]);
  1040. }
  1041. }
  1042. //================================================
  1043. // Main process
  1044. //================================================
  1045. void InitialPsuData()
  1046. {
  1047. ShmPsuData->SystemPresentPsuQuantity = 0;
  1048. PRINTF_FUNC("InitialPsuData : PSU Group = %d \n", ShmPsuData->GroupCount);
  1049. for (byte _groupCount = 0; _groupCount < ShmPsuData->GroupCount; _groupCount++)
  1050. {
  1051. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  1052. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  1053. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  1054. chargingInfo[_groupCount]->PresentChargingVoltage = 0;
  1055. chargingInfo[_groupCount]->PresentChargingCurrent = 0;
  1056. }
  1057. }
  1058. void Initialization()
  1059. {
  1060. bool isPass = false;
  1061. while(!isPass)
  1062. {
  1063. isPass = true;
  1064. for (byte _index = 0; _index < _gunCount; _index++)
  1065. {
  1066. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  1067. {
  1068. DEBUG_ERROR("Module_PsuComm : FindChargingInfoData false \n");
  1069. isPass = false;
  1070. break;
  1071. }
  1072. }
  1073. sleep(1);
  1074. }
  1075. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  1076. ShmPsuData->GroupCount = 1;
  1077. else
  1078. ShmPsuData->GroupCount = _gunCount;
  1079. ShmPsuData->SystemAvailableCurrent = 0;
  1080. ShmPsuData->SystemAvailablePower = 0;
  1081. }
  1082. void CheckSmartChargingStep(bool isWaitingAver, bool isCharging, bool canAverageCharging)
  1083. {
  1084. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  1085. {
  1086. if (isWaitingAver)
  1087. {
  1088. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1089. {
  1090. ShmSysConfigAndInfo->SysInfo.CanAverageCharging = canAverageCharging;
  1091. if (canAverageCharging)
  1092. {
  1093. PRINTF_FUNC("======= Only to get the charging side capacity (Step 2) ======= \n");
  1094. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  1095. }
  1096. else
  1097. {
  1098. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  1099. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1100. }
  1101. }
  1102. else
  1103. {
  1104. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  1105. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1106. }
  1107. }
  1108. }
  1109. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  1110. {
  1111. if (isCharging)
  1112. {
  1113. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1114. {
  1115. PRINTF_FUNC("======= To raise voltage of idle module to charing voltage (Step 12) ======= \n");
  1116. preChargingCur = preChargingTarget = 0;
  1117. gettimeofday(&_max_time, NULL);
  1118. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  1119. }
  1120. else
  1121. {
  1122. PRINTF_FUNC("======= The Change to maximum charge mode is complete. (Step 15) ======= \n");
  1123. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1124. }
  1125. }
  1126. else
  1127. {
  1128. PRINTF_FUNC("======= The Change to maximum charge mode is complete. (Step 15) ======= \n");
  1129. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1130. }
  1131. }
  1132. }
  1133. void PreCheckSmartChargingStep()
  1134. {
  1135. isCharging = false;
  1136. isWaitingAver = false;
  1137. isReadToCharging = false;
  1138. CanAverageCharging = true;
  1139. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1140. {
  1141. // Pooling Status
  1142. //GetStatus(index);
  1143. // 取得模塊輸出額定電流能力
  1144. //GetModuleIavailable(index);
  1145. if (chargingInfo[index]->SystemStatus == S_CHARGING)
  1146. {
  1147. isCharging = true;
  1148. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  1149. {
  1150. if (toAverVolPoint == (chargingInfo[index]->EvBatterytargetCurrent * 10))
  1151. {
  1152. // 欲最大充 -> 均充需要等待充電中的輸出電流拉高到目標電流
  1153. if ((chargingInfo[index]->PresentChargingCurrent * 10) >=
  1154. (chargingInfo[index]->EvBatterytargetCurrent * 10) - CHK_CUR_RANGE)
  1155. {
  1156. if (toAverVolCount == 0)
  1157. isWaitingAver = true;
  1158. else
  1159. toAverVolCount--;
  1160. }
  1161. }
  1162. else
  1163. {
  1164. toAverVolPoint = (chargingInfo[index]->EvBatterytargetCurrent * 10);
  1165. toAverVolCount = 3;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. toAverVolPoint = 0;
  1171. toAverVolCount = 3;
  1172. }
  1173. }
  1174. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  1175. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1176. {
  1177. isReadToCharging = true;
  1178. }
  1179. if (chargingInfo[index]->DeratingChargingCurrent < STOP_CURRENT)
  1180. {
  1181. CanAverageCharging = false;
  1182. }
  1183. }
  1184. CheckSmartChargingStep(isWaitingAver, isCharging, CanAverageCharging);
  1185. }
  1186. void Await()
  1187. {
  1188. usleep(CMD_DELAY_TIME);
  1189. }
  1190. int main(void)
  1191. {
  1192. if(InitShareMemory() == FAIL)
  1193. {
  1194. #ifdef SystemLogMessage
  1195. DEBUG_ERROR("InitShareMemory NG\n");
  1196. #endif
  1197. if(ShmStatusCodeData != NULL)
  1198. {
  1199. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  1200. }
  1201. sleep(5);
  1202. return 0;
  1203. }
  1204. PRINTF_FUNC("InitShareMemory OK\n");
  1205. // register callback function
  1206. RefreshStatus(&GetStatusCallback);
  1207. RefreshModuleCount(&GetModuleCountCallback);
  1208. RefreshAvailableCap(&GetAvailableCapCallback);
  1209. RefreshFwVersion(&GetFwCallback);
  1210. RefreshInputVol(&GetInputVoltageCallback);
  1211. RefreshGetOutput(&GetPresentOutputCallback);
  1212. RefreshMisInfo(&GetMisCallback);
  1213. RefreshIavailable(&GetIavailableCallback);
  1214. RefreshGetOutputF(&GetPresentOutputFCallback);
  1215. // GetPresentOutputCallback & GetStatusCallback
  1216. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  1217. // GetStatusCallback
  1218. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  1219. // GetInputVoltageCallback
  1220. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  1221. sleep(2);
  1222. _gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1223. // initial object
  1224. InitialPsuData();
  1225. Initialization();
  1226. libInitialize = InitialCommunication();
  1227. byte isInitialComp = NO;
  1228. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  1229. {
  1230. PRINTF_FUNC("Alter native mode. \n");
  1231. }
  1232. //main loop
  1233. while (libInitialize)
  1234. {
  1235. // 斷電狀態
  1236. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1237. {
  1238. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  1239. if (!isInitialComp)
  1240. {
  1241. ShmPsuData->Work_Step = INITIAL_START;
  1242. psuCmdSeq = _PSU_CMD_STATUS;
  1243. sleep(2);
  1244. InitialPsuData();
  1245. isInitialComp = YES;
  1246. }
  1247. sleep(1);
  1248. continue;
  1249. }
  1250. else
  1251. isInitialComp = NO;
  1252. // 自檢失敗
  1253. if (ShmPsuData->Work_Step == _NO_WORKING)
  1254. {
  1255. PRINTF_FUNC("== PSU == self test fail. \n");
  1256. sleep(5);
  1257. }
  1258. switch(ShmPsuData->Work_Step)
  1259. {
  1260. case INITIAL_START:
  1261. {
  1262. PRINTF_FUNC("== PSU == INITIAL_START \n");
  1263. gettimeofday(&_cmdSubPriority_time, NULL);
  1264. sleep(5);
  1265. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1266. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  1267. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1268. isStartOutputSwitch[index] = false;
  1269. ShmPsuData->Work_Step = GET_PSU_COUNT;
  1270. }
  1271. break;
  1272. case GET_PSU_COUNT:
  1273. {
  1274. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1275. byte moduleCount = 0;
  1276. // 發送取得目前全部模組數量
  1277. GetModuleCount(SYSTEM_CMD);
  1278. if (time > 2000)
  1279. {
  1280. // if (ShmPsuData->GroupCount == 0)
  1281. // ShmPsuData->GroupCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1282. // 分別取各群模組數量
  1283. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1284. {
  1285. // 總和各群模組數量
  1286. moduleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  1287. // 取各群模組數量
  1288. GetModuleCount(index);
  1289. }
  1290. PRINTF_FUNC("== PSU == Connector Count = %d, moduleCount = %d, sysCount = %d\n",
  1291. ShmPsuData->GroupCount, moduleCount, ShmPsuData->SystemPresentPsuQuantity);
  1292. // 判斷系統數量與各群數量一致
  1293. if(moduleCount == ShmPsuData->SystemPresentPsuQuantity && moduleCount > 0)
  1294. {
  1295. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  1296. {
  1297. // 電樁在 Booting 的狀態 - 自檢
  1298. PRINTF_FUNC("== PSU == GET_SYS_CAP \n");
  1299. ShmPsuData->Work_Step = GET_SYS_CAP;
  1300. }
  1301. else
  1302. {
  1303. PRINTF_FUNC("== PSU == _WORK_CHARGING \n");
  1304. ShmPsuData->Work_Step = _WORK_CHARGING;
  1305. }
  1306. }
  1307. gettimeofday(&_cmdSubPriority_time, NULL);
  1308. }
  1309. }
  1310. break;
  1311. case GET_SYS_CAP:
  1312. {
  1313. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1314. if (time > 500)
  1315. {
  1316. bool isFinish = true;
  1317. for (byte psuCount = 0; psuCount < ShmPsuData->SystemPresentPsuQuantity; psuCount++)
  1318. {
  1319. if (strcmp((char *)ShmPsuData->PsuVersion[psuCount].FwPrimaryVersion, "") == EQUAL ||
  1320. ShmPsuData->SystemAvailablePower <= 0 || ShmPsuData->SystemAvailableCurrent <= 0)
  1321. {
  1322. isFinish = false;
  1323. break;
  1324. }
  1325. }
  1326. if (!isFinish)
  1327. {
  1328. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1329. {
  1330. if (psuCmdSeq == _PSU_CMD_STATUS)
  1331. {
  1332. // 取得狀態 : 支援模塊不須按照順序功能
  1333. GetStatus(index);
  1334. }
  1335. else if (psuCmdSeq == _PSU_CMD_CAP)
  1336. {
  1337. // 取系統總輸出能力
  1338. GetModuleCap(index);
  1339. }
  1340. else if (psuCmdSeq == _PSU_CMD_VERSION)
  1341. {
  1342. // 取版號
  1343. GetModuleVer(index);
  1344. }
  1345. }
  1346. if (psuCmdSeq == _PSU_CMD_STATUS)
  1347. psuCmdSeq = _PSU_CMD_CAP;
  1348. else if (psuCmdSeq == _PSU_CMD_CAP)
  1349. psuCmdSeq = _PSU_CMD_VERSION;
  1350. else
  1351. psuCmdSeq = _PSU_CMD_STATUS;
  1352. }
  1353. else
  1354. {
  1355. // 判斷系統輸出額定功率與電流
  1356. PRINTF_FUNC("SystemAvailableCurrent = %d, SystemAvailablePower = %d \n",
  1357. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  1358. PRINTF_FUNC("== PSU == BOOTING_COMPLETE \n");
  1359. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  1360. }
  1361. gettimeofday(&_cmdSubPriority_time, NULL);
  1362. gettimeofday(&_log_time, NULL);
  1363. }
  1364. }
  1365. break;
  1366. case BOOTING_COMPLETE:
  1367. {
  1368. bool isSelfTestPass = true;
  1369. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1370. {
  1371. if (chargingInfo[groupIndex]->SystemStatus == S_BOOTING)
  1372. {
  1373. isSelfTestPass = false;
  1374. }
  1375. }
  1376. if (isSelfTestPass)
  1377. ShmPsuData->Work_Step = _WORK_CHARGING;
  1378. sleep(1);
  1379. }
  1380. break;
  1381. case _WORK_CHARGING:
  1382. {
  1383. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1384. // 低 Priority 的指令
  1385. if (time > 1500)
  1386. {
  1387. PreCheckSmartChargingStep();
  1388. gettimeofday(&_cmdSubPriority_time, NULL);
  1389. }
  1390. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1391. {
  1392. if (psuCmdSeq == _PSU_CMD_CAP)
  1393. {
  1394. // 取系統總輸出能力
  1395. GetModuleCap(groupIndex);
  1396. }
  1397. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1398. {
  1399. // 取各群輸出電壓電流 (float)
  1400. GetModuleOutputF(groupIndex);
  1401. }
  1402. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1403. {
  1404. // 取得模塊輸出額定電流能力
  1405. GetModuleIavailable(groupIndex);
  1406. }
  1407. else if (psuCmdSeq == _PSU_CMD_TEMP)
  1408. {
  1409. // 取得模塊溫度
  1410. GetDcTemperature(groupIndex);
  1411. }
  1412. }
  1413. Await();
  1414. if (psuCmdSeq == _PSU_CMD_CAP)
  1415. psuCmdSeq = _PSU_CMD_OUTPUT;
  1416. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1417. psuCmdSeq = _PSU_CMD_IVAILIABLE;
  1418. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1419. psuCmdSeq = _PSU_CMD_TEMP;
  1420. else
  1421. psuCmdSeq = _PSU_CMD_CAP;
  1422. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1423. {
  1424. // 針對各槍當前狀態,傳送需要回傳的資料指令
  1425. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  1426. (chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING && chargingInfo[groupIndex]->Type == _Type_GB) ||
  1427. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1428. {
  1429. if (GetTimeoutValue(_log_time) / 1000 > 1000)
  1430. {
  1431. OutputChargingLogFuncion(groupIndex);
  1432. gettimeofday(&_log_time, NULL);
  1433. }
  1434. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1435. {
  1436. //PRINTF_FUNC("index = %d, SystemStatus = %d, Ev = %f, curCur = %f \n", groupIndex,
  1437. // chargingInfo[groupIndex]->SystemStatus, chargingInfo[groupIndex]->EvBatterytargetCurrent,
  1438. // (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  1439. // 智能判斷 Start -----------------------------------------------------------
  1440. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1441. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1442. {
  1443. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  1444. {
  1445. if (chargingInfo[groupIndex]->DividChargingCurrent == 0)
  1446. {
  1447. chargingInfo[groupIndex]->DividChargingCurrent = ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent;
  1448. }
  1449. }
  1450. PRINTF_FUNC("Index = %d, DividChargingCurrent = %f \n", groupIndex, chargingInfo[groupIndex]->DividChargingCurrent);
  1451. }
  1452. else
  1453. {
  1454. chargingInfo[groupIndex]->DividChargingCurrent = 0;
  1455. chargingInfo[groupIndex]->MaxChargingToAverPassFlag = 0;
  1456. }
  1457. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  1458. {
  1459. if (ShmPsuData->SystemAvailableCurrent != chargingInfo[groupIndex]->AvailableChargingCurrent)
  1460. {
  1461. // 車端要求電流為該充電槍的額定輸出電流的範圍內
  1462. if ((chargingInfo[groupIndex]->EvBatterytargetCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP ||
  1463. deratingKeepCount >= DERATING_COUNT)
  1464. {
  1465. // 車端降載完成
  1466. PRINTF_FUNC("Index = %d, newEvCurrent = %f \n", groupIndex, (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1467. PRINTF_FUNC("======= Get the charging current req of EV and shutdown the module of stop side (Step 3) ======= \n");
  1468. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_M_TO_A;
  1469. gettimeofday(&_derating_time, NULL);
  1470. deratingKeepCount = 0;
  1471. }
  1472. else
  1473. {
  1474. deratingKeepCount++;
  1475. PRINTF_FUNC("Max To Ava mode (2) : Index = %d, EvBatterytargetCurrent = %f, TargetCurrent = %d, Count = %d \n",
  1476. groupIndex,
  1477. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10),
  1478. (ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP),
  1479. deratingKeepCount);
  1480. }
  1481. }
  1482. }
  1483. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_M_TO_A)
  1484. {
  1485. bool isChanged = false;
  1486. if (chargingInfo[groupIndex]->AvailableChargingCurrent <= (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10))
  1487. {
  1488. PRINTF_FUNC("Max To Ava mode (3-1) : Gun_%d, PresentChargingCurrent = %f, AvailableChargingCurrent = %f, EvBatterytargetCurrent = %f \n", groupIndex,
  1489. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1490. chargingInfo[groupIndex]->AvailableChargingCurrent,
  1491. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1492. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1493. {
  1494. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1495. {
  1496. // 當 B 模塊輸出電流小於 5A 及退開 relay
  1497. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= 50)
  1498. isChanged = true;
  1499. break;
  1500. }
  1501. }
  1502. }
  1503. else if (((chargingInfo[groupIndex]->PresentChargingCurrent * 10) >= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent - CHK_CUR_RANGE) &&
  1504. ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + CHK_CUR_RANGE))
  1505. {
  1506. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1507. {
  1508. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1509. {
  1510. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= CHK_CUR_RANGE)
  1511. isChanged = true;
  1512. break;
  1513. }
  1514. }
  1515. }
  1516. if (isChanged)
  1517. {
  1518. PRINTF_FUNC("Max To Ava mode (3-2) : Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d \n", groupIndex,
  1519. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1520. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1521. // 輸出端與車端要求電流接近
  1522. PRINTF_FUNC("======= disable the Parallel relay (Step 4) ======= \n");
  1523. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1524. }
  1525. else
  1526. {
  1527. if ((GetTimeoutValue(_derating_time) / 1000) > 1000)
  1528. {
  1529. gettimeofday(&_derating_time, NULL);
  1530. }
  1531. }
  1532. }
  1533. // 調整輸出電流 : 漸進調整方式
  1534. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1535. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_M_TO_A)
  1536. {
  1537. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A)
  1538. {
  1539. // 當前充電中的目標電壓
  1540. float targetVol = (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10);
  1541. byte reassignIndex = ELEMENT_NOT_FIND;
  1542. // 找到等待分配的槍
  1543. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1544. {
  1545. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1546. {
  1547. reassignIndex = subIndex;
  1548. break;
  1549. }
  1550. }
  1551. if (reassignIndex != ELEMENT_NOT_FIND)
  1552. {
  1553. if ((GetTimeoutValue(_derating_time) / 1000) <= 50 ||
  1554. chargingInfo[groupIndex]->MaxChargingToAverPassFlag == 0)
  1555. {
  1556. chargingInfo[groupIndex]->MaxChargingToAverPassFlag = 1;
  1557. PresentOutputVol(groupIndex, targetVol, (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1558. PresentOutputVol(reassignIndex, targetVol, CHK_CUR_RANGE); Await();
  1559. }
  1560. }
  1561. }
  1562. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1563. {
  1564. bool isNeedToClosePower = false;
  1565. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1566. {
  1567. if (isStartOutputSwitch[index])
  1568. {
  1569. isNeedToClosePower = true;
  1570. }
  1571. isStartOutputSwitch[index] = false;
  1572. }
  1573. if (isNeedToClosePower)
  1574. {
  1575. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1576. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1577. }
  1578. }
  1579. }
  1580. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY_M_TO_A)
  1581. {
  1582. //PRINTF_FUNC("set out (%d) value = %f******** 3 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1583. PresentOutputVol(groupIndex,
  1584. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1585. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1586. }
  1587. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1588. {
  1589. //PRINTF_FUNC("set out (sys) value = %f, smart step = %d******** 4 \n",
  1590. // chargingInfo[groupIndex]->EvBatterytargetCurrent, ShmSysConfigAndInfo->SysInfo.ReAssignedFlag);
  1591. // 該充電槍的目標電壓與目標電流
  1592. //sdlu
  1593. // if ((chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1594. // chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1595. // {
  1596. // PresentOutputVol(SYSTEM_CMD,
  1597. // (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1598. // 0);
  1599. // }
  1600. // else
  1601. // {
  1602. // PresentOutputVol(SYSTEM_CMD,
  1603. // (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - 100,
  1604. // (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1605. // }
  1606. // printf("groupIndex = %d, EvBatterytargetCurrent = %f \n", groupIndex,
  1607. // (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1608. PresentOutputVol(SYSTEM_CMD,
  1609. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1610. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1611. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1612. {
  1613. bool isNeedToClosePower = false;
  1614. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1615. {
  1616. if (isStartOutputSwitch[index])
  1617. {
  1618. isNeedToClosePower = true;
  1619. }
  1620. isStartOutputSwitch[index] = false;
  1621. }
  1622. if (isNeedToClosePower)
  1623. {
  1624. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1625. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1626. }
  1627. }
  1628. else
  1629. {
  1630. bool isNeedToOpenPower = false;
  1631. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1632. {
  1633. if (!isStartOutputSwitch[index])
  1634. {
  1635. isNeedToOpenPower = true;
  1636. }
  1637. isStartOutputSwitch[index] = true;
  1638. }
  1639. if (isNeedToOpenPower)
  1640. {
  1641. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1642. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1643. }
  1644. }
  1645. }
  1646. }
  1647. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1648. {
  1649. // 智能判斷 Start -----------------------------------------------------------
  1650. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  1651. {
  1652. bool balanceVol = true;
  1653. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1654. {
  1655. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1656. chargingInfo[subIndex]->SystemStatus == S_RESERVATION)
  1657. {
  1658. // 各群電壓接近平衡
  1659. if (((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE) ||
  1660. ((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - CHK_VOL_RANGE))
  1661. {
  1662. PRINTF_FUNC("Ava To Max mode (12) : Gun_%d, PresentChargingVoltage = %f, PresentChargingVoltage_V = %f, EvBatterytargetVoltage = %f \n", subIndex,
  1663. (chargingInfo[subIndex]->PresentChargingVoltage * 10),
  1664. ((chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE),
  1665. ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - CHK_VOL_RANGE));
  1666. balanceVol = false;
  1667. }
  1668. break;
  1669. }
  1670. }
  1671. if (balanceVol)
  1672. {
  1673. // 閒置端與車端要求電壓接近
  1674. PRINTF_FUNC("======= Enable the Parallel relay (Step 13) ======= \n");
  1675. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_A_TO_M;
  1676. }
  1677. else
  1678. {
  1679. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1680. {
  1681. gettimeofday(&_max_time, NULL);
  1682. }
  1683. }
  1684. }
  1685. else if(ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_WAITING)
  1686. {
  1687. int idleCurrent = 0;
  1688. int chargingCurrent = 0;
  1689. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1690. {
  1691. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1692. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1693. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1694. idleCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1695. else
  1696. chargingCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1697. }
  1698. if (idleCurrent >= chargingCurrent - PRE_CHARG_RANGE)
  1699. {
  1700. PRINTF_FUNC("======= The Change to maximum charge mode is complete. (Step 15) ======= \n");
  1701. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1702. }
  1703. else
  1704. {
  1705. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1706. {
  1707. gettimeofday(&_max_time, NULL);
  1708. }
  1709. }
  1710. }
  1711. if (chargingInfo[groupIndex]->AvailableChargingCurrent > 0)
  1712. {
  1713. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_A_TO_M)
  1714. {
  1715. byte reassignIndex = ELEMENT_NOT_FIND;
  1716. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1717. {
  1718. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1719. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1720. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1721. {
  1722. reassignIndex = subIndex;
  1723. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1724. {
  1725. preChargingCur = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1726. }
  1727. else
  1728. preChargingCur = 0;
  1729. }
  1730. else
  1731. {
  1732. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING) &&
  1733. (preChargingCur >= preChargingTarget - ZERO_CURRENT))
  1734. preChargingTarget += PRE_CHARG_STEP_CUR;
  1735. if (preChargingTarget >= (chargingInfo[subIndex]->EvBatterytargetCurrent * 10) / 2)
  1736. preChargingTarget = (chargingInfo[subIndex]->EvBatterytargetCurrent * 10) / 2;
  1737. }
  1738. }
  1739. if (reassignIndex != ELEMENT_NOT_FIND)
  1740. {
  1741. if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1742. {
  1743. //PRINTF_FUNC("set out (%d) value = %d******** 5 \n", reassignIndex, ZERO_CURRENT + preChargingTarget);
  1744. // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1745. PresentOutputVol(reassignIndex,
  1746. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1747. ZERO_CURRENT + preChargingTarget); Await();
  1748. byte _ovCahrgingCur = 0;
  1749. if (preChargingCur > PRE_CHARG_STEP_CUR)
  1750. _ovCahrgingCur = PRE_CHARG_STEP_CUR;
  1751. //PRINTF_FUNC("set out (%d) value = %f******** 6 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent - preChargingCur - _ovCahrgingCur);
  1752. PresentOutputVol(groupIndex,
  1753. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1754. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10) - preChargingCur - _ovCahrgingCur); Await();
  1755. }
  1756. }
  1757. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1758. {
  1759. bool isNeedToClosePower = false;
  1760. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1761. {
  1762. if (isStartOutputSwitch[index])
  1763. {
  1764. isNeedToClosePower = true;
  1765. }
  1766. isStartOutputSwitch[index] = false;
  1767. }
  1768. if (isNeedToClosePower)
  1769. {
  1770. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1771. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1772. }
  1773. }
  1774. else
  1775. {
  1776. bool isNeedToOpenPower = false;
  1777. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1778. {
  1779. if (!isStartOutputSwitch[index])
  1780. {
  1781. isNeedToOpenPower = true;
  1782. }
  1783. isStartOutputSwitch[index] = true;
  1784. }
  1785. if (isNeedToOpenPower)
  1786. {
  1787. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1788. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1789. }
  1790. }
  1791. }
  1792. else
  1793. {
  1794. //PRINTF_FUNC("set out (%d) value = %f******** 7 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1795. PresentOutputVol(groupIndex,
  1796. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1797. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1798. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1799. {
  1800. if (isStartOutputSwitch[groupIndex])
  1801. {
  1802. isStartOutputSwitch[groupIndex] = false;
  1803. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  1804. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  1805. }
  1806. }
  1807. else
  1808. {
  1809. if (!isStartOutputSwitch[groupIndex])
  1810. {
  1811. isStartOutputSwitch[groupIndex] = true;
  1812. SwitchPower(groupIndex, PSU_POWER_ON); Await();
  1813. FlashLed(groupIndex, PSU_FLASH_ON); Await();
  1814. }
  1815. }
  1816. }
  1817. }
  1818. }
  1819. }
  1820. else if ((chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  1821. chargingInfo[groupIndex]->SystemStatus <= S_COMPLETE) ||
  1822. chargingInfo[groupIndex]->SystemStatus == S_ALARM)
  1823. {
  1824. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1825. {
  1826. if (!isCharging)
  1827. {
  1828. bool isNeedToClosePower = false;
  1829. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1830. {
  1831. if (isStartOutputSwitch[index])
  1832. {
  1833. isNeedToClosePower = true;
  1834. }
  1835. isStartOutputSwitch[index] = false;
  1836. }
  1837. if (isNeedToClosePower)
  1838. {
  1839. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1840. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1841. }
  1842. if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1843. {
  1844. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1845. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1846. {
  1847. // 代表在切換的過程中,停止充電了
  1848. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= STOP_CURRENT)
  1849. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1850. }
  1851. }
  1852. }
  1853. else if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1854. {
  1855. // 代表充電的槍依舊在充電,欲進入充電的槍取消充電了
  1856. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1857. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1858. {
  1859. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1860. }
  1861. }
  1862. }
  1863. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1864. {
  1865. if (!isReadToCharging)
  1866. {
  1867. bool isNeedToClosePower = false;
  1868. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1869. {
  1870. if (isStartOutputSwitch[index])
  1871. {
  1872. isNeedToClosePower = true;
  1873. }
  1874. isStartOutputSwitch[index] = false;
  1875. }
  1876. if (isNeedToClosePower)
  1877. {
  1878. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1879. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1880. }
  1881. }
  1882. else
  1883. {
  1884. if (isStartOutputSwitch[groupIndex])
  1885. {
  1886. isStartOutputSwitch[groupIndex] = false;
  1887. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  1888. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  1889. }
  1890. }
  1891. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1892. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1893. else
  1894. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1895. }
  1896. }
  1897. else if ((chargingInfo[groupIndex]->SystemStatus >= S_PREPARNING && chargingInfo[groupIndex]->SystemStatus <= S_PREPARING_FOR_EV) &&
  1898. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1899. {
  1900. //PRINTF_FUNC("%d ******** 7 \n", groupIndex);
  1901. if (isStartOutputSwitch[groupIndex])
  1902. {
  1903. isStartOutputSwitch[groupIndex] = false;
  1904. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  1905. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  1906. }
  1907. }
  1908. }
  1909. break;
  1910. }
  1911. case _TEST_MODE:
  1912. {
  1913. // 在測試模式中,保持與模塊的通訊
  1914. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1915. if (time > 1500)
  1916. {
  1917. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1918. {
  1919. // 取系統總輸出能力
  1920. GetModuleCap(index); Await();
  1921. // 取各群輸出電壓電流 (float)
  1922. GetModuleOutputF(index); Await();
  1923. }
  1924. gettimeofday(&_cmdSubPriority_time, NULL);
  1925. }
  1926. byte _switch = 0x00;
  1927. if ((chargingInfo[0]->EvBatterytargetVoltage * 10) > 0 && (chargingInfo[0]->EvBatterytargetCurrent * 10) > 0)
  1928. _switch = 0x01;
  1929. for (byte _groupCount_1 = 0; _groupCount_1 < conn_1_count; _groupCount_1++)
  1930. {
  1931. SetDirModulePresentOutput(connector_1[_groupCount_1],
  1932. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  1933. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  1934. _switch, _switch); Await();
  1935. }
  1936. for (byte _groupCount_2 = 0; _groupCount_2 < conn_2_count; _groupCount_2++)
  1937. {
  1938. SetDirModulePresentOutput(connector_2[_groupCount_2],
  1939. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  1940. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  1941. _switch, _switch); Await();
  1942. }
  1943. }
  1944. break;
  1945. }
  1946. usleep(20000);
  1947. }
  1948. return FAIL;
  1949. }