Module_EvTxComm.c 47 KB

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