Module_PsuComm.c 40 KB

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