Module_InternalComm.c 41 KB

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