Module_PsuComm.c 72 KB

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