Module_PsuComm.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  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].AlarmCode = alarm;
  925. AbnormalStopAnalysis(group1, alarm);
  926. }
  927. void GetModuleInputCallback(byte address, unsigned short inputR,
  928. unsigned short inputS, unsigned short inputT)
  929. {
  930. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  931. return;
  932. if (IsOverModuleCount(address))
  933. return;
  934. byte group = FindTargetGroup(address);
  935. if (group == 1)
  936. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  937. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = inputR;
  938. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = inputS;
  939. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = inputT;
  940. //PRINTF_FUNC("***Input*** address = %d, R = %d, S = %d, T = %d \n",
  941. // address, inputR, inputS, inputT);
  942. }
  943. //==========================================
  944. // Init all share memory
  945. //==========================================
  946. int InitShareMemory()
  947. {
  948. int result = PASS;
  949. int MeterSMId;
  950. //creat ShmSysConfigAndInfo
  951. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  952. {
  953. #ifdef SystemLogMessage
  954. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG %d \n");
  955. #endif
  956. result = FAIL;
  957. }
  958. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  959. {
  960. #ifdef SystemLogMessage
  961. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG \n");
  962. #endif
  963. result = FAIL;
  964. }
  965. else
  966. {}
  967. //creat ShmStatusCodeData
  968. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  969. {
  970. #ifdef SystemLogMessage
  971. DEBUG_ERROR("shmget ShmStatusCodeData NG \n");
  972. #endif
  973. result = FAIL;
  974. }
  975. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  976. {
  977. #ifdef SystemLogMessage
  978. DEBUG_ERROR("shmat ShmStatusCodeData NG \n");
  979. #endif
  980. result = FAIL;
  981. }
  982. else
  983. {}
  984. //creat ShmPsuData
  985. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  986. {
  987. #ifdef SystemLogMessage
  988. DEBUG_ERROR("shmget ShmPsuData NG \n");
  989. #endif
  990. result = FAIL;
  991. }
  992. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  993. {
  994. #ifdef SystemLogMessage
  995. DEBUG_ERROR("shmat ShmPsuData NG \n");
  996. #endif
  997. result = FAIL;
  998. }
  999. memset(ShmPsuData,0,sizeof(struct PsuData));
  1000. return result;
  1001. }
  1002. //================================================
  1003. // Main process
  1004. //================================================
  1005. void InitialPsuData()
  1006. {
  1007. ShmPsuData->SystemPresentPsuQuantity = 0;
  1008. ShmPsuData->SystemAvailablePower = 0;
  1009. PRINTF_FUNC("************ psu Group = %d \n", ShmPsuData->GroupCount);
  1010. for (byte _groupCount = 0; _groupCount < ShmPsuData->GroupCount; _groupCount++)
  1011. {
  1012. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  1013. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  1014. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  1015. }
  1016. }
  1017. void Initialization()
  1018. {
  1019. bool isPass = false;
  1020. while(!isPass)
  1021. {
  1022. isPass = true;
  1023. for (byte _index = 0; _index < _gunCount; _index++)
  1024. {
  1025. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  1026. {
  1027. DEBUG_ERROR("Module_PsuComm : FindChargingInfoData false \n");
  1028. isPass = false;
  1029. break;
  1030. }
  1031. }
  1032. sleep(1);
  1033. }
  1034. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  1035. ShmPsuData->GroupCount = 1;
  1036. else
  1037. ShmPsuData->GroupCount = _gunCount;
  1038. }
  1039. void CheckSmartChargingStep(bool isWaitingAver, bool isCharging, bool canAverageCharging)
  1040. {
  1041. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  1042. {
  1043. if (isWaitingAver)
  1044. {
  1045. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1046. {
  1047. ShmSysConfigAndInfo->SysInfo.CanAverageCharging = canAverageCharging;
  1048. if (canAverageCharging)
  1049. {
  1050. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_GET_NEW_CAP============= Step 2 \n");
  1051. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  1052. }
  1053. else
  1054. {
  1055. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  1056. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1057. }
  1058. }
  1059. else
  1060. {
  1061. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  1062. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1063. }
  1064. }
  1065. }
  1066. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  1067. {
  1068. if (isCharging)
  1069. {
  1070. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1071. {
  1072. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_A_TO_M============= Step 12 \n");
  1073. preChargingCur = preChargingTarget = 0;
  1074. gettimeofday(&_max_time, NULL);
  1075. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  1076. }
  1077. else
  1078. {
  1079. PRINTF_FUNC("=============Smart Charging_1 : _REASSIGNED_COMP============= Step 15 \n");
  1080. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1081. }
  1082. }
  1083. else
  1084. {
  1085. PRINTF_FUNC("=============Smart Charging_2 : _REASSIGNED_COMP============= Step 15 \n");
  1086. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1087. }
  1088. }
  1089. }
  1090. void PreCheckSmartChargingStep()
  1091. {
  1092. isCharging = false;
  1093. isWaitingAver = false;
  1094. isReadToCharging = false;
  1095. CanAverageCharging = true;
  1096. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1097. {
  1098. // Pooling Status
  1099. //GetStatus(index);
  1100. // 取得模塊輸出額定電流能力
  1101. //GetModuleIavailable(index);
  1102. if (chargingInfo[index]->SystemStatus == S_CHARGING)
  1103. {
  1104. isCharging = true;
  1105. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  1106. {
  1107. if (toAverVolPoint == (chargingInfo[index]->EvBatterytargetCurrent * 10))
  1108. {
  1109. // 欲最大充 -> 均充需要等待充電中的輸出電流拉高到目標電流
  1110. if ((chargingInfo[index]->PresentChargingCurrent * 10) >=
  1111. (chargingInfo[index]->EvBatterytargetCurrent * 10) - CHK_CUR_RANGE)
  1112. {
  1113. if (toAverVolCount == 0)
  1114. isWaitingAver = true;
  1115. else
  1116. toAverVolCount--;
  1117. }
  1118. }
  1119. else
  1120. {
  1121. toAverVolPoint = (chargingInfo[index]->EvBatterytargetCurrent * 10);
  1122. toAverVolCount = 3;
  1123. }
  1124. }
  1125. else
  1126. {
  1127. toAverVolPoint = 0;
  1128. toAverVolCount = 3;
  1129. }
  1130. }
  1131. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  1132. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1133. {
  1134. isReadToCharging = true;
  1135. }
  1136. if (chargingInfo[index]->DeratingChargingCurrent < STOP_CURRENT)
  1137. {
  1138. CanAverageCharging = false;
  1139. }
  1140. }
  1141. CheckSmartChargingStep(isWaitingAver, isCharging, CanAverageCharging);
  1142. }
  1143. void Await()
  1144. {
  1145. usleep(CMD_DELAY_TIME);
  1146. }
  1147. int main(void)
  1148. {
  1149. if(InitShareMemory() == FAIL)
  1150. {
  1151. #ifdef SystemLogMessage
  1152. DEBUG_ERROR("InitShareMemory NG\n");
  1153. #endif
  1154. if(ShmStatusCodeData != NULL)
  1155. {
  1156. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  1157. }
  1158. sleep(5);
  1159. return 0;
  1160. }
  1161. PRINTF_FUNC("InitShareMemory OK\n");
  1162. // register callback function
  1163. RefreshStatus(&GetStatusCallback);
  1164. RefreshModuleCount(&GetModuleCountCallback);
  1165. RefreshAvailableCap(&GetAvailableCapCallback);
  1166. RefreshFwVersion(&GetFwCallback);
  1167. RefreshInputVol(&GetInputVoltageCallback);
  1168. RefreshGetOutput(&GetPresentOutputCallback);
  1169. RefreshMisInfo(&GetMisCallback);
  1170. RefreshIavailable(&GetIavailableCallback);
  1171. RefreshGetOutputF(&GetPresentOutputFCallback);
  1172. // GetPresentOutputCallback & GetStatusCallback
  1173. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  1174. // GetStatusCallback
  1175. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  1176. // GetInputVoltageCallback
  1177. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  1178. sleep(2);
  1179. _gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1180. // initial object
  1181. InitialPsuData();
  1182. Initialization();
  1183. libInitialize = InitialCommunication();
  1184. byte isInitialComp = NO;
  1185. PRINTF_FUNC("ALTERNATIVE_CONG = %d \n", ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf);
  1186. //main loop
  1187. while (libInitialize)
  1188. {
  1189. // 斷電狀態
  1190. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1191. {
  1192. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  1193. if (!isInitialComp)
  1194. {
  1195. ShmPsuData->Work_Step = INITIAL_START;
  1196. psuCmdSeq = _PSU_CMD_STATUS;
  1197. sleep(2);
  1198. InitialPsuData();
  1199. isInitialComp = YES;
  1200. }
  1201. sleep(1);
  1202. continue;
  1203. }
  1204. else
  1205. isInitialComp = NO;
  1206. // 自檢失敗
  1207. if (ShmPsuData->Work_Step == _NO_WORKING)
  1208. {
  1209. PRINTF_FUNC("== PSU == self test fail. \n");
  1210. sleep(5);
  1211. }
  1212. switch(ShmPsuData->Work_Step)
  1213. {
  1214. case INITIAL_START:
  1215. {
  1216. PRINTF_FUNC("== PSU == INITIAL_START \n");
  1217. gettimeofday(&_cmdSubPriority_time, NULL);
  1218. sleep(5);
  1219. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1220. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  1221. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1222. isStartOutputSwitch[index] = false;
  1223. ShmPsuData->Work_Step = GET_PSU_COUNT;
  1224. }
  1225. break;
  1226. case GET_PSU_COUNT:
  1227. {
  1228. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1229. byte moduleCount = 0;
  1230. // 發送取得目前全部模組數量
  1231. GetModuleCount(SYSTEM_CMD);
  1232. if (time > 2000)
  1233. {
  1234. // if (ShmPsuData->GroupCount == 0)
  1235. // ShmPsuData->GroupCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1236. // 分別取各群模組數量
  1237. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1238. {
  1239. // 總和各群模組數量
  1240. moduleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  1241. // 取各群模組數量
  1242. GetModuleCount(index);
  1243. }
  1244. PRINTF_FUNC("== PSU == indexCount = %d, moduleCount = %d, sysCount = %d\n",
  1245. ShmPsuData->GroupCount, moduleCount, ShmPsuData->SystemPresentPsuQuantity);
  1246. // 判斷系統數量與各群數量一致
  1247. if(moduleCount == ShmPsuData->SystemPresentPsuQuantity && moduleCount > 0)
  1248. {
  1249. PRINTF_FUNC("Psu Count = %d \n", moduleCount);
  1250. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  1251. {
  1252. // 電樁在 Booting 的狀態 - 自檢
  1253. PRINTF_FUNC("== PSU == GET_SYS_CAP \n");
  1254. ShmPsuData->Work_Step = GET_SYS_CAP;
  1255. }
  1256. else
  1257. {
  1258. PRINTF_FUNC("== PSU == _WORK_CHARGING \n");
  1259. ShmPsuData->Work_Step = _WORK_CHARGING;
  1260. gettimeofday(&_test_time, NULL);
  1261. }
  1262. }
  1263. gettimeofday(&_cmdSubPriority_time, NULL);
  1264. }
  1265. }
  1266. break;
  1267. case GET_SYS_CAP:
  1268. {
  1269. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1270. if (time > 500)
  1271. {
  1272. bool isFinish = true;
  1273. for (byte psuCount = 0; psuCount < ShmPsuData->SystemPresentPsuQuantity; psuCount++)
  1274. {
  1275. if (strcmp((char *)ShmPsuData->PsuVersion[psuCount].FwPrimaryVersion, "") == EQUAL ||
  1276. ShmPsuData->SystemAvailablePower <= 0 || ShmPsuData->SystemAvailableCurrent <= 0)
  1277. {
  1278. isFinish = false;
  1279. break;
  1280. }
  1281. }
  1282. if (!isFinish)
  1283. {
  1284. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1285. {
  1286. if (psuCmdSeq == _PSU_CMD_STATUS)
  1287. {
  1288. // 取得狀態 : 支援模塊不須按照順序功能
  1289. GetStatus(index);
  1290. }
  1291. else if (psuCmdSeq == _PSU_CMD_CAP)
  1292. {
  1293. // 取系統總輸出能力
  1294. GetModuleCap(index);
  1295. }
  1296. else if (psuCmdSeq == _PSU_CMD_VERSION)
  1297. {
  1298. // 取版號
  1299. GetModuleVer(index);
  1300. }
  1301. }
  1302. if (psuCmdSeq == _PSU_CMD_STATUS)
  1303. psuCmdSeq = _PSU_CMD_CAP;
  1304. else if (psuCmdSeq == _PSU_CMD_CAP)
  1305. psuCmdSeq = _PSU_CMD_VERSION;
  1306. else
  1307. psuCmdSeq = _PSU_CMD_STATUS;
  1308. }
  1309. else
  1310. {
  1311. // 判斷系統輸出額定功率與電流
  1312. PRINTF_FUNC("SystemAvailableCurrent = %d, SystemAvailablePower = %d \n",
  1313. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  1314. PRINTF_FUNC("== PSU == BOOTING_COMPLETE \n");
  1315. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  1316. }
  1317. gettimeofday(&_cmdSubPriority_time, NULL);
  1318. }
  1319. }
  1320. break;
  1321. case BOOTING_COMPLETE:
  1322. {
  1323. bool isSelfTestPass = true;
  1324. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1325. {
  1326. if (chargingInfo[groupIndex]->SystemStatus == S_BOOTING)
  1327. {
  1328. isSelfTestPass = false;
  1329. }
  1330. }
  1331. if (isSelfTestPass)
  1332. ShmPsuData->Work_Step = _WORK_CHARGING;
  1333. sleep(1);
  1334. }
  1335. break;
  1336. case _WORK_CHARGING:
  1337. {
  1338. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1339. // 低 Priority 的指令
  1340. if (time > 1500)
  1341. {
  1342. PreCheckSmartChargingStep();
  1343. gettimeofday(&_cmdSubPriority_time, NULL);
  1344. }
  1345. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1346. {
  1347. if (psuCmdSeq == _PSU_CMD_CAP)
  1348. {
  1349. // 取系統總輸出能力
  1350. GetModuleCap(groupIndex);
  1351. }
  1352. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1353. {
  1354. // 取各群輸出電壓電流 (float)
  1355. GetModuleOutputF(groupIndex);
  1356. }
  1357. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1358. {
  1359. // 取得模塊輸出額定電流能力
  1360. GetModuleIavailable(groupIndex);
  1361. }
  1362. else if (psuCmdSeq == _PSU_CMD_TEMP)
  1363. {
  1364. // 取得模塊溫度
  1365. GetDcTemperature(groupIndex);
  1366. }
  1367. }
  1368. Await();
  1369. if (psuCmdSeq == _PSU_CMD_CAP)
  1370. psuCmdSeq = _PSU_CMD_OUTPUT;
  1371. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1372. psuCmdSeq = _PSU_CMD_IVAILIABLE;
  1373. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1374. psuCmdSeq = _PSU_CMD_TEMP;
  1375. else
  1376. psuCmdSeq = _PSU_CMD_CAP;
  1377. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1378. {
  1379. // 針對各槍當前狀態,傳送需要回傳的資料指令
  1380. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  1381. (chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING && chargingInfo[groupIndex]->Type == _Type_GB) ||
  1382. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1383. {
  1384. if (chargingInfo[groupIndex]->FireChargingVoltage > 0 &&
  1385. evseOutVol != (chargingInfo[groupIndex]->FireChargingVoltage / 10))
  1386. {
  1387. evseOutVol = (chargingInfo[groupIndex]->FireChargingVoltage / 10);
  1388. PRINTF_FUNC("groupIndex = %d, ev need vol = %f, evse output vol = %f \n", groupIndex,
  1389. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1390. chargingInfo[groupIndex]->FireChargingVoltage);
  1391. }
  1392. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) > 0 &&
  1393. evseOutCur != (chargingInfo[groupIndex]->PresentChargingCurrent * 10))
  1394. {
  1395. evseOutCur = (chargingInfo[groupIndex]->PresentChargingCurrent * 10);
  1396. PRINTF_FUNC("groupIndex = %d, ev need cur = %f, evse output cur = %f \n", groupIndex,
  1397. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10),
  1398. (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  1399. }
  1400. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1401. {
  1402. //PRINTF_FUNC("index = %d, SystemStatus = %d, Ev = %f, curCur = %f \n", groupIndex,
  1403. // chargingInfo[groupIndex]->SystemStatus, chargingInfo[groupIndex]->EvBatterytargetCurrent,
  1404. // (chargingInfo[groupIndex]->PresentChargingCurrent * 10));
  1405. // 智能判斷 Start -----------------------------------------------------------
  1406. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1407. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1408. {
  1409. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  1410. {
  1411. if (chargingInfo[groupIndex]->DividChargingCurrent == 0)
  1412. {
  1413. chargingInfo[groupIndex]->DividChargingCurrent = ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent;
  1414. }
  1415. }
  1416. PRINTF_FUNC("Index = %d, DividChargingCurrent = %f \n", groupIndex, chargingInfo[groupIndex]->DividChargingCurrent);
  1417. }
  1418. else
  1419. {
  1420. chargingInfo[groupIndex]->DividChargingCurrent = 0;
  1421. chargingInfo[groupIndex]->MaxChargingToAverPassFlag = 0;
  1422. }
  1423. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  1424. {
  1425. if (ShmPsuData->SystemAvailableCurrent != chargingInfo[groupIndex]->AvailableChargingCurrent)
  1426. {
  1427. // 車端要求電流為該充電槍的額定輸出電流的範圍內
  1428. if ((chargingInfo[groupIndex]->EvBatterytargetCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP ||
  1429. deratingKeepCount >= DERATING_COUNT)
  1430. {
  1431. // 車端降載完成
  1432. PRINTF_FUNC("Index = %d, newEvCurrent = %f \n", groupIndex, (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1433. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_ADJUST_M_TO_A============= Step 3 \n");
  1434. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_M_TO_A;
  1435. gettimeofday(&_derating_time, NULL);
  1436. deratingKeepCount = 0;
  1437. }
  1438. else
  1439. {
  1440. deratingKeepCount++;
  1441. PRINTF_FUNC("** Step 2 ** : Index = %d, EvBatterytargetCurrent = %f, TargetCurrent = %d, Count = %d \n",
  1442. groupIndex,
  1443. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10),
  1444. (ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP),
  1445. deratingKeepCount);
  1446. }
  1447. }
  1448. }
  1449. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_M_TO_A)
  1450. {
  1451. bool isChanged = false;
  1452. if (chargingInfo[groupIndex]->AvailableChargingCurrent <= (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10))
  1453. {
  1454. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A ** Gun_%d, PresentChargingCurrent = %f, AvailableChargingCurrent = %f, EvBatterytargetCurrent = %f \n", groupIndex,
  1455. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1456. chargingInfo[groupIndex]->AvailableChargingCurrent,
  1457. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1458. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1459. {
  1460. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1461. {
  1462. // 當 B 模塊輸出電流小於 5A 及退開 relay
  1463. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= 50)
  1464. isChanged = true;
  1465. break;
  1466. }
  1467. }
  1468. }
  1469. else if (((chargingInfo[groupIndex]->PresentChargingCurrent * 10) >= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent - CHK_CUR_RANGE) &&
  1470. ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + CHK_CUR_RANGE))
  1471. {
  1472. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1473. {
  1474. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1475. {
  1476. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= CHK_CUR_RANGE)
  1477. isChanged = true;
  1478. break;
  1479. }
  1480. }
  1481. }
  1482. if (isChanged)
  1483. {
  1484. PRINTF_FUNC("** _REASSIGNED_ADJUST_M_TO_A To 4** Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d \n", groupIndex,
  1485. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1486. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1487. // 輸出端與車端要求電流接近
  1488. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_M_TO_A============= Step 4 \n");
  1489. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1490. }
  1491. else
  1492. {
  1493. if ((GetTimeoutValue(_derating_time) / 1000) > 1000)
  1494. {
  1495. gettimeofday(&_derating_time, NULL);
  1496. }
  1497. }
  1498. }
  1499. // 調整輸出電流 : 漸進調整方式
  1500. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1501. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_M_TO_A)
  1502. {
  1503. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A)
  1504. {
  1505. // 當前充電中的目標電壓
  1506. float targetVol = (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10);
  1507. byte reassignIndex = ELEMENT_NOT_FIND;
  1508. // 找到等待分配的槍
  1509. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1510. {
  1511. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1512. {
  1513. reassignIndex = subIndex;
  1514. break;
  1515. }
  1516. }
  1517. if (reassignIndex != ELEMENT_NOT_FIND)
  1518. {
  1519. if ((GetTimeoutValue(_derating_time) / 1000) <= 50 ||
  1520. chargingInfo[groupIndex]->MaxChargingToAverPassFlag == 0)
  1521. {
  1522. chargingInfo[groupIndex]->MaxChargingToAverPassFlag = 1;
  1523. PresentOutputVol(groupIndex, targetVol, (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1524. PresentOutputVol(reassignIndex, targetVol, CHK_CUR_RANGE); Await();
  1525. }
  1526. }
  1527. }
  1528. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1529. {
  1530. bool isNeedToClosePower = false;
  1531. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1532. {
  1533. if (isStartOutputSwitch[index])
  1534. {
  1535. isNeedToClosePower = true;
  1536. }
  1537. isStartOutputSwitch[index] = false;
  1538. }
  1539. if (isNeedToClosePower)
  1540. {
  1541. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1542. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1543. }
  1544. }
  1545. }
  1546. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY_M_TO_A)
  1547. {
  1548. //PRINTF_FUNC("set out (%d) value = %f******** 3 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1549. PresentOutputVol(groupIndex,
  1550. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1551. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1552. }
  1553. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1554. {
  1555. //PRINTF_FUNC("set out (sys) value = %f, smart step = %d******** 4 \n",
  1556. // chargingInfo[groupIndex]->EvBatterytargetCurrent, ShmSysConfigAndInfo->SysInfo.ReAssignedFlag);
  1557. // 該充電槍的目標電壓與目標電流
  1558. //sdlu
  1559. // if ((chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1560. // chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1561. // {
  1562. // PresentOutputVol(SYSTEM_CMD,
  1563. // (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1564. // 0);
  1565. // }
  1566. // else
  1567. // {
  1568. // PresentOutputVol(SYSTEM_CMD,
  1569. // (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - 100,
  1570. // (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1571. // }
  1572. // printf("groupIndex = %d, EvBatterytargetCurrent = %f \n", groupIndex,
  1573. // (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1574. PresentOutputVol(SYSTEM_CMD,
  1575. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1576. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1577. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1578. {
  1579. bool isNeedToClosePower = false;
  1580. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1581. {
  1582. if (isStartOutputSwitch[index])
  1583. {
  1584. isNeedToClosePower = true;
  1585. }
  1586. isStartOutputSwitch[index] = false;
  1587. }
  1588. if (isNeedToClosePower)
  1589. {
  1590. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1591. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1592. }
  1593. }
  1594. else
  1595. {
  1596. bool isNeedToOpenPower = false;
  1597. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1598. {
  1599. if (!isStartOutputSwitch[index])
  1600. {
  1601. isNeedToOpenPower = true;
  1602. }
  1603. isStartOutputSwitch[index] = true;
  1604. }
  1605. if (isNeedToOpenPower)
  1606. {
  1607. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1608. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1609. }
  1610. }
  1611. }
  1612. }
  1613. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1614. {
  1615. // 智能判斷 Start -----------------------------------------------------------
  1616. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  1617. {
  1618. bool balanceVol = true;
  1619. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1620. {
  1621. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1622. chargingInfo[subIndex]->SystemStatus == S_RESERVATION)
  1623. {
  1624. // 各群電壓接近平衡
  1625. if (((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE) ||
  1626. ((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - CHK_VOL_RANGE))
  1627. {
  1628. PRINTF_FUNC("** _REASSIGNED_ADJUST_A_TO_M ** Gun_%d, PresentChargingVoltage = %f, PresentChargingVoltage_V = %f, EvBatterytargetVoltage = %f \n", subIndex,
  1629. (chargingInfo[subIndex]->PresentChargingVoltage * 10),
  1630. ((chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE),
  1631. ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - CHK_VOL_RANGE));
  1632. balanceVol = false;
  1633. }
  1634. break;
  1635. }
  1636. }
  1637. if (balanceVol)
  1638. {
  1639. // 閒置端與車端要求電壓接近
  1640. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_RELAY_A_TO_M============= Step 13 \n");
  1641. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_A_TO_M;
  1642. }
  1643. else
  1644. {
  1645. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1646. {
  1647. gettimeofday(&_max_time, NULL);
  1648. }
  1649. }
  1650. }
  1651. else if(ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_WAITING)
  1652. {
  1653. int idleCurrent = 0;
  1654. int chargingCurrent = 0;
  1655. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1656. {
  1657. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1658. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1659. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1660. idleCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1661. else
  1662. chargingCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1663. }
  1664. if (idleCurrent >= chargingCurrent - PRE_CHARG_RANGE)
  1665. {
  1666. PRINTF_FUNC("=============Smart Charging_0 : _REASSIGNED_COMP============= Step 15 \n");
  1667. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1668. }
  1669. else
  1670. {
  1671. if ((GetTimeoutValue(_max_time) / 1000) > 500)
  1672. {
  1673. gettimeofday(&_max_time, NULL);
  1674. }
  1675. }
  1676. }
  1677. if (chargingInfo[groupIndex]->AvailableChargingCurrent > 0)
  1678. {
  1679. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_A_TO_M)
  1680. {
  1681. byte reassignIndex = ELEMENT_NOT_FIND;
  1682. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1683. {
  1684. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1685. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1686. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1687. {
  1688. reassignIndex = subIndex;
  1689. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1690. {
  1691. preChargingCur = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1692. }
  1693. else
  1694. preChargingCur = 0;
  1695. }
  1696. else
  1697. {
  1698. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING) &&
  1699. (preChargingCur >= preChargingTarget - ZERO_CURRENT))
  1700. preChargingTarget += PRE_CHARG_STEP_CUR;
  1701. if (preChargingTarget >= (chargingInfo[subIndex]->EvBatterytargetCurrent * 10) / 2)
  1702. preChargingTarget = (chargingInfo[subIndex]->EvBatterytargetCurrent * 10) / 2;
  1703. }
  1704. }
  1705. if (reassignIndex != ELEMENT_NOT_FIND)
  1706. {
  1707. if ((GetTimeoutValue(_max_time) / 1000) <= 50)
  1708. {
  1709. //PRINTF_FUNC("set out (%d) value = %d******** 5 \n", reassignIndex, ZERO_CURRENT + preChargingTarget);
  1710. // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1711. PresentOutputVol(reassignIndex,
  1712. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1713. ZERO_CURRENT + preChargingTarget); Await();
  1714. byte _ovCahrgingCur = 0;
  1715. if (preChargingCur > PRE_CHARG_STEP_CUR)
  1716. _ovCahrgingCur = PRE_CHARG_STEP_CUR;
  1717. //PRINTF_FUNC("set out (%d) value = %f******** 6 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent - preChargingCur - _ovCahrgingCur);
  1718. PresentOutputVol(groupIndex,
  1719. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1720. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10) - preChargingCur - _ovCahrgingCur); Await();
  1721. }
  1722. }
  1723. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1724. {
  1725. bool isNeedToClosePower = false;
  1726. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1727. {
  1728. if (isStartOutputSwitch[index])
  1729. {
  1730. isNeedToClosePower = true;
  1731. }
  1732. isStartOutputSwitch[index] = false;
  1733. }
  1734. if (isNeedToClosePower)
  1735. {
  1736. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1737. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1738. }
  1739. }
  1740. else
  1741. {
  1742. bool isNeedToOpenPower = false;
  1743. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1744. {
  1745. if (!isStartOutputSwitch[index])
  1746. {
  1747. isNeedToOpenPower = true;
  1748. }
  1749. isStartOutputSwitch[index] = true;
  1750. }
  1751. if (isNeedToOpenPower)
  1752. {
  1753. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1754. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1755. }
  1756. }
  1757. }
  1758. else
  1759. {
  1760. //PRINTF_FUNC("set out (%d) value = %f******** 7 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1761. PresentOutputVol(groupIndex,
  1762. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1763. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1764. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1765. {
  1766. if (isStartOutputSwitch[groupIndex])
  1767. {
  1768. isStartOutputSwitch[groupIndex] = false;
  1769. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  1770. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  1771. }
  1772. }
  1773. else
  1774. {
  1775. if (!isStartOutputSwitch[groupIndex])
  1776. {
  1777. isStartOutputSwitch[groupIndex] = true;
  1778. SwitchPower(groupIndex, PSU_POWER_ON); Await();
  1779. FlashLed(groupIndex, PSU_FLASH_ON); Await();
  1780. }
  1781. }
  1782. }
  1783. }
  1784. }
  1785. }
  1786. else if ((chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  1787. chargingInfo[groupIndex]->SystemStatus <= S_COMPLETE) ||
  1788. chargingInfo[groupIndex]->SystemStatus == S_ALARM)
  1789. {
  1790. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1791. {
  1792. if (!isCharging)
  1793. {
  1794. bool isNeedToClosePower = false;
  1795. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1796. {
  1797. if (isStartOutputSwitch[index])
  1798. {
  1799. isNeedToClosePower = true;
  1800. }
  1801. isStartOutputSwitch[index] = false;
  1802. }
  1803. if (isNeedToClosePower)
  1804. {
  1805. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1806. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1807. }
  1808. if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1809. {
  1810. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1811. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1812. {
  1813. // 代表在切換的過程中,停止充電了
  1814. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= STOP_CURRENT)
  1815. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1816. }
  1817. }
  1818. }
  1819. else if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  1820. {
  1821. // 代表充電的槍依舊在充電,欲進入充電的槍取消充電了
  1822. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1823. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1824. {
  1825. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1826. }
  1827. }
  1828. }
  1829. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1830. {
  1831. if (!isReadToCharging)
  1832. {
  1833. bool isNeedToClosePower = false;
  1834. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1835. {
  1836. if (isStartOutputSwitch[index])
  1837. {
  1838. isNeedToClosePower = true;
  1839. }
  1840. isStartOutputSwitch[index] = false;
  1841. }
  1842. if (isNeedToClosePower)
  1843. {
  1844. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1845. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1846. }
  1847. }
  1848. else
  1849. {
  1850. if (isStartOutputSwitch[groupIndex])
  1851. {
  1852. isStartOutputSwitch[groupIndex] = false;
  1853. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  1854. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  1855. }
  1856. }
  1857. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1858. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1859. else
  1860. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1861. }
  1862. }
  1863. else if ((chargingInfo[groupIndex]->SystemStatus >= S_PREPARNING && chargingInfo[groupIndex]->SystemStatus <= S_PREPARING_FOR_EV) &&
  1864. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1865. {
  1866. //PRINTF_FUNC("%d ******** 7 \n", groupIndex);
  1867. if (isStartOutputSwitch[groupIndex])
  1868. {
  1869. isStartOutputSwitch[groupIndex] = false;
  1870. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  1871. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  1872. }
  1873. }
  1874. }
  1875. break;
  1876. }
  1877. case _TEST_MODE:
  1878. {
  1879. // 在測試模式中,保持與模塊的通訊
  1880. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1881. if (time > 1500)
  1882. {
  1883. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1884. {
  1885. // 取系統總輸出能力
  1886. GetModuleCap(index); Await();
  1887. // 取各群輸出電壓電流 (float)
  1888. GetModuleOutputF(index); Await();
  1889. }
  1890. gettimeofday(&_cmdSubPriority_time, NULL);
  1891. }
  1892. byte _switch = 0x00;
  1893. if ((chargingInfo[0]->EvBatterytargetVoltage * 10) > 0 && (chargingInfo[0]->EvBatterytargetCurrent * 10) > 0)
  1894. _switch = 0x01;
  1895. for (byte _groupCount_1 = 0; _groupCount_1 < conn_1_count; _groupCount_1++)
  1896. {
  1897. SetDirModulePresentOutput(connector_1[_groupCount_1],
  1898. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  1899. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  1900. _switch, _switch); Await();
  1901. }
  1902. for (byte _groupCount_2 = 0; _groupCount_2 < conn_2_count; _groupCount_2++)
  1903. {
  1904. SetDirModulePresentOutput(connector_2[_groupCount_2],
  1905. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  1906. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  1907. _switch, _switch); Await();
  1908. }
  1909. }
  1910. break;
  1911. }
  1912. usleep(20000);
  1913. }
  1914. return FAIL;
  1915. }