Module_EvTxComm.c 57 KB

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