Module_PsuComm.c 72 KB

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