Module_EvTxComm.c 48 KB

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