Module_EvTxComm.c 47 KB

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