Module_PsuComm.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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 evseOutVol = 0;
  28. float evseOutCur = 0;
  29. void PRINTF_FUNC(char *string, ...);
  30. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. unsigned long GetTimeoutValue(struct timeval _sour_time);
  34. unsigned long GetTimeoutValue(struct timeval _sour_time)
  35. {
  36. struct timeval _end_time;
  37. gettimeofday(&_end_time, NULL);
  38. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  39. }
  40. int StoreLogMsg(const char *fmt, ...)
  41. {
  42. char Buf[4096+256];
  43. char buffer[4096];
  44. va_list args;
  45. struct timeb SeqEndTime;
  46. struct tm *tm;
  47. va_start(args, fmt);
  48. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  49. va_end(args);
  50. memset(Buf,0,sizeof(Buf));
  51. ftime(&SeqEndTime);
  52. SeqEndTime.time = time(NULL);
  53. tm=localtime(&SeqEndTime.time);
  54. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  55. {
  56. sprintf(Buf,"%02d:%02d:%02d.%03d - %s",
  57. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  58. printf("%s \n", Buf);
  59. }
  60. else
  61. {
  62. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d.%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  63. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  64. buffer,
  65. tm->tm_year+1900,tm->tm_mon+1);
  66. system(Buf);
  67. }
  68. return rc;
  69. }
  70. void PRINTF_FUNC(char *string, ...)
  71. {
  72. va_list args;
  73. char buffer[4096];
  74. va_start(args, string);
  75. vsnprintf(buffer, sizeof(buffer), string, args);
  76. va_end(args);
  77. DEBUG_INFO("%s \n", buffer);
  78. }
  79. //=================================
  80. // Common routine
  81. //=================================
  82. size_t FindIndex(const int a[], size_t size, int value, byte group)
  83. {
  84. size_t index = 0;
  85. while ( index < size && a[index] != value ) ++index;
  86. return (index == size ? ELEMENT_NOT_FIND : group);
  87. }
  88. byte FindTargetGroup(byte address)
  89. {
  90. byte _group = ELEMENT_NOT_FIND;
  91. if (ShmPsuData->GroupCount == 1)
  92. _group = 0;
  93. else
  94. {
  95. _group = FindIndex(connector_1, ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity, address, 0);
  96. if (_group == ELEMENT_NOT_FIND)
  97. _group = FindIndex(connector_2, ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity, address, 1);
  98. }
  99. return _group;
  100. }
  101. bool IsOverModuleCount(byte count)
  102. {
  103. bool result = false;
  104. if (count >= ShmPsuData->SystemPresentPsuQuantity)
  105. result = true;
  106. return result;
  107. }
  108. //=================================
  109. // Save data to share memory Function
  110. //=================================
  111. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  112. {
  113. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  114. {
  115. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  116. {
  117. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  118. return true;
  119. }
  120. }
  121. for (byte index = 0; index < CCS_QUANTITY; index++)
  122. {
  123. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  124. {
  125. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  126. return true;
  127. }
  128. }
  129. for (byte index = 0; index < GB_QUANTITY; index++)
  130. {
  131. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  132. {
  133. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. //=================================
  140. // Alarm code mapping to share memory Function
  141. //=================================
  142. // 檢查 Byte 中某個 Bit 的值
  143. // _byte : 欲改變的 byte
  144. // _bit : 該 byte 的第幾個 bit
  145. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  146. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  147. {
  148. return ( _byte & mask_table[_bit] ) != 0x00;
  149. }
  150. void AbnormalStopAnalysis(byte gun_index, int errCode)
  151. {
  152. for (char i = 0; i < 3; i++)
  153. {
  154. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  155. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  156. {
  157. if(DetectBitValue(byteIndex , bitIndex) == 1)
  158. {
  159. switch(byteIndex)
  160. {
  161. case 0:
  162. {
  163. if (bitIndex == 0)
  164. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  165. else if (bitIndex == 5)
  166. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  167. }
  168. break;
  169. case 1:
  170. {
  171. if (bitIndex == 1)
  172. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  173. else if (bitIndex == 2)
  174. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = YES;
  175. else if (bitIndex == 3)
  176. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  177. else if (bitIndex == 4)
  178. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  179. else if (bitIndex == 5)
  180. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  181. }
  182. break;
  183. case 2:
  184. {
  185. if (bitIndex == 0)
  186. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  187. else if (bitIndex == 1)
  188. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  189. else if (bitIndex == 2)
  190. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = YES;
  191. else if (bitIndex == 3)
  192. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  193. else if (bitIndex == 4)
  194. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  195. else if (bitIndex == 5)
  196. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = YES;
  197. else if (bitIndex == 6)
  198. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  199. }
  200. break;
  201. }
  202. }
  203. // else
  204. // {
  205. // switch (byteIndex) {
  206. // case 0: {
  207. // if (bitIndex == 0)
  208. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  209. // else if (bitIndex == 5)
  210. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  211. // }
  212. // break;
  213. // case 1: {
  214. // if (bitIndex == 1)
  215. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  216. // else if (bitIndex == 2)
  217. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  218. // else if (bitIndex == 3)
  219. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  220. // else if (bitIndex == 4)
  221. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  222. // else if (bitIndex == 5)
  223. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  224. // }
  225. // break;
  226. // case 2: {
  227. // if (bitIndex == 1)
  228. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  229. // if (bitIndex == 2)
  230. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  231. // else if (bitIndex == 3)
  232. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  233. // else if (bitIndex == 4)
  234. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  235. // else if (bitIndex == 5)
  236. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  237. // else if (bitIndex == 6)
  238. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  239. // }
  240. // break;
  241. // }
  242. // }
  243. }
  244. }
  245. }
  246. //=================================
  247. // Callback Function
  248. //=================================
  249. // no using -- GetOutputAndTempCallback
  250. void GetStatusCallback(byte group, byte address, byte temp, int alarm)
  251. {
  252. // if (ShmPsuData->Work_Step == INITIAL_START)
  253. // return;
  254. //
  255. // if (IsOverModuleCount(address))
  256. // return;
  257. //
  258. // byte group1 = FindTargetGroup(address);
  259. //
  260. // if (group1 == 1)
  261. // address -= ShmPsuData->PsuGroup[group1 - 1].GroupPresentPsuQuantity;
  262. //
  263. // ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp1 = temp;
  264. // ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp2 = temp;
  265. // ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp3 = temp;
  266. // ShmPsuData->PsuGroup[group1].PsuModule[address].ExletTemp = temp;
  267. // ShmPsuData->PsuGroup[group1].PsuModule[address].AlarmCode = alarm;
  268. // AbnormalStopAnalysis(group1, alarm);
  269. // PRINTF_FUNC("***Status*** address = %d, temp = %d, err1 = %d, err2 = %d, err3 = %d, err4 = %d \n",
  270. // address, temp,
  271. // (alarm >> 24) & 0xFF,
  272. // (alarm >> 16) & 0xFF,
  273. // (alarm >> 8) & 0xFF,
  274. // alarm & 0xFF);
  275. }
  276. // no using -- GetOutputAndTempCallback End
  277. void GetModuleCountCallback(byte group, byte count)
  278. {
  279. if (group == SYSTEM_CMD)
  280. ShmPsuData->SystemPresentPsuQuantity = count;
  281. else
  282. {
  283. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  284. }
  285. }
  286. void GetMaxPowerAndCur(unsigned char mode, int ratingCur, int *pow, int *cur)
  287. {
  288. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  289. return;
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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 < GET_SYS_CAP)
  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. ShmPsuData->SystemAvailablePower = 0;
  791. PRINTF_FUNC("************ psu Group = %d \n", ShmPsuData->GroupCount);
  792. for (byte _groupCount = 0; _groupCount < ShmPsuData->GroupCount; _groupCount++)
  793. {
  794. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  795. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  796. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  797. }
  798. }
  799. void Initialization()
  800. {
  801. bool isPass = false;
  802. while(!isPass)
  803. {
  804. isPass = true;
  805. for (byte _index = 0; _index < _gunCount; _index++)
  806. {
  807. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  808. {
  809. DEBUG_ERROR("Module_PsuComm : FindChargingInfoData false \n");
  810. isPass = false;
  811. break;
  812. }
  813. }
  814. }
  815. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  816. ShmPsuData->GroupCount = 1;
  817. else
  818. ShmPsuData->GroupCount = _gunCount;
  819. char EvsePower[2];
  820. EvsePower[2] = '\0';
  821. char count = 0;
  822. byte psuTarIndex = 0;
  823. // 解析 ModelName 取得各槍各有幾個 PSU 及編號
  824. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  825. {
  826. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  827. if (strcmp(EvsePower, "15") == EQUAL)
  828. count = 5;
  829. else if (strcmp(EvsePower, "30") == EQUAL)
  830. count = 1;
  831. else if (strcmp(EvsePower, "60") == EQUAL)
  832. count = 2;
  833. else if (strcmp(EvsePower, "18") == EQUAL)
  834. count = 6;
  835. else if (strcmp(EvsePower, "36") == EQUAL)
  836. count = 12;
  837. if (count > 0)
  838. {
  839. if (ShmPsuData->GroupCount == 1)
  840. conn_1_count = count;
  841. else if (ShmPsuData->GroupCount == 2)
  842. {
  843. if(count % 2 > 0)
  844. conn_1_count = (count / 2) + 1;
  845. else
  846. conn_1_count = (count / 2);
  847. conn_2_count = count - conn_1_count;
  848. }
  849. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  850. {
  851. connector_1[psuIndex] = psuTarIndex;
  852. psuTarIndex++;
  853. }
  854. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  855. {
  856. connector_2[psuIndex] = psuTarIndex;
  857. psuTarIndex++;
  858. }
  859. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  860. PRINTF_FUNC("Connector 1 - Number = %d \n", connector_1[psuIndex]);
  861. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  862. PRINTF_FUNC("Connector 2 - Number = %d \n", connector_2[psuIndex]);
  863. }
  864. else
  865. DEBUG_ERROR("Module_PsuComm : Can't parsing model name. \n");
  866. }
  867. }
  868. void CheckSmartChargingStep(bool isWaitingAver, bool isCharging, bool canAverageCharging)
  869. {
  870. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  871. {
  872. if (isWaitingAver)
  873. {
  874. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  875. {
  876. ShmSysConfigAndInfo->SysInfo.CanAverageCharging = canAverageCharging;
  877. if (canAverageCharging)
  878. {
  879. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_GET_NEW_CAP============= Step 2 \n");
  880. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  881. }
  882. else
  883. {
  884. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  885. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  886. }
  887. }
  888. else
  889. {
  890. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  891. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  892. }
  893. }
  894. }
  895. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  896. {
  897. if (isCharging)
  898. {
  899. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  900. {
  901. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_A_TO_M============= Step 12 \n");
  902. preChargingCur = preChargingTarget = 0;
  903. gettimeofday(&_max_time, NULL);
  904. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  905. }
  906. else
  907. {
  908. PRINTF_FUNC("=============Smart Charging_1 : _REASSIGNED_COMP============= Step 15 \n");
  909. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  910. }
  911. }
  912. else
  913. {
  914. PRINTF_FUNC("=============Smart Charging_2 : _REASSIGNED_COMP============= Step 15 \n");
  915. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  916. }
  917. }
  918. }
  919. int main(void)
  920. {
  921. if(InitShareMemory() == FAIL)
  922. {
  923. #ifdef SystemLogMessage
  924. DEBUG_ERROR("InitShareMemory NG\n");
  925. #endif
  926. if(ShmStatusCodeData != NULL)
  927. {
  928. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  929. }
  930. sleep(5);
  931. return 0;
  932. }
  933. PRINTF_FUNC("InitShareMemory OK\n");
  934. // register callback function
  935. RefreshStatus(&GetStatusCallback);
  936. RefreshModuleCount(&GetModuleCountCallback);
  937. RefreshAvailableCap(&GetAvailableCapCallback);
  938. RefreshFwVersion(&GetFwCallback);
  939. RefreshInputVol(&GetInputVoltageCallback);
  940. RefreshGetOutput(&GetPresentOutputCallback);
  941. RefreshFanInfo(&GetFanSpeedCallback);
  942. RefreshIavailable(&GetIavailableCallback);
  943. // GetPresentOutputCallback & GetStatusCallback
  944. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  945. // GetStatusCallback
  946. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  947. // GetInputVoltageCallback
  948. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  949. sleep(2);
  950. _gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  951. // initial object
  952. InitialPsuData();
  953. Initialization();
  954. libInitialize = InitialCommunication();
  955. byte isInitialComp = NO;
  956. PRINTF_FUNC("ALTERNATIVE_CONG = %d \n", ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf);
  957. //main loop
  958. while (libInitialize)
  959. {
  960. // 斷電狀態
  961. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  962. {
  963. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  964. if (!isInitialComp)
  965. {
  966. ShmPsuData->Work_Step = INITIAL_START;
  967. InitialPsuData();
  968. isInitialComp = YES;
  969. }
  970. sleep(1);
  971. continue;
  972. }
  973. else
  974. isInitialComp = NO;
  975. // 自檢失敗
  976. if (ShmPsuData->Work_Step == _NO_WORKING)
  977. {
  978. PRINTF_FUNC("== PSU == self test fail. \n");
  979. sleep(5);
  980. }
  981. switch(ShmPsuData->Work_Step)
  982. {
  983. case INITIAL_START:
  984. {
  985. PRINTF_FUNC("== PSU == INITIAL_START \n");
  986. gettimeofday(&_cmdSubPriority_time, NULL);
  987. sleep(5);
  988. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  989. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  990. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  991. isStartOutputSwitch[index] = false;
  992. ShmPsuData->Work_Step = GET_PSU_COUNT;
  993. }
  994. break;
  995. case GET_PSU_COUNT:
  996. {
  997. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  998. byte moduleCount = 0;
  999. if (time > 2000)
  1000. {
  1001. PRINTF_FUNC("== PSU == indexCount = %d, moduleCount = %d, sysCount = %d\n",
  1002. ShmPsuData->GroupCount, moduleCount, ShmPsuData->SystemPresentPsuQuantity);
  1003. // if (ShmPsuData->GroupCount == 0)
  1004. // ShmPsuData->GroupCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1005. // 分別取各群模組數量
  1006. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1007. {
  1008. // 總和各群模組數量
  1009. moduleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  1010. // 取各群模組數量
  1011. GetModuleCount(index);
  1012. }
  1013. // 發送取得目前全部模組數量
  1014. GetModuleCount(SYSTEM_CMD);
  1015. // 判斷系統數量與各群數量一致
  1016. if(moduleCount == ShmPsuData->SystemPresentPsuQuantity && moduleCount > 0)
  1017. {
  1018. PRINTF_FUNC("Psu Count = %d \n", moduleCount);
  1019. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  1020. {
  1021. // 電樁在 Booting 的狀態 - 自檢
  1022. PRINTF_FUNC("== PSU == GET_SYS_CAP \n");
  1023. ShmPsuData->Work_Step = GET_SYS_CAP;
  1024. }
  1025. else
  1026. {
  1027. PRINTF_FUNC("== PSU == _WORK_CHARGING \n");
  1028. ShmPsuData->Work_Step = _WORK_CHARGING;
  1029. //sdlu test
  1030. gettimeofday(&_test_time, NULL);
  1031. }
  1032. }
  1033. _getCapDelayCount = 7;
  1034. gettimeofday(&_cmdSubPriority_time, NULL);
  1035. }
  1036. }
  1037. break;
  1038. case GET_SYS_CAP:
  1039. {
  1040. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1041. if (time > 1000)
  1042. {
  1043. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1044. {
  1045. // Pooling Status
  1046. //GetStatus(index);
  1047. // 取系統總輸出能力
  1048. GetModuleCap(index);
  1049. // 取版號
  1050. GetModuleVer(index);
  1051. }
  1052. _getCapDelayCount--;
  1053. gettimeofday(&_cmdSubPriority_time, NULL);
  1054. }
  1055. // 判斷系統輸出額定功率與電流
  1056. if (ShmPsuData->SystemAvailablePower > 0 && ShmPsuData->SystemAvailableCurrent > 0 &&
  1057. _getCapDelayCount <= 0)
  1058. {
  1059. PRINTF_FUNC("SystemAvailableCurrent = %d, SystemAvailablePower = %d \n",
  1060. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  1061. PRINTF_FUNC("== PSU == BOOTING_COMPLETE \n");
  1062. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  1063. }
  1064. }
  1065. break;
  1066. case BOOTING_COMPLETE:
  1067. {
  1068. bool isSelfTestPass = true;
  1069. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1070. {
  1071. if (chargingInfo[groupIndex]->SystemStatus == S_BOOTING)
  1072. {
  1073. isSelfTestPass = false;
  1074. }
  1075. }
  1076. if (isSelfTestPass)
  1077. ShmPsuData->Work_Step = _WORK_CHARGING;
  1078. sleep(1);
  1079. }
  1080. break;
  1081. case _WORK_CHARGING:
  1082. {
  1083. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1084. // 低 Priority 的指令
  1085. if (time > 1500)
  1086. {
  1087. isCharging = false;
  1088. isWaitingAver = false;
  1089. isReadToCharging = false;
  1090. CanAverageCharging = true;
  1091. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1092. {
  1093. // Pooling Status
  1094. //GetStatus(index);
  1095. // 取得模塊輸出額定電流能力
  1096. GetModuleIavailable(index);
  1097. if (chargingInfo[index]->SystemStatus == S_CHARGING)
  1098. {
  1099. isCharging = true;
  1100. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  1101. {
  1102. if (toAverVolPoint > 0 &&
  1103. toAverVolPoint == chargingInfo[index]->EvBatterytargetCurrent)
  1104. {
  1105. // 欲最大充 -> 均充需要等待充電中的輸出電流拉高到目標電流
  1106. if ((chargingInfo[index]->PresentChargingCurrent * 10) >=
  1107. chargingInfo[index]->EvBatterytargetCurrent - CHK_CUR_RANGE)
  1108. {
  1109. if (toAverVolCount == 0)
  1110. isWaitingAver = true;
  1111. else
  1112. toAverVolCount--;
  1113. }
  1114. }
  1115. else
  1116. {
  1117. toAverVolPoint = chargingInfo[index]->EvBatterytargetCurrent;
  1118. toAverVolCount = 3;
  1119. }
  1120. }
  1121. else
  1122. {
  1123. toAverVolPoint = 0;
  1124. toAverVolCount = 3;
  1125. }
  1126. }
  1127. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  1128. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1129. {
  1130. isReadToCharging = true;
  1131. }
  1132. if (chargingInfo[index]->DeratingChargingCurrent < STOP_CURRENT)
  1133. {
  1134. CanAverageCharging = false;
  1135. }
  1136. }
  1137. gettimeofday(&_cmdSubPriority_time, NULL);
  1138. CheckSmartChargingStep(isWaitingAver, isCharging, CanAverageCharging);
  1139. }
  1140. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1141. {
  1142. // 取系統總輸出能力
  1143. GetModuleCap(groupIndex);
  1144. // 針對各槍當前狀態,傳送需要回傳的資料指令
  1145. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  1146. (chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING && chargingInfo[groupIndex]->Type == _Type_GB) ||
  1147. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1148. {
  1149. if (time > 1500)
  1150. {
  1151. if (chargingInfo[groupIndex]->FireChargingVoltage > 0 &&
  1152. evseOutVol != (chargingInfo[groupIndex]->FireChargingVoltage / 10))
  1153. {
  1154. evseOutVol = (chargingInfo[groupIndex]->FireChargingVoltage / 10);
  1155. PRINTF_FUNC("groupIndex = %d, ev need vol = %f, evse output vol = %f \n", groupIndex,
  1156. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1157. chargingInfo[groupIndex]->FireChargingVoltage);
  1158. }
  1159. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) > 0 &&
  1160. evseOutCur != (chargingInfo[groupIndex]->PresentChargingCurrent * 10))
  1161. {
  1162. evseOutCur = (chargingInfo[groupIndex]->PresentChargingCurrent * 10);
  1163. PRINTF_FUNC("groupIndex = %d, evse output cur = %f \n", groupIndex,
  1164. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10),
  1165. (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  1166. }
  1167. }
  1168. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1169. {
  1170. // PRINTF_FUNC("index = %d, SystemStatus = %d, Ev = %f, curCur = %f \n", groupIndex,
  1171. // chargingInfo[groupIndex]->SystemStatus, chargingInfo[groupIndex]->EvBatterytargetCurrent,
  1172. // (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  1173. // 智能判斷 Start -----------------------------------------------------------
  1174. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1175. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1176. {
  1177. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  1178. {
  1179. //PRINTF_FUNC("group = %d, GroupPresentOutputCurrent = %d \n",
  1180. // groupIndex, ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1181. if (chargingInfo[groupIndex]->DividChargingCurrent == 0)
  1182. {
  1183. chargingInfo[groupIndex]->DividChargingCurrent = ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent;
  1184. }
  1185. }
  1186. PRINTF_FUNC("Index = %d, DividChargingCurrent = %f \n", groupIndex, chargingInfo[groupIndex]->DividChargingCurrent);
  1187. }
  1188. else
  1189. {
  1190. chargingInfo[groupIndex]->DividChargingCurrent = 0;
  1191. }
  1192. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  1193. {
  1194. if (ShmPsuData->SystemAvailableCurrent != chargingInfo[groupIndex]->AvailableChargingCurrent)
  1195. {
  1196. // 車端要求電流為該充電槍的額定輸出電流的範圍內
  1197. if (chargingInfo[groupIndex]->EvBatterytargetCurrent <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP ||
  1198. deratingKeepCount >= DERATING_COUNT)
  1199. {
  1200. // 車端降載完成
  1201. PRINTF_FUNC("Index = %d, newEvCurrent = %f \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1202. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_M_TO_A============= Step 3 \n");
  1203. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_M_TO_A;
  1204. gettimeofday(&_derating_time, NULL);
  1205. deratingKeepCount = 0;
  1206. }
  1207. else
  1208. {
  1209. deratingKeepCount++;
  1210. PRINTF_FUNC("** Step 2 ** : Index = %d, EvBatterytargetCurrent = %f, TargetCurrent = %d, Count = %d \n",
  1211. groupIndex,
  1212. chargingInfo[groupIndex]->EvBatterytargetCurrent,
  1213. (ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP),
  1214. deratingKeepCount);
  1215. }
  1216. }
  1217. }
  1218. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_M_TO_A)
  1219. {
  1220. bool isChanged = false;
  1221. if (chargingInfo[groupIndex]->AvailableChargingCurrent <= chargingInfo[groupIndex]->EvBatterytargetCurrent)
  1222. {
  1223. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A ** Gun_%d, PresentChargingCurrent = %f, AvailableChargingCurrent = %f, EvBatterytargetCurrent = %f \n", groupIndex,
  1224. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1225. chargingInfo[groupIndex]->AvailableChargingCurrent,
  1226. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1227. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1228. {
  1229. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1230. {
  1231. // 當 B 模塊輸出電流小於 5A 及退開 relay
  1232. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= 50)
  1233. isChanged = true;
  1234. break;
  1235. }
  1236. }
  1237. }
  1238. else if (((chargingInfo[groupIndex]->PresentChargingCurrent * 10) >= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent - CHK_CUR_RANGE) &&
  1239. ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + CHK_CUR_RANGE))
  1240. {
  1241. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1242. {
  1243. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1244. {
  1245. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= CHK_CUR_RANGE)
  1246. isChanged = true;
  1247. break;
  1248. }
  1249. }
  1250. }
  1251. if (isChanged)
  1252. {
  1253. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A To 4** Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d \n", groupIndex,
  1254. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1255. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1256. // 輸出端與車端要求電流接近
  1257. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_M_TO_A============= Step 4 \n");
  1258. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1259. }
  1260. else
  1261. {
  1262. if ((GetTimeoutValue(_derating_time) / 1000) > 1000)
  1263. {
  1264. gettimeofday(&_derating_time, NULL);
  1265. }
  1266. }
  1267. }
  1268. //if (ShmPsuData->SystemAvailablePower > 0)
  1269. {
  1270. // 調整輸出電流 : 漸進調整方式
  1271. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1272. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_M_TO_A)
  1273. {
  1274. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A)
  1275. {
  1276. // 當前充電中的目標電壓
  1277. float targetVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  1278. byte reassignIndex = ELEMENT_NOT_FIND;
  1279. // 找到等待分配的槍
  1280. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1281. {
  1282. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1283. {
  1284. reassignIndex = subIndex;
  1285. break;
  1286. }
  1287. }
  1288. if (reassignIndex != ELEMENT_NOT_FIND)
  1289. {
  1290. if ((GetTimeoutValue(_derating_time) / 1000) <= 50)
  1291. {
  1292. // A 模塊維持當前電壓電流
  1293. //PRINTF_FUNC("A : index = %d, cur = %d \n", groupIndex, ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1294. //PresentOutputVol(groupIndex, targetVol, ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1295. //PRINTF_FUNC("set out (%d) value = %f******** 1 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1296. PresentOutputVol(groupIndex, targetVol, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1297. // }
  1298. //
  1299. // if ((GetTimeoutValue(_derating_time) / 1000) <= 50)
  1300. // {
  1301. // 直接拉掉 B 模塊的電流
  1302. //PRINTF_FUNC("B : index = %d, cur = %d \n", reassignIndex, CHK_CUR_RANGE);
  1303. //PRINTF_FUNC("set out (%d) value = %d******** 2 \n", reassignIndex, CHK_CUR_RANGE);
  1304. PresentOutputVol(reassignIndex, targetVol, CHK_CUR_RANGE);
  1305. }
  1306. }
  1307. }
  1308. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1309. {
  1310. //PRINTF_FUNC("sys ******** 1 \n");
  1311. bool isNeedToClosePower = false;
  1312. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1313. {
  1314. if (isStartOutputSwitch[index])
  1315. {
  1316. isNeedToClosePower = true;
  1317. }
  1318. isStartOutputSwitch[index] = false;
  1319. }
  1320. if (isNeedToClosePower)
  1321. {
  1322. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1323. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1324. }
  1325. }
  1326. }
  1327. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY_M_TO_A)
  1328. {
  1329. //PRINTF_FUNC("set out (%d) value = %f******** 3 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1330. PresentOutputVol(groupIndex,
  1331. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1332. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1333. }
  1334. else
  1335. {
  1336. PRINTF_FUNC("set out (sys) value = %f, smart step = %d******** 4 \n",
  1337. chargingInfo[groupIndex]->EvBatterytargetCurrent, ShmSysConfigAndInfo->SysInfo.ReAssignedFlag);
  1338. // 該充電槍的目標電壓與目標電流
  1339. PresentOutputVol(SYSTEM_CMD,
  1340. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1341. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1342. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1343. {
  1344. //PRINTF_FUNC("sys ******** 2 \n");
  1345. bool isNeedToClosePower = false;
  1346. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1347. {
  1348. if (isStartOutputSwitch[index])
  1349. {
  1350. isNeedToClosePower = true;
  1351. }
  1352. isStartOutputSwitch[index] = false;
  1353. }
  1354. if (isNeedToClosePower)
  1355. {
  1356. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1357. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1358. }
  1359. }
  1360. else
  1361. {
  1362. bool isNeedToOpenPower = false;
  1363. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1364. {
  1365. if (!isStartOutputSwitch[index])
  1366. {
  1367. isNeedToOpenPower = true;
  1368. }
  1369. isStartOutputSwitch[index] = true;
  1370. }
  1371. if (isNeedToOpenPower)
  1372. {
  1373. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1374. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1375. }
  1376. }
  1377. }
  1378. }
  1379. }
  1380. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1381. {
  1382. // 智能判斷 Start -----------------------------------------------------------
  1383. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  1384. {
  1385. bool balanceVol = true;
  1386. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1387. {
  1388. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1389. chargingInfo[subIndex]->SystemStatus == S_RESERVATION)
  1390. {
  1391. // 各群電壓接近平衡
  1392. if (((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE) ||
  1393. ((chargingInfo[subIndex]->PresentChargingVoltage * 10) < chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE))
  1394. {
  1395. PRINTF_FUNC("** _REASSIGNED_ADJUST_A_TO_M ** Gun_%d, PresentChargingVoltage = %f, PresentChargingVoltage_V = %f, EvBatterytargetVoltage = %f \n", subIndex,
  1396. (chargingInfo[subIndex]->PresentChargingVoltage * 10),
  1397. ((chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE),
  1398. (chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE));
  1399. balanceVol = false;
  1400. }
  1401. break;
  1402. }
  1403. }
  1404. if (balanceVol)
  1405. {
  1406. // 閒置端與車端要求電壓接近
  1407. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_A_TO_M============= Step 13 \n");
  1408. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_A_TO_M;
  1409. }
  1410. else
  1411. {
  1412. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1413. {
  1414. gettimeofday(&_max_time, NULL);
  1415. }
  1416. }
  1417. }
  1418. else if(ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_WAITING)
  1419. {
  1420. int idleCurrent = 0;
  1421. int chargingCurrent = 0;
  1422. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1423. {
  1424. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1425. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1426. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1427. idleCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1428. else
  1429. chargingCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1430. }
  1431. if (idleCurrent >= chargingCurrent - PRE_CHARG_RANGE)
  1432. {
  1433. PRINTF_FUNC("=============Smart Charging_0 : _REASSIGNED_COMP============= Step 15 \n");
  1434. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1435. }
  1436. else
  1437. {
  1438. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1439. {
  1440. gettimeofday(&_max_time, NULL);
  1441. }
  1442. }
  1443. }
  1444. if (chargingInfo[groupIndex]->AvailableChargingCurrent > 0)
  1445. {
  1446. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_A_TO_M)
  1447. {
  1448. byte reassignIndex = ELEMENT_NOT_FIND;
  1449. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1450. {
  1451. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1452. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1453. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1454. {
  1455. reassignIndex = subIndex;
  1456. // if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1457. // {
  1458. // // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1459. // PresentOutputVol(subIndex,
  1460. // chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1461. // ZERO_CURRENT + preChargingTarget);
  1462. // }
  1463. //PRINTF_FUNC("preChargingCur = %d \n", preChargingCur);
  1464. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1465. {
  1466. preChargingCur = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1467. }
  1468. else
  1469. preChargingCur = 0;
  1470. }
  1471. else
  1472. {
  1473. //PRINTF_FUNC("CurOutputCurrent = %d \n", ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent - preChargingCur);
  1474. // 充電中的模塊維持輸出
  1475. // if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1476. // {
  1477. // PresentOutputVol(subIndex,
  1478. // chargingInfo[subIndex]->EvBatterytargetVoltage,
  1479. // chargingInfo[subIndex]->EvBatterytargetCurrent - preChargingCur);
  1480. // }
  1481. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING) &&
  1482. (preChargingCur >= preChargingTarget - ZERO_CURRENT))
  1483. preChargingTarget += PRE_CHARG_STEP_CUR;
  1484. if (preChargingTarget >= chargingInfo[subIndex]->EvBatterytargetCurrent / 2)
  1485. preChargingTarget = chargingInfo[subIndex]->EvBatterytargetCurrent / 2;
  1486. }
  1487. }
  1488. if (reassignIndex != ELEMENT_NOT_FIND)
  1489. {
  1490. if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1491. {
  1492. //PRINTF_FUNC("set out (%d) value = %d******** 5 \n", reassignIndex, ZERO_CURRENT + preChargingTarget);
  1493. // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1494. PresentOutputVol(reassignIndex,
  1495. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1496. ZERO_CURRENT + preChargingTarget);
  1497. byte _ovCahrgingCur = 0;
  1498. if (preChargingCur > PRE_CHARG_STEP_CUR)
  1499. _ovCahrgingCur = PRE_CHARG_STEP_CUR;
  1500. //PRINTF_FUNC("set out (%d) value = %f******** 6 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent - preChargingCur - _ovCahrgingCur);
  1501. PresentOutputVol(groupIndex,
  1502. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1503. chargingInfo[groupIndex]->EvBatterytargetCurrent - preChargingCur - _ovCahrgingCur);
  1504. }
  1505. }
  1506. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1507. {
  1508. //PRINTF_FUNC("sys ******** 3 \n");
  1509. bool isNeedToClosePower = false;
  1510. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1511. {
  1512. if (isStartOutputSwitch[index])
  1513. {
  1514. isNeedToClosePower = true;
  1515. }
  1516. isStartOutputSwitch[index] = false;
  1517. }
  1518. if (isNeedToClosePower)
  1519. {
  1520. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1521. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1522. }
  1523. }
  1524. else
  1525. {
  1526. bool isNeedToOpenPower = false;
  1527. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1528. {
  1529. if (!isStartOutputSwitch[index])
  1530. {
  1531. isNeedToOpenPower = true;
  1532. }
  1533. isStartOutputSwitch[index] = true;
  1534. }
  1535. if (isNeedToOpenPower)
  1536. {
  1537. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1538. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1539. }
  1540. }
  1541. }
  1542. else
  1543. {
  1544. //PRINTF_FUNC("set out (%d) value = %f******** 7 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1545. PresentOutputVol(groupIndex,
  1546. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1547. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1548. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1549. {
  1550. //PRINTF_FUNC("%d ******** 4 \n", groupIndex);
  1551. if (isStartOutputSwitch[groupIndex])
  1552. {
  1553. isStartOutputSwitch[groupIndex] = false;
  1554. SwitchPower(groupIndex, PSU_POWER_OFF);
  1555. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1556. }
  1557. }
  1558. else
  1559. {
  1560. if (!isStartOutputSwitch[groupIndex])
  1561. {
  1562. isStartOutputSwitch[groupIndex] = true;
  1563. SwitchPower(groupIndex, PSU_POWER_ON);
  1564. FlashLed(groupIndex, PSU_FLASH_ON);
  1565. }
  1566. }
  1567. }
  1568. }
  1569. }
  1570. }
  1571. else if (chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  1572. chargingInfo[groupIndex]->SystemStatus <= S_COMPLETE)
  1573. {
  1574. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1575. {
  1576. if (!isCharging)
  1577. {
  1578. //PRINTF_FUNC("sys ******** 5 \n");
  1579. bool isNeedToClosePower = false;
  1580. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1581. {
  1582. if (isStartOutputSwitch[index])
  1583. {
  1584. isNeedToClosePower = true;
  1585. }
  1586. isStartOutputSwitch[index] = false;
  1587. }
  1588. if (isNeedToClosePower)
  1589. {
  1590. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1591. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1592. }
  1593. if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1594. {
  1595. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1596. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1597. {
  1598. // 代表在切換的過程中,停止充電了
  1599. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= STOP_CURRENT)
  1600. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1601. }
  1602. }
  1603. }
  1604. else if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1605. {
  1606. // 代表充電的槍依舊在充電,欲進入充電的槍取消充電了
  1607. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1608. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1609. {
  1610. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1611. }
  1612. }
  1613. }
  1614. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1615. {
  1616. if (!isReadToCharging)
  1617. {
  1618. bool isNeedToClosePower = false;
  1619. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1620. {
  1621. if (isStartOutputSwitch[index])
  1622. {
  1623. isNeedToClosePower = true;
  1624. }
  1625. isStartOutputSwitch[index] = false;
  1626. }
  1627. if (isNeedToClosePower)
  1628. {
  1629. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1630. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1631. }
  1632. }
  1633. else
  1634. {
  1635. if (isStartOutputSwitch[groupIndex])
  1636. {
  1637. isStartOutputSwitch[groupIndex] = false;
  1638. SwitchPower(groupIndex, PSU_POWER_OFF);
  1639. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1640. }
  1641. }
  1642. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1643. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1644. else
  1645. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1646. }
  1647. }
  1648. else if ((chargingInfo[groupIndex]->SystemStatus >= S_PREPARNING && chargingInfo[groupIndex]->SystemStatus <= S_PREPARING_FOR_EV) &&
  1649. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1650. {
  1651. //PRINTF_FUNC("%d ******** 7 \n", groupIndex);
  1652. if (isStartOutputSwitch[groupIndex])
  1653. {
  1654. isStartOutputSwitch[groupIndex] = false;
  1655. SwitchPower(groupIndex, PSU_POWER_OFF);
  1656. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1657. }
  1658. }
  1659. }
  1660. break;
  1661. }
  1662. }
  1663. usleep(20000);
  1664. }
  1665. return FAIL;
  1666. }