Module_PsuComm.c 74 KB

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