Module_InternalComm.c 59 KB

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