Module_PsuComm.c 72 KB

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