Module_EvTxComm.c 51 KB

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