Module_PsuComm.c 74 KB

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