Module_EvTxComm.c 45 KB

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