Module_PsuComm.c 72 KB

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