Module_EvTxComm.c 50 KB

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