Module_PsuComm.c 81 KB

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