Module_PsuComm.c 83 KB

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