Module_PsuComm.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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 PRE_CHARG_STEP_CUR 30
  18. #define PRE_CHARG_RANGE 50
  19. #define EQUAL 0
  20. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  21. struct StatusCodeData *ShmStatusCodeData;
  22. struct PsuData *ShmPsuData;
  23. bool libInitialize = false;
  24. byte getAvailableCapOffset = 5;
  25. byte deratingKeepCount = 0;
  26. float carReqVol = 0;
  27. float carReqCur = 0;
  28. float evseOutVol = 0;
  29. float evseOutCur = 0;
  30. void PRINTF_FUNC(char *string, ...);
  31. int StoreLogMsg(const char *fmt, ...);
  32. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  34. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  35. unsigned long GetTimeoutValue(struct timeval _sour_time);
  36. unsigned long GetTimeoutValue(struct timeval _sour_time)
  37. {
  38. struct timeval _end_time;
  39. gettimeofday(&_end_time, NULL);
  40. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  41. }
  42. int StoreLogMsg(const char *fmt, ...)
  43. {
  44. char Buf[4096+256];
  45. char buffer[4096];
  46. va_list args;
  47. struct timeb SeqEndTime;
  48. struct tm *tm;
  49. va_start(args, fmt);
  50. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  51. va_end(args);
  52. memset(Buf,0,sizeof(Buf));
  53. ftime(&SeqEndTime);
  54. SeqEndTime.time = time(NULL);
  55. tm=localtime(&SeqEndTime.time);
  56. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  57. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  58. buffer,
  59. tm->tm_year+1900,tm->tm_mon+1);
  60. system(Buf);
  61. return rc;
  62. }
  63. void PRINTF_FUNC(char *string, ...)
  64. {
  65. va_list args;
  66. char buffer[4096];
  67. va_start(args, string);
  68. vsnprintf(buffer, sizeof(buffer), string, args);
  69. va_end(args);
  70. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  71. printf("%s \n", buffer);
  72. else
  73. DEBUG_INFO("%s \n", buffer);
  74. }
  75. //=================================
  76. // Common routine
  77. //=================================
  78. size_t FindIndex(const int a[], size_t size, int value, byte group)
  79. {
  80. size_t index = 0;
  81. while ( index < size && a[index] != value ) ++index;
  82. return (index == size ? ELEMENT_NOT_FIND : group);
  83. }
  84. byte FindTargetGroup(byte address)
  85. {
  86. byte _group = ELEMENT_NOT_FIND;
  87. if (ShmPsuData->GroupCount == 1)
  88. _group = 0;
  89. else
  90. {
  91. _group = FindIndex(connector_1, ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity, address, 0);
  92. if (_group == ELEMENT_NOT_FIND)
  93. _group = FindIndex(connector_2, ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity, address, 1);
  94. }
  95. return _group;
  96. }
  97. bool IsOverModuleCount(byte count)
  98. {
  99. bool result = false;
  100. if (count >= ShmPsuData->SystemPresentPsuQuantity)
  101. result = true;
  102. return result;
  103. }
  104. //=================================
  105. // Save data to share memory Function
  106. //=================================
  107. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  108. {
  109. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  110. {
  111. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  112. {
  113. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  114. return true;
  115. }
  116. }
  117. for (byte index = 0; index < CCS_QUANTITY; index++)
  118. {
  119. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  120. {
  121. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  122. return true;
  123. }
  124. }
  125. for (byte index = 0; index < GB_QUANTITY; index++)
  126. {
  127. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  128. {
  129. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. //=================================
  136. // Alarm code mapping to share memory Function
  137. //=================================
  138. // 檢查 Byte 中某個 Bit 的值
  139. // _byte : 欲改變的 byte
  140. // _bit : 該 byte 的第幾個 bit
  141. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  142. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  143. {
  144. return ( _byte & mask_table[_bit] ) != 0x00;
  145. }
  146. void AbnormalStopAnalysis(byte gun_index, int errCode)
  147. {
  148. for (char i = 0; i < 3; i++)
  149. {
  150. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  151. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  152. {
  153. if(DetectBitValue(byteIndex , bitIndex) == 1)
  154. {
  155. switch(byteIndex)
  156. {
  157. case 0:
  158. {
  159. if (bitIndex == 0)
  160. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  161. else if (bitIndex == 5)
  162. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  163. }
  164. break;
  165. case 1:
  166. {
  167. if (bitIndex == 1)
  168. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  169. else if (bitIndex == 2)
  170. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = YES;
  171. else if (bitIndex == 3)
  172. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  173. else if (bitIndex == 4)
  174. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  175. else if (bitIndex == 5)
  176. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  177. }
  178. break;
  179. case 2:
  180. {
  181. if (bitIndex == 0)
  182. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  183. else if (bitIndex == 1)
  184. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  185. else if (bitIndex == 2)
  186. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = YES;
  187. else if (bitIndex == 3)
  188. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  189. else if (bitIndex == 4)
  190. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  191. else if (bitIndex == 5)
  192. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = YES;
  193. else if (bitIndex == 6)
  194. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  195. }
  196. break;
  197. }
  198. }
  199. // else
  200. // {
  201. // switch (byteIndex) {
  202. // case 0: {
  203. // if (bitIndex == 0)
  204. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  205. // else if (bitIndex == 5)
  206. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  207. // }
  208. // break;
  209. // case 1: {
  210. // if (bitIndex == 1)
  211. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  212. // else if (bitIndex == 2)
  213. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  214. // else if (bitIndex == 3)
  215. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  216. // else if (bitIndex == 4)
  217. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  218. // else if (bitIndex == 5)
  219. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  220. // }
  221. // break;
  222. // case 2: {
  223. // if (bitIndex == 1)
  224. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  225. // if (bitIndex == 2)
  226. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  227. // else if (bitIndex == 3)
  228. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  229. // else if (bitIndex == 4)
  230. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  231. // else if (bitIndex == 5)
  232. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  233. // else if (bitIndex == 6)
  234. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  235. // }
  236. // break;
  237. // }
  238. // }
  239. }
  240. }
  241. }
  242. //=================================
  243. // Callback Function
  244. //=================================
  245. void GetStatusCallback(byte group, byte address, byte temp, int alarm)
  246. {
  247. if (IsOverModuleCount(address))
  248. return;
  249. byte group1 = FindTargetGroup(address);
  250. if (group1 == 1)
  251. address -= ShmPsuData->PsuGroup[group1 - 1].GroupPresentPsuQuantity;
  252. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp1 = temp;
  253. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp2 = temp;
  254. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp3 = temp;
  255. ShmPsuData->PsuGroup[group1].PsuModule[address].ExletTemp = temp;
  256. ShmPsuData->PsuGroup[group1].PsuModule[address].AlarmCode = alarm;
  257. AbnormalStopAnalysis(group1, alarm);
  258. //printf("alarm = %d \n", alarm);
  259. }
  260. void GetModuleCountCallback(byte group, byte count)
  261. {
  262. if (group == SYSTEM_CMD)
  263. ShmPsuData->SystemPresentPsuQuantity = count;
  264. else
  265. {
  266. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  267. }
  268. }
  269. void GetMaxPowerAndCur(unsigned char mode, int ratingCur, int *pow, int *cur)
  270. {
  271. unsigned short maxCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  272. unsigned short maxPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  273. if (mode == _MAIN_CHARGING_MODE_AVER)
  274. {
  275. maxCurrent /= 2;
  276. maxPower /= 2;
  277. }
  278. if (maxPower != 0 && maxPower <= *pow)
  279. *pow = maxPower;
  280. if (maxCurrent != 0 && maxCurrent <= *cur)
  281. *cur = maxCurrent;
  282. if (ratingCur != 0 && ratingCur <= *cur)
  283. *cur = ratingCur;
  284. }
  285. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  286. {
  287. int _groupPower = 0, _groupCurrent = 0;
  288. byte group = FindTargetGroup(address);
  289. if (group == 1)
  290. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  291. if (chargingInfo[group]->DeratingChargingCurrent == 0)
  292. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = PSU_MIN_CUR;
  293. else
  294. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = maxCur;
  295. ShmPsuData->PsuGroup[group].PsuModule[address].AvailablePower = totalPow;
  296. // 總和該 Group 的可輸出電流
  297. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  298. {
  299. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[index].AvailableCurrent;
  300. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[index].AvailablePower;
  301. }
  302. // 各群得到最大輸出能力 (電流、Power)
  303. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  304. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  305. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  306. int _power = 0, _current = 0, _ratingcurrent = 0;
  307. bool isGetAllDeratingCurrent = true;
  308. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  309. {
  310. _power += ShmPsuData->PsuGroup[index].GroupAvailablePower;
  311. _current += ShmPsuData->PsuGroup[index].GroupAvailableCurrent;
  312. _ratingcurrent += chargingInfo[index]->DeratingChargingCurrent;
  313. if (chargingInfo[index]->DeratingChargingCurrent == 0)
  314. isGetAllDeratingCurrent = false;
  315. }
  316. // 如果不是所有群都得到 Derating current,則先不採樣該次的 ratingCurrent
  317. if (!isGetAllDeratingCurrent) _ratingcurrent = 0;
  318. ShmPsuData->SystemAvailableCurrent = _current;
  319. ShmPsuData->SystemAvailablePower = _power;
  320. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER ||
  321. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  322. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  323. {
  324. int halfPow = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  325. int halfCur = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  326. int ratingCur = chargingInfo[group]->DeratingChargingCurrent;
  327. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  328. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  329. {
  330. if (chargingInfo[group]->DividChargingCurrent == 0)
  331. return;
  332. else
  333. {
  334. halfCur = chargingInfo[group]->DividChargingCurrent;
  335. ratingCur = 0;
  336. }
  337. }
  338. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_AVER, ratingCur, &halfPow, &halfCur);
  339. // if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  340. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  341. // {
  342. // chargingInfo[group]->AvailableChargingCurrent = DERATING_RANGE;
  343. // chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  344. // }
  345. // else
  346. {
  347. // 以下狀況 -> 槍資訊中的最大輸出能力,為該群的輸出能力
  348. // 1. 如不是最大充
  349. // 2. 智能切換成均充過程
  350. chargingInfo[group]->AvailableChargingCurrent = halfCur;
  351. chargingInfo[group]->AvailableChargingPower = halfPow;
  352. }
  353. }
  354. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  355. {
  356. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_MAX, _ratingcurrent, &_power, &_current);
  357. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  358. {
  359. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  360. {
  361. chargingInfo[count]->MaximumChargingVoltage = maxVol;
  362. chargingInfo[count]->AvailableChargingCurrent = _current;
  363. chargingInfo[count]->AvailableChargingPower = _power;
  364. }
  365. }
  366. else
  367. {
  368. // 如果是最大充,該槍資訊中的輸出能力為各群輸出能力的和
  369. chargingInfo[group]->AvailableChargingCurrent = _current;
  370. chargingInfo[group]->AvailableChargingPower = _power;
  371. }
  372. }
  373. }
  374. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  375. {
  376. if (IsOverModuleCount(address))
  377. return;
  378. byte group = FindTargetGroup(address);
  379. sprintf((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  380. sprintf((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, "PFC %d.%02d", (pfcSwVer & 0xFF00) >> 8, pfcSwVer & 0xFF);
  381. if (group == 1)
  382. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  383. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  384. //DEBUG_INFO("fw Ver. = %s \n", ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion);
  385. }
  386. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  387. {
  388. if (IsOverModuleCount(address))
  389. return;
  390. byte group = FindTargetGroup(address);
  391. if (group == 1)
  392. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  393. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = vol1;
  394. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = vol2;
  395. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = vol3;
  396. }
  397. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  398. {
  399. //if (outCur != ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent)
  400. //{
  401. // PRINTF_FUNC("Gp_%d, gp output cur = %d \n", group, outCur);
  402. //}
  403. // PSU Group - 電壓
  404. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outVol;
  405. // PSU Group - 電流
  406. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outCur;
  407. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  408. (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  409. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  410. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  411. )
  412. {
  413. unsigned short outputVol = 0;
  414. unsigned short outputCur = 0;
  415. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  416. {
  417. bool needtoAdd = true;
  418. if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  419. outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  420. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  421. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  422. {
  423. // PRINTF_FUNC("Gp_%d, DividChargingCurrent = %d \n", index,
  424. // chargingInfo[index]->DividChargingCurrent);
  425. if (chargingInfo[index]->DividChargingCurrent == 0)
  426. needtoAdd = false;
  427. }
  428. if (needtoAdd)
  429. outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  430. }
  431. // 黑白機
  432. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  433. {
  434. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  435. {
  436. float _vol_buf = outputVol;
  437. float _cur_buf = outputCur;
  438. // EVSE - 電壓
  439. _vol_buf /= 10;
  440. chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  441. // EVSE - 電流
  442. _cur_buf /= 10;
  443. chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  444. }
  445. }
  446. if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  447. (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  448. {
  449. float _vol_buf = outputVol;
  450. float _cur_buf = outputCur;
  451. // EVSE - 電壓
  452. _vol_buf /= 10;
  453. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  454. // EVSE - 電流
  455. _cur_buf /= 10;
  456. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  457. }
  458. }
  459. else
  460. {
  461. float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  462. float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  463. // EVSE - 電壓
  464. _vol_buf /= 10;
  465. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  466. // EVSE - 電流
  467. _cur_buf /= 10;
  468. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  469. }
  470. // PRINTF_FUNC("Gun_%d, PresentChargingCurrent = %f \n", group,
  471. // chargingInfo[group]->PresentChargingCurrent);
  472. }
  473. void GetFanSpeedCallback(byte address, unsigned int fanSpeed)
  474. {
  475. if (IsOverModuleCount(address))
  476. return;
  477. byte group = FindTargetGroup(address);
  478. if (group == 1)
  479. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  480. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_1 = fanSpeed;
  481. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_2 = fanSpeed;
  482. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_3 = fanSpeed;
  483. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_4 = fanSpeed;
  484. }
  485. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  486. {
  487. if (IsOverModuleCount(address))
  488. return;
  489. //PRINTF_FUNC("address = %d, Iavail = %d \n", address, Iavail);
  490. byte group = FindTargetGroup(address);
  491. if (group == 1)
  492. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  493. //PRINTF_FUNC("group = %d, address_%d, Iavail = %d \n", group, address, Iavail);
  494. ShmPsuData->PsuGroup[group].PsuModule[address].IAvailableCurrent = Iavail;
  495. bool isPass = true;
  496. int totalCur = 0;
  497. if (Iavail == 0)
  498. {
  499. for (byte count = 0; count < 2; count++)
  500. {
  501. chargingInfo[group]->SampleChargingCur[count] = Iavail;
  502. }
  503. }
  504. else
  505. {
  506. // 該群的可輸出電流
  507. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  508. {
  509. totalCur += ShmPsuData->PsuGroup[group].PsuModule[index].IAvailableCurrent;
  510. }
  511. for (byte count = 0; count < 2; count++)
  512. {
  513. if (chargingInfo[group]->SampleChargingCur[count] == 0)
  514. {
  515. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  516. return;
  517. }
  518. else
  519. {
  520. if (chargingInfo[group]->SampleChargingCur[count] != totalCur)
  521. {
  522. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  523. isPass = false;
  524. break;
  525. }
  526. }
  527. }
  528. }
  529. if (isPass)
  530. {
  531. //PRINTF_FUNC("rating pass value = %d \n", totalCur);
  532. chargingInfo[group]->DeratingChargingCurrent = totalCur;
  533. }
  534. }
  535. //==========================================
  536. // Init all share memory
  537. //==========================================
  538. int InitShareMemory()
  539. {
  540. int result = PASS;
  541. int MeterSMId;
  542. //creat ShmSysConfigAndInfo
  543. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  544. {
  545. #ifdef SystemLogMessage
  546. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG %d \n");
  547. #endif
  548. result = FAIL;
  549. }
  550. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  551. {
  552. #ifdef SystemLogMessage
  553. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG \n");
  554. #endif
  555. result = FAIL;
  556. }
  557. else
  558. {}
  559. //creat ShmStatusCodeData
  560. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  561. {
  562. #ifdef SystemLogMessage
  563. DEBUG_ERROR("shmget ShmStatusCodeData NG \n");
  564. #endif
  565. result = FAIL;
  566. }
  567. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  568. {
  569. #ifdef SystemLogMessage
  570. DEBUG_ERROR("shmat ShmStatusCodeData NG \n");
  571. #endif
  572. result = FAIL;
  573. }
  574. else
  575. {}
  576. //creat ShmPsuData
  577. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  578. {
  579. #ifdef SystemLogMessage
  580. DEBUG_ERROR("shmget ShmPsuData NG \n");
  581. #endif
  582. result = FAIL;
  583. }
  584. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  585. {
  586. #ifdef SystemLogMessage
  587. DEBUG_ERROR("shmat ShmPsuData NG \n");
  588. #endif
  589. result = FAIL;
  590. }
  591. memset(ShmPsuData,0,sizeof(struct PsuData));
  592. return result;
  593. }
  594. //================================================
  595. // Main process
  596. //================================================
  597. void InitialPsuData()
  598. {
  599. ShmPsuData->SystemPresentPsuQuantity = 0;
  600. for (byte _groupCount = 0; _groupCount < ARRAY_SIZE(ShmPsuData->PsuGroup); _groupCount++)
  601. {
  602. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  603. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  604. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  605. }
  606. ShmPsuData->Work_Step = _INIT_PSU_STATUS;
  607. }
  608. void Initialization()
  609. {
  610. bool isPass = false;
  611. while(!isPass)
  612. {
  613. isPass = true;
  614. for (byte _index = 0; _index < _gunCount; _index++)
  615. {
  616. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  617. {
  618. DEBUG_ERROR("Module_PsuComm : FindChargingInfoData false \n");
  619. isPass = false;
  620. break;
  621. }
  622. }
  623. }
  624. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  625. ShmPsuData->GroupCount = 1;
  626. else
  627. ShmPsuData->GroupCount = _gunCount;
  628. char EvsePower[2];
  629. EvsePower[2] = '\0';
  630. char count = 0;
  631. byte psuTarIndex = 0;
  632. // 解析 ModelName 取得各槍各有幾個 PSU 及編號
  633. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  634. {
  635. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  636. if (strcmp(EvsePower, "15") == EQUAL)
  637. count = 5;
  638. else if (strcmp(EvsePower, "30") == EQUAL)
  639. count = 1;
  640. else if (strcmp(EvsePower, "60") == EQUAL)
  641. count = 2;
  642. else if (strcmp(EvsePower, "18") == EQUAL)
  643. count = 6;
  644. else if (strcmp(EvsePower, "36") == EQUAL)
  645. count = 12;
  646. if (count > 0)
  647. {
  648. if (ShmPsuData->GroupCount == 1)
  649. conn_1_count = count;
  650. else if (ShmPsuData->GroupCount == 2)
  651. {
  652. if(count % 2 > 0)
  653. conn_1_count = (count / 2) + 1;
  654. else
  655. conn_1_count = (count / 2);
  656. conn_2_count = count - conn_1_count;
  657. }
  658. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  659. {
  660. connector_1[psuIndex] = psuTarIndex;
  661. psuTarIndex++;
  662. }
  663. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  664. {
  665. connector_2[psuIndex] = psuTarIndex;
  666. psuTarIndex++;
  667. }
  668. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  669. PRINTF_FUNC("Connector 1 - Number = %d \n", connector_1[psuIndex]);
  670. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  671. PRINTF_FUNC("Connector 2 - Number = %d \n", connector_2[psuIndex]);
  672. }
  673. else
  674. DEBUG_ERROR("Module_PsuComm : Can't parsing model name. \n");
  675. }
  676. }
  677. void CheckSmartChargingStep(bool isWaitingAver, bool isCharging)
  678. {
  679. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  680. {
  681. if (isWaitingAver)
  682. {
  683. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  684. {
  685. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_GET_NEW_CAP============= Step 2 \n");
  686. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  687. }
  688. else
  689. {
  690. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  691. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  692. }
  693. }
  694. }
  695. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  696. {
  697. if (isCharging)
  698. {
  699. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  700. {
  701. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_A_TO_M============= Step 12 \n");
  702. preChargingCur = preChargingTarget = 0;
  703. gettimeofday(&_max_time, NULL);
  704. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  705. }
  706. else
  707. {
  708. PRINTF_FUNC("=============Smart Charging_1 : _REASSIGNED_COMP============= Step 15 \n");
  709. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  710. }
  711. }
  712. else
  713. {
  714. PRINTF_FUNC("=============Smart Charging_2 : _REASSIGNED_COMP============= Step 15 \n");
  715. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  716. }
  717. }
  718. }
  719. int main(void)
  720. {
  721. if(InitShareMemory() == FAIL)
  722. {
  723. #ifdef SystemLogMessage
  724. DEBUG_ERROR("InitShareMemory NG\n");
  725. #endif
  726. if(ShmStatusCodeData != NULL)
  727. {
  728. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  729. }
  730. sleep(5);
  731. return 0;
  732. }
  733. PRINTF_FUNC("InitShareMemory OK\n");
  734. // register callback function
  735. RefreshStatus(&GetStatusCallback);
  736. RefreshModuleCount(&GetModuleCountCallback);
  737. RefreshAvailableCap(&GetAvailableCapCallback);
  738. RefreshFwVersion(&GetFwCallback);
  739. RefreshInputVol(&GetInputVoltageCallback);
  740. RefreshGetOutput(&GetPresentOutputCallback);
  741. RefreshFanInfo(&GetFanSpeedCallback);
  742. RefreshIavailable(&GetIavailableCallback);
  743. sleep(2);
  744. _gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  745. // initial object
  746. InitialPsuData();
  747. Initialization();
  748. libInitialize = InitialCommunication();
  749. byte isInitialComp = NO;
  750. PRINTF_FUNC("ALTERNATIVE_CONG = %d \n", ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf);
  751. //main loop
  752. while (libInitialize)
  753. {
  754. // 斷電狀態
  755. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  756. {
  757. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  758. if (!isInitialComp)
  759. {
  760. InitialPsuData();
  761. ShmPsuData->Work_Step = INITIAL_START;
  762. isInitialComp = YES;
  763. }
  764. sleep(1);
  765. continue;
  766. }
  767. else
  768. isInitialComp = NO;
  769. // 自檢失敗
  770. if (ShmPsuData->Work_Step == _NO_WORKING)
  771. {
  772. PRINTF_FUNC("== PSU == self test fail. \n");
  773. sleep(5);
  774. }
  775. switch(ShmPsuData->Work_Step)
  776. {
  777. case INITIAL_START:
  778. {
  779. PRINTF_FUNC("== PSU == INITIAL_START \n");
  780. gettimeofday(&_cmdSubPriority_time, NULL);
  781. sleep(5);
  782. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  783. ShmPsuData->Work_Step = GET_PSU_COUNT;
  784. }
  785. break;
  786. case GET_PSU_COUNT:
  787. {
  788. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  789. byte moduleCount = 0;
  790. if (time > 2000)
  791. {
  792. PRINTF_FUNC("== PSU == indexCount = %d, moduleCount = %d, sysCount = %d\n",
  793. ShmPsuData->GroupCount, moduleCount, ShmPsuData->SystemPresentPsuQuantity);
  794. // if (ShmPsuData->GroupCount == 0)
  795. // ShmPsuData->GroupCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  796. // 分別取各群模組數量
  797. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  798. {
  799. // 總和各群模組數量
  800. moduleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  801. // 取各群模組數量
  802. GetModuleCount(index);
  803. // 取版號
  804. GetModuleVer(index);
  805. }
  806. // 發送取得目前全部模組數量
  807. GetModuleCount(SYSTEM_CMD);
  808. // 判斷系統數量與各群數量一致
  809. if(moduleCount == ShmPsuData->SystemPresentPsuQuantity && moduleCount > 0)
  810. {
  811. PRINTF_FUNC("Psu Count = %d \n", moduleCount);
  812. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  813. {
  814. // 電樁在 Booting 的狀態 - 自檢
  815. PRINTF_FUNC("== PSU == GET_SYS_CAP \n");
  816. ShmPsuData->Work_Step = GET_SYS_CAP;
  817. }
  818. else
  819. {
  820. PRINTF_FUNC("== PSU == _WORK_CHARGING \n");
  821. ShmPsuData->Work_Step = _WORK_CHARGING;
  822. //sdlu test
  823. gettimeofday(&_test_time, NULL);
  824. }
  825. }
  826. _getCapDelayCount = 3;
  827. gettimeofday(&_cmdSubPriority_time, NULL);
  828. }
  829. }
  830. break;
  831. case GET_SYS_CAP:
  832. {
  833. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  834. if (time > 1000)
  835. {
  836. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  837. {
  838. // Pooling Status
  839. GetStatus(index);
  840. // 取系統總輸出能力
  841. GetModuleCap(index);
  842. }
  843. _getCapDelayCount--;
  844. gettimeofday(&_cmdSubPriority_time, NULL);
  845. }
  846. // 判斷系統輸出額定功率與電流
  847. if (ShmPsuData->SystemAvailablePower > 0 && ShmPsuData->SystemAvailableCurrent > 0 &&
  848. _getCapDelayCount <= 0)
  849. {
  850. PRINTF_FUNC("SystemAvailableCurrent = %d, SystemAvailablePower = %d \n",
  851. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  852. PRINTF_FUNC("== PSU == BOOTING_COMPLETE \n");
  853. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  854. }
  855. }
  856. break;
  857. case BOOTING_COMPLETE:
  858. {
  859. bool isSelfTestPass = true;
  860. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  861. {
  862. if (chargingInfo[groupIndex]->SystemStatus == S_BOOTING)
  863. {
  864. isSelfTestPass = false;
  865. }
  866. }
  867. if (isSelfTestPass)
  868. ShmPsuData->Work_Step = _WORK_CHARGING;
  869. sleep(1);
  870. }
  871. break;
  872. case _WORK_CHARGING:
  873. {
  874. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  875. // 低 Priority 的指令
  876. if (time > 1500)
  877. {
  878. isCharging = false;
  879. isWaitingAver = false;
  880. isReadToCharging = false;
  881. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  882. {
  883. // Pooling Status
  884. GetStatus(index);
  885. // 取得模塊輸入電壓
  886. GetModuleInput(index);
  887. // 取得模塊輸出額定電流能力
  888. GetModuleIavailable(index);
  889. if (chargingInfo[index]->SystemStatus == S_CHARGING)
  890. {
  891. isCharging = true;
  892. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  893. {
  894. if (toAverVolPoint > 0 &&
  895. toAverVolPoint == chargingInfo[index]->EvBatterytargetCurrent)
  896. {
  897. // 欲最大充 -> 均充需要等待充電中的輸出電流拉高到目標電流
  898. if ((chargingInfo[index]->PresentChargingCurrent * 10) >=
  899. chargingInfo[index]->EvBatterytargetCurrent - CHK_CUR_RANGE)
  900. {
  901. if (toAverVolCount == 0)
  902. isWaitingAver = true;
  903. else
  904. toAverVolCount--;
  905. }
  906. }
  907. else
  908. {
  909. toAverVolPoint = chargingInfo[index]->EvBatterytargetCurrent;
  910. toAverVolCount = 3;
  911. }
  912. }
  913. else
  914. {
  915. toAverVolPoint = 0;
  916. toAverVolCount = 3;
  917. }
  918. }
  919. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  920. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  921. {
  922. isReadToCharging = true;
  923. }
  924. }
  925. gettimeofday(&_cmdSubPriority_time, NULL);
  926. CheckSmartChargingStep(isWaitingAver, isCharging);
  927. }
  928. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  929. {
  930. // 取系統總輸出能力
  931. GetModuleCap(groupIndex);
  932. // 取得模塊的當前輸出
  933. GetModuleOutput(groupIndex);
  934. // 針對各槍當前狀態,傳送需要回傳的資料指令
  935. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  936. (chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING && chargingInfo[groupIndex]->Type == _Type_GB) ||
  937. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  938. {
  939. if (chargingInfo[groupIndex]->EvBatterytargetVoltage > 0 &&
  940. carReqVol != chargingInfo[groupIndex]->EvBatterytargetVoltage)
  941. {
  942. carReqVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  943. DEBUG_INFO("ev need vol = %f \n", chargingInfo[groupIndex]->EvBatterytargetVoltage);
  944. }
  945. if (chargingInfo[groupIndex]->EvBatterytargetCurrent > 0 &&
  946. carReqCur != chargingInfo[groupIndex]->EvBatterytargetCurrent)
  947. {
  948. carReqCur = chargingInfo[groupIndex]->EvBatterytargetCurrent;
  949. DEBUG_INFO("ev need cur = %f \n", chargingInfo[groupIndex]->EvBatterytargetCurrent);
  950. }
  951. if (time > 1500)
  952. {
  953. if (chargingInfo[groupIndex]->FireChargingVoltage > 0 &&
  954. evseOutVol != (chargingInfo[groupIndex]->FireChargingVoltage / 10))
  955. {
  956. evseOutVol = (chargingInfo[groupIndex]->FireChargingVoltage / 10);
  957. PRINTF_FUNC("groupIndex = %d, evse output vol = %f \n", groupIndex,
  958. chargingInfo[groupIndex]->FireChargingVoltage);
  959. }
  960. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) > 0 &&
  961. evseOutCur != (chargingInfo[groupIndex]->PresentChargingCurrent * 10))
  962. {
  963. evseOutCur = (chargingInfo[groupIndex]->PresentChargingCurrent * 10);
  964. PRINTF_FUNC("groupIndex = %d, evse output cur = %f \n", groupIndex,
  965. (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  966. }
  967. }
  968. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  969. {
  970. // PRINTF_FUNC("index = %d, SystemStatus = %d, Ev = %f, curCur = %f \n", groupIndex,
  971. // chargingInfo[groupIndex]->SystemStatus, chargingInfo[groupIndex]->EvBatterytargetCurrent,
  972. // (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  973. // 智能判斷 Start -----------------------------------------------------------
  974. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  975. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  976. {
  977. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  978. {
  979. //PRINTF_FUNC("group = %d, GroupPresentOutputCurrent = %d \n",
  980. // groupIndex, ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  981. if (chargingInfo[groupIndex]->DividChargingCurrent == 0)
  982. {
  983. chargingInfo[groupIndex]->DividChargingCurrent = ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent;
  984. }
  985. }
  986. PRINTF_FUNC("Index = %d, DividChargingCurrent = %f \n", groupIndex, chargingInfo[groupIndex]->DividChargingCurrent);
  987. }
  988. else
  989. {
  990. chargingInfo[groupIndex]->DividChargingCurrent = 0;
  991. }
  992. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  993. {
  994. if (ShmPsuData->SystemAvailableCurrent != chargingInfo[groupIndex]->AvailableChargingCurrent)
  995. {
  996. // 車端要求電流為該充電槍的額定輸出電流的範圍內
  997. if (chargingInfo[groupIndex]->EvBatterytargetCurrent <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP ||
  998. deratingKeepCount >= DERATING_COUNT)
  999. {
  1000. // 車端降載完成
  1001. PRINTF_FUNC("Index = %d, newEvCurrent = %f \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1002. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_M_TO_A============= Step 3 \n");
  1003. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_M_TO_A;
  1004. gettimeofday(&_derating_time, NULL);
  1005. deratingKeepCount = 0;
  1006. }
  1007. else
  1008. {
  1009. deratingKeepCount++;
  1010. PRINTF_FUNC("** Step 2 ** : Index = %d, EvBatterytargetCurrent = %f, TargetCurrent = %d, Count = %d \n",
  1011. groupIndex,
  1012. chargingInfo[groupIndex]->EvBatterytargetCurrent,
  1013. (ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP),
  1014. deratingKeepCount);
  1015. }
  1016. }
  1017. }
  1018. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_M_TO_A)
  1019. {
  1020. bool isChanged = false;
  1021. if (chargingInfo[groupIndex]->AvailableChargingCurrent <= chargingInfo[groupIndex]->EvBatterytargetCurrent)
  1022. {
  1023. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A ** Gun_%d, PresentChargingCurrent = %f, AvailableChargingCurrent = %f, EvBatterytargetCurrent = %f \n", groupIndex,
  1024. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1025. chargingInfo[groupIndex]->AvailableChargingCurrent,
  1026. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1027. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1028. {
  1029. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1030. {
  1031. // 當 B 模塊輸出電流小於 5A 及退開 relay
  1032. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= 50)
  1033. isChanged = true;
  1034. break;
  1035. }
  1036. }
  1037. }
  1038. else if (((chargingInfo[groupIndex]->PresentChargingCurrent * 10) >= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent - CHK_CUR_RANGE) &&
  1039. ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + CHK_CUR_RANGE))
  1040. {
  1041. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1042. {
  1043. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1044. {
  1045. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= CHK_CUR_RANGE)
  1046. isChanged = true;
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. if (isChanged)
  1052. {
  1053. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A To 4** Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d \n", groupIndex,
  1054. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1055. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1056. // 輸出端與車端要求電流接近
  1057. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_M_TO_A============= Step 4 \n");
  1058. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1059. }
  1060. else
  1061. {
  1062. if ((GetTimeoutValue(_derating_time) / 1000) > 1000)
  1063. {
  1064. gettimeofday(&_derating_time, NULL);
  1065. }
  1066. }
  1067. }
  1068. //if (ShmPsuData->SystemAvailablePower > 0)
  1069. {
  1070. // 調整輸出電流 : 漸進調整方式
  1071. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1072. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_M_TO_A)
  1073. {
  1074. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A)
  1075. {
  1076. // 當前充電中的目標電壓
  1077. float targetVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  1078. byte reassignIndex = ELEMENT_NOT_FIND;
  1079. // 找到等待分配的槍
  1080. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1081. {
  1082. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1083. {
  1084. reassignIndex = subIndex;
  1085. break;
  1086. }
  1087. }
  1088. if (reassignIndex != ELEMENT_NOT_FIND)
  1089. {
  1090. if ((GetTimeoutValue(_derating_time) / 1000) <= 50)
  1091. {
  1092. // A 模塊維持當前電壓電流
  1093. //PRINTF_FUNC("A : index = %d, cur = %d \n", groupIndex, ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1094. //PresentOutputVol(groupIndex, targetVol, ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1095. //PRINTF_FUNC("set out (%d) value = %f******** 1 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1096. PresentOutputVol(groupIndex, targetVol, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1097. // }
  1098. //
  1099. // if ((GetTimeoutValue(_derating_time) / 1000) <= 50)
  1100. // {
  1101. // 直接拉掉 B 模塊的電流
  1102. //PRINTF_FUNC("B : index = %d, cur = %d \n", reassignIndex, CHK_CUR_RANGE);
  1103. //PRINTF_FUNC("set out (%d) value = %d******** 2 \n", reassignIndex, CHK_CUR_RANGE);
  1104. PresentOutputVol(reassignIndex, targetVol, CHK_CUR_RANGE);
  1105. }
  1106. }
  1107. }
  1108. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1109. {
  1110. //PRINTF_FUNC("sys ******** 1 \n");
  1111. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1112. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1113. }
  1114. }
  1115. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY_M_TO_A)
  1116. {
  1117. //PRINTF_FUNC("set out (%d) value = %f******** 3 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1118. PresentOutputVol(groupIndex,
  1119. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1120. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1121. }
  1122. else
  1123. {
  1124. //PRINTF_FUNC("set out (sys) value = %f******** 4 \n", chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1125. // 該充電槍的目標電壓與目標電流
  1126. PresentOutputVol(SYSTEM_CMD,
  1127. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1128. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1129. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1130. {
  1131. //PRINTF_FUNC("sys ******** 2 \n");
  1132. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1133. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1134. }
  1135. else
  1136. {
  1137. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1138. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1139. }
  1140. }
  1141. }
  1142. }
  1143. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1144. {
  1145. // 智能判斷 Start -----------------------------------------------------------
  1146. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  1147. {
  1148. bool balanceVol = true;
  1149. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1150. {
  1151. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1152. chargingInfo[subIndex]->SystemStatus == S_RESERVATION)
  1153. {
  1154. // 各群電壓接近平衡
  1155. if (((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE) ||
  1156. ((chargingInfo[subIndex]->PresentChargingVoltage * 10) < chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE))
  1157. {
  1158. PRINTF_FUNC("** _REASSIGNED_ADJUST_A_TO_M ** Gun_%d, PresentChargingVoltage = %f, PresentChargingVoltage_V = %f, EvBatterytargetVoltage = %f \n", subIndex,
  1159. (chargingInfo[subIndex]->PresentChargingVoltage * 10),
  1160. ((chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE),
  1161. (chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE));
  1162. balanceVol = false;
  1163. }
  1164. break;
  1165. }
  1166. }
  1167. if (balanceVol)
  1168. {
  1169. // 閒置端與車端要求電壓接近
  1170. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_A_TO_M============= Step 13 \n");
  1171. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_A_TO_M;
  1172. }
  1173. else
  1174. {
  1175. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1176. {
  1177. gettimeofday(&_max_time, NULL);
  1178. }
  1179. }
  1180. }
  1181. else if(ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_WAITING)
  1182. {
  1183. int idleCurrent = 0;
  1184. int chargingCurrent = 0;
  1185. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1186. {
  1187. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1188. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1189. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1190. idleCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1191. else
  1192. chargingCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1193. }
  1194. if (idleCurrent >= chargingCurrent - PRE_CHARG_RANGE)
  1195. {
  1196. PRINTF_FUNC("=============Smart Charging_0 : _REASSIGNED_COMP============= Step 15 \n");
  1197. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1198. }
  1199. else
  1200. {
  1201. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1202. {
  1203. gettimeofday(&_max_time, NULL);
  1204. }
  1205. }
  1206. }
  1207. if (chargingInfo[groupIndex]->AvailableChargingCurrent > 0)
  1208. {
  1209. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_A_TO_M)
  1210. {
  1211. byte reassignIndex = ELEMENT_NOT_FIND;
  1212. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1213. {
  1214. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1215. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1216. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1217. {
  1218. reassignIndex = subIndex;
  1219. // if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1220. // {
  1221. // // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1222. // PresentOutputVol(subIndex,
  1223. // chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1224. // ZERO_CURRENT + preChargingTarget);
  1225. // }
  1226. //PRINTF_FUNC("preChargingCur = %d \n", preChargingCur);
  1227. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1228. {
  1229. preChargingCur = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1230. }
  1231. else
  1232. preChargingCur = 0;
  1233. }
  1234. else
  1235. {
  1236. //PRINTF_FUNC("CurOutputCurrent = %d \n", ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent - preChargingCur);
  1237. // 充電中的模塊維持輸出
  1238. // if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1239. // {
  1240. // PresentOutputVol(subIndex,
  1241. // chargingInfo[subIndex]->EvBatterytargetVoltage,
  1242. // chargingInfo[subIndex]->EvBatterytargetCurrent - preChargingCur);
  1243. // }
  1244. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING) &&
  1245. (preChargingCur >= preChargingTarget - ZERO_CURRENT))
  1246. preChargingTarget += PRE_CHARG_STEP_CUR;
  1247. if (preChargingTarget >= chargingInfo[subIndex]->EvBatterytargetCurrent / 2)
  1248. preChargingTarget = chargingInfo[subIndex]->EvBatterytargetCurrent / 2;
  1249. }
  1250. }
  1251. if (reassignIndex != ELEMENT_NOT_FIND)
  1252. {
  1253. if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1254. {
  1255. //PRINTF_FUNC("set out (%d) value = %d******** 5 \n", reassignIndex, ZERO_CURRENT + preChargingTarget);
  1256. // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1257. PresentOutputVol(reassignIndex,
  1258. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1259. ZERO_CURRENT + preChargingTarget);
  1260. byte _ovCahrgingCur = 0;
  1261. if (preChargingCur > PRE_CHARG_STEP_CUR)
  1262. _ovCahrgingCur = PRE_CHARG_STEP_CUR;
  1263. //PRINTF_FUNC("set out (%d) value = %f******** 6 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent - preChargingCur - _ovCahrgingCur);
  1264. PresentOutputVol(groupIndex,
  1265. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1266. chargingInfo[groupIndex]->EvBatterytargetCurrent - preChargingCur - _ovCahrgingCur);
  1267. }
  1268. }
  1269. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1270. {
  1271. //PRINTF_FUNC("sys ******** 3 \n");
  1272. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1273. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1274. }
  1275. else
  1276. {
  1277. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1278. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1279. }
  1280. }
  1281. else
  1282. {
  1283. //PRINTF_FUNC("set out (%d) value = %f******** 7 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1284. PresentOutputVol(groupIndex,
  1285. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1286. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1287. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1288. {
  1289. //PRINTF_FUNC("%d ******** 4 \n", groupIndex);
  1290. SwitchPower(groupIndex, PSU_POWER_OFF);
  1291. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1292. }
  1293. else
  1294. {
  1295. SwitchPower(groupIndex, PSU_POWER_ON);
  1296. FlashLed(groupIndex, PSU_FLASH_ON);
  1297. }
  1298. }
  1299. }
  1300. }
  1301. }
  1302. else if (chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  1303. chargingInfo[groupIndex]->SystemStatus <= S_COMPLETE)
  1304. {
  1305. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1306. {
  1307. if (!isCharging)
  1308. {
  1309. //PRINTF_FUNC("sys ******** 5 \n");
  1310. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1311. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1312. if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1313. {
  1314. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1315. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1316. {
  1317. // 代表在切換的過程中,停止充電了
  1318. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= STOP_CURRENT)
  1319. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1320. }
  1321. }
  1322. }
  1323. else if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1324. {
  1325. // 代表充電的槍依舊在充電,欲進入充電的槍取消充電了
  1326. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1327. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1328. {
  1329. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1330. }
  1331. }
  1332. }
  1333. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1334. {
  1335. if (!isReadToCharging)
  1336. {
  1337. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1338. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1339. }
  1340. else
  1341. {
  1342. SwitchPower(groupIndex, PSU_POWER_OFF);
  1343. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1344. }
  1345. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1346. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1347. else
  1348. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1349. }
  1350. }
  1351. else if ((chargingInfo[groupIndex]->SystemStatus >= S_PREPARNING && chargingInfo[groupIndex]->SystemStatus <= S_PREPARING_FOR_EV) &&
  1352. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1353. {
  1354. //PRINTF_FUNC("%d ******** 7 \n", groupIndex);
  1355. SwitchPower(groupIndex, PSU_POWER_OFF);
  1356. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1357. }
  1358. }
  1359. break;
  1360. }
  1361. }
  1362. usleep(20000);
  1363. }
  1364. return FAIL;
  1365. }