Module_PsuComm.c 88 KB

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