Module_EvTxComm.c 44 KB

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