Module_PsuComm.c 71 KB

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