Module_PsuComm.c 71 KB

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