Module_PsuComm.c 76 KB

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