Module_PsuComm.c 72 KB

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