Module_PsuComm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 30
  8. #define ELEMENT_NOT_FIND 255
  9. #define CHK_VOL_RANGE 20
  10. #define CHK_CUR_RANGE 10
  11. #define DERATING_RANGE 100
  12. #define ZERO_CURRENT 0
  13. #define ZERO_VOLTAGE 10
  14. #define STOP_CURRENT 30
  15. #define PSU_MIN_CUR 100
  16. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  17. struct StatusCodeData *ShmStatusCodeData;
  18. struct PsuData *ShmPsuData;
  19. bool libInitialize = false;
  20. byte gun_count = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  21. byte getAvailableCapOffset = 5;
  22. byte deratingKeepCount = 0;
  23. float carReqVol = 0;
  24. float carReqCur = 0;
  25. float evseOutVol = 0;
  26. float evseOutCur = 0;
  27. void PRINTF_FUNC(char *string, ...);
  28. int StoreLogMsg(const char *fmt, ...);
  29. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  30. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. unsigned long GetTimeoutValue(struct timeval _sour_time);
  33. unsigned long GetTimeoutValue(struct timeval _sour_time)
  34. {
  35. struct timeval _end_time;
  36. gettimeofday(&_end_time, NULL);
  37. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  38. }
  39. int StoreLogMsg(const char *fmt, ...)
  40. {
  41. char Buf[4096+256];
  42. char buffer[4096];
  43. time_t CurrentTime;
  44. struct tm *tm;
  45. va_list args;
  46. va_start(args, fmt);
  47. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  48. va_end(args);
  49. memset(Buf,0,sizeof(Buf));
  50. CurrentTime = time(NULL);
  51. tm=localtime(&CurrentTime);
  52. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  53. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  54. buffer,
  55. tm->tm_year+1900,tm->tm_mon+1);
  56. system(Buf);
  57. return rc;
  58. }
  59. void PRINTF_FUNC(char *string, ...)
  60. {
  61. va_list args;
  62. char buffer[4096];
  63. va_start(args, string);
  64. vsnprintf(buffer, sizeof(buffer), string, args);
  65. va_end(args);
  66. if (DEBUG)
  67. printf("%s \n", buffer);
  68. else
  69. DEBUG_INFO("%s \n", buffer);
  70. }
  71. //=================================
  72. // Common routine
  73. //=================================
  74. size_t FindIndex(const int a[], size_t size, int value, byte group)
  75. {
  76. size_t index = 0;
  77. while ( index < size && a[index] != value ) ++index;
  78. return (index == size ? ELEMENT_NOT_FIND : group);
  79. }
  80. byte FindTargetGroup(byte address)
  81. {
  82. byte _group = ELEMENT_NOT_FIND;
  83. if (ShmPsuData->GroupCount == 1)
  84. _group = 0;
  85. else
  86. {
  87. _group = FindIndex(connector_1, ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity, address, 0);
  88. if (_group == ELEMENT_NOT_FIND)
  89. _group = FindIndex(connector_2, ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity, address, 1);
  90. }
  91. return _group;
  92. }
  93. bool IsOverModuleCount(byte count)
  94. {
  95. bool result = false;
  96. if (count >= ShmPsuData->SystemPresentPsuQuantity)
  97. result = true;
  98. return result;
  99. }
  100. //=================================
  101. // Save data to share memory Function
  102. //=================================
  103. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  104. {
  105. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  106. {
  107. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  108. {
  109. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  110. return true;
  111. }
  112. }
  113. for (byte index = 0; index < CCS_QUANTITY; index++)
  114. {
  115. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  116. {
  117. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  118. return true;
  119. }
  120. }
  121. for (byte index = 0; index < GB_QUANTITY; index++)
  122. {
  123. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  124. {
  125. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. //=================================
  132. // Alarm code mapping to share memory Function
  133. //=================================
  134. // 檢查 Byte 中某個 Bit 的值
  135. // _byte : 欲改變的 byte
  136. // _bit : 該 byte 的第幾個 bit
  137. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  138. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  139. {
  140. return ( _byte & mask_table[_bit] ) != 0x00;
  141. }
  142. void AbnormalStopAnalysis(byte gun_index, int errCode)
  143. {
  144. for (char i = 0; i < 3; i++)
  145. {
  146. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  147. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  148. {
  149. if(DetectBitValue(byteIndex , bitIndex) == 1)
  150. {
  151. switch(byteIndex)
  152. {
  153. case 0:
  154. {
  155. if (bitIndex == 0)
  156. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  157. else if (bitIndex == 5)
  158. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  159. }
  160. break;
  161. case 1:
  162. {
  163. if (bitIndex == 1)
  164. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  165. else if (bitIndex == 2)
  166. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = YES;
  167. else if (bitIndex == 3)
  168. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  169. else if (bitIndex == 4)
  170. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  171. else if (bitIndex == 5)
  172. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  173. }
  174. break;
  175. case 2:
  176. {
  177. if (bitIndex == 0)
  178. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  179. else if (bitIndex == 1)
  180. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  181. else if (bitIndex == 2)
  182. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = YES;
  183. else if (bitIndex == 3)
  184. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  185. else if (bitIndex == 4)
  186. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  187. else if (bitIndex == 5)
  188. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = YES;
  189. else if (bitIndex == 6)
  190. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  191. }
  192. break;
  193. }
  194. }
  195. // else
  196. // {
  197. // switch (byteIndex) {
  198. // case 0: {
  199. // if (bitIndex == 0)
  200. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  201. // else if (bitIndex == 5)
  202. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  203. // }
  204. // break;
  205. // case 1: {
  206. // if (bitIndex == 1)
  207. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  208. // else if (bitIndex == 2)
  209. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  210. // else if (bitIndex == 3)
  211. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  212. // else if (bitIndex == 4)
  213. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  214. // else if (bitIndex == 5)
  215. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  216. // }
  217. // break;
  218. // case 2: {
  219. // if (bitIndex == 1)
  220. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  221. // if (bitIndex == 2)
  222. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  223. // else if (bitIndex == 3)
  224. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  225. // else if (bitIndex == 4)
  226. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  227. // else if (bitIndex == 5)
  228. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  229. // else if (bitIndex == 6)
  230. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  231. // }
  232. // break;
  233. // }
  234. // }
  235. }
  236. }
  237. }
  238. //=================================
  239. // Callback Function
  240. //=================================
  241. void GetStatusCallback(byte group, byte address, byte temp, int alarm)
  242. {
  243. if (IsOverModuleCount(address))
  244. return;
  245. byte group1 = FindTargetGroup(address);
  246. if (group1 == 1)
  247. address -= ShmPsuData->PsuGroup[group1 - 1].GroupPresentPsuQuantity;
  248. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp1 = temp;
  249. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp2 = temp;
  250. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp3 = temp;
  251. ShmPsuData->PsuGroup[group1].PsuModule[address].ExletTemp = temp;
  252. ShmPsuData->PsuGroup[group1].PsuModule[address].AlarmCode = alarm;
  253. AbnormalStopAnalysis(group1, alarm);
  254. //printf("alarm = %d \n", alarm);
  255. }
  256. void GetModuleCountCallback(byte group, byte count)
  257. {
  258. if (group == SYSTEM_CMD)
  259. ShmPsuData->SystemPresentPsuQuantity = count;
  260. else
  261. {
  262. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  263. }
  264. }
  265. void GetMaxPowerAndCur(unsigned char mode, int ratingCur, int *pow, int *cur)
  266. {
  267. unsigned short maxCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  268. unsigned short maxPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  269. if (mode == _MAIN_CHARGING_MODE_AVER)
  270. {
  271. maxCurrent /= 2;
  272. maxPower /= 2;
  273. }
  274. if (maxPower != 0 && maxPower <= *pow)
  275. *pow = maxPower;
  276. if (maxCurrent != 0 && maxCurrent <= *cur)
  277. *cur = maxCurrent;
  278. if (ratingCur != 0 && ratingCur <= *cur)
  279. *cur = ratingCur;
  280. }
  281. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  282. {
  283. int _groupPower = 0, _groupCurrent = 0;
  284. byte group = FindTargetGroup(address);
  285. if (group == 1)
  286. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  287. if (chargingInfo[group]->DeratingChargingCurrent == 0)
  288. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = PSU_MIN_CUR;
  289. else
  290. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = maxCur;
  291. ShmPsuData->PsuGroup[group].PsuModule[address].AvailablePower = totalPow;
  292. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  293. {
  294. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[index].AvailableCurrent;
  295. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[index].AvailablePower;
  296. }
  297. // 各群得到最大輸出能力 (電流、Power)
  298. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  299. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  300. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  301. int _power = 0, _current = 0, _ratingcurrent = 0;
  302. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  303. {
  304. _power += ShmPsuData->PsuGroup[index].GroupAvailablePower;
  305. _current += ShmPsuData->PsuGroup[index].GroupAvailableCurrent;
  306. _ratingcurrent += chargingInfo[address]->DeratingChargingCurrent;
  307. }
  308. ShmPsuData->SystemAvailableCurrent = _current;
  309. ShmPsuData->SystemAvailablePower = _power;
  310. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER ||
  311. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  312. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  313. {
  314. int halfPow = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  315. int halfCur = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  316. int ratingCur = chargingInfo[group]->DeratingChargingCurrent;
  317. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_AVER, ratingCur, &halfPow, &halfCur);
  318. // if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  319. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  320. // {
  321. // chargingInfo[group]->AvailableChargingCurrent = DERATING_RANGE;
  322. // chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  323. // }
  324. // else
  325. {
  326. // 以下狀況 -> 槍資訊中的最大輸出能力,為該群的輸出能力
  327. // 1. 如不是最大充
  328. // 2. 智能切換成均充過程
  329. chargingInfo[group]->AvailableChargingCurrent = halfCur;
  330. chargingInfo[group]->AvailableChargingPower = halfPow;
  331. }
  332. }
  333. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  334. {
  335. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_MAX, _ratingcurrent, &_power, &_current);
  336. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  337. {
  338. byte _count = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  339. for (byte count = 0; count < _count; count++)
  340. {
  341. chargingInfo[count]->MaximumChargingVoltage = maxVol;
  342. chargingInfo[count]->AvailableChargingCurrent = _current;
  343. chargingInfo[count]->AvailableChargingPower = _power;
  344. }
  345. }
  346. else
  347. {
  348. // 如果是最大充,該槍資訊中的輸出能力為各群輸出能力的和
  349. chargingInfo[group]->AvailableChargingCurrent = _current;
  350. chargingInfo[group]->AvailableChargingPower = _power;
  351. }
  352. }
  353. }
  354. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  355. {
  356. if (IsOverModuleCount(address))
  357. return;
  358. byte group = FindTargetGroup(address);
  359. if (group == 1)
  360. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  361. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  362. //DEBUG_INFO("fw Ver. = %s \n", ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion);
  363. }
  364. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  365. {
  366. if (IsOverModuleCount(address))
  367. return;
  368. byte group = FindTargetGroup(address);
  369. if (group == 1)
  370. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  371. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = vol1;
  372. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = vol2;
  373. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = vol3;
  374. }
  375. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  376. {
  377. unsigned short outputVol = outVol;
  378. unsigned short outputCur = outCur;
  379. // PSU Group - 電壓
  380. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outputVol;
  381. // PSU Group - 電流
  382. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outputCur;
  383. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  384. {
  385. outputVol = 0;
  386. outputCur = 0;
  387. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  388. {
  389. if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  390. outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  391. outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  392. }
  393. // 黑白機
  394. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  395. {
  396. byte _count = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  397. for (byte count = 0; count < _count; count++)
  398. {
  399. // EVSE - 電壓
  400. chargingInfo[count]->PresentChargingVoltage = outputVol;
  401. // EVSE - 電流
  402. chargingInfo[count]->PresentChargingCurrent = outputCur;
  403. }
  404. }
  405. if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_CHARGING) ||
  406. (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  407. {
  408. // EVSE - 電壓
  409. chargingInfo[group]->PresentChargingVoltage = outputVol;
  410. // EVSE - 電流
  411. chargingInfo[group]->PresentChargingCurrent = outputCur;
  412. }
  413. }
  414. else
  415. {
  416. // EVSE - 電壓
  417. chargingInfo[group]->PresentChargingVoltage = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  418. // EVSE - 電流
  419. chargingInfo[group]->PresentChargingCurrent = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  420. }
  421. // PRINTF_FUNC("Gun_%d, PresentChargingCurrent = %f \n", group,
  422. // chargingInfo[group]->PresentChargingCurrent);
  423. }
  424. void GetFanSpeedCallback(byte address, unsigned int fanSpeed)
  425. {
  426. if (IsOverModuleCount(address))
  427. return;
  428. byte group = FindTargetGroup(address);
  429. if (group == 1)
  430. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  431. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_1 = fanSpeed;
  432. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_2 = fanSpeed;
  433. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_3 = fanSpeed;
  434. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_4 = fanSpeed;
  435. }
  436. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  437. {
  438. //PRINTF_FUNC("GetIavailableCallback address_%d, Vext = %d, Iavail = %d \n", address, Vext, Iavail);
  439. bool isPass = true;
  440. if (Iavail == 0)
  441. {
  442. for (byte count = 0; count < 2; count++)
  443. {
  444. chargingInfo[address]->SampleChargingCur[count] = Iavail;
  445. }
  446. }
  447. else
  448. {
  449. for (byte count = 0; count < 2; count++)
  450. {
  451. if (chargingInfo[address]->SampleChargingCur[count] == 0)
  452. {
  453. chargingInfo[address]->SampleChargingCur[count] = Iavail;
  454. return;
  455. }
  456. else
  457. {
  458. if (chargingInfo[address]->SampleChargingCur[count] != Iavail)
  459. {
  460. chargingInfo[address]->SampleChargingCur[count] = Iavail;
  461. isPass = false;
  462. continue;
  463. }
  464. }
  465. }
  466. }
  467. if (isPass)
  468. {
  469. chargingInfo[address]->DeratingChargingCurrent = Iavail;
  470. }
  471. }
  472. //==========================================
  473. // Init all share memory
  474. //==========================================
  475. int InitShareMemory()
  476. {
  477. int result = PASS;
  478. int MeterSMId;
  479. //creat ShmSysConfigAndInfo
  480. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  481. {
  482. #ifdef SystemLogMessage
  483. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG %d \n");
  484. #endif
  485. result = FAIL;
  486. }
  487. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  488. {
  489. #ifdef SystemLogMessage
  490. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG \n");
  491. #endif
  492. result = FAIL;
  493. }
  494. else
  495. {}
  496. //creat ShmStatusCodeData
  497. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  498. {
  499. #ifdef SystemLogMessage
  500. DEBUG_ERROR("shmget ShmStatusCodeData NG \n");
  501. #endif
  502. result = FAIL;
  503. }
  504. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  505. {
  506. #ifdef SystemLogMessage
  507. DEBUG_ERROR("shmat ShmStatusCodeData NG \n");
  508. #endif
  509. result = FAIL;
  510. }
  511. else
  512. {}
  513. //creat ShmPsuData
  514. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  515. {
  516. #ifdef SystemLogMessage
  517. DEBUG_ERROR("shmget ShmPsuData NG \n");
  518. #endif
  519. result = FAIL;
  520. }
  521. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  522. {
  523. #ifdef SystemLogMessage
  524. DEBUG_ERROR("shmat ShmPsuData NG \n");
  525. #endif
  526. result = FAIL;
  527. }
  528. memset(ShmPsuData,0,sizeof(struct PsuData));
  529. return result;
  530. }
  531. //================================================
  532. // Main process
  533. //================================================
  534. void InitialPsuData()
  535. {
  536. ShmPsuData->SystemPresentPsuQuantity = 0;
  537. for (byte _groupCount = 0; _groupCount < ARRAY_SIZE(ShmPsuData->PsuGroup); _groupCount++)
  538. {
  539. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  540. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  541. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  542. }
  543. ShmPsuData->Work_Step = _INIT_PSU_STATUS;
  544. }
  545. void Initialization()
  546. {
  547. bool isPass = false;
  548. while(!isPass)
  549. {
  550. isPass = true;
  551. for (byte _index = 0; _index < _gunCount; _index++)
  552. {
  553. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  554. {
  555. DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
  556. isPass = false;
  557. break;
  558. }
  559. }
  560. }
  561. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  562. ShmPsuData->GroupCount = 1;
  563. else
  564. ShmPsuData->GroupCount = _gunCount;
  565. }
  566. void CheckSmartChargingStep(bool isCharging)
  567. {
  568. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  569. {
  570. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  571. {
  572. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_GET_NEW_CAP============= Step 2 \n");
  573. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  574. }
  575. else
  576. {
  577. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  578. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  579. }
  580. }
  581. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  582. {
  583. if (isCharging)
  584. {
  585. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  586. {
  587. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_A_TO_M============= Step 12 \n");
  588. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  589. }
  590. else
  591. {
  592. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 15 \n");
  593. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  594. }
  595. }
  596. }
  597. }
  598. int main(void)
  599. {
  600. PRINTF_FUNC("Psu Task boot .... \n");
  601. if(InitShareMemory() == FAIL)
  602. {
  603. #ifdef SystemLogMessage
  604. DEBUG_ERROR("InitShareMemory NG\n");
  605. #endif
  606. if(ShmStatusCodeData != NULL)
  607. {
  608. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  609. }
  610. sleep(5);
  611. return 0;
  612. }
  613. PRINTF_FUNC("InitShareMemory OK\n");
  614. // register callback function
  615. RefreshStatus(&GetStatusCallback);
  616. RefreshModuleCount(&GetModuleCountCallback);
  617. RefreshAvailableCap(&GetAvailableCapCallback);
  618. RefreshFwVersion(&GetFwCallback);
  619. RefreshInputVol(&GetInputVoltageCallback);
  620. RefreshGetOutput(&GetPresentOutputCallback);
  621. RefreshFanInfo(&GetFanSpeedCallback);
  622. RefreshIavailable(&GetIavailableCallback);
  623. // initial object
  624. InitialPsuData();
  625. Initialization();
  626. libInitialize = InitialCommunication();
  627. byte isInitialComp = NO;
  628. PRINTF_FUNC("ALTERNATIVE_CONG = %d \n", ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf);
  629. //main loop
  630. while (libInitialize)
  631. {
  632. // 斷電狀態
  633. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  634. {
  635. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  636. if (!isInitialComp)
  637. {
  638. InitialPsuData();
  639. ShmPsuData->Work_Step = INITIAL_START;
  640. isInitialComp = YES;
  641. }
  642. sleep(1);
  643. continue;
  644. }
  645. else
  646. isInitialComp = NO;
  647. // 自檢失敗
  648. if (ShmPsuData->Work_Step == _NO_WORKING)
  649. {
  650. PRINTF_FUNC("== PSU == self test fail. \n");
  651. sleep(5);
  652. }
  653. switch(ShmPsuData->Work_Step)
  654. {
  655. case INITIAL_START:
  656. {
  657. PRINTF_FUNC("== PSU == INITIAL_START \n");
  658. gettimeofday(&_cmdSubPriority_time, NULL);
  659. sleep(5);
  660. ShmPsuData->Work_Step = GET_PSU_COUNT;
  661. }
  662. break;
  663. case GET_PSU_COUNT:
  664. {
  665. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  666. byte moduleCount = 0;
  667. if (time > 2000)
  668. {
  669. PRINTF_FUNC("== PSU == GET_PSU_COUNT \n");
  670. // 分別取各群模組數量
  671. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  672. {
  673. // 總和各群模組數量
  674. moduleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  675. // 取各群模組數量
  676. GetModuleCount(index);
  677. // 取版號
  678. GetModuleVer(index);
  679. }
  680. // 發送取得目前全部模組數量
  681. GetModuleCount(SYSTEM_CMD);
  682. // 判斷系統數量與各群數量一致
  683. if(moduleCount == ShmPsuData->SystemPresentPsuQuantity && moduleCount > 0)
  684. {
  685. PRINTF_FUNC("Psu Count = %d \n", moduleCount);
  686. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  687. {
  688. // 電樁在 Booting 的狀態 - 自檢
  689. PRINTF_FUNC("== PSU == GET_SYS_CAP \n");
  690. ShmPsuData->Work_Step = GET_SYS_CAP;
  691. }
  692. else
  693. {
  694. PRINTF_FUNC("== PSU == _WORK_CHARGING \n");
  695. ShmPsuData->Work_Step = _WORK_CHARGING;
  696. //sdlu test
  697. gettimeofday(&_test_time, NULL);
  698. }
  699. }
  700. _getCapDelayCount = 3;
  701. gettimeofday(&_cmdSubPriority_time, NULL);
  702. }
  703. }
  704. break;
  705. case GET_SYS_CAP:
  706. {
  707. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  708. if (time > 1000)
  709. {
  710. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  711. {
  712. // Pooling Status
  713. GetStatus(index);
  714. // 取系統總輸出能力
  715. GetModuleCap(index);
  716. }
  717. _getCapDelayCount--;
  718. gettimeofday(&_cmdSubPriority_time, NULL);
  719. }
  720. // 判斷系統輸出額定功率與電流
  721. if (ShmPsuData->SystemAvailablePower > 0 && ShmPsuData->SystemAvailableCurrent > 0 &&
  722. _getCapDelayCount <= 0)
  723. {
  724. PRINTF_FUNC("SystemAvailableCurrent = %d, SystemAvailablePower = %d \n",
  725. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  726. PRINTF_FUNC("== PSU == BOOTING_COMPLETE \n");
  727. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  728. }
  729. }
  730. break;
  731. case BOOTING_COMPLETE:
  732. {
  733. sleep(1);
  734. }
  735. break;
  736. case _WORK_CHARGING:
  737. {
  738. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  739. // sdlu - test
  740. int testtime = GetTimeoutValue(_test_time) / 1000;
  741. bool isCharging = false;
  742. // 低 Priority 的指令
  743. if (time > 1500)
  744. {
  745. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  746. {
  747. // Pooling Status
  748. GetStatus(index);
  749. // 取系統總輸出能力
  750. GetModuleCap(index);
  751. // 取得模塊輸入電壓
  752. GetModuleInput(index);
  753. // 取得模塊輸出額定電流能力
  754. GetModuleIavailable(index);
  755. if (chargingInfo[index]->SystemStatus == S_CHARGING)
  756. isCharging = true;
  757. }
  758. gettimeofday(&_cmdSubPriority_time, NULL);
  759. }
  760. CheckSmartChargingStep(isCharging);
  761. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  762. {
  763. GetModuleOutput(groupIndex);
  764. // 針對各槍當前狀態,傳送需要回傳的資料指令
  765. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  766. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  767. {
  768. if (chargingInfo[groupIndex]->EvBatterytargetVoltage > 0 &&
  769. carReqVol != chargingInfo[groupIndex]->EvBatterytargetVoltage)
  770. {
  771. carReqVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  772. DEBUG_INFO("ev need vol = %f \n", chargingInfo[groupIndex]->EvBatterytargetVoltage);
  773. }
  774. if (chargingInfo[groupIndex]->EvBatterytargetCurrent > 0 &&
  775. carReqCur != chargingInfo[groupIndex]->EvBatterytargetCurrent)
  776. {
  777. carReqCur = chargingInfo[groupIndex]->EvBatterytargetCurrent;
  778. DEBUG_INFO("ev need cur = %f \n", chargingInfo[groupIndex]->EvBatterytargetCurrent);
  779. }
  780. if (chargingInfo[groupIndex]->FireChargingVoltage > 0 &&
  781. evseOutVol != chargingInfo[groupIndex]->FireChargingVoltage)
  782. {
  783. evseOutVol = chargingInfo[groupIndex]->FireChargingVoltage;
  784. PRINTF_FUNC("groupIndex = %d, evse output vol = %f \n", groupIndex,
  785. chargingInfo[groupIndex]->FireChargingVoltage);
  786. }
  787. if (chargingInfo[groupIndex]->PresentChargingCurrent > 0 &&
  788. evseOutCur != chargingInfo[groupIndex]->PresentChargingCurrent)
  789. {
  790. evseOutCur = chargingInfo[groupIndex]->PresentChargingCurrent;
  791. PRINTF_FUNC("groupIndex = %d, evse output cur = %f \n", groupIndex,
  792. chargingInfo[groupIndex]->PresentChargingCurrent);
  793. }
  794. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  795. {
  796. // 智能判斷 Start -----------------------------------------------------------
  797. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  798. {
  799. if (ShmPsuData->SystemAvailableCurrent != chargingInfo[groupIndex]->AvailableChargingCurrent)
  800. {
  801. // 車端要求電流為該充電槍的額定輸出電流的範圍內
  802. if (chargingInfo[groupIndex]->EvBatterytargetCurrent <= chargingInfo[groupIndex]->AvailableChargingCurrent ||
  803. deratingKeepCount >= DERATING)
  804. {
  805. // 車端降載完成
  806. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_M_TO_A============= Step 3 \n");
  807. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_M_TO_A;
  808. gettimeofday(&_derating_time, NULL);
  809. deratingKeepCount = 0;
  810. }
  811. else
  812. {
  813. deratingKeepCount++;
  814. }
  815. }
  816. }
  817. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_M_TO_A)
  818. {
  819. bool isChanged = false;
  820. // 需求電流不降低的情況下 -> 依然要切
  821. if (chargingInfo[groupIndex]->AvailableChargingCurrent < chargingInfo[groupIndex]->EvBatterytargetCurrent)
  822. {
  823. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A ** Gun_%d, AvailableChargingCurrent = %f, EvBatterytargetCurrent = %f \n", groupIndex,
  824. chargingInfo[groupIndex]->PresentChargingCurrent,
  825. chargingInfo[groupIndex]->AvailableChargingCurrent);
  826. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  827. {
  828. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  829. {
  830. if (chargingInfo[subIndex]->PresentChargingCurrent <= CHK_CUR_RANGE)
  831. isChanged = true;
  832. break;
  833. }
  834. }
  835. // 這狀況下輸出端的電流載滿載衝的狀況下,並不會降電流
  836. // 所以只能拉載到該槍端的最大輸出能力
  837. if (chargingInfo[groupIndex]->PresentChargingCurrent >= chargingInfo[groupIndex]->AvailableChargingCurrent - CHK_CUR_RANGE ||
  838. chargingInfo[groupIndex]->PresentChargingCurrent <= CHK_CUR_RANGE)
  839. {
  840. isChanged = true;
  841. }
  842. }
  843. else if ((chargingInfo[groupIndex]->PresentChargingCurrent >= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent - CHK_CUR_RANGE) &&
  844. (chargingInfo[groupIndex]->PresentChargingCurrent <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + CHK_CUR_RANGE))
  845. {
  846. isChanged = true;
  847. }
  848. if (isChanged)
  849. {
  850. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A ** Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d \n", groupIndex,
  851. chargingInfo[groupIndex]->PresentChargingCurrent,
  852. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  853. // 輸出端與車端要求電流接近
  854. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_M_TO_A============= Step 4 \n");
  855. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  856. }
  857. }
  858. // 智能判斷 End -----------------------------------------------------------
  859. if (testtime > 500)
  860. {
  861. PRINTF_FUNC("Gun_%d, AvailableChargingCurrent = %f, AvailableChargingPower = %f \n", groupIndex,
  862. chargingInfo[groupIndex]->AvailableChargingCurrent,
  863. chargingInfo[groupIndex]->AvailableChargingPower);
  864. PRINTF_FUNC("Gun_%d, NeedVol = %f, NeedCur = %f \n", groupIndex,
  865. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  866. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  867. PRINTF_FUNC("Gun_%d OutputVol = %f, OutputCur = %f \n", groupIndex,
  868. chargingInfo[groupIndex]->PresentChargingVoltage,
  869. chargingInfo[groupIndex]->PresentChargingCurrent);
  870. gettimeofday(&_test_time, NULL);
  871. }
  872. if (ShmPsuData->SystemAvailablePower > 0)
  873. {
  874. // 調整輸出電流 : 漸進調整方式
  875. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A)
  876. {
  877. // 當前充電中的目標電壓
  878. float targetVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  879. // 當前充電中的目標電流
  880. float targetCur = 0;
  881. // 準備切出去的模塊電流
  882. float deratingCur = 0;
  883. byte reassignIndex = ELEMENT_NOT_FIND;
  884. // 找到等待分配的槍
  885. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  886. {
  887. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  888. {
  889. reassignIndex = subIndex;
  890. }
  891. }
  892. if (reassignIndex != ELEMENT_NOT_FIND)
  893. {
  894. //int derating = GetTimeoutValue(_derating_time) / 1000;
  895. //if (derating > 1000)
  896. {
  897. if (ShmPsuData->PsuGroup[reassignIndex].GroupPresentOutputCurrent > 0)
  898. {
  899. deratingCur = ShmPsuData->PsuGroup[reassignIndex].GroupPresentOutputCurrent - DERATING_RANGE;
  900. if (deratingCur <= CHK_CUR_RANGE)
  901. deratingCur = CHK_CUR_RANGE;
  902. PresentOutputVol(reassignIndex, targetVol, deratingCur);
  903. gettimeofday(&_derating_time, NULL);
  904. }
  905. }
  906. // 因為爬的速度沒有降的速度快,所以採兩倍速度爬升
  907. targetCur = ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + (DERATING_RANGE * 2);
  908. if (targetCur >= chargingInfo[groupIndex]->EvBatterytargetCurrent)
  909. targetCur = chargingInfo[groupIndex]->EvBatterytargetCurrent;
  910. if (targetVol == 0)
  911. {
  912. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  913. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  914. }
  915. else
  916. {
  917. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  918. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  919. }
  920. }
  921. }
  922. else
  923. {
  924. // 該充電槍的目標電壓與目標電流
  925. PresentOutputVol(SYSTEM_CMD,
  926. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  927. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  928. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  929. {
  930. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  931. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  932. }
  933. else
  934. {
  935. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  936. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  937. }
  938. }
  939. }
  940. }
  941. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  942. {
  943. // 智能判斷 Start -----------------------------------------------------------
  944. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  945. {
  946. bool balanceVol = true;
  947. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  948. {
  949. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  950. chargingInfo[subIndex]->SystemStatus == S_RESERVATION)
  951. {
  952. // 各群電壓接近平衡
  953. if ((chargingInfo[subIndex]->PresentChargingVoltage < chargingInfo[groupIndex]->PresentChargingVoltage - ZERO_VOLTAGE) ||
  954. (chargingInfo[subIndex]->PresentChargingVoltage < chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE))
  955. {
  956. PRINTF_FUNC("** _REASSIGNED_ADJUST_A_TO_M ** Gun_%d, PresentChargingVoltage = %f, PresentChargingVoltage_V = %f, EvBatterytargetVoltage = %f \n", subIndex,
  957. chargingInfo[subIndex]->PresentChargingVoltage,
  958. (chargingInfo[groupIndex]->PresentChargingVoltage - ZERO_VOLTAGE),
  959. (chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE));
  960. balanceVol = false;
  961. }
  962. break;
  963. }
  964. }
  965. if (balanceVol)
  966. {
  967. // 閒置端與車端要求電壓接近
  968. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_A_TO_M============= Step 13 \n");
  969. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_A_TO_M;
  970. GetTimeoutValue(_averageComp_time);
  971. }
  972. }
  973. else if(ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_WAITING)
  974. {
  975. int avrTime = GetTimeoutValue(_averageComp_time) / 1000;
  976. if (avrTime > 3000)
  977. {
  978. // 閒置端與車端要求電壓接近
  979. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 15 \n");
  980. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  981. }
  982. }
  983. // 智能判斷 End -----------------------------------------------------------
  984. if (testtime > 500)
  985. {
  986. PRINTF_FUNC("Gun_%d, AvailableChargingCurrent = %f, AvailableChargingPower = %f \n", groupIndex,
  987. chargingInfo[groupIndex]->AvailableChargingCurrent,
  988. chargingInfo[groupIndex]->AvailableChargingPower);
  989. PRINTF_FUNC("Gun_%d, NeedVol = %f, NeedCur = %f \n", groupIndex,
  990. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  991. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  992. PRINTF_FUNC("Gun_%d OutputVol = %f, OutputCur = %f \n", groupIndex,
  993. chargingInfo[groupIndex]->PresentChargingVoltage,
  994. chargingInfo[groupIndex]->PresentChargingCurrent);
  995. gettimeofday(&_test_time, NULL);
  996. }
  997. if (chargingInfo[groupIndex]->AvailableChargingCurrent > 0)
  998. {
  999. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  1000. {
  1001. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1002. {
  1003. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1004. chargingInfo[subIndex]->SystemStatus == S_RESERVATION)
  1005. {
  1006. // 閒置模塊升壓
  1007. PresentOutputVol(subIndex,
  1008. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1009. ZERO_CURRENT);
  1010. }
  1011. else
  1012. {
  1013. // 充電中的模塊維持輸出
  1014. PresentOutputVol(subIndex,
  1015. chargingInfo[subIndex]->EvBatterytargetVoltage,
  1016. chargingInfo[subIndex]->EvBatterytargetCurrent);
  1017. }
  1018. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1019. {
  1020. SwitchPower(subIndex, PSU_POWER_OFF);
  1021. FlashLed(subIndex, PSU_FLASH_NORMAL);
  1022. }
  1023. else
  1024. {
  1025. SwitchPower(subIndex, PSU_POWER_ON);
  1026. FlashLed(subIndex, PSU_FLASH_ON);
  1027. }
  1028. }
  1029. }
  1030. else
  1031. {
  1032. PresentOutputVol(groupIndex,
  1033. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1034. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1035. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1036. {
  1037. SwitchPower(groupIndex, PSU_POWER_OFF);
  1038. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1039. }
  1040. else
  1041. {
  1042. SwitchPower(groupIndex, PSU_POWER_ON);
  1043. FlashLed(groupIndex, PSU_FLASH_ON);
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. else if (chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  1050. chargingInfo[groupIndex]->SystemStatus <= S_COMPLETE)
  1051. {
  1052. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1053. {
  1054. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1055. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1056. if (chargingInfo[groupIndex]->SystemStatus == S_TERMINATING)
  1057. {
  1058. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1059. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1060. {
  1061. // 代表在切換的過程中,停止充電了
  1062. if (chargingInfo[groupIndex]->PresentChargingCurrent <= STOP_CURRENT)
  1063. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1064. }
  1065. }
  1066. }
  1067. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1068. {
  1069. SwitchPower(groupIndex, PSU_POWER_OFF);
  1070. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1071. }
  1072. }
  1073. }
  1074. break;
  1075. }
  1076. }
  1077. usleep(20000);
  1078. }
  1079. return FAIL;
  1080. }