Module_PsuComm.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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 30
  8. #define ELEMENT_NOT_FIND 255
  9. #define CHK_VOL_RANGE 50
  10. #define CHK_CUR_RANGE 10
  11. #define DERATING_RANGE 100
  12. #define ZERO_CURRENT 10 // 該值須保持最小為 1A
  13. #define ZERO_VOLTAGE 50
  14. #define STOP_CURRENT 30
  15. #define PSU_MIN_CUR 1000
  16. #define PRE_CHARG_STEP_CUR 30
  17. #define PRE_CHARG_RANGE 5
  18. #define EQUAL 0
  19. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  20. struct StatusCodeData *ShmStatusCodeData;
  21. struct PsuData *ShmPsuData;
  22. bool libInitialize = false;
  23. byte getAvailableCapOffset = 5;
  24. byte deratingKeepCount = 0;
  25. float carReqVol = 0;
  26. float carReqCur = 0;
  27. float evseOutVol = 0;
  28. float evseOutCur = 0;
  29. void PRINTF_FUNC(char *string, ...);
  30. int StoreLogMsg(const char *fmt, ...);
  31. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  34. unsigned long GetTimeoutValue(struct timeval _sour_time);
  35. unsigned long GetTimeoutValue(struct timeval _sour_time)
  36. {
  37. struct timeval _end_time;
  38. gettimeofday(&_end_time, NULL);
  39. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  40. }
  41. int StoreLogMsg(const char *fmt, ...)
  42. {
  43. char Buf[4096+256];
  44. char buffer[4096];
  45. va_list args;
  46. struct timeb SeqEndTime;
  47. struct tm *tm;
  48. va_start(args, fmt);
  49. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  50. va_end(args);
  51. memset(Buf,0,sizeof(Buf));
  52. ftime(&SeqEndTime);
  53. SeqEndTime.time = time(NULL);
  54. tm=localtime(&SeqEndTime.time);
  55. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d.%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  56. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  57. buffer,
  58. tm->tm_year+1900,tm->tm_mon+1);
  59. system(Buf);
  60. return rc;
  61. }
  62. void PRINTF_FUNC(char *string, ...)
  63. {
  64. va_list args;
  65. char buffer[4096];
  66. va_start(args, string);
  67. vsnprintf(buffer, sizeof(buffer), string, args);
  68. va_end(args);
  69. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  70. printf("%s \n", buffer);
  71. else
  72. DEBUG_INFO("%s \n", buffer);
  73. }
  74. //=================================
  75. // Common routine
  76. //=================================
  77. size_t FindIndex(const int a[], size_t size, int value, byte group)
  78. {
  79. size_t index = 0;
  80. while ( index < size && a[index] != value ) ++index;
  81. return (index == size ? ELEMENT_NOT_FIND : group);
  82. }
  83. byte FindTargetGroup(byte address)
  84. {
  85. byte _group = ELEMENT_NOT_FIND;
  86. if (ShmPsuData->GroupCount == 1)
  87. _group = 0;
  88. else
  89. {
  90. _group = FindIndex(connector_1, ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity, address, 0);
  91. if (_group == ELEMENT_NOT_FIND)
  92. _group = FindIndex(connector_2, ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity, address, 1);
  93. }
  94. return _group;
  95. }
  96. bool IsOverModuleCount(byte count)
  97. {
  98. bool result = false;
  99. if (count >= ShmPsuData->SystemPresentPsuQuantity)
  100. result = true;
  101. return result;
  102. }
  103. //=================================
  104. // Save data to share memory Function
  105. //=================================
  106. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  107. {
  108. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  109. {
  110. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  111. {
  112. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  113. return true;
  114. }
  115. }
  116. for (byte index = 0; index < CCS_QUANTITY; index++)
  117. {
  118. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  119. {
  120. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  121. return true;
  122. }
  123. }
  124. for (byte index = 0; index < GB_QUANTITY; index++)
  125. {
  126. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  127. {
  128. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. //=================================
  135. // Alarm code mapping to share memory Function
  136. //=================================
  137. // 檢查 Byte 中某個 Bit 的值
  138. // _byte : 欲改變的 byte
  139. // _bit : 該 byte 的第幾個 bit
  140. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  141. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  142. {
  143. return ( _byte & mask_table[_bit] ) != 0x00;
  144. }
  145. void AbnormalStopAnalysis(byte gun_index, int errCode)
  146. {
  147. for (char i = 0; i < 3; i++)
  148. {
  149. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  150. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  151. {
  152. if(DetectBitValue(byteIndex , bitIndex) == 1)
  153. {
  154. switch(byteIndex)
  155. {
  156. case 0:
  157. {
  158. if (bitIndex == 0)
  159. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  160. else if (bitIndex == 5)
  161. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  162. }
  163. break;
  164. case 1:
  165. {
  166. if (bitIndex == 1)
  167. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  168. else if (bitIndex == 2)
  169. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = YES;
  170. else if (bitIndex == 3)
  171. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  172. else if (bitIndex == 4)
  173. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  174. else if (bitIndex == 5)
  175. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  176. }
  177. break;
  178. case 2:
  179. {
  180. if (bitIndex == 0)
  181. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  182. else if (bitIndex == 1)
  183. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  184. else if (bitIndex == 2)
  185. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = YES;
  186. else if (bitIndex == 3)
  187. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  188. else if (bitIndex == 4)
  189. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = YES;
  190. else if (bitIndex == 5)
  191. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = YES;
  192. else if (bitIndex == 6)
  193. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  194. }
  195. break;
  196. }
  197. }
  198. // else
  199. // {
  200. // switch (byteIndex) {
  201. // case 0: {
  202. // if (bitIndex == 0)
  203. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  204. // else if (bitIndex == 5)
  205. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  206. // }
  207. // break;
  208. // case 1: {
  209. // if (bitIndex == 1)
  210. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  211. // else if (bitIndex == 2)
  212. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  213. // else if (bitIndex == 3)
  214. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  215. // else if (bitIndex == 4)
  216. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  217. // else if (bitIndex == 5)
  218. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  219. // }
  220. // break;
  221. // case 2: {
  222. // if (bitIndex == 1)
  223. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  224. // if (bitIndex == 2)
  225. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  226. // else if (bitIndex == 3)
  227. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  228. // else if (bitIndex == 4)
  229. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  230. // else if (bitIndex == 5)
  231. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  232. // else if (bitIndex == 6)
  233. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  234. // }
  235. // break;
  236. // }
  237. // }
  238. }
  239. }
  240. }
  241. //=================================
  242. // Callback Function
  243. //=================================
  244. void GetStatusCallback(byte group, byte address, byte temp, int alarm)
  245. {
  246. if (IsOverModuleCount(address))
  247. return;
  248. byte group1 = FindTargetGroup(address);
  249. if (group1 == 1)
  250. address -= ShmPsuData->PsuGroup[group1 - 1].GroupPresentPsuQuantity;
  251. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp1 = temp;
  252. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp2 = temp;
  253. ShmPsuData->PsuGroup[group1].PsuModule[address].CriticalTemp3 = temp;
  254. ShmPsuData->PsuGroup[group1].PsuModule[address].ExletTemp = temp;
  255. ShmPsuData->PsuGroup[group1].PsuModule[address].AlarmCode = alarm;
  256. AbnormalStopAnalysis(group1, alarm);
  257. //printf("alarm = %d \n", alarm);
  258. }
  259. void GetModuleCountCallback(byte group, byte count)
  260. {
  261. if (group == SYSTEM_CMD)
  262. ShmPsuData->SystemPresentPsuQuantity = count;
  263. else
  264. {
  265. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  266. }
  267. }
  268. void GetMaxPowerAndCur(unsigned char mode, int ratingCur, int *pow, int *cur)
  269. {
  270. unsigned short maxCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  271. unsigned short maxPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  272. if (mode == _MAIN_CHARGING_MODE_AVER)
  273. {
  274. maxCurrent /= 2;
  275. maxPower /= 2;
  276. }
  277. if (maxPower != 0 && maxPower <= *pow)
  278. *pow = maxPower;
  279. if (maxCurrent != 0 && maxCurrent <= *cur)
  280. *cur = maxCurrent;
  281. if (ratingCur != 0 && ratingCur <= *cur)
  282. *cur = ratingCur;
  283. }
  284. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  285. {
  286. int _groupPower = 0, _groupCurrent = 0;
  287. byte group = FindTargetGroup(address);
  288. if (group == 1)
  289. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  290. if (chargingInfo[group]->DeratingChargingCurrent == 0)
  291. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = PSU_MIN_CUR;
  292. else
  293. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = maxCur;
  294. ShmPsuData->PsuGroup[group].PsuModule[address].AvailablePower = totalPow;
  295. // 總和該 Group 的可輸出電流
  296. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  297. {
  298. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[index].AvailableCurrent;
  299. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[index].AvailablePower;
  300. }
  301. // 各群得到最大輸出能力 (電流、Power)
  302. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  303. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  304. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  305. int _power = 0, _current = 0, _ratingcurrent = 0;
  306. bool isGetAllDeratingCurrent = true;
  307. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  308. {
  309. _power += ShmPsuData->PsuGroup[index].GroupAvailablePower;
  310. _current += ShmPsuData->PsuGroup[index].GroupAvailableCurrent;
  311. _ratingcurrent += chargingInfo[index]->DeratingChargingCurrent;
  312. if (chargingInfo[index]->DeratingChargingCurrent == 0)
  313. isGetAllDeratingCurrent = false;
  314. }
  315. // 如果不是所有群都得到 Derating current,則先不採樣該次的 ratingCurrent
  316. if (!isGetAllDeratingCurrent) _ratingcurrent = 0;
  317. ShmPsuData->SystemAvailableCurrent = _current;
  318. ShmPsuData->SystemAvailablePower = _power;
  319. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER ||
  320. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  321. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  322. {
  323. int halfPow = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  324. int halfCur = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  325. int ratingCur = chargingInfo[group]->DeratingChargingCurrent;
  326. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_AVER, ratingCur, &halfPow, &halfCur);
  327. // if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  328. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  329. // {
  330. // chargingInfo[group]->AvailableChargingCurrent = DERATING_RANGE;
  331. // chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  332. // }
  333. // else
  334. {
  335. // 以下狀況 -> 槍資訊中的最大輸出能力,為該群的輸出能力
  336. // 1. 如不是最大充
  337. // 2. 智能切換成均充過程
  338. chargingInfo[group]->AvailableChargingCurrent = halfCur;
  339. chargingInfo[group]->AvailableChargingPower = halfPow;
  340. }
  341. }
  342. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  343. {
  344. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_MAX, _ratingcurrent, &_power, &_current);
  345. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  346. {
  347. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  348. {
  349. chargingInfo[count]->MaximumChargingVoltage = maxVol;
  350. chargingInfo[count]->AvailableChargingCurrent = _current;
  351. chargingInfo[count]->AvailableChargingPower = _power;
  352. }
  353. }
  354. else
  355. {
  356. // 如果是最大充,該槍資訊中的輸出能力為各群輸出能力的和
  357. chargingInfo[group]->AvailableChargingCurrent = _current;
  358. chargingInfo[group]->AvailableChargingPower = _power;
  359. }
  360. }
  361. }
  362. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  363. {
  364. if (IsOverModuleCount(address))
  365. return;
  366. byte group = FindTargetGroup(address);
  367. sprintf((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  368. sprintf((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, "PFC %d.%02d", (pfcSwVer & 0xFF00) >> 8, pfcSwVer & 0xFF);
  369. if (group == 1)
  370. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  371. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  372. //DEBUG_INFO("fw Ver. = %s \n", ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion);
  373. }
  374. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  375. {
  376. if (IsOverModuleCount(address))
  377. return;
  378. byte group = FindTargetGroup(address);
  379. if (group == 1)
  380. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  381. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = vol1;
  382. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = vol2;
  383. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = vol3;
  384. }
  385. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  386. {
  387. unsigned short outputVol = outVol;
  388. unsigned short outputCur = outCur;
  389. // PSU Group - 電壓
  390. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outputVol;
  391. // PSU Group - 電流
  392. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outputCur;
  393. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  394. (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  395. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  396. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  397. )
  398. {
  399. outputVol = 0;
  400. outputCur = 0;
  401. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  402. {
  403. if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  404. outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  405. outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  406. }
  407. // 黑白機
  408. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  409. {
  410. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  411. {
  412. float _vol_buf = outputVol;
  413. float _cur_buf = outputCur;
  414. // EVSE - 電壓
  415. _vol_buf /= 10;
  416. chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  417. // EVSE - 電流
  418. _cur_buf /= 10;
  419. chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  420. }
  421. }
  422. if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  423. (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  424. {
  425. float _vol_buf = outputVol;
  426. float _cur_buf = outputCur;
  427. // EVSE - 電壓
  428. _vol_buf /= 10;
  429. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  430. // EVSE - 電流
  431. _cur_buf /= 10;
  432. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  433. }
  434. }
  435. else
  436. {
  437. float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  438. float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  439. // EVSE - 電壓
  440. _vol_buf /= 10;
  441. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  442. // EVSE - 電流
  443. _cur_buf /= 10;
  444. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  445. }
  446. // PRINTF_FUNC("Gun_%d, PresentChargingCurrent = %f \n", group,
  447. // chargingInfo[group]->PresentChargingCurrent);
  448. }
  449. void GetFanSpeedCallback(byte address, unsigned int fanSpeed)
  450. {
  451. if (IsOverModuleCount(address))
  452. return;
  453. byte group = FindTargetGroup(address);
  454. if (group == 1)
  455. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  456. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_1 = fanSpeed;
  457. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_2 = fanSpeed;
  458. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_3 = fanSpeed;
  459. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_4 = fanSpeed;
  460. }
  461. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  462. {
  463. if (IsOverModuleCount(address))
  464. return;
  465. //PRINTF_FUNC("address = %d, Iavail = %d \n", address, Iavail);
  466. byte group = FindTargetGroup(address);
  467. if (group == 1)
  468. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  469. //PRINTF_FUNC("group = %d, address_%d, Iavail = %d \n", group, address, Iavail);
  470. ShmPsuData->PsuGroup[group].PsuModule[address].IAvailableCurrent = Iavail;
  471. bool isPass = true;
  472. int totalCur = 0;
  473. if (Iavail == 0)
  474. {
  475. for (byte count = 0; count < 2; count++)
  476. {
  477. chargingInfo[group]->SampleChargingCur[count] = Iavail;
  478. }
  479. }
  480. else
  481. {
  482. // 該群的可輸出電流
  483. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  484. {
  485. totalCur += ShmPsuData->PsuGroup[group].PsuModule[index].IAvailableCurrent;
  486. }
  487. for (byte count = 0; count < 2; count++)
  488. {
  489. if (chargingInfo[group]->SampleChargingCur[count] == 0)
  490. {
  491. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  492. return;
  493. }
  494. else
  495. {
  496. if (chargingInfo[group]->SampleChargingCur[count] != totalCur)
  497. {
  498. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  499. isPass = false;
  500. break;
  501. }
  502. }
  503. }
  504. }
  505. if (isPass)
  506. {
  507. //PRINTF_FUNC("rating pass value = %d \n", totalCur);
  508. chargingInfo[group]->DeratingChargingCurrent = totalCur;
  509. }
  510. }
  511. //==========================================
  512. // Init all share memory
  513. //==========================================
  514. int InitShareMemory()
  515. {
  516. int result = PASS;
  517. int MeterSMId;
  518. //creat ShmSysConfigAndInfo
  519. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  520. {
  521. #ifdef SystemLogMessage
  522. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG %d \n");
  523. #endif
  524. result = FAIL;
  525. }
  526. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  527. {
  528. #ifdef SystemLogMessage
  529. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG \n");
  530. #endif
  531. result = FAIL;
  532. }
  533. else
  534. {}
  535. //creat ShmStatusCodeData
  536. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  537. {
  538. #ifdef SystemLogMessage
  539. DEBUG_ERROR("shmget ShmStatusCodeData NG \n");
  540. #endif
  541. result = FAIL;
  542. }
  543. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  544. {
  545. #ifdef SystemLogMessage
  546. DEBUG_ERROR("shmat ShmStatusCodeData NG \n");
  547. #endif
  548. result = FAIL;
  549. }
  550. else
  551. {}
  552. //creat ShmPsuData
  553. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  554. {
  555. #ifdef SystemLogMessage
  556. DEBUG_ERROR("shmget ShmPsuData NG \n");
  557. #endif
  558. result = FAIL;
  559. }
  560. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  561. {
  562. #ifdef SystemLogMessage
  563. DEBUG_ERROR("shmat ShmPsuData NG \n");
  564. #endif
  565. result = FAIL;
  566. }
  567. memset(ShmPsuData,0,sizeof(struct PsuData));
  568. return result;
  569. }
  570. //================================================
  571. // Main process
  572. //================================================
  573. void InitialPsuData()
  574. {
  575. ShmPsuData->SystemPresentPsuQuantity = 0;
  576. for (byte _groupCount = 0; _groupCount < ARRAY_SIZE(ShmPsuData->PsuGroup); _groupCount++)
  577. {
  578. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  579. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  580. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  581. }
  582. ShmPsuData->Work_Step = _INIT_PSU_STATUS;
  583. }
  584. void Initialization()
  585. {
  586. bool isPass = false;
  587. while(!isPass)
  588. {
  589. isPass = true;
  590. for (byte _index = 0; _index < _gunCount; _index++)
  591. {
  592. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  593. {
  594. DEBUG_ERROR("Module_PsuComm : FindChargingInfoData false \n");
  595. isPass = false;
  596. break;
  597. }
  598. }
  599. }
  600. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  601. ShmPsuData->GroupCount = 1;
  602. else
  603. ShmPsuData->GroupCount = _gunCount;
  604. char EvsePower[2];
  605. EvsePower[2] = '\0';
  606. char count = 0;
  607. byte psuTarIndex = 0;
  608. // 解析 ModelName 取得各槍各有幾個 PSU 及編號
  609. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  610. {
  611. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  612. if (strcmp(EvsePower, "15") == EQUAL)
  613. count = 5;
  614. else if (strcmp(EvsePower, "30") == EQUAL)
  615. count = 1;
  616. else if (strcmp(EvsePower, "60") == EQUAL)
  617. count = 2;
  618. else if (strcmp(EvsePower, "18") == EQUAL)
  619. count = 6;
  620. else if (strcmp(EvsePower, "36") == EQUAL)
  621. count = 12;
  622. if (count > 0)
  623. {
  624. if (ShmPsuData->GroupCount == 1)
  625. conn_1_count = count;
  626. else if (ShmPsuData->GroupCount == 2)
  627. {
  628. if(count % 2 > 0)
  629. conn_1_count = (count / 2) + 1;
  630. else
  631. conn_1_count = (count / 2);
  632. conn_2_count = count - conn_1_count;
  633. }
  634. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  635. {
  636. connector_1[psuIndex] = psuTarIndex;
  637. psuTarIndex++;
  638. }
  639. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  640. {
  641. connector_2[psuIndex] = psuTarIndex;
  642. psuTarIndex++;
  643. }
  644. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  645. PRINTF_FUNC("Connector 1 - Number = %d \n", connector_1[psuIndex]);
  646. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  647. PRINTF_FUNC("Connector 2 - Number = %d \n", connector_2[psuIndex]);
  648. }
  649. else
  650. DEBUG_ERROR("Module_PsuComm : Can't parsing model name. \n");
  651. }
  652. }
  653. void CheckSmartChargingStep(bool isWaitingAver, bool isCharging)
  654. {
  655. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  656. {
  657. if (isWaitingAver)
  658. {
  659. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  660. {
  661. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_GET_NEW_CAP============= Step 2 \n");
  662. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  663. }
  664. else
  665. {
  666. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  667. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  668. }
  669. }
  670. }
  671. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  672. {
  673. if (isCharging)
  674. {
  675. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  676. {
  677. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_A_TO_M============= Step 12 \n");
  678. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  679. }
  680. else
  681. {
  682. PRINTF_FUNC("=============Smart Charging_1 : _REASSIGNED_COMP============= Step 15 \n");
  683. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  684. }
  685. }
  686. else
  687. {
  688. PRINTF_FUNC("=============Smart Charging_2 : _REASSIGNED_COMP============= Step 15 \n");
  689. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  690. }
  691. }
  692. }
  693. int main(void)
  694. {
  695. if(InitShareMemory() == FAIL)
  696. {
  697. #ifdef SystemLogMessage
  698. DEBUG_ERROR("InitShareMemory NG\n");
  699. #endif
  700. if(ShmStatusCodeData != NULL)
  701. {
  702. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  703. }
  704. sleep(5);
  705. return 0;
  706. }
  707. PRINTF_FUNC("InitShareMemory OK\n");
  708. // register callback function
  709. RefreshStatus(&GetStatusCallback);
  710. RefreshModuleCount(&GetModuleCountCallback);
  711. RefreshAvailableCap(&GetAvailableCapCallback);
  712. RefreshFwVersion(&GetFwCallback);
  713. RefreshInputVol(&GetInputVoltageCallback);
  714. RefreshGetOutput(&GetPresentOutputCallback);
  715. RefreshFanInfo(&GetFanSpeedCallback);
  716. RefreshIavailable(&GetIavailableCallback);
  717. sleep(2);
  718. _gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  719. // initial object
  720. InitialPsuData();
  721. Initialization();
  722. libInitialize = InitialCommunication();
  723. byte isInitialComp = NO;
  724. PRINTF_FUNC("ALTERNATIVE_CONG = %d \n", ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf);
  725. //main loop
  726. while (libInitialize)
  727. {
  728. // 斷電狀態
  729. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  730. {
  731. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  732. if (!isInitialComp)
  733. {
  734. InitialPsuData();
  735. ShmPsuData->Work_Step = INITIAL_START;
  736. isInitialComp = YES;
  737. }
  738. sleep(1);
  739. continue;
  740. }
  741. else
  742. isInitialComp = NO;
  743. // 自檢失敗
  744. if (ShmPsuData->Work_Step == _NO_WORKING)
  745. {
  746. PRINTF_FUNC("== PSU == self test fail. \n");
  747. sleep(5);
  748. }
  749. switch(ShmPsuData->Work_Step)
  750. {
  751. case INITIAL_START:
  752. {
  753. PRINTF_FUNC("== PSU == INITIAL_START \n");
  754. gettimeofday(&_cmdSubPriority_time, NULL);
  755. sleep(5);
  756. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  757. ShmPsuData->Work_Step = GET_PSU_COUNT;
  758. }
  759. break;
  760. case GET_PSU_COUNT:
  761. {
  762. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  763. byte moduleCount = 0;
  764. if (time > 2000)
  765. {
  766. PRINTF_FUNC("== PSU == indexCount = %d, moduleCount = %d, sysCount = %d\n",
  767. ShmPsuData->GroupCount, moduleCount, ShmPsuData->SystemPresentPsuQuantity);
  768. // if (ShmPsuData->GroupCount == 0)
  769. // ShmPsuData->GroupCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  770. // 分別取各群模組數量
  771. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  772. {
  773. // 總和各群模組數量
  774. moduleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  775. // 取各群模組數量
  776. GetModuleCount(index);
  777. // 取版號
  778. GetModuleVer(index);
  779. }
  780. // 發送取得目前全部模組數量
  781. GetModuleCount(SYSTEM_CMD);
  782. // 判斷系統數量與各群數量一致
  783. if(moduleCount == ShmPsuData->SystemPresentPsuQuantity && moduleCount > 0)
  784. {
  785. PRINTF_FUNC("Psu Count = %d \n", moduleCount);
  786. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  787. {
  788. // 電樁在 Booting 的狀態 - 自檢
  789. PRINTF_FUNC("== PSU == GET_SYS_CAP \n");
  790. ShmPsuData->Work_Step = GET_SYS_CAP;
  791. }
  792. else
  793. {
  794. PRINTF_FUNC("== PSU == _WORK_CHARGING \n");
  795. ShmPsuData->Work_Step = _WORK_CHARGING;
  796. //sdlu test
  797. gettimeofday(&_test_time, NULL);
  798. }
  799. }
  800. _getCapDelayCount = 3;
  801. gettimeofday(&_cmdSubPriority_time, NULL);
  802. }
  803. }
  804. break;
  805. case GET_SYS_CAP:
  806. {
  807. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  808. if (time > 1000)
  809. {
  810. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  811. {
  812. // Pooling Status
  813. GetStatus(index);
  814. // 取系統總輸出能力
  815. GetModuleCap(index);
  816. }
  817. _getCapDelayCount--;
  818. gettimeofday(&_cmdSubPriority_time, NULL);
  819. }
  820. // 判斷系統輸出額定功率與電流
  821. if (ShmPsuData->SystemAvailablePower > 0 && ShmPsuData->SystemAvailableCurrent > 0 &&
  822. _getCapDelayCount <= 0)
  823. {
  824. PRINTF_FUNC("SystemAvailableCurrent = %d, SystemAvailablePower = %d \n",
  825. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  826. PRINTF_FUNC("== PSU == BOOTING_COMPLETE \n");
  827. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  828. }
  829. }
  830. break;
  831. case BOOTING_COMPLETE:
  832. {
  833. bool isSelfTestPass = true;
  834. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  835. {
  836. if (chargingInfo[groupIndex]->SystemStatus == S_BOOTING)
  837. {
  838. isSelfTestPass = false;
  839. }
  840. }
  841. if (isSelfTestPass)
  842. ShmPsuData->Work_Step = _WORK_CHARGING;
  843. sleep(1);
  844. }
  845. break;
  846. case _WORK_CHARGING:
  847. {
  848. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  849. // 低 Priority 的指令
  850. if (time > 1500)
  851. {
  852. isCharging = false;
  853. isWaitingAver = false;
  854. isReadToCharging = false;
  855. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  856. {
  857. // Pooling Status
  858. GetStatus(index);
  859. // 取系統總輸出能力
  860. GetModuleCap(index);
  861. // 取得模塊輸入電壓
  862. GetModuleInput(index);
  863. // 取得模塊輸出額定電流能力
  864. GetModuleIavailable(index);
  865. if (chargingInfo[index]->SystemStatus == S_CHARGING)
  866. {
  867. isCharging = true;
  868. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  869. {
  870. if (toAverVolPoint > 0 &&
  871. toAverVolPoint == chargingInfo[index]->EvBatterytargetCurrent)
  872. {
  873. // 欲最大充 -> 均充需要等待充電中的輸出電流拉高到目標電流
  874. if ((chargingInfo[index]->PresentChargingCurrent * 10) >=
  875. chargingInfo[index]->EvBatterytargetCurrent - CHK_CUR_RANGE)
  876. {
  877. if (toAverVolCount == 0)
  878. isWaitingAver = true;
  879. else
  880. toAverVolCount--;
  881. }
  882. }
  883. else
  884. {
  885. toAverVolPoint = chargingInfo[index]->EvBatterytargetCurrent;
  886. toAverVolCount = 3;
  887. }
  888. }
  889. else
  890. {
  891. toAverVolPoint = 0;
  892. toAverVolCount = 3;
  893. }
  894. }
  895. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  896. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  897. {
  898. isReadToCharging = true;
  899. }
  900. }
  901. gettimeofday(&_cmdSubPriority_time, NULL);
  902. CheckSmartChargingStep(isWaitingAver, isCharging);
  903. }
  904. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  905. {
  906. GetModuleOutput(groupIndex);
  907. // 針對各槍當前狀態,傳送需要回傳的資料指令
  908. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  909. (chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING && chargingInfo[groupIndex]->Type == _Type_GB) ||
  910. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  911. {
  912. if (chargingInfo[groupIndex]->EvBatterytargetVoltage > 0 &&
  913. carReqVol != chargingInfo[groupIndex]->EvBatterytargetVoltage)
  914. {
  915. carReqVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  916. DEBUG_INFO("ev need vol = %f \n", chargingInfo[groupIndex]->EvBatterytargetVoltage);
  917. }
  918. if (chargingInfo[groupIndex]->EvBatterytargetCurrent > 0 &&
  919. carReqCur != chargingInfo[groupIndex]->EvBatterytargetCurrent)
  920. {
  921. carReqCur = chargingInfo[groupIndex]->EvBatterytargetCurrent;
  922. DEBUG_INFO("ev need cur = %f \n", chargingInfo[groupIndex]->EvBatterytargetCurrent);
  923. }
  924. if (time > 1500)
  925. {
  926. if (chargingInfo[groupIndex]->FireChargingVoltage > 0 &&
  927. evseOutVol != (chargingInfo[groupIndex]->FireChargingVoltage / 10))
  928. {
  929. evseOutVol = (chargingInfo[groupIndex]->FireChargingVoltage / 10);
  930. PRINTF_FUNC("groupIndex = %d, evse output vol = %f \n", groupIndex,
  931. chargingInfo[groupIndex]->FireChargingVoltage);
  932. }
  933. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) > 0 &&
  934. evseOutCur != (chargingInfo[groupIndex]->PresentChargingCurrent * 10))
  935. {
  936. evseOutCur = (chargingInfo[groupIndex]->PresentChargingCurrent * 10);
  937. PRINTF_FUNC("groupIndex = %d, evse output cur = %f \n", groupIndex,
  938. (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  939. }
  940. }
  941. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  942. {
  943. // PRINTF_FUNC("index = %d, SystemStatus = %d, Ev = %f, curCur = %f \n", groupIndex,
  944. // chargingInfo[groupIndex]->SystemStatus, chargingInfo[groupIndex]->EvBatterytargetCurrent,
  945. // (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  946. // 智能判斷 Start -----------------------------------------------------------
  947. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  948. {
  949. if (ShmPsuData->SystemAvailableCurrent != chargingInfo[groupIndex]->AvailableChargingCurrent)
  950. {
  951. // 車端要求電流為該充電槍的額定輸出電流的範圍內
  952. if (chargingInfo[groupIndex]->EvBatterytargetCurrent <= chargingInfo[groupIndex]->AvailableChargingCurrent ||
  953. deratingKeepCount >= DERATING)
  954. {
  955. // 車端降載完成
  956. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_M_TO_A============= Step 3 \n");
  957. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_M_TO_A;
  958. gettimeofday(&_derating_time, NULL);
  959. deratingKeepCount = 0;
  960. }
  961. else
  962. {
  963. deratingKeepCount++;
  964. }
  965. }
  966. }
  967. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_M_TO_A)
  968. {
  969. bool isChanged = false;
  970. // 需求電流不降低的情況下 -> 依然要切
  971. if (chargingInfo[groupIndex]->AvailableChargingCurrent < chargingInfo[groupIndex]->EvBatterytargetCurrent)
  972. {
  973. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A ** Gun_%d, AvailableChargingCurrent = %f, EvBatterytargetCurrent = %f \n", groupIndex,
  974. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  975. chargingInfo[groupIndex]->AvailableChargingCurrent);
  976. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  977. {
  978. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  979. {
  980. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= CHK_CUR_RANGE)
  981. isChanged = true;
  982. break;
  983. }
  984. }
  985. // 這狀況下輸出端的電流載滿載衝的狀況下,並不會降電流
  986. // 所以只能拉載到該槍端的最大輸出能力
  987. // if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) >= chargingInfo[groupIndex]->AvailableChargingCurrent - CHK_CUR_RANGE ||
  988. // (chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= CHK_CUR_RANGE)
  989. // {
  990. // isChanged = true;
  991. // }
  992. }
  993. else if (((chargingInfo[groupIndex]->PresentChargingCurrent * 10) >= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent - CHK_CUR_RANGE) &&
  994. ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + CHK_CUR_RANGE))
  995. {
  996. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  997. {
  998. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  999. {
  1000. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= CHK_CUR_RANGE)
  1001. isChanged = true;
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. if (isChanged)
  1007. {
  1008. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A ** Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d \n", groupIndex,
  1009. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1010. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1011. // 輸出端與車端要求電流接近
  1012. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_M_TO_A============= Step 4 \n");
  1013. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1014. }
  1015. }
  1016. if (ShmPsuData->SystemAvailablePower > 0)
  1017. {
  1018. // 調整輸出電流 : 漸進調整方式
  1019. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A)
  1020. {
  1021. // 當前充電中的目標電壓
  1022. float targetVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  1023. // 當前充電中的目標電流
  1024. //float targetCur = 0;
  1025. // 準備切出去的模塊電流
  1026. float deratingCur = 0;
  1027. byte reassignIndex = ELEMENT_NOT_FIND;
  1028. // 找到等待分配的槍
  1029. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1030. {
  1031. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1032. {
  1033. reassignIndex = subIndex;
  1034. }
  1035. }
  1036. if (reassignIndex != ELEMENT_NOT_FIND)
  1037. {
  1038. //int derating = GetTimeoutValue(_derating_time) / 1000;
  1039. //if (derating > 1000)
  1040. {
  1041. if (ShmPsuData->PsuGroup[reassignIndex].GroupPresentOutputCurrent > 0)
  1042. {
  1043. deratingCur = ShmPsuData->PsuGroup[reassignIndex].GroupPresentOutputCurrent - DERATING_RANGE;
  1044. if (deratingCur <= CHK_CUR_RANGE)
  1045. deratingCur = CHK_CUR_RANGE;
  1046. PresentOutputVol(reassignIndex, targetVol, deratingCur);
  1047. gettimeofday(&_derating_time, NULL);
  1048. }
  1049. }
  1050. // ***********直接降低要移除的模塊電流即可*************
  1051. // 因為爬的速度沒有降的速度快,所以採兩倍速度爬升
  1052. // targetCur = ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + (DERATING_RANGE * 2);
  1053. // if (targetCur >= chargingInfo[groupIndex]->EvBatterytargetCurrent)
  1054. // targetCur = chargingInfo[groupIndex]->EvBatterytargetCurrent;
  1055. if (targetVol == 0)
  1056. {
  1057. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1058. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1059. }
  1060. else
  1061. {
  1062. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1063. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1064. }
  1065. }
  1066. }
  1067. else
  1068. {
  1069. // 該充電槍的目標電壓與目標電流
  1070. PresentOutputVol(SYSTEM_CMD,
  1071. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1072. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1073. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1074. {
  1075. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1076. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1077. }
  1078. else
  1079. {
  1080. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1081. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1082. }
  1083. }
  1084. }
  1085. }
  1086. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1087. {
  1088. // 智能判斷 Start -----------------------------------------------------------
  1089. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  1090. {
  1091. bool balanceVol = true;
  1092. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1093. {
  1094. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1095. chargingInfo[subIndex]->SystemStatus == S_RESERVATION)
  1096. {
  1097. // 各群電壓接近平衡
  1098. if (((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE) ||
  1099. ((chargingInfo[subIndex]->PresentChargingVoltage * 10) < chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE))
  1100. {
  1101. PRINTF_FUNC("** _REASSIGNED_ADJUST_A_TO_M ** Gun_%d, PresentChargingVoltage = %f, PresentChargingVoltage_V = %f, EvBatterytargetVoltage = %f \n", subIndex,
  1102. (chargingInfo[subIndex]->PresentChargingVoltage * 10),
  1103. ((chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE),
  1104. (chargingInfo[groupIndex]->EvBatterytargetVoltage - CHK_VOL_RANGE));
  1105. balanceVol = false;
  1106. }
  1107. break;
  1108. }
  1109. }
  1110. if (balanceVol)
  1111. {
  1112. // 閒置端與車端要求電壓接近
  1113. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_A_TO_M============= Step 13 \n");
  1114. preChargingCur = preChargingTarget = 0;
  1115. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_A_TO_M;
  1116. }
  1117. }
  1118. else if(ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_WAITING)
  1119. {
  1120. int idleCurrent = 0;
  1121. int chargingCurrent = 0;
  1122. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1123. {
  1124. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1125. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1126. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1127. idleCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1128. else
  1129. chargingCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1130. }
  1131. if (idleCurrent >= chargingCurrent - PRE_CHARG_RANGE)
  1132. {
  1133. PRINTF_FUNC("=============Smart Charging_0 : _REASSIGNED_COMP============= Step 15 \n");
  1134. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1135. }
  1136. }
  1137. if (chargingInfo[groupIndex]->AvailableChargingCurrent > 0)
  1138. {
  1139. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_A_TO_M)
  1140. {
  1141. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1142. {
  1143. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1144. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1145. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1146. {
  1147. // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1148. PresentOutputVol(subIndex,
  1149. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1150. ZERO_CURRENT + preChargingTarget);
  1151. preChargingCur = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1152. }
  1153. else
  1154. {
  1155. PRINTF_FUNC("CurOutputCurrent = %d \n", ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent + preChargingCur);
  1156. // 充電中的模塊維持輸出
  1157. PresentOutputVol(subIndex,
  1158. chargingInfo[subIndex]->EvBatterytargetVoltage,
  1159. chargingInfo[subIndex]->EvBatterytargetCurrent - preChargingCur);
  1160. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING) &&
  1161. (preChargingCur >= preChargingTarget - ZERO_CURRENT))
  1162. preChargingTarget += PRE_CHARG_STEP_CUR;
  1163. if (preChargingTarget >= chargingInfo[subIndex]->EvBatterytargetCurrent / 2)
  1164. preChargingTarget = chargingInfo[subIndex]->EvBatterytargetCurrent / 2;
  1165. }
  1166. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1167. {
  1168. SwitchPower(subIndex, PSU_POWER_OFF);
  1169. FlashLed(subIndex, PSU_FLASH_NORMAL);
  1170. }
  1171. else
  1172. {
  1173. SwitchPower(subIndex, PSU_POWER_ON);
  1174. FlashLed(subIndex, PSU_FLASH_ON);
  1175. }
  1176. }
  1177. }
  1178. else
  1179. {
  1180. PresentOutputVol(groupIndex,
  1181. chargingInfo[groupIndex]->EvBatterytargetVoltage,
  1182. chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1183. if (chargingInfo[groupIndex]->EvBatterytargetVoltage == 0)
  1184. {
  1185. SwitchPower(groupIndex, PSU_POWER_OFF);
  1186. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1187. }
  1188. else
  1189. {
  1190. SwitchPower(groupIndex, PSU_POWER_ON);
  1191. FlashLed(groupIndex, PSU_FLASH_ON);
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. else if (chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  1198. chargingInfo[groupIndex]->SystemStatus <= S_COMPLETE)
  1199. {
  1200. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1201. {
  1202. if (!isCharging)
  1203. {
  1204. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1205. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1206. if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1207. {
  1208. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1209. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1210. {
  1211. // 代表在切換的過程中,停止充電了
  1212. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= STOP_CURRENT)
  1213. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1214. }
  1215. }
  1216. }
  1217. else if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1218. {
  1219. // 代表充電的槍依舊在充電,欲進入充電的槍取消充電了
  1220. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1221. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1222. {
  1223. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1224. }
  1225. }
  1226. }
  1227. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1228. {
  1229. if (!isReadToCharging)
  1230. {
  1231. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1232. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1233. }
  1234. else
  1235. {
  1236. SwitchPower(groupIndex, PSU_POWER_OFF);
  1237. FlashLed(groupIndex, PSU_FLASH_NORMAL);
  1238. }
  1239. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1240. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1241. else
  1242. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1243. }
  1244. }
  1245. }
  1246. break;
  1247. }
  1248. }
  1249. usleep(20000);
  1250. }
  1251. return FAIL;
  1252. }