Module_EvTxComm.c 57 KB

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