Module_EvTxComm.c 55 KB

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