Module_InternalComm.c 38 KB

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