Module_EvTxComm.c 54 KB

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