Module_PsuComm.c 77 KB

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