Module_InternalComm.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "internalComm.h"
  31. #include <stdbool.h>
  32. #define Debug
  33. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  34. #define PASS 1
  35. #define FAIL -1
  36. #define YES 1
  37. #define NO 0
  38. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  39. struct StatusCodeData *ShmStatusCodeData;
  40. struct FanModuleData *ShmFanModuleData;
  41. struct RelayModuleData *ShmRelayModuleData;
  42. struct CHAdeMOData *ShmCHAdeMOData;
  43. struct CcsData *ShmCcsData;
  44. #define VIN_MAX_VOLTAGE 250 // 大於該值 : OVP
  45. #define VIN_MIN_VOLTAGE 170 // 小於該值 : UVP
  46. #define VIN_DROP_VOLTAGE 150 // 小於該值 : ac drop
  47. #define VOUT_MAX_VOLTAGE 750
  48. #define VOUT_MIN_VOLTAGE 150
  49. #define IOUT_MAX_CURRENT 50
  50. #define MAX_FAN_SPEED 6000
  51. #define MIN_FAN_SPEED 300
  52. // GFD Status
  53. #define GFD_IDLE 0
  54. #define GFD_CABLECHK 1
  55. #define GFD_PRECHARGE 2
  56. #define GFD_CHARGING 3
  57. // 最小切換 Relay 電壓
  58. #define SELF_TO_CHANGE_RELAY_STATUS 600
  59. // 透過電壓確認 Relay 是否搭上的依據電壓
  60. #define CHECK_RELAY_STATUS 300
  61. #define CHECK_RELAY_STATUS_GAP 100
  62. // 安全在停止充電程序中斷開 Relay 的電流
  63. #define SEFETY_SWITCH_RELAY_CUR 20
  64. // 確認 Relay Welding 電壓
  65. #define RELAY_WELDING_DET 300
  66. byte gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  67. // 槍資訊
  68. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  69. byte gfdChkFailCount[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  70. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
  71. int Uart5Fd;
  72. char *relayRs485PortName = "/dev/ttyS5";
  73. unsigned short fanSpeedSmoothValue = 100;
  74. struct timeval _priority_time;
  75. Ver ver;
  76. PresentInputVoltage inputVoltage;
  77. PresentOutputVoltage outputVoltage;
  78. FanSpeed fanSpeed;
  79. Temperature temperature;
  80. AuxPower auxPower;
  81. Gfd gfd_adc;
  82. Gfd_config gfd_config;
  83. Gpio_in gpio_in;
  84. Gpio_out gpio_out;
  85. Relay outputRelay;
  86. Relay regRelay;
  87. int StoreLogMsg(const char *fmt, ...);
  88. unsigned long GetTimeoutValue(struct timeval _sour_time);
  89. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  90. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  91. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  92. unsigned long GetTimeoutValue(struct timeval _sour_time)
  93. {
  94. struct timeval _end_time;
  95. gettimeofday(&_end_time, NULL);
  96. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  97. }
  98. int StoreLogMsg(const char *fmt, ...)
  99. {
  100. char Buf[4096+256];
  101. char buffer[4096];
  102. time_t CurrentTime;
  103. struct tm *tm;
  104. va_list args;
  105. va_start(args, fmt);
  106. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  107. va_end(args);
  108. memset(Buf,0,sizeof(Buf));
  109. CurrentTime = time(NULL);
  110. tm=localtime(&CurrentTime);
  111. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  112. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  113. buffer,
  114. tm->tm_year+1900,tm->tm_mon+1);
  115. system(Buf);
  116. return rc;
  117. }
  118. int DiffTimeb(struct timeb ST, struct timeb ET)
  119. {
  120. //return milli-second
  121. unsigned int StartTime,StopTime;
  122. StartTime=(unsigned int)ST.time;
  123. StopTime=(unsigned int)ET.time;
  124. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  125. }
  126. unsigned short MaxValue(unsigned short value1, unsigned short value2)
  127. {
  128. return value1 >= value2 ? value1 : value2;
  129. }
  130. //==========================================
  131. // Communication Function
  132. //==========================================
  133. void GetFwAndHwVersion_Aux()
  134. {
  135. if (Query_FW_Ver(Uart5Fd, Addr.Aux, &ver) == PASS)
  136. {
  137. // SystemInfo
  138. strcpy((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, ver.Version_FW);
  139. printf("s1 = %s \n", ver.Version_FW);
  140. }
  141. if (Query_HW_Ver(Uart5Fd, Addr.Aux, &ver) == PASS)
  142. {
  143. // SystemInfo
  144. strcpy((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, ver.Version_HW);
  145. printf("s2 = %s \n", ver.Version_HW);
  146. }
  147. }
  148. void GetFwAndHwVersion_Fan()
  149. {
  150. if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  151. {
  152. // FanModuleData
  153. strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
  154. // SystemInfo
  155. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
  156. printf("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
  157. }
  158. if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  159. {
  160. // SystemInfo
  161. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
  162. printf("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
  163. }
  164. }
  165. void GetFwAndHwVersion_Relay()
  166. {
  167. if (Query_FW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  168. {
  169. // FanModuleData
  170. strcpy((char *) ShmRelayModuleData->version, ver.Version_FW);
  171. // SystemInfo
  172. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
  173. printf("GetFwAndHwVersion_Relay s1 = %s \n", ver.Version_FW);
  174. }
  175. if (Query_HW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  176. {
  177. // SystemInfo
  178. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  179. printf("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
  180. }
  181. }
  182. void GetTemperature_Aux()
  183. {
  184. memset(temperature.temperature, 0, ARRAY_SIZE(temperature.temperature));
  185. if (Query_Temperature(Uart5Fd, Addr.Aux, &temperature) == PASS)
  186. {
  187. // aux temp
  188. // UI 實際顯示溫度轉換
  189. //char s[4];
  190. //sprintf(s,"%d",(-60 + temperature[2].temperature[4])),
  191. //printf("s = %s \n",s);
  192. printf("Aux temp = %d,%d,%d,%d,%d,%d,%d,%d \n",
  193. (-60 + temperature.temperature[0]),
  194. (-60 + temperature.temperature[1]),
  195. (-60 + temperature.temperature[2]),
  196. (-60 + temperature.temperature[3]),
  197. (-60 + temperature.temperature[4]),
  198. (-60 + temperature.temperature[5]),
  199. (-60 + temperature.temperature[6]),
  200. (-60 + temperature.temperature[7]));
  201. }
  202. }
  203. void GetTemperature_Relay()
  204. {
  205. memset(temperature.temperature, 0, ARRAY_SIZE(temperature.temperature));
  206. if (Query_Temperature(Uart5Fd, Addr.Relay, &temperature) == PASS)
  207. {
  208. // relay temp
  209. printf("Relay temp = %d,%d,%d,%d,%d,%d,%d,%d \n",
  210. (-60 + temperature.temperature[0]),
  211. (-60 + temperature.temperature[1]),
  212. (-60 + temperature.temperature[2]),
  213. (-60 + temperature.temperature[3]),
  214. (-60 + temperature.temperature[4]),
  215. (-60 + temperature.temperature[5]),
  216. (-60 + temperature.temperature[6]),
  217. (-60 + temperature.temperature[7]));
  218. }
  219. }
  220. // AC 三相輸入電壓
  221. void GetPresentInputVol()
  222. {
  223. if (Query_Present_InputVoltage(Uart5Fd, Addr.Relay, &inputVoltage) == PASS)
  224. {
  225. // resolution : 0.1
  226. //printf("InputVoltageR = %f \n", inputVoltage.L1N_L12);
  227. //printf("InputVoltageS = %f \n", inputVoltage.L2N_L23);
  228. //printf("InputVoltageT = %f \n", inputVoltage.L3N_L31);
  229. ShmRelayModuleData->InputL1Volt = inputVoltage.L1N_L12;
  230. ShmRelayModuleData->InputL2Volt = inputVoltage.L2N_L23;
  231. ShmRelayModuleData->InputL3Volt = inputVoltage.L3N_L31;
  232. //********************************************************************************************************//
  233. // VIN < 170
  234. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE)
  235. {
  236. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x01;
  237. }
  238. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE)
  239. {
  240. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x01;
  241. }
  242. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE)
  243. {
  244. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x01;
  245. }
  246. //********************************************************************************************************//
  247. // VIN > 250
  248. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE)
  249. {
  250. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x01;
  251. }
  252. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE)
  253. {
  254. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x01;
  255. }
  256. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE)
  257. {
  258. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x01;
  259. }
  260. //********************************************************************************************************//
  261. // VIN < 150
  262. if (inputVoltage.L1N_L12 < VIN_DROP_VOLTAGE)
  263. {
  264. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x01;
  265. }
  266. if (inputVoltage.L2N_L23 < VIN_DROP_VOLTAGE)
  267. {
  268. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x01;
  269. }
  270. if (inputVoltage.L3N_L31 < VIN_DROP_VOLTAGE)
  271. {
  272. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x01;
  273. }
  274. //********************************************************************************************************//
  275. // 150 <= VIN < 160
  276. // if (inputVoltage.L1N_L12 >= VIN_MIN_VOLTAGE && inputVoltage.L1N_L12 <= VIN_LOW_VOLTAGE)
  277. // {
  278. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x00;
  279. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x00;
  280. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x00;
  281. // }
  282. // if (inputVoltage.L2N_L23 >= VIN_MIN_VOLTAGE && inputVoltage.L2N_L23 <= VIN_LOW_VOLTAGE)
  283. // {
  284. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x00;
  285. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x00;
  286. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x00;
  287. // }
  288. // if (inputVoltage.L3N_L31 >= VIN_MIN_VOLTAGE && inputVoltage.L3N_L31 <= VIN_LOW_VOLTAGE)
  289. // {
  290. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x00;
  291. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x00;
  292. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x00;
  293. // }
  294. }
  295. }
  296. // 左右槍的 Relay 前後的輸出電壓
  297. void GetPersentOutputVol()
  298. {
  299. if (Query_Present_OutputVoltage(Uart5Fd, Addr.Relay, &outputVoltage) == PASS)
  300. {
  301. // printf("Conn1 fuse 1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  302. // printf("Conn1 relay 1 = %f \n", outputVoltage.behindRelay_Voltage_C1);
  303. // printf("Conn2 fuse 2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  304. // printf("Conn2 relay 2 = %f \n", outputVoltage.behindRelay_Voltage_C2);
  305. //printf("outputVoltage.behindFuse_Voltage_C1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  306. //printf("outputVoltage.behindFuse_Voltage_C2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  307. ShmRelayModuleData->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  308. ShmRelayModuleData->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  309. ShmRelayModuleData->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  310. ShmRelayModuleData->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  311. for (int index = 0; index < gunCount; index++)
  312. {
  313. if (index == 0)
  314. {
  315. if (_chargingData[index]->Evboard_id == 0x01)
  316. {
  317. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun1FuseOutputVolt;
  318. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun1RelayOutputVolt;
  319. }
  320. else if (_chargingData[index]->Evboard_id == 0x02)
  321. {
  322. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  323. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  324. }
  325. }
  326. else if (index == 1)
  327. {
  328. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  329. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  330. }
  331. unsigned short Ovp = 0;
  332. unsigned short Ocp = 0;
  333. //Ovp = MIN [VOUT_MAX_VOLTAGE, EV_BATTERY_VOLTAGE] // 最大輸出電壓與電池電壓最大值
  334. //Ocp = MIN [IOUT_MAX_CURRENT, EV_CURRENT_REQ] // 最大輸出電流與需求電流最小值
  335. if (_chargingData[index]->Type == _Type_Chademo)
  336. {
  337. Ovp = MaxValue(_chargingData[index]->MaximumChargingVoltage, _chargingData[index]->EvBatteryMaxVoltage);
  338. Ocp = MaxValue(_chargingData[index]->PresentChargingCurrent, ShmCHAdeMOData->ev[_chargingData[index]->type_index].ChargingCurrentRequest);
  339. if (_chargingData[index]->PresentChargingVoltage >= Ovp)
  340. {
  341. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = 0x01;
  342. }
  343. if (_chargingData[index]->PresentChargingCurrent >= Ocp)
  344. {
  345. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOCP = 0x01;
  346. }
  347. }
  348. else if (_chargingData[index]->Type == _Type_CCS)
  349. {
  350. }
  351. }
  352. }
  353. }
  354. // 風扇速度
  355. void GetFanSpeed()
  356. {
  357. printf("Get fan board speed \n");
  358. if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
  359. {
  360. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  361. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  362. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  363. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  364. printf("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  365. printf("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
  366. printf("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
  367. printf("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
  368. // Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
  369. //SysInfoData (SystemFanRotaSpeed)
  370. }
  371. }
  372. // 讀取 Relay 狀態
  373. void GetRelayOutputStatus()
  374. {
  375. if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
  376. {
  377. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  378. }
  379. }
  380. // 確認 K1 K2 relay 的狀態
  381. void CheckK1K2RelayOutput(byte index)
  382. {
  383. if (index == 0)
  384. {
  385. if (_chargingData[index]->Evboard_id == 0x01)
  386. {
  387. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
  388. _chargingData[index]->RelayK1K2Status = YES;
  389. else
  390. _chargingData[index]->RelayK1K2Status = NO;
  391. if(_chargingData[index]->Type == _Type_CCS)
  392. {
  393. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  394. _chargingData[index]->RelayKPK2Status = YES;
  395. else
  396. _chargingData[index]->RelayKPK2Status = NO;
  397. }
  398. }
  399. else if (_chargingData[index]->Evboard_id == 0x02)
  400. {
  401. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  402. _chargingData[index]->RelayK1K2Status = YES;
  403. else
  404. _chargingData[index]->RelayK1K2Status = NO;
  405. if(_chargingData[index]->Type == _Type_CCS)
  406. {
  407. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  408. _chargingData[index]->RelayKPK2Status = YES;
  409. else
  410. _chargingData[index]->RelayKPK2Status = NO;
  411. }
  412. }
  413. }
  414. else if (index == 1)
  415. {
  416. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  417. _chargingData[index]->RelayK1K2Status = YES;
  418. else
  419. _chargingData[index]->RelayK1K2Status = NO;
  420. if(_chargingData[index]->Type == _Type_CCS)
  421. {
  422. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  423. _chargingData[index]->RelayKPK2Status = YES;
  424. else
  425. _chargingData[index]->RelayKPK2Status = NO;
  426. }
  427. }
  428. if (regRelay.relay_event.bits.Gun1_Parallel_N == YES && regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  429. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = YES;
  430. else
  431. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = NO;
  432. }
  433. void GetGfdAdc()
  434. {
  435. // define : 每 0.2 ~ 1 秒一次
  436. // occur : <= 75k 歐姆 @ 150 - 750 Vdc
  437. // warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
  438. if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
  439. {
  440. for (int i = 0; i < gunCount; i++)
  441. {
  442. if (i == 0)
  443. {
  444. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
  445. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  446. {
  447. DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
  448. i, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  449. }
  450. }
  451. else if (i == 1)
  452. {
  453. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
  454. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  455. {
  456. DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
  457. i, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  458. }
  459. }
  460. }
  461. //if (gfd_adc.result_conn1 != 0)
  462. {
  463. // printf("******************Resister_conn1 = %d, voltage_conn1 = %d, result_conn1 = %d, step = %d \n",
  464. // gfd_adc.Resister_conn1,
  465. // gfd_adc.voltage_conn1,
  466. // gfd_adc.result_conn1,
  467. // gfd_adc.rb_step_1);
  468. }
  469. }
  470. }
  471. void GetGpioInput()
  472. {
  473. if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
  474. {
  475. // AC Contactor Status
  476. //ShmSysConfigAndInfo->SysInfo.AcContactorStatus = gpio_in.AC_Connector;
  477. if (gpio_in.AC_MainBreaker == 1)
  478. {
  479. // AC Main Breaker ON
  480. printf("RB AC Main Breaker. \n");
  481. }
  482. if (gpio_in.SPD == 1)
  483. {
  484. // SPD (雷擊保護) ON
  485. printf("RB SPD. \n");
  486. }
  487. if (gpio_in.Door_Open == 1)
  488. {
  489. // Door Open
  490. printf("RB Door Open. \n");
  491. }
  492. if (gpio_in.GFD[0] == 1)
  493. {
  494. // GFD_1 Trigger
  495. }
  496. if (gpio_in.GFD[1] == 1)
  497. {
  498. // GFD_2 Trigger
  499. }
  500. if (gpio_in.AC_Drop == 1)
  501. {
  502. // AC Drop
  503. printf("RB AC Drop. \n");
  504. }
  505. if (gpio_in.Emergency_IO == 1)
  506. {
  507. // Emergency IO ON
  508. printf("RB Emergency IO ON. \n");
  509. }
  510. if (gpio_in.Button_Emergency_Press == 1)
  511. {
  512. // Emergency button Press
  513. }
  514. if (gpio_in.Button_On_Press == 1)
  515. {
  516. // On button Press
  517. }
  518. if (gpio_in.Button_Off_Press == 1)
  519. {
  520. // Off button Press
  521. }
  522. if (gpio_in.Key_1_Press == 1)
  523. {
  524. // key 1 press
  525. }
  526. if (gpio_in.Key_2_Press == 1)
  527. {
  528. // key 2 press
  529. }
  530. if (gpio_in.Key_3_Press == 1)
  531. {
  532. // key 3 press
  533. }
  534. if (gpio_in.Key_4_Press == 1)
  535. {
  536. // key 4 press
  537. }
  538. }
  539. }
  540. // 5V 12V 24V 48V
  541. void GetAuxPower()
  542. {
  543. if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
  544. {
  545. ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
  546. ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
  547. //ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
  548. //ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
  549. // aux power voltage
  550. //printf("aux1 = %x, \n", auxPower.voltage[0]);
  551. //printf("aux2 = %x, \n", auxPower.voltage[1]);
  552. }
  553. }
  554. void SetFanModuleSpeed()
  555. {
  556. // 調整風扇速度要漸進式 : 500 rpm/p
  557. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed ||
  558. ShmFanModuleData->PresentFan2Speed != ShmFanModuleData->SetFan2Speed ||
  559. ShmFanModuleData->PresentFan3Speed != ShmFanModuleData->SetFan3Speed ||
  560. ShmFanModuleData->PresentFan4Speed != ShmFanModuleData->SetFan4Speed)
  561. {
  562. FanSpeed _fanSpeed;
  563. unsigned short speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  564. if (speed >= ShmFanModuleData->SetFan1Speed)
  565. speed = ShmFanModuleData->SetFan1Speed;
  566. _fanSpeed.speed[0] = speed & 0xff;
  567. _fanSpeed.speed[1] = (speed >> 8) & 0xff;
  568. speed = ShmFanModuleData->PresentFan2Speed + fanSpeedSmoothValue;
  569. if (speed >= ShmFanModuleData->SetFan2Speed)
  570. speed = ShmFanModuleData->SetFan2Speed;
  571. _fanSpeed.speed[2] = speed & 0xff;
  572. _fanSpeed.speed[3] = (speed >> 8) & 0xff;
  573. speed = ShmFanModuleData->PresentFan3Speed + fanSpeedSmoothValue;
  574. if (speed >= ShmFanModuleData->SetFan3Speed)
  575. speed = ShmFanModuleData->SetFan3Speed;
  576. _fanSpeed.speed[4] = speed & 0xff;
  577. _fanSpeed.speed[5] = (speed >> 8) & 0xff;
  578. speed = ShmFanModuleData->PresentFan4Speed + fanSpeedSmoothValue;
  579. if (speed >= ShmFanModuleData->SetFan4Speed)
  580. speed = ShmFanModuleData->SetFan4Speed;
  581. _fanSpeed.speed[6] = speed & 0xff;
  582. _fanSpeed.speed[7] = (speed >> 8) & 0xff;
  583. if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
  584. {
  585. printf("successfully Fan\n");
  586. }
  587. }
  588. }
  589. void SetRelayModuleFanSpeed()
  590. {
  591. // 調整風扇速度要漸進式 : 100 rpm/p
  592. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed)
  593. {
  594. FanSpeed _fanSpeed;
  595. unsigned short speed = 0;
  596. if (ShmFanModuleData->SetFan1Speed > ShmFanModuleData->PresentFan1Speed)
  597. {
  598. speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  599. if (speed >= ShmFanModuleData->SetFan1Speed)
  600. speed = ShmFanModuleData->SetFan1Speed;
  601. }
  602. else
  603. {
  604. speed = ShmFanModuleData->PresentFan1Speed - fanSpeedSmoothValue;
  605. if (speed <= 0)
  606. speed = ShmFanModuleData->SetFan1Speed;
  607. }
  608. _fanSpeed.speed[0] = speed & 0xff;
  609. _fanSpeed.speed[1] = (speed >> 8) & 0xff;
  610. ShmFanModuleData->PresentFan1Speed = speed;
  611. Config_Fan_Speed(Uart5Fd, Addr.Relay, &_fanSpeed);
  612. }
  613. }
  614. void GetRelayModuleFanSpeed()
  615. {
  616. printf("Get fan board speed \n");
  617. if (Query_Fan_Speed(Uart5Fd, Addr.Relay, &fanSpeed) == PASS)
  618. {
  619. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0] + (fanSpeed.speed[1] >> 8);
  620. printf("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  621. }
  622. }
  623. //==========================================
  624. // Common Function
  625. //==========================================
  626. void SetK1K2RelayStatus(byte index)
  627. {
  628. if (_chargingData[index]->SystemStatus < S_PREPARING_FOR_EVSE)
  629. {
  630. if (_chargingData[index]->Evboard_id == 0x01)
  631. {
  632. if(regRelay.relay_event.bits.Gun1_P == YES)
  633. outputRelay.relay_event.bits.Gun1_P = NO;
  634. else if (regRelay.relay_event.bits.Gun1_N == YES)
  635. outputRelay.relay_event.bits.Gun1_N = NO;
  636. if (_chargingData[index]->Type == _Type_CCS)
  637. {
  638. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  639. outputRelay.relay_event.bits.CCS_Precharge = NO;
  640. }
  641. }
  642. else if (_chargingData[index]->Evboard_id == 0x02)
  643. {
  644. if(regRelay.relay_event.bits.Gun2_P == YES)
  645. outputRelay.relay_event.bits.Gun2_P = NO;
  646. else if (regRelay.relay_event.bits.Gun2_N == YES)
  647. outputRelay.relay_event.bits.Gun2_N = NO;
  648. if (_chargingData[index]->Type == _Type_CCS)
  649. {
  650. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  651. outputRelay.relay_event.bits.CCS_Precharge = NO;
  652. }
  653. }
  654. }
  655. else if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING))
  656. {
  657. if (_chargingData[index]->Evboard_id == 0x01)
  658. {
  659. if(regRelay.relay_event.bits.Gun1_N == NO)
  660. outputRelay.relay_event.bits.Gun1_N = YES;
  661. else if (regRelay.relay_event.bits.Gun1_P == NO)
  662. outputRelay.relay_event.bits.Gun1_P = YES;
  663. }
  664. else if (_chargingData[index]->Evboard_id == 0x02)
  665. {
  666. if(regRelay.relay_event.bits.Gun2_N == NO)
  667. outputRelay.relay_event.bits.Gun2_N = YES;
  668. else if (regRelay.relay_event.bits.Gun2_P == NO)
  669. outputRelay.relay_event.bits.Gun2_P = YES;
  670. }
  671. }
  672. else if (_chargingData[index]->SystemStatus == S_COMPLETE)
  673. {
  674. if (_chargingData[index]->PresentChargingCurrent <= SEFETY_SWITCH_RELAY_CUR)
  675. {
  676. if (_chargingData[index]->Evboard_id == 0x01)
  677. {
  678. if(regRelay.relay_event.bits.Gun1_P == YES)
  679. outputRelay.relay_event.bits.Gun1_P = NO;
  680. else if (regRelay.relay_event.bits.Gun1_N == YES)
  681. outputRelay.relay_event.bits.Gun1_N = NO;
  682. }
  683. else if (_chargingData[index]->Evboard_id == 0x02)
  684. {
  685. if(regRelay.relay_event.bits.Gun2_P == YES)
  686. outputRelay.relay_event.bits.Gun2_P = NO;
  687. else if (regRelay.relay_event.bits.Gun2_N == YES)
  688. outputRelay.relay_event.bits.Gun2_N = NO;
  689. }
  690. }
  691. }
  692. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
  693. {
  694. if (_chargingData[index]->Evboard_id == 0x01)
  695. {
  696. if (_chargingData[index]->Type == _Type_CCS)
  697. {
  698. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  699. outputRelay.relay_event.bits.CCS_Precharge = YES;
  700. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  701. outputRelay.relay_event.bits.Gun1_P = NO;
  702. }
  703. }
  704. else if (_chargingData[index]->Evboard_id == 0x02)
  705. {
  706. if (_chargingData[index]->Type == _Type_CCS)
  707. {
  708. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  709. outputRelay.relay_event.bits.CCS_Precharge = YES;
  710. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  711. outputRelay.relay_event.bits.Gun2_P = NO;
  712. }
  713. }
  714. }
  715. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST1)
  716. {
  717. if (_chargingData[index]->Evboard_id == 0x01)
  718. {
  719. if (_chargingData[index]->Type == _Type_CCS)
  720. {
  721. if (regRelay.relay_event.bits.Gun1_P == NO)
  722. outputRelay.relay_event.bits.Gun1_P = YES;
  723. else if(regRelay.relay_event.bits.Gun1_P == YES)
  724. outputRelay.relay_event.bits.CCS_Precharge = NO;
  725. }
  726. }
  727. else if (_chargingData[index]->Evboard_id == 0x02)
  728. {
  729. if (_chargingData[index]->Type == _Type_CCS)
  730. {
  731. if (regRelay.relay_event.bits.Gun2_P == NO)
  732. outputRelay.relay_event.bits.Gun2_P = YES;
  733. else if(regRelay.relay_event.bits.Gun2_P == YES)
  734. outputRelay.relay_event.bits.CCS_Precharge = NO;
  735. }
  736. }
  737. }
  738. }
  739. void SetParalleRelayStatus()
  740. {
  741. if (gunCount >= 2)
  742. {
  743. if (_chargingData[0]->SystemStatus == S_BOOTING || _chargingData[1]->SystemStatus == S_BOOTING)
  744. {
  745. // 初始化~ 不搭橋接
  746. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  747. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  748. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  749. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  750. }
  751. else
  752. {
  753. // 單槍充電中 - 搭上橋接
  754. if((_chargingData[0]->IsReadyToCharging == YES && _chargingData[1]->IsReadyToCharging == NO) ||
  755. (_chargingData[0]->IsReadyToCharging == NO && _chargingData[1]->IsReadyToCharging == YES))
  756. {
  757. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY)
  758. {
  759. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  760. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  761. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  762. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  763. }
  764. else
  765. {
  766. if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
  767. outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
  768. else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
  769. outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
  770. }
  771. }
  772. else
  773. {
  774. // 雙槍充電中~ 不搭橋接
  775. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  776. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  777. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  778. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  779. }
  780. }
  781. }
  782. }
  783. //==========================================
  784. // Init all share memory
  785. //==========================================
  786. int InitShareMemory()
  787. {
  788. int result = PASS;
  789. int MeterSMId;
  790. //creat ShmSysConfigAndInfo
  791. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  792. {
  793. #ifdef SystemLogMessage
  794. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  795. #endif
  796. result = FAIL;
  797. }
  798. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  799. {
  800. #ifdef SystemLogMessage
  801. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  802. #endif
  803. result = FAIL;
  804. }
  805. //creat ShmStatusCodeData
  806. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  807. {
  808. #ifdef SystemLogMessage
  809. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  810. #endif
  811. result = FAIL;
  812. }
  813. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  814. {
  815. #ifdef SystemLogMessage
  816. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  817. #endif
  818. result = FAIL;
  819. }
  820. //creat ShmFanModuleData
  821. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
  822. {
  823. #ifdef SystemLogMessage
  824. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  825. #endif
  826. result = FAIL;
  827. }
  828. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  829. {
  830. #ifdef SystemLogMessage
  831. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  832. #endif
  833. result = FAIL;
  834. }
  835. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  836. //creat ShmRelayModuleData
  837. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  838. {
  839. #ifdef SystemLogMessage
  840. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  841. #endif
  842. result = FAIL;
  843. }
  844. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  845. {
  846. #ifdef SystemLogMessage
  847. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  848. #endif
  849. result = FAIL;
  850. }
  851. if(CHAdeMO_QUANTITY > 0)
  852. {
  853. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  854. {
  855. #ifdef SystemLogMessage
  856. DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
  857. #endif
  858. return FAIL;
  859. }
  860. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  861. #ifdef SystemLogMessage
  862. DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
  863. #endif
  864. return FAIL;
  865. }
  866. }
  867. if(CCS_QUANTITY > 0)
  868. {
  869. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  870. {
  871. #ifdef SystemLogMessage
  872. DEBUG_ERROR("shmget ShmCcsData NG \n");
  873. #endif
  874. return FAIL;
  875. }
  876. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  877. {
  878. #ifdef SystemLogMessage
  879. DEBUG_ERROR("shmat ShmCcsData NG \n");
  880. #endif
  881. return FAIL;
  882. }
  883. }
  884. return result;
  885. }
  886. int InitComPort()
  887. {
  888. int fd;
  889. struct termios tios;
  890. fd = open(relayRs485PortName, O_RDWR);
  891. if(fd <= 0)
  892. {
  893. #ifdef SystemLogMessage
  894. DEBUG_ERROR("Module_InternalComm. InitComPort NG\n");
  895. #endif
  896. if(ShmStatusCodeData!=NULL)
  897. {
  898. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  899. }
  900. sleep(5);
  901. return -1;
  902. }
  903. ioctl (fd, TCGETS, &tios);
  904. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  905. tios.c_lflag = 0;
  906. tios.c_iflag = 0;
  907. tios.c_oflag = 0;
  908. tios.c_cc[VMIN]=0;
  909. tios.c_cc[VTIME]=(byte)0; // timeout 0.5 second
  910. tios.c_lflag=0;
  911. tcflush(fd, TCIFLUSH);
  912. ioctl (fd, TCSETS, &tios);
  913. return fd;
  914. }
  915. //================================================
  916. // Main process
  917. //================================================
  918. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  919. {
  920. for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
  921. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
  922. == target) {
  923. chargingData[target] =
  924. &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  925. return true;
  926. }
  927. }
  928. for (byte index = 0; index < CCS_QUANTITY; index++) {
  929. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
  930. == target) {
  931. chargingData[target] =
  932. &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  933. return true;
  934. }
  935. }
  936. for (byte index = 0; index < GB_QUANTITY; index++) {
  937. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
  938. == target) {
  939. chargingData[target] =
  940. &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  941. return true;
  942. }
  943. }
  944. return false;
  945. }
  946. void Initialization()
  947. {
  948. bool isPass = false;
  949. for (byte index = 0; index < ARRAY_SIZE(outputRelay.relay_event.relay_status); index++)
  950. {
  951. outputRelay.relay_event.relay_status[index] = 0x00;
  952. }
  953. while(!isPass)
  954. {
  955. isPass = true;
  956. for (byte _index = 0; _index < gunCount; _index++)
  957. {
  958. if (!FindChargingInfoData(_index, &_chargingData[0]))
  959. {
  960. DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
  961. isPass = false;
  962. break;
  963. }
  964. }
  965. }
  966. }
  967. bool IsNoneMatchRelayStatus()
  968. {
  969. bool result = false;
  970. // printf("Real Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  971. // regRelay.relay_event.bits.AC_Contactor,
  972. // regRelay.relay_event.bits.Gun1_P,
  973. // regRelay.relay_event.bits.Gun1_N,
  974. // regRelay.relay_event.bits.Gun2_P,
  975. // regRelay.relay_event.bits.Gun2_N,
  976. // regRelay.relay_event.bits.CCS_Precharge,
  977. // regRelay.relay_event.bits.Gun1_Parallel_P,
  978. // regRelay.relay_event.bits.Gun1_Parallel_N);
  979. if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
  980. (regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge) ||
  981. (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
  982. (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
  983. (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
  984. (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
  985. (regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P) ||
  986. (regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N))
  987. {
  988. result = true;
  989. }
  990. return result;
  991. }
  992. void MatchRelayStatus()
  993. {
  994. // 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
  995. //regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  996. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  997. regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  998. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  999. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  1000. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  1001. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  1002. regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  1003. regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  1004. }
  1005. void CheckRelayStatusByADC()
  1006. {
  1007. if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
  1008. (ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
  1009. {
  1010. // Relay 前後電壓一致
  1011. _chargingData[0]->RelayK1K2Status = 0x01;
  1012. }
  1013. else
  1014. _chargingData[0]->RelayK1K2Status = 0x00;
  1015. if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
  1016. (ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
  1017. {
  1018. // Relay 前後電壓一致
  1019. _chargingData[1]->RelayK1K2Status = 0x01;
  1020. }
  1021. else
  1022. _chargingData[1]->RelayK1K2Status = 0x00;
  1023. }
  1024. void SetGfdConfig(byte index, byte resister)
  1025. {
  1026. gfd_config.index = index;
  1027. gfd_config.state = resister;
  1028. //printf("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
  1029. if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
  1030. {
  1031. // printf("Set reqVol = %f, resister = %d \n",
  1032. // gfd_config.reqVol,
  1033. // gfd_config.resister);
  1034. }
  1035. }
  1036. void CableCheckDetected(byte index)
  1037. {
  1038. // Cable Check
  1039. // 當火線上的電壓 = 車端要求的電壓電流
  1040. // _chargingData[targetGun]->EvBatterytargetVoltage
  1041. // 才可以開始偵測 1s
  1042. // Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
  1043. // Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
  1044. // SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
  1045. if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING) ||
  1046. (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1047. {
  1048. if (_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE)
  1049. {
  1050. SetGfdConfig(index, GFD_CABLECHK);
  1051. }
  1052. else if (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1053. _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)
  1054. {
  1055. SetGfdConfig(index, GFD_PRECHARGE);
  1056. }
  1057. else if (_chargingData[index]->SystemStatus <= S_CHARGING)
  1058. {
  1059. SetGfdConfig(index, GFD_CHARGING);
  1060. }
  1061. }
  1062. else if(_chargingData[index]->SystemStatus == S_COMPLETE || _chargingData[index]->SystemStatus == S_PREPARNING
  1063. || _chargingData[index]->SystemStatus == S_IDLE)
  1064. {
  1065. SetGfdConfig(index, GFD_IDLE);
  1066. }
  1067. }
  1068. int main(void)
  1069. {
  1070. if(InitShareMemory() == FAIL)
  1071. {
  1072. #ifdef SystemLogMessage
  1073. DEBUG_ERROR("InitShareMemory NG\n");
  1074. #endif
  1075. if(ShmStatusCodeData!=NULL)
  1076. {
  1077. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1078. }
  1079. sleep(5);
  1080. return 0;
  1081. }
  1082. // Open Uart5 for RB
  1083. Uart5Fd = InitComPort();
  1084. Initialization();
  1085. sleep(1);
  1086. if(Uart5Fd < 0)
  1087. {
  1088. printf ("open port error. \n");
  1089. return 0;
  1090. }
  1091. outputRelay.relay_event.bits.AC_Contactor = 0x00;
  1092. //outputRelay.relay_event.bits.CCS_Precharge = 0x00;
  1093. //outputRelay.relay_event.bits.Gun1_Parallel_P = 0x01;
  1094. //outputRelay.relay_event.bits.Gun1_Parallel_N = 0x01;
  1095. //outputRelay.relay_event.bits.Gun1_N = 0x01;
  1096. //outputRelay.relay_event.bits.Gun2_N = 0x01;
  1097. //outputRelay.relay_event.bits.Gun2_P = 0x01;
  1098. Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay);
  1099. gettimeofday(&_priority_time, NULL);
  1100. bool printRelayStatus = false;
  1101. for(;;)
  1102. {
  1103. bool isCharging = false;
  1104. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  1105. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1106. {
  1107. GetFwAndHwVersion_Relay();
  1108. sleep(1);
  1109. }
  1110. if (ShmFanModuleData->SelfTest_Comp == NO)
  1111. {
  1112. //GetFwAndHwVersion_Fan();
  1113. sleep(1);
  1114. }
  1115. if (ShmRelayModuleData->SelfTest_Comp == YES)
  1116. {
  1117. // ==============優先權最高 10 ms ==============
  1118. // 輸出電壓
  1119. GetPersentOutputVol();
  1120. // 三相輸入電壓
  1121. GetPresentInputVol();
  1122. // 讀取當前 relay 狀態
  1123. GetRelayOutputStatus();
  1124. for (int i = 0; i < gunCount; i++)
  1125. {
  1126. // Cable check (Set)
  1127. CableCheckDetected(i);
  1128. // check k1 k2 relay 狀態
  1129. CheckK1K2RelayOutput(i);
  1130. // 依據當前各槍的狀態選擇 搭上/放開 Relay
  1131. SetK1K2RelayStatus(i);
  1132. if (_chargingData[i]->SystemStatus == S_IDLE)
  1133. gfdChkFailCount[i] = 0;
  1134. if (_chargingData[i]->SystemStatus == S_BOOTING ||
  1135. (_chargingData[i]->SystemStatus >= S_PREPARNING && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
  1136. (_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1137. {
  1138. _chargingData[i]->IsReadyToCharging = YES;
  1139. isCharging = true;
  1140. }
  1141. else
  1142. _chargingData[i]->IsReadyToCharging = NO;
  1143. }
  1144. // Cable check (Get)
  1145. GetGfdAdc();
  1146. SetParalleRelayStatus();
  1147. // 搭上 AC Contactor
  1148. if (isCharging)
  1149. outputRelay.relay_event.bits.AC_Contactor = YES;
  1150. else
  1151. outputRelay.relay_event.bits.AC_Contactor = NO;
  1152. // 搭上/鬆開 Relay
  1153. // 放開 Relay 之前要先確認輸出的電壓電流是否已經降到某個值
  1154. if(IsNoneMatchRelayStatus())
  1155. {
  1156. printRelayStatus = false;
  1157. if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
  1158. {}
  1159. }
  1160. else
  1161. {
  1162. if (!printRelayStatus)
  1163. {
  1164. printf("Match Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  1165. regRelay.relay_event.bits.AC_Contactor,
  1166. regRelay.relay_event.bits.Gun1_P,
  1167. regRelay.relay_event.bits.Gun1_N,
  1168. regRelay.relay_event.bits.Gun2_P,
  1169. regRelay.relay_event.bits.Gun2_N,
  1170. regRelay.relay_event.bits.CCS_Precharge,
  1171. regRelay.relay_event.bits.Gun1_Parallel_P,
  1172. regRelay.relay_event.bits.Gun1_Parallel_N);
  1173. }
  1174. printRelayStatus = true;
  1175. }
  1176. }
  1177. if (ShmFanModuleData->SelfTest_Comp == YES)
  1178. {
  1179. //GetFanSpeed();
  1180. if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
  1181. {
  1182. gettimeofday(&_priority_time, NULL);
  1183. if (isCharging)
  1184. {
  1185. if (ShmFanModuleData->PresentFan1Speed < MAX_FAN_SPEED ||
  1186. ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
  1187. ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
  1188. ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
  1189. {
  1190. ShmFanModuleData->SetFan1Speed = MAX_FAN_SPEED;
  1191. ShmFanModuleData->SetFan2Speed = MAX_FAN_SPEED;
  1192. ShmFanModuleData->SetFan3Speed = MAX_FAN_SPEED;
  1193. ShmFanModuleData->SetFan4Speed = MAX_FAN_SPEED;
  1194. }
  1195. }
  1196. else
  1197. {
  1198. if (ShmFanModuleData->PresentFan1Speed > MIN_FAN_SPEED ||
  1199. ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
  1200. ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
  1201. ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
  1202. {
  1203. ShmFanModuleData->SetFan1Speed = MIN_FAN_SPEED;
  1204. ShmFanModuleData->SetFan2Speed = MIN_FAN_SPEED;
  1205. ShmFanModuleData->SetFan3Speed = MIN_FAN_SPEED;
  1206. ShmFanModuleData->SetFan4Speed = MIN_FAN_SPEED;
  1207. }
  1208. }
  1209. //SetFanModuleSpeed();
  1210. }
  1211. }
  1212. usleep(10000);
  1213. }
  1214. return FAIL;
  1215. }