Module_EvTxComm.c 45 KB

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