Module_EvTxComm.c 46 KB

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