Module_EvTxComm.c 50 KB

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