Module_EvTxComm.c 47 KB

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