Module_EvTxComm.c 42 KB

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