Module_PsuComm.c 74 KB

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