Module_EvTxComm.c 49 KB

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