Module_PsuComm.c 82 KB

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