Module_EvTxComm.c 51 KB

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