Module_PsuComm.c 71 KB

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