Module_EvTxComm.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/stat.h>
  10. #include <sys/timeb.h>
  11. #include <sys/time.h>
  12. #include <net/if.h>
  13. #include <linux/can.h>
  14. #include <linux/can/raw.h>
  15. #include <signal.h>
  16. #include "../Config.h"
  17. #include "../Log/log.h"
  18. #include "../Define/define.h"
  19. #include "../ShareMemory/shmMem.h"
  20. #include "../SelectGun/SelectGun.h"
  21. #include "Ev_Comm.h"
  22. #include "Module_EvComm.h"
  23. #include "../CSU/main.h"
  24. //------------------------------------------------------------------------------
  25. static struct SysConfigData *pSysConfig = NULL;
  26. static struct SysInfoData *pSysInfo = NULL;
  27. static struct FaultCodeData *pFaultCode = NULL;
  28. static struct AlarmCodeData *pAlarmCode = NULL;
  29. static struct CHAdeMOData *ShmCHAdeMOData = NULL;
  30. static struct GBTData *ShmGBTData = NULL;
  31. static struct CcsData *ShmCcsData = NULL;
  32. static DcCommonInfo *ShmDcCommonData = NULL;
  33. static SelectGunInfo *ShmSelectGunInfo = NULL;
  34. // 限制最大充電電壓,因應不同 type 槍線來限制
  35. // Chademo : 500V, 125A,
  36. // GB : 750, 120A
  37. // CCS : 950V, 120A
  38. //DS60-120 add
  39. //static double chademoVol = 5000;
  40. //static double ccsVol = 9500;
  41. //static double gbVol = 7500;
  42. static float maxChargingVol[2] = {0, 0}; // 限制最大充電電壓,如依照模塊則填上 0
  43. // 限制最大充電電流與能量透過 Web
  44. static float maxChargingCur[2] = {0, 0}; // 限制最大充電電流,如依照模塊則填上 0
  45. static float maxChargingPow = 0; // 限制最大充電能量,如依照模塊則填上 0
  46. static float LogInfo[2][10]; //DS60-120 add
  47. static int CanFd = -1;
  48. bool psuOutputReady[2] = {0, 0};
  49. //------------------------------------------------------------------------------
  50. extern void CANReceiver(int fd);
  51. extern void ClearAbnormalStatus_Chademo(uint8_t gun_index);
  52. extern void ClearAbnormalStatus_GB(uint8_t gun_index);
  53. extern void ClearAbnormalStatus_CCS(uint8_t gun_index);
  54. //------------------------------------------------------------------------------
  55. int GetCanFd(void)
  56. {
  57. return CanFd;
  58. }
  59. uint32_t GetTimeoutValue(struct timeval _sour_time)
  60. {
  61. struct timeval _end_time;
  62. gettimeofday(&_end_time, NULL);
  63. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  64. }
  65. int InitCanBus(void)
  66. {
  67. int s0, nbytes;
  68. struct timeval tv;
  69. struct ifreq ifr0;
  70. struct sockaddr_can addr0;
  71. struct can_filter rxfilter[3];
  72. system("/sbin/ip link set can0 down");
  73. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  74. system("/sbin/ip link set can0 up");
  75. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  76. tv.tv_sec = 0;
  77. tv.tv_usec = 10000;
  78. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
  79. log_error("Set SO_RCVTIMEO NG");
  80. }
  81. nbytes = 40960;
  82. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0) {
  83. log_error("Set SO_RCVBUF NG");
  84. }
  85. nbytes = 40960;
  86. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0) {
  87. log_error("Set SO_SNDBUF NG");
  88. }
  89. rxfilter[0].can_id = 0x01;
  90. rxfilter[0].can_mask = 0x000000FF;
  91. rxfilter[1].can_id = 0x02;
  92. rxfilter[1].can_mask = 0x000000FF;
  93. rxfilter[2].can_id = 0x01FF;
  94. rxfilter[2].can_mask = 0x00000FFF;
  95. if (setsockopt(s0, SOL_CAN_RAW, CAN_RAW_FILTER,
  96. &rxfilter, sizeof(struct can_filter) * 3) < 0) {
  97. log_error("RX setsockopt CAN_RAW_FILTER failed");
  98. }
  99. strcpy(ifr0.ifr_name, "can0");
  100. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  101. addr0.can_family = AF_CAN;
  102. addr0.can_ifindex = ifr0.ifr_ifindex;
  103. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  104. return s0;
  105. }
  106. float GetMaxChargingVol(uint8_t index)
  107. {
  108. return maxChargingVol[index];
  109. }
  110. float GetMaxCharginigCur(uint8_t index)
  111. {
  112. return maxChargingCur[index];
  113. }
  114. static void SendCommunicationOnly(uint8_t index)
  115. {
  116. struct ChargingInfoData *pDcCharginigInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  117. uint8_t targetID = pDcCharginigInfo->Evboard_id;
  118. /*
  119. if (pSysConfig->TotalConnectorCount == 1 &&
  120. pDcCharginigInfo->Type == _Type_CCS_2 &&
  121. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0) {
  122. targetID += 1;
  123. }
  124. */
  125. SetChargingPermission(index,
  126. COMMUNICATION,
  127. pDcCharginigInfo->AvailableChargingPower,
  128. 0,
  129. 0,
  130. targetID);
  131. }
  132. static void SendStopOnly(uint8_t index)
  133. {
  134. struct ChargingInfoData *pDcCharginigInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  135. uint8_t targetID = pDcCharginigInfo->Evboard_id;
  136. /*
  137. if (pSysConfig->TotalConnectorCount == 1 &&
  138. pDcCharginigInfo->Type == _Type_CCS_2 &&
  139. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0) {
  140. targetID += 1;
  141. }
  142. */
  143. SetChargingPermission(index,
  144. STOP,
  145. pDcCharginigInfo->AvailableChargingPower,
  146. 0,
  147. 0,
  148. targetID);
  149. }
  150. static uint8_t GetStopChargingReasonByEvse(uint8_t gunIndex, uint8_t *reason)
  151. {
  152. uint8_t result = NO;
  153. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  154. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == 0x01) {
  155. // 012251
  156. *(reason + 5) = 0;
  157. *(reason + 4) = 1;
  158. *(reason + 3) = 2;
  159. *(reason + 2) = 2;
  160. *(reason + 1) = 5;
  161. *(reason + 0) = 1;
  162. result = YES;
  163. }
  164. if (pDcChargingInfo->Type == _Type_Chademo) {
  165. if (pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault == YES) {
  166. // 011012
  167. *(reason + 5) = 0;
  168. *(reason + 4) = 1;
  169. *(reason + 3) = 1;
  170. *(reason + 2) = 0;
  171. *(reason + 1) = 1;
  172. *(reason + 0) = 2;
  173. result = YES;
  174. } else if (pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail == YES) {
  175. // 012289
  176. *(reason + 5) = 0;
  177. *(reason + 4) = 1;
  178. *(reason + 3) = 2;
  179. *(reason + 2) = 2;
  180. *(reason + 1) = 8;
  181. *(reason + 0) = 9;
  182. result = YES;
  183. } else if (pAlarmCode->AlarmEvents.bits.ChademoGfdTrip == YES) {
  184. // 012234
  185. *(reason + 5) = 0;
  186. *(reason + 4) = 1;
  187. *(reason + 3) = 2;
  188. *(reason + 2) = 2;
  189. *(reason + 1) = 3;
  190. *(reason + 0) = 4;
  191. result = YES;
  192. }
  193. } else if (pDcChargingInfo->Type == _Type_GB) {
  194. if (pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault == YES) {
  195. // 012290
  196. *(reason + 5) = 0;
  197. *(reason + 4) = 1;
  198. *(reason + 3) = 2;
  199. *(reason + 2) = 2;
  200. *(reason + 1) = 9;
  201. *(reason + 0) = 0;
  202. result = YES;
  203. } else if (pAlarmCode->AlarmEvents.bits.GbGfdTrip == YES) {
  204. // 012236
  205. *(reason + 5) = 0;
  206. *(reason + 4) = 1;
  207. *(reason + 3) = 2;
  208. *(reason + 2) = 2;
  209. *(reason + 1) = 3;
  210. *(reason + 0) = 6;
  211. result = YES;
  212. }
  213. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  214. if (pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault == YES) {
  215. // 011014
  216. *(reason + 5) = 0;
  217. *(reason + 4) = 1;
  218. *(reason + 3) = 1;
  219. *(reason + 2) = 0;
  220. *(reason + 1) = 1;
  221. *(reason + 0) = 4;
  222. result = YES;
  223. } else if (pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail == YES) {
  224. // 012288
  225. *(reason + 5) = 0;
  226. *(reason + 4) = 1;
  227. *(reason + 3) = 2;
  228. *(reason + 2) = 2;
  229. *(reason + 1) = 8;
  230. *(reason + 0) = 8;
  231. result = YES;
  232. } else if (pAlarmCode->AlarmEvents.bits.CcsGfdTrip == YES) {
  233. // 012235
  234. *(reason + 5) = 0;
  235. *(reason + 4) = 1;
  236. *(reason + 3) = 2;
  237. *(reason + 2) = 2;
  238. *(reason + 1) = 3;
  239. *(reason + 0) = 5;
  240. result = YES;
  241. }
  242. }
  243. return result;
  244. }
  245. static void setCurrentOutput(void)
  246. {
  247. struct ChargingInfoData *chargingData_1 = NULL;
  248. struct ChargingInfoData *chargingData_2 = NULL;
  249. if (pSysConfig->TotalConnectorCount == 1) {
  250. chargingData_1 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  251. //chargingData_2 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  252. if (chargingData_1->FireChargingVoltage <= 500) { //DS60-120 add
  253. chargingData_1->PresentChargingCurrent = 0;
  254. }
  255. } else if (pSysConfig->TotalConnectorCount == 2) {
  256. chargingData_1 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  257. chargingData_2 = (struct ChargingInfoData *)GetDcChargingInfoData(1);
  258. if (chargingData_1->FireChargingVoltage <= 500) { //DS60-120 add
  259. chargingData_1->PresentChargingCurrent = 0;
  260. }
  261. if (chargingData_2->FireChargingVoltage <= 500) {
  262. chargingData_2->PresentChargingCurrent = 0;
  263. }
  264. }
  265. }
  266. static void SetPresentChargingOutputCap(void)
  267. {
  268. float pow1 = 0, cur1 = 0;
  269. float pow2 = 0, cur2 = 0;
  270. struct ChargingInfoData *chargingData_1 = NULL;
  271. struct ChargingInfoData *chargingData_2 = NULL;
  272. if (pSysConfig->TotalConnectorCount == 1) {
  273. chargingData_1 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  274. chargingData_2 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  275. } else if (pSysConfig->TotalConnectorCount == 2) {
  276. chargingData_1 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  277. chargingData_2 = (struct ChargingInfoData *)GetDcChargingInfoData(1);
  278. }
  279. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  280. float vol = 0;
  281. #endif //!defined DD360 && !defined DD360Audi
  282. pow1 = chargingData_1->AvailableChargingPower;
  283. cur1 = chargingData_1->AvailableChargingCurrent;
  284. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  285. vol = chargingData_1->MaximumChargingVoltage;
  286. GetMaxVolAndCurMethod(chargingData_1->Index, &vol, &cur1);
  287. GetMaxPowerMethod(chargingData_1->Index, &pow1);
  288. #endif //!defined DD360 && !defined DD360Audi
  289. //DS60-120 add
  290. if (pow1 <= 0) {
  291. cur1 = 0;
  292. } else {
  293. if (chargingData_1->SystemStatus == S_CHARGING &&
  294. chargingData_1->FireChargingVoltage > 1500) {
  295. float maxCur = 0;
  296. maxCur = (pow1 * 1000) / chargingData_1->FireChargingVoltage;
  297. if (maxCur * 10 <= cur1) {
  298. //log_info("Gun1 -> MaxCharging Current = %f, Cap Current = %f ", (maxCur * 10), cur1);
  299. cur1 = maxCur * 10;
  300. }
  301. }
  302. }
  303. pow2 = chargingData_2->AvailableChargingPower;
  304. cur2 = chargingData_2->AvailableChargingCurrent;
  305. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  306. vol = chargingData_2->MaximumChargingVoltage;
  307. GetMaxVolAndCurMethod(chargingData_2->Index, &vol, &cur2);
  308. GetMaxPowerMethod(chargingData_2->Index, &pow2);
  309. #endif //!defined DD360 && !defined DD360Audi
  310. //DS60-120 add
  311. if (pow2 <= 0) {
  312. cur2 = 0;
  313. } else {
  314. if (chargingData_2->SystemStatus == S_CHARGING &&
  315. chargingData_2->FireChargingVoltage > 1500) {
  316. float maxCur = 0;
  317. maxCur = (pow2 * 1000) / chargingData_2->FireChargingVoltage;
  318. if (maxCur * 10 <= cur2) {
  319. //log_info("Gun2 -> MaxCharging Current = %f, Cap Current = %f ", (maxCur * 10), cur2);
  320. cur2 = maxCur * 10;
  321. }
  322. }
  323. }
  324. //DS60-120 add
  325. if ((LogInfo[0][EV_LOG_OUTPUT_CAP_POW] <= pow1 - 5 ||
  326. LogInfo[0][EV_LOG_OUTPUT_CAP_POW] >= pow1 + 5) ||
  327. (LogInfo[0][EV_LOG_OUTPUT_CAP_CUR] <= cur1 - 5 ||
  328. LogInfo[0][EV_LOG_OUTPUT_CAP_CUR] >= cur1 + 5) ||
  329. (LogInfo[1][EV_LOG_OUTPUT_CAP_POW] <= pow2 - 5 ||
  330. LogInfo[1][EV_LOG_OUTPUT_CAP_POW] >= pow2 + 5) ||
  331. (LogInfo[1][EV_LOG_OUTPUT_CAP_CUR] <= cur2 - 5 ||
  332. LogInfo[1][EV_LOG_OUTPUT_CAP_CUR] >= cur2 + 5)
  333. ) {
  334. //log_info("----------------------------------------------------- ");
  335. log_info("To EV (Real) Power_1 = %.1f, Cur_1 = %.1f, Power_2 = %.1f, Cur_2 = %.1f",
  336. pow1 / 10, cur1 / 10, pow2 / 10, cur2 / 10);
  337. //log_info("----------------------------------------------------- ");
  338. LogInfo[0][EV_LOG_OUTPUT_CAP_POW] = pow1;
  339. LogInfo[0][EV_LOG_OUTPUT_CAP_CUR] = cur1;
  340. LogInfo[1][EV_LOG_OUTPUT_CAP_POW] = pow2;
  341. LogInfo[1][EV_LOG_OUTPUT_CAP_CUR] = cur2;
  342. chargingData_1->RealMaxCurrent = cur1;
  343. chargingData_1->RealMaxPower = pow1;
  344. if (pSysConfig->TotalConnectorCount == 2) {
  345. chargingData_2->RealMaxCurrent = cur2;
  346. chargingData_2->RealMaxPower = pow2;
  347. }
  348. }
  349. SetPresentOutputCapacity(pow1, cur1, pow2, cur2);
  350. }
  351. static void GetMaxVolAndCurMethod(uint8_t index, float *vol, float *cur)
  352. {
  353. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  354. if (maxChargingVol[index] != 0 && maxChargingVol[index] <= *vol) {
  355. *vol = maxChargingVol[index];
  356. }
  357. if (maxChargingCur[index] != 0 && maxChargingCur[index] <= *cur) {
  358. *cur = maxChargingCur[index];
  359. }
  360. if (((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  361. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  362. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  363. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) &&
  364. pDcChargingInfo->ChargingProfileCurrent >= 0 &&
  365. pDcChargingInfo->ChargingProfileCurrent <= *cur
  366. ) {
  367. *cur = pDcChargingInfo->ChargingProfileCurrent;
  368. }
  369. }
  370. static uint8_t waitPsuVolwithRealyVol(uint8_t gunIndex)
  371. {
  372. PcPsuOutput *pPcPsuOutput = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[gunIndex];
  373. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  374. int vol = 0;
  375. vol = abs(pPcPsuOutput->Voltage - pDcChargingInfo->FireChargingVoltage);
  376. if (vol <= 10) {
  377. return YES;
  378. }
  379. return NO;
  380. }
  381. /**
  382. * [SetPresentChargingOutputFromPcPsu 充電狀態讀取電源櫃PSU輸出電壓電流,縮小誤差值]
  383. * @Author Jerry
  384. * @DateTime 2021-07-05
  385. */
  386. static void SetPresentChargingOutputFromPcPsu(uint8_t gunCount)
  387. {
  388. float vol1 = 0, cur1 = 0;
  389. float vol2 = 0, cur2 = 0;
  390. PcPsuOutput *pPcPsuOutput0 = NULL;
  391. PcPsuOutput *pPcPsuOutput1 = NULL;
  392. struct ChargingInfoData *chargingData0 = NULL;
  393. struct ChargingInfoData *chargingData1 = NULL;
  394. switch (gunCount) {
  395. case 1:
  396. pPcPsuOutput0 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[0];
  397. pPcPsuOutput1 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[0];
  398. chargingData0 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  399. chargingData1 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  400. break;
  401. case 2:
  402. pPcPsuOutput0 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[0];
  403. pPcPsuOutput1 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[1];
  404. chargingData0 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  405. chargingData1 = (struct ChargingInfoData *)GetDcChargingInfoData(1);
  406. break;
  407. }
  408. vol1 = pPcPsuOutput0->Voltage == 0 ? chargingData0->FireChargingVoltage : (((float)pPcPsuOutput0->Voltage));
  409. cur1 = (chargingData0->PresentChargingCurrent * 10);//(((float)pPcPsuOutput0->Current) * 0.1);
  410. vol2 = pPcPsuOutput1->Voltage == 0 ? chargingData1->FireChargingVoltage : (((float)pPcPsuOutput1->Voltage));
  411. cur2 = (chargingData1->PresentChargingCurrent * 10);//(((float)pPcPsuOutput1->Current) * 0.1);
  412. if (
  413. (LogInfo[0][EV_LOG_NOW_OUTPUT_VOL] >= vol1 + CHK_VOL_RANGE) ||
  414. (LogInfo[0][EV_LOG_NOW_OUTPUT_VOL] <= vol1 - CHK_VOL_RANGE) ||
  415. (LogInfo[0][EV_LOG_NOW_OUTPUT_CUR] >= cur1 + CHK_CUR_RANGE) ||
  416. (LogInfo[0][EV_LOG_NOW_OUTPUT_CUR] <= cur1 - CHK_CUR_RANGE) ||
  417. (LogInfo[1][EV_LOG_NOW_OUTPUT_VOL] >= vol2 + CHK_VOL_RANGE) ||
  418. (LogInfo[1][EV_LOG_NOW_OUTPUT_VOL] <= vol2 - CHK_VOL_RANGE) ||
  419. (LogInfo[1][EV_LOG_NOW_OUTPUT_CUR] >= cur2 + CHK_CUR_RANGE) ||
  420. (LogInfo[1][EV_LOG_NOW_OUTPUT_CUR] <= cur2 - CHK_CUR_RANGE)
  421. ) {
  422. log_info("G1->Out Vol=%.1f,Out Cur=%.1f - G2->Out Vol=%.1f,Out Cur=%.1f",
  423. vol1,
  424. cur1 / 10,
  425. vol2,
  426. cur2 / 10);
  427. LogInfo[0][EV_LOG_NOW_OUTPUT_VOL] = vol1;
  428. LogInfo[0][EV_LOG_NOW_OUTPUT_CUR] = cur1;
  429. LogInfo[1][EV_LOG_NOW_OUTPUT_VOL] = vol2;
  430. LogInfo[1][EV_LOG_NOW_OUTPUT_CUR] = cur2;
  431. }
  432. SetPresentOutputPower(vol1, cur1, vol2, cur2);
  433. }
  434. static void SetPresentChargingOutputPower(void)
  435. {
  436. float vol1 = 0, cur1 = 0;
  437. float vol2 = 0, cur2 = 0;
  438. PcPsuOutput *pPcPsuOutput1 = NULL;
  439. PcPsuOutput *pPcPsuOutput2 = NULL;
  440. struct ChargingInfoData *chargingData_1 = NULL;
  441. struct ChargingInfoData *chargingData_2 = NULL;
  442. bool isPsuVol1 = false, isPsuVol2 = false, isPsuCur1 = false, isPsuCur2 = false;
  443. if (pSysConfig->TotalConnectorCount == 1) {
  444. pPcPsuOutput1 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[0];
  445. pPcPsuOutput2 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[0];
  446. chargingData_1 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  447. chargingData_2 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  448. } else if (pSysConfig->TotalConnectorCount == 2) {
  449. pPcPsuOutput1 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[0];
  450. pPcPsuOutput2 = (PcPsuOutput *)&ShmDcCommonData->PcPsuOutput[1];
  451. chargingData_1 = (struct ChargingInfoData *)GetDcChargingInfoData(0);
  452. chargingData_2 = (struct ChargingInfoData *)GetDcChargingInfoData(1);
  453. }
  454. psuOutputReady[0] = chargingData_1->SystemStatus != S_CHARGING ? false : psuOutputReady[0];
  455. psuOutputReady[1] = chargingData_2->SystemStatus != S_CHARGING ? false : psuOutputReady[1];
  456. isPsuVol1 = chargingData_1->PantographFlag ? true : (pPcPsuOutput1->Voltage != 0 && psuOutputReady[0] == true);
  457. isPsuVol2 = chargingData_2->PantographFlag ? true : (pPcPsuOutput2->Voltage != 0 && psuOutputReady[1] == true);
  458. isPsuCur1 = chargingData_1->PantographFlag ? true : false;
  459. isPsuCur2 = chargingData_2->PantographFlag ? true : false;
  460. //vol1 = chargingData_1->FireChargingVoltage;
  461. vol1 = isPsuVol1 == false ? chargingData_1->FireChargingVoltage : (((float)pPcPsuOutput1->Voltage));
  462. cur1 = isPsuCur1 == false ? (chargingData_1->PresentChargingCurrent * 10) : pPcPsuOutput1->Current;
  463. //vol2 = chargingData_2->FireChargingVoltage;
  464. vol2 = isPsuVol2 == false ? chargingData_2->FireChargingVoltage : (((float)pPcPsuOutput2->Voltage));
  465. cur2 = isPsuCur2 == false ? (chargingData_2->PresentChargingCurrent * 10) : pPcPsuOutput2->Current;
  466. //DS60-120 add
  467. if ((LogInfo[0][EV_LOG_NOW_OUTPUT_VOL] >= vol1 + CHK_VOL_RANGE) ||
  468. (LogInfo[0][EV_LOG_NOW_OUTPUT_VOL] <= vol1 - CHK_VOL_RANGE) ||
  469. (LogInfo[0][EV_LOG_NOW_OUTPUT_CUR] >= cur1 + CHK_CUR_RANGE) ||
  470. (LogInfo[0][EV_LOG_NOW_OUTPUT_CUR] <= cur1 - CHK_CUR_RANGE) ||
  471. (LogInfo[1][EV_LOG_NOW_OUTPUT_VOL] >= vol2 + CHK_VOL_RANGE) ||
  472. (LogInfo[1][EV_LOG_NOW_OUTPUT_VOL] <= vol2 - CHK_VOL_RANGE) ||
  473. (LogInfo[1][EV_LOG_NOW_OUTPUT_CUR] >= cur2 + CHK_CUR_RANGE) ||
  474. (LogInfo[1][EV_LOG_NOW_OUTPUT_CUR] <= cur2 - CHK_CUR_RANGE)
  475. ) {
  476. log_info("G1-> Vol=%.1f, Cur=%.1f - G2-> Vol = %.1f, Cur = %.1f",
  477. vol1 / 10,
  478. cur1 / 10,
  479. vol2 / 10,
  480. cur2 / 10);
  481. LogInfo[0][EV_LOG_NOW_OUTPUT_VOL] = vol1;
  482. LogInfo[0][EV_LOG_NOW_OUTPUT_CUR] = cur1;
  483. LogInfo[1][EV_LOG_NOW_OUTPUT_VOL] = vol2;
  484. LogInfo[1][EV_LOG_NOW_OUTPUT_CUR] = cur2;
  485. }
  486. //if (_outVol_1 != vol1 ||
  487. // _outCur_1 != cur1 ||
  488. // _outVol_2 != vol2 ||
  489. // _outCur_2 != cur2) {
  490. /*log_info("G1 -> Output Vol = %f, Output Cur = %f -- G2 -> Output Vol = %f, Output Cur = %f ",
  491. vol1, cur1, vol2, cur2);
  492. */
  493. // _outVol_1 = vol1; _outCur_1 = cur1; _outVol_2 = vol2; _outCur_2 = cur2;
  494. //}
  495. SetPresentOutputPower(vol1, cur1, vol2, cur2);
  496. }
  497. static void checkConnectorOVPState(uint8_t gunIndex)
  498. {
  499. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  500. // 避免槍溫偵測誤判
  501. static uint8_t gunTempAllowCount[2] = {0};
  502. bool isOTP = false;
  503. switch (pDcChargingInfo->Type) {
  504. case _Type_Chademo:
  505. if (ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP) {
  506. isOTP = true;
  507. }
  508. break;
  509. case _Type_CCS_2:
  510. if (ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP) {
  511. isOTP = true;
  512. }
  513. break;
  514. case _Type_GB:
  515. if (ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP) {
  516. isOTP = true;
  517. }
  518. break;
  519. }
  520. if (ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP == YES) {
  521. isOTP = true;
  522. }
  523. if (isOTP) {
  524. if (gunTempAllowCount[gunIndex] >= 2) {
  525. pDcChargingInfo->StopChargeFlag = YES;
  526. } else {
  527. gunTempAllowCount[gunIndex] += 1;
  528. }
  529. } else {
  530. gunTempAllowCount[gunIndex] = 0;
  531. }
  532. }
  533. static time_t GetRtcInfoForEpoch(void)
  534. {
  535. struct timeb csuTime;
  536. struct tm *tmCSU;
  537. struct tm t;
  538. time_t result;
  539. ftime(&csuTime);
  540. tmCSU = localtime(&csuTime.time);
  541. t.tm_year = tmCSU->tm_year;
  542. t.tm_mon = tmCSU->tm_mon;
  543. t.tm_mday = tmCSU->tm_mday;
  544. t.tm_hour = tmCSU->tm_hour;
  545. t.tm_min = tmCSU->tm_min;
  546. t.tm_sec = tmCSU->tm_sec;
  547. t.tm_isdst = -1;
  548. result = mktime(&t);
  549. return result;
  550. }
  551. static void FormatVoltageAndCurrent(void)
  552. {
  553. uint8_t gunIndex = 0;
  554. ParsingRatedCur parsingRatedCur = {0};
  555. RateCurInfo *pRatedCurInfo = NULL;
  556. if (RatedCurrentParsing((char *)pSysConfig->ModelName, &parsingRatedCur) != PASS) {
  557. log_error("Parsing rated current failed");
  558. return;
  559. }
  560. maxChargingPow = parsingRatedCur.Power;
  561. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  562. pRatedCurInfo = (RateCurInfo *)&parsingRatedCur.ParsingInfo[gunIndex];
  563. maxChargingVol[gunIndex] = pRatedCurInfo->Voltage;
  564. maxChargingCur[gunIndex] = pRatedCurInfo->Current;
  565. log_info("Conn %d GunType = %d, MaxVol = %f, MaxCur = %f ",
  566. gunIndex,
  567. pRatedCurInfo->GunType,
  568. maxChargingVol[gunIndex],
  569. maxChargingCur[gunIndex]);
  570. }
  571. }
  572. static int DiffTimeb(struct timeb ST, struct timeb ET)
  573. {
  574. //return milli-second
  575. unsigned int StartTime, StopTime;
  576. StartTime = (unsigned int)ST.time;
  577. StopTime = (unsigned int)ET.time;
  578. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  579. }
  580. int main(int argc, char *argv[])
  581. {
  582. bool chkChademoPermission[2] = {false};
  583. int isContinue = 1;
  584. uint8_t gunIndex = 0;
  585. uint8_t typeIndex = 0;
  586. uint8_t priorityLow = 1;
  587. uint8_t SendErrorCount[2] = {0, 0};
  588. uint8_t gfgResult = 0;
  589. uint32_t _timeBuf = 0;
  590. uint32_t chargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY] = {0};
  591. float maxVol, maxCur;
  592. struct timeval _chk_ratingPower_timeout[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  593. struct timeval _chk_chademo_permission_timeout[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  594. time_t rtc = {0};
  595. struct ChargingInfoData *pDcChargingInfo = NULL;
  596. struct timeb waitChargingTime;
  597. struct timeb nowTime;
  598. uint8_t Comcont = 0;
  599. uint8_t evstatus;
  600. if (CreateAllCsuShareMemory() == FAIL) {
  601. log_error("create share memory error");
  602. return FAIL;
  603. }
  604. MappingGunChargingInfo("EvComm Task");
  605. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  606. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  607. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  608. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  609. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  610. ShmCHAdeMOData = (struct CHAdeMOData *)GetShmCHAdeMOData();
  611. ShmGBTData = (struct GBTData *)GetShmGBTData();
  612. ShmCcsData = (struct CcsData *)GetShmCcsData();
  613. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  614. CanFd = InitCanBus();
  615. FormatVoltageAndCurrent();
  616. signal(SIGCHLD,SIG_IGN);
  617. CANReceiver(CanFd);
  618. rtc = GetRtcInfoForEpoch();
  619. while (isContinue) {
  620. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  621. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  622. typeIndex = pDcChargingInfo->type_index;
  623. if (priorityLow == 1) {
  624. // 優先權較低 - 只要有回應即不會再詢問
  625. if (pDcChargingInfo->Type == _Type_Chademo &&
  626. ShmCHAdeMOData->evse[typeIndex].SelfTest_Comp != PASS) {
  627. SyncRtcInfo(gunIndex, pDcChargingInfo->Evboard_id, (int)rtc);
  628. GetFirmwareVersion(gunIndex, pDcChargingInfo->Evboard_id);
  629. } else if (pDcChargingInfo->Type == _Type_GB &&
  630. ShmGBTData->evse[typeIndex].SelfTest_Comp != PASS) {
  631. SyncRtcInfo(gunIndex, pDcChargingInfo->Evboard_id, (int)rtc);
  632. GetFirmwareVersion(gunIndex, pDcChargingInfo->Evboard_id);
  633. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  634. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121 &&
  635. ShmCcsData->V2GMessage_DIN70121[typeIndex].SelfTest_Comp != PASS) {
  636. SyncRtcInfo(gunIndex, pDcChargingInfo->Evboard_id, (int)rtc);
  637. GetFirmwareVersion(gunIndex, pDcChargingInfo->Evboard_id);
  638. }
  639. }
  640. /*
  641. if (pDcChargingInfo->Type == _Type_Chademo) {
  642. pAlarmCode->AlarmEvents.bits.ChademoboardStestFail =
  643. (ShmCHAdeMOData->evse[typeIndex].SelfTest_Comp != PASS) ? true : false;
  644. pAlarmCode->AlarmEvents.bits.ChademoModuleCommFail =
  645. ( CanFd < 0 ) ? true : false;
  646. } else if (pDcChargingInfo->Type == _Type_GB) {
  647. pAlarmCode->AlarmEvents.bits.GbtboardStestFail =
  648. (ShmGBTData->evse[typeIndex].SelfTest_Comp != PASS) ? true : false;
  649. pAlarmCode->AlarmEvents.bits.mFail =
  650. ( CanFd < 0 ) ? true : false;
  651. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  652. pAlarmCode->AlarmEvents.bits.CCSboardStestFail =
  653. (ShmCcsData->V2GMessage_DIN70121[typeIndex].SelfTest_Comp != PASS) ? true : false;
  654. pAlarmCode->AlarmEvents.bits.ChademoModuleCommFail =
  655. ( CanFd < 0 ) ? true : false;
  656. }*/
  657. //固定要取得的資訊 : 1.槍鎖狀態, 2."Connector 1" 溫度, 3."Connector 2" 溫度, 4.Pilot Voltage
  658. //log_info("GetMiscellaneousInfo. index = %d, Eid = %d ",
  659. // gunIndex,
  660. // pDcChargingInfo->Evboard_id);
  661. GetMiscellaneousInfo(gunIndex,
  662. pDcChargingInfo->RelayK1K2Status,
  663. pDcChargingInfo->PresentChargedEnergy,
  664. (pDcChargingInfo->PresentChargingVoltage * 10),
  665. pDcChargingInfo->Evboard_id);
  666. //checkConnectorOVPState(gunIndex);
  667. }
  668. switch (pDcChargingInfo->SystemStatus) {
  669. case S_IDLE:
  670. case S_RESERVATION:
  671. if (pDcChargingInfo->Type == _Type_Chademo) {
  672. ClearAbnormalStatus_Chademo(gunIndex);
  673. if (pSysInfo->PageIndex == _LCM_WAIT_FOR_PLUG) {
  674. if (!chkChademoPermission[gunIndex]) {
  675. chkChademoPermission[gunIndex] = true;
  676. gettimeofday(&_chk_chademo_permission_timeout[gunIndex], NULL);
  677. SendCommunicationOnly(gunIndex);
  678. } else {
  679. _timeBuf = GetTimeoutValue(_chk_chademo_permission_timeout[gunIndex]);
  680. if (_timeBuf < 0) {
  681. gettimeofday(&_chk_chademo_permission_timeout[gunIndex], NULL);
  682. } else {
  683. if (_timeBuf / 1000 > 10000) {
  684. SendCommunicationOnly(gunIndex);
  685. gettimeofday(&_chk_chademo_permission_timeout[gunIndex], NULL);
  686. }
  687. }
  688. }
  689. } else if (chkChademoPermission[gunIndex]) {
  690. chkChademoPermission[gunIndex] = false;
  691. SendStopOnly(gunIndex);
  692. }
  693. if (ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP == NO) {
  694. pDcChargingInfo->StopChargeFlag = NO;
  695. }
  696. } else if (pDcChargingInfo->Type == _Type_GB) {
  697. ClearAbnormalStatus_GB(gunIndex);
  698. if (ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP == NO) {
  699. pDcChargingInfo->StopChargeFlag = NO;
  700. }
  701. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  702. ClearAbnormalStatus_CCS(gunIndex);
  703. if (ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP == NO) {
  704. pDcChargingInfo->StopChargeFlag = NO;
  705. }
  706. }
  707. // Set Ev board in communication mode
  708. // Get EVCCID for authorize when gun plug-in only for CCS
  709. if ( pDcChargingInfo->ConnectorPlugIn && pSysConfig->isAuthrizeByEVCCID &&
  710. ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  711. if (Comcont == 1 && evstatus < 15) {
  712. GetEVCCIDReq(gunIndex,pDcChargingInfo->Evboard_id);
  713. SendCommunicationOnly(gunIndex);
  714. }
  715. }
  716. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121 && priorityLow == 1) {
  717. GetOutputReq(gunIndex,pDcChargingInfo->Evboard_id);
  718. if (evstatus != ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].PresentMsgFlowStatus) {
  719. evstatus = ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  720. }
  721. if ( evstatus > 19 && evstatus < 255 )
  722. GetEVCCIDReq(gunIndex,pDcChargingInfo->Evboard_id);
  723. }
  724. if (priorityLow == 1) {
  725. pDcChargingInfo->PresentChargedEnergy = 0;
  726. pDcChargingInfo->PresentChargingPower = 0;
  727. pDcChargingInfo->GroundFaultStatus = GFD_WAIT;
  728. pDcChargingInfo->RealRatingPower = 0;
  729. pDcChargingInfo->StopChargeFlag = NO;
  730. pDcChargingInfo->NormalStopChargeFlag = NO;//DS60-120 add
  731. pDcChargingInfo->ChargingFee = 0.0;
  732. pDcChargingInfo->EvBatterySoc = 0;
  733. pDcChargingInfo->EvBatteryStartSoc = 0; //DS60-120 add
  734. pDcChargingInfo->EvBatteryMaxVoltage = 0; //DS60-120 add
  735. pDcChargingInfo->ChargingProfilePower = -1; //DS60-120 add
  736. pDcChargingInfo->ChargingProfileCurrent = -1; //DS60-120 add
  737. if (pSysInfo->MainChargingMode == _MAIN_CHARGING_MODE_MAX) { //DS60-120 add
  738. pDcChargingInfo->PresentChargingVoltage = 0;
  739. pDcChargingInfo->PresentChargingCurrent = 0;
  740. pDcChargingInfo->EvBatteryMaxVoltage = 0;
  741. }
  742. chargingTime[gunIndex] = 0;
  743. //maxChargingCur[gunIndex] = pSysConfig->MaxChargingCurrent * 10;
  744. //maxChargingPow = (pSysConfig->MaxChargingPower * 10);
  745. //DS60-120 add
  746. SendErrorCount[gunIndex] = 0;
  747. //maxChargingPow = pSysConfig->MaxChargingPower * 10;
  748. // ShmPsuData->SystemAvailablePower 已是 * 10
  749. //maxChargingPow = ShmPsuData->SystemAvailablePower;
  750. if (pSysConfig->MaxChargingPower * 10 != 0 &&
  751. pSysConfig->MaxChargingPower * 10 < maxChargingPow) {
  752. maxChargingPow = pSysConfig->MaxChargingPower * 10;
  753. }
  754. LogInfo[gunIndex][EV_LOG_EVSE_MAX_VOL] = 0;
  755. LogInfo[gunIndex][EV_LOG_EVSE_MAX_CUR] = 0;
  756. LogInfo[gunIndex][EV_LOG_MAX_BATT_VOL] = 0;
  757. LogInfo[gunIndex][EV_LOG_SOC] = 0;
  758. SetPresentChargingOutputPower();
  759. }
  760. break;
  761. case S_PREPARNING:
  762. chkChademoPermission[gunIndex] = false; //DS60-120 add
  763. // 設定當前輸出
  764. SetPresentChargingOutputPower();
  765. pDcChargingInfo->PowerConsumption = 0;
  766. break;
  767. case S_PREPARING_FOR_EV:
  768. // 開始確認車端是否同意開始充電 : 1.SOC, 2.Target Vol, 3.Target Cur, 4.Charging remaining time
  769. GetOutputReq(gunIndex, pDcChargingInfo->Evboard_id);
  770. //log_info("PresentChargingVoltage = %f ", pDcChargingInfo->PresentChargingVoltage);
  771. //log_info("PresentChargingCurrent = %f ", pDcChargingInfo->PresentChargingCurrent);
  772. //log_info("AvailableChargingPower = %f ", pDcChargingInfo->AvailableChargingPower);
  773. //log_info("AvailableChargingCurrent = %f ", pDcChargingInfo->AvailableChargingCurrent);
  774. //log_info("MaximumChargingVoltage = %f ", pDcChargingInfo->MaximumChargingVoltage);
  775. // 設定當前輸出
  776. SetPresentChargingOutputPower();
  777. if (ShmSelectGunInfo->WaitDoCommPermission[gunIndex] == YES) {
  778. ShmSelectGunInfo->WaitDoCommPermission[gunIndex] = NO;
  779. //if (priorityLow == 1) {
  780. // 樁端輸出能力
  781. maxVol = pDcChargingInfo->MaximumChargingVoltage;
  782. maxCur = pDcChargingInfo->AvailableChargingCurrent;
  783. GetMaxVolAndCurMethod(gunIndex, &maxVol, &maxCur);
  784. //DS60-120 add
  785. if (LogInfo[gunIndex][EV_LOG_EVSE_MAX_VOL] != maxVol ||
  786. LogInfo[gunIndex][EV_LOG_EVSE_MAX_CUR] != maxCur) {
  787. LogInfo[gunIndex][EV_LOG_EVSE_MAX_VOL] = maxVol;
  788. LogInfo[gunIndex][EV_LOG_EVSE_MAX_CUR] = maxCur;
  789. log_info("To EV_%d Max_Vol = %.1f, Cap_Cur = %.1f, Cap_Pow = %.1f",
  790. gunIndex,
  791. maxVol / 10,
  792. maxCur / 10,
  793. pDcChargingInfo->AvailableChargingPower / 10);
  794. }
  795. pDcChargingInfo->RealMaxVoltage = maxVol;
  796. SetChargingPermission(gunIndex,
  797. START,
  798. pDcChargingInfo->AvailableChargingPower,
  799. maxCur,
  800. maxVol,
  801. pDcChargingInfo->Evboard_id);
  802. // 取得車端電池資訊 : 1.AC or DC ? 2.Total battery cap, 3.Max battery vol, 4.Max battery cur
  803. GetEvBatteryInfo(gunIndex, pDcChargingInfo->Evboard_id);
  804. }
  805. gettimeofday(&_chk_ratingPower_timeout[gunIndex], NULL);
  806. break;
  807. case S_PREPARING_FOR_EVSE:
  808. case S_CCS_PRECHARGE_ST0:
  809. case S_CCS_PRECHARGE_ST1:
  810. // 開始確認車端是否同意開始充電
  811. GetOutputReq(gunIndex, pDcChargingInfo->Evboard_id);
  812. // 設定當前輸出
  813. SetPresentChargingOutputPower();
  814. if (priorityLow % 5 == 1) {
  815. // 取得車端電池資訊 : 1.AC or DC ? 2.Total battery cap, 3.Max battery vol, 4.Max battery cur
  816. GetEvBatteryInfo(gunIndex, pDcChargingInfo->Evboard_id); //DS60-120 add
  817. // 樁端輸出能力改變
  818. SetPresentChargingOutputCap();
  819. }
  820. //DS60-120 add
  821. if (LogInfo[gunIndex][EV_LOG_MAX_BATT_VOL] != pDcChargingInfo->EvBatteryMaxVoltage) {
  822. LogInfo[gunIndex][EV_LOG_MAX_BATT_VOL] = pDcChargingInfo->EvBatteryMaxVoltage;
  823. log_info("index = %d, Ev Maximum Battery Voltage = %f ",
  824. gunIndex,
  825. pDcChargingInfo->EvBatteryMaxVoltage);
  826. }
  827. if (LogInfo[gunIndex][EV_LOG_SOC] != pDcChargingInfo->EvBatterySoc) {
  828. LogInfo[gunIndex][EV_LOG_SOC] = pDcChargingInfo->EvBatterySoc;
  829. log_info("index = %d, SOC = %d ",
  830. gunIndex,
  831. pDcChargingInfo->EvBatterySoc);
  832. }
  833. // 持續通知 Isolation 測試狀態
  834. if (priorityLow == 1) {
  835. // 拉 500 V 如果在一秒鐘內 GFD 都符合則 PASS
  836. // if (_chargingData[_index]->FireChargingVoltage >= 3500)
  837. // pDcChargingInfo->GroundFaultStatus = GFD_PASS;
  838. //log_info("To EV_%d GFD = %d ", _index,pDcChargingInfo->GroundFaultStatus);
  839. //if(_chargingData[_index]->GroundFaultStatus != GFD_WAIT)
  840. {
  841. //if ((GetTimeoutValue(_derating_time) / 1000) > 1000)
  842. gfgResult = pDcChargingInfo->GroundFaultStatus;
  843. // GB & Chademo ~ Warning 也先算 Pass,因為 CCS 認證會驗 Warning 故不可更動
  844. if (pDcChargingInfo->Type == _Type_Chademo ||
  845. pDcChargingInfo->Type == _Type_GB) {
  846. if (gfgResult == GFD_WARNING) {
  847. gfgResult = GFD_PASS;
  848. }
  849. }
  850. if (gfgResult == GFD_WARNING || gfgResult == GFD_PASS) {
  851. if (((GetTimeoutValue(_chk_ratingPower_timeout[gunIndex]) / 1000) > 12000 &&
  852. pDcChargingInfo->RealRatingPower > 0) ||
  853. (GetTimeoutValue(_chk_ratingPower_timeout[gunIndex]) / 1000) > 14000) {
  854. //log_info("**********EvComm : gunIndex= %d, RealRatingPower = %d ",
  855. // gunIndex,pDcChargingInfo->RealRatingPower);
  856. //gfgResult = GFD_PASS;
  857. //DS60-120 add
  858. if (LogInfo[gunIndex][EV_LOG_REAL_CAP_POW] != pDcChargingInfo->RealRatingPower) {
  859. LogInfo[gunIndex][EV_LOG_REAL_CAP_POW] = pDcChargingInfo->RealRatingPower;
  860. log_info("Conn %d, RealRatingPower = %d ",
  861. gunIndex,
  862. pDcChargingInfo->RealRatingPower);
  863. }
  864. } else {
  865. gfgResult = GFD_WAIT;
  866. }
  867. }
  868. SetIsolationStatus(gunIndex, gfgResult, pDcChargingInfo->Evboard_id);
  869. }
  870. if (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0 &&
  871. pDcChargingInfo->PrechargeStatus == PRECHARGE_READY
  872. ) {
  873. SetEvsePrechargeInfo(gunIndex, PRECHARGE_PRERELAY_PASS, pDcChargingInfo->Evboard_id);
  874. }
  875. }
  876. ftime(&waitChargingTime);
  877. break;
  878. case S_CHARGING:
  879. //if (waitPsuVolwithRealyVol(gunIndex) == NO) {
  880. // continue;
  881. //}
  882. GetEvBatteryInfo(gunIndex, pDcChargingInfo->Evboard_id); //DS60-120 add
  883. // 計算 Power
  884. pDcChargingInfo->PresentChargingPower =
  885. ((float)((pDcChargingInfo->PresentChargingVoltage) *
  886. (pDcChargingInfo->PresentChargingCurrent)) / 1000);
  887. //DS60-120 remove
  888. if (chargingTime[gunIndex] == 0 ||
  889. chargingTime[gunIndex] > pDcChargingInfo->PresentChargedDuration) {
  890. chargingTime[gunIndex] = pDcChargingInfo->PresentChargedDuration;
  891. } else {
  892. int passTime = pDcChargingInfo->PresentChargedDuration - chargingTime[gunIndex];
  893. if (passTime > 0) {
  894. float changingPow = (pDcChargingInfo->PresentChargingPower) * passTime / 3600;
  895. if (pSysConfig->BillingData.isBilling) {
  896. pDcChargingInfo->ChargingFee += changingPow * pSysConfig->BillingData.Cur_fee;
  897. }
  898. pDcChargingInfo->PresentChargedEnergy += changingPow;
  899. ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption += changingPow;
  900. pDcChargingInfo->PowerConsumption += changingPow;
  901. chargingTime[gunIndex] = pDcChargingInfo->PresentChargedDuration;
  902. }
  903. }
  904. // 開始確認車端是否同意開始充電
  905. GetOutputReq(gunIndex, pDcChargingInfo->Evboard_id);
  906. // 設定當前輸出
  907. ftime(&nowTime);
  908. if (!(DiffTimeb(waitChargingTime, nowTime) < 5000 ||
  909. DiffTimeb(waitChargingTime, nowTime) < 0)) {
  910. psuOutputReady[gunIndex] = true;
  911. }
  912. SetPresentChargingOutputPower();
  913. // for test end
  914. if (priorityLow % 5 == 0) {
  915. // 樁端輸出能力改變
  916. SetPresentChargingOutputCap();
  917. }
  918. if ((pDcChargingInfo->GroundFaultStatus == GFD_FAIL) ||
  919. (pDcChargingInfo->Type == _Type_CCS_2)) {
  920. SetIsolationStatus(gunIndex,
  921. pDcChargingInfo->GroundFaultStatus,
  922. pDcChargingInfo->Evboard_id);
  923. }
  924. /*
  925. else if (pDcChargingInfo->Type == _Type_CCS_2) {
  926. SetIsolationStatus(gunIndex, pDcChargingInfo->GroundFaultStatus, pDcChargingInfo->Evboard_id);
  927. }*/
  928. // GFD 失敗再通知
  929. if (priorityLow == 1) {
  930. if (pDcChargingInfo->Type == _Type_CCS_2 &&
  931. pDcChargingInfo->PrechargeStatus == PRECHARGE_READY) {
  932. SetEvsePrechargeInfo(gunIndex,
  933. PRECHARGE_CHARELAY_PASS,
  934. pDcChargingInfo->Evboard_id);
  935. }
  936. }
  937. break;
  938. case S_ALARM:
  939. case S_TERMINATING:
  940. // 設定當前輸出
  941. setCurrentOutput();
  942. SetPresentChargingOutputPower();
  943. // 槍鎖還在,則代表是樁端要求的停止
  944. if (pDcChargingInfo->GunLocked == START ||
  945. pDcChargingInfo->Type == _Type_CCS_2) {
  946. uint8_t normalStop = 0x01;
  947. uint8_t stopReason[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  948. if (GetStopChargingReasonByEvse(gunIndex, stopReason)) {
  949. normalStop = 0x02;
  950. }
  951. EvseStopChargingEvent(normalStop,
  952. stopReason,
  953. pDcChargingInfo->Evboard_id);
  954. SendErrorCount[gunIndex] += 1; //DS60-120 add
  955. }
  956. if (pDcChargingInfo->Type == _Type_CCS_2) {
  957. SetIsolationStatus(gunIndex,
  958. pDcChargingInfo->GroundFaultStatus,
  959. pDcChargingInfo->Evboard_id);
  960. }
  961. GetOutputReq(gunIndex, pDcChargingInfo->Evboard_id);
  962. //DS60-120 add
  963. if (pDcChargingInfo->SystemStatus == S_ALARM) {
  964. if (priorityLow == 1) {
  965. // 樁端輸出能力
  966. maxVol = pDcChargingInfo->MaximumChargingVoltage;
  967. maxCur = pDcChargingInfo->AvailableChargingCurrent;
  968. GetMaxVolAndCurMethod(gunIndex, &maxVol, &maxCur);
  969. SetChargingPermission(gunIndex,
  970. STOP,
  971. pDcChargingInfo->AvailableChargingPower,
  972. maxCur,
  973. maxVol,
  974. pDcChargingInfo->Evboard_id);
  975. }
  976. }
  977. break;
  978. case S_COMPLETE:
  979. // 設定當前輸出
  980. SetPresentChargingOutputPower();
  981. if (priorityLow == 1) {
  982. // 樁端輸出能力
  983. maxVol = pDcChargingInfo->MaximumChargingVoltage;
  984. maxCur = pDcChargingInfo->AvailableChargingCurrent;
  985. GetMaxVolAndCurMethod(gunIndex, &maxVol, &maxCur);
  986. SetChargingPermission(gunIndex,
  987. STOP,
  988. pDcChargingInfo->AvailableChargingPower,
  989. maxCur,
  990. maxVol,
  991. pDcChargingInfo->Evboard_id);
  992. //DS60-120 add
  993. //if (pDcChargingInfo->EvBatterySoc >= 100) {
  994. // //滿電,則直接清掉錯誤
  995. // if (pDcChargingInfo->Type == _Type_Chademo) {
  996. // ClearAbnormalStatus_Chademo(gunIndex);
  997. // } else if (pDcChargingInfo->Type == _Type_GB) {
  998. // ClearAbnormalStatus_GB(gunIndex);
  999. // } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1000. // ClearAbnormalStatus_CCS(gunIndex);
  1001. // }
  1002. //}
  1003. }
  1004. break;
  1005. }//switch
  1006. }//for
  1007. Comcont >= 200 ? Comcont = 1: Comcont++;
  1008. priorityLow >= 20 ? priorityLow = 1 : priorityLow++;
  1009. usleep(50000);
  1010. }//while
  1011. return 0;
  1012. }