Module_PsuComm.c 72 KB

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