Module_EvTxComm.c 45 KB

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