Module_InternalComm.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  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. #include "Config.h"
  33. #include "Common.h"
  34. #define TEN_MINUTES 600
  35. #define ENV_TEMP_MIN 45
  36. #define ENV_TEMP_MAX 50
  37. #define DEFAULT_AC_INDEX 2
  38. #define COLOR_MAX_LV 100
  39. #define COLOR_MIN_LV 0
  40. #define AC_DEFAULT_VOL 220
  41. #define NO_DEFINE 255
  42. #define NDEFAULT_AC_INDEX 2
  43. #define OVP_UVP_CHK_COUNT 5
  44. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  45. struct StatusCodeData *ShmStatusCodeData;
  46. struct FanModuleData *ShmFanModuleData;
  47. struct RelayModuleData *ShmRelayModuleData[2];
  48. struct LedModuleData *ShmLedModuleData;
  49. struct PsuData *ShmPsuData;
  50. struct OCPP16Data *ShmOCPP16Data;
  51. ChargerInfoData *ShmChargerInfo;
  52. PsuGroupingInfoData *ShmPsuGrouping;
  53. PsuGroupOutputRelay *ShmOutputRelayConfig[MAX_GROUP_QUANTITY];
  54. PsuGroupOutputRelay *ShmOutputRelayConfirmed[MAX_GROUP_QUANTITY];
  55. PsuGroupParallelRelay *ShmParallelRelayConfig;
  56. PsuGroupParallelRelay *ShmParallelRelayConfirmed;
  57. OutputRelayControl *LocationOutputRelayCtrl[MAX_GROUP_QUANTITY];
  58. OutputRelayControl *LocationOutputRelayResponse[MAX_GROUP_QUANTITY];
  59. unsigned char LocationParallelRelayCtrl[PARALLEL_RELAY_COUNT];
  60. unsigned char LocationParallelRelayResponse[PARALLEL_RELAY_COUNT];
  61. Connector_GFD *LocaltionGfd[MAX_GROUP_QUANTITY];
  62. #define VIN_MAX_VOLTAGE_IEC 285 // 大於該值 : OVP
  63. #define VIN_MAX_REV_VOLTAGE_IEC 275 // 小於賦歸 OVP
  64. #define VIN_MIN_VOLTAGE_IEC 160 // 小於該值 : UVP
  65. #define VIN_MIN_REV_VOLTAGE_IEC 170 // 大於賦歸 UVP
  66. #define VIN_MAX_VOLTAGE_UL 315 // 大於該值 : OVP // 美規 (W)
  67. #define VIN_MAX_REV_VOLTAGE_UL 305 // 小於賦歸 OVP
  68. #define VIN_MIN_VOLTAGE_UL 210 // 小於該值 : UVP
  69. #define VIN_MIN_REV_VOLTAGE_UL 220 // 大於賦歸 UVP
  70. #define DCIN_OVP_THRESHOLD_VOL 825 // dc input ovp threshold voltage
  71. #define DCIN_OVP_RECOVERY_VOL 815 // dc input ovp recovery voltage
  72. #define DCIN_UVP_THRESHOLD_VOL 400 // dc input uvp threshold voltage
  73. #define DCIN_UVP_RECOVERY_VOL 410 // dc input uvp recovery voltage
  74. #define VIN_DROP_VOLTAGE 150 // 小於該值 : ac drop
  75. #define VOUT_MAX_VOLTAGE 995
  76. #define VOUT_MIN_VOLTAGE 150
  77. #define IOUT_MAX_CURRENT 50
  78. #define MAX_FAN_SPEED 14000
  79. #define MIN_FAN_SPEED 3000
  80. #define NORMAL_FAN_SPEED 7000
  81. // GFD Status
  82. #define GFD_IDLE 0
  83. #define GFD_CABLECHK 1
  84. #define GFD_PRECHARGE 2
  85. #define GFD_CHARGING 3
  86. // LED Intensity (rate)
  87. #define LED_INTENSITY_DARKEST 0.2
  88. #define LED_INTENSITY_MEDIUM 0.6
  89. #define LED_INTENSITY_BRIGHTEST 1
  90. // EE Spec
  91. #define LED_BRIGHTNESS_LV_HIGH 1
  92. #define LED_BRIGHTNESS_LV_MID 0.5
  93. #define LED_BRIGHTNESS_LV_LOW 0.2
  94. // 最小切換 Relay 電壓
  95. #define SELF_TO_CHANGE_RELAY_STATUS 600
  96. // 透過電壓確認 Relay 是否搭上的依據電壓
  97. #define CHECK_RELAY_STATUS 300
  98. #define CHECK_RELAY_STATUS_GAP 100
  99. // 安全在停止充電程序中斷開 Relay 的電流
  100. #define SEFETY_SWITCH_RELAY_CUR 50
  101. // 確認 Relay Welding 電壓
  102. #define RELAY_WELDING_DET 300
  103. #if SAFETY_TEST_ENABLE
  104. #define RELAY_OPEN_AT_PRECHARGE 1
  105. #else
  106. #define RELAY_OPEN_AT_PRECHARGE 0
  107. #endif
  108. #define RELAY_DEBUG_MSG 0
  109. byte gunCount;
  110. byte acgunCount;
  111. // 槍資訊
  112. struct ChargingInfoData *_chargingData[CONNECTOR_QUANTITY];
  113. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  114. bool _isOutputNoneMatch[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  115. struct timespec _checkOutputNoneMatchTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  116. bool _isRelayWelding[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  117. struct timespec _checkRelayWeldingTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  118. byte _dcOvpCnt = 0;
  119. byte _dcUvpCnt = 0;
  120. byte _threePhaseOvp[3] = {0, 0, 0};
  121. byte _threePhaseUvp[3] = {0, 0, 0};
  122. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
  123. int Uart5Fd;
  124. char *relayRs485PortName = "/dev/ttyS5";
  125. unsigned short fanSpeedSmoothValue = 500;
  126. struct timespec _priority_time;
  127. struct timespec _led_priority_time;
  128. unsigned short _setFanSpeed = 0;
  129. float _beforeChargingTotalEnergy = 0.0;
  130. byte _checkLedChanged = 3;
  131. byte _RelaySelfTestOK;
  132. bool _isGfdEnable = false;
  133. Ver ver;
  134. PresentInputVoltage inputVoltage;
  135. DCInputVoltage dcInputVoltage;
  136. PresentOutputVoltage outputVoltage;
  137. FanSpeed fanSpeed;
  138. Temperature temperature;
  139. AuxPower auxPower;
  140. Gfd gfd_adc[2];
  141. Gfd_config gfd_config;
  142. Gpio_in gpio_in;
  143. Gpio_out gpio_out;
  144. Relay outputRelay[2];
  145. Relay regRelay[2];
  146. Relay TempRegRelay[2];
  147. Rtc rtc;
  148. Led_Color cur_led_color;
  149. Led_Color led_color;
  150. #define AC_OVP 1
  151. #define AC_UVP 2
  152. #define AC_OCP 4
  153. #define AC_OTP 8
  154. #define AC_GMI_FAULT 16
  155. #define AC_CP_ERROR 32
  156. #define AC_AC_LEAKAGE 64
  157. #define AC_DC_LEAKAGE 128
  158. #define AC_SYSTEM_SELFTEST_FAULT 256
  159. #define AC_HANDSHAKE_TIMEOUT 512
  160. #define AC_EMC_STOP 1024
  161. #define AC_RELAY_WELDING 2048
  162. #define AC_GF_MODULE_FAULT 4096
  163. #define AC_SHUTTER_FAULT 8192
  164. #define AC_LOCKER_FAULT 16384
  165. #define AC_POWER_DROP 32768
  166. #define AC_CIRCUIT_SHORT 65536
  167. #define AC_ROTARY_SWITCH_FAULT 131072
  168. #define AC_RELAY_DRIVE_FAULT 262144
  169. int _alarm_code[] = {AC_OVP, AC_UVP, AC_OCP, AC_OTP, AC_GMI_FAULT, AC_CP_ERROR, AC_AC_LEAKAGE
  170. , AC_DC_LEAKAGE, AC_SYSTEM_SELFTEST_FAULT, AC_HANDSHAKE_TIMEOUT, AC_EMC_STOP, AC_RELAY_WELDING
  171. , AC_GF_MODULE_FAULT, AC_SHUTTER_FAULT, AC_LOCKER_FAULT, AC_POWER_DROP, AC_CIRCUIT_SHORT
  172. , AC_ROTARY_SWITCH_FAULT, AC_RELAY_DRIVE_FAULT};
  173. int DiffTimeb(struct timeb ST, struct timeb ET)
  174. {
  175. //return milli-second
  176. unsigned int StartTime, StopTime;
  177. StartTime = (unsigned int) ST.time;
  178. StopTime = (unsigned int) ET.time;
  179. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  180. return (StopTime - StartTime);
  181. }
  182. unsigned short MaxValue(unsigned short value1, unsigned short value2)
  183. {
  184. return value1 >= value2 ? value1 : value2;
  185. }
  186. //==========================================
  187. // Communication Function
  188. //==========================================
  189. void GetFwAndHwVersion_Fan()
  190. {
  191. if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  192. {
  193. // FanModuleData
  194. strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
  195. // SystemInfo
  196. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
  197. LOG_INFO("GetFwAndHwVersion_Fan s1 = %s", ver.Version_FW);
  198. }
  199. if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  200. {
  201. // SystemInfo
  202. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
  203. LOG_INFO("GetFwAndHwVersion_Fan s2 = %s", ver.Version_HW);
  204. }
  205. }
  206. void GetFwAndHwVersion_Relay()
  207. {
  208. if (Query_FW_Ver(Uart5Fd, Addr.DO360_RC1, &ver) == PASS)
  209. {
  210. // RelayModuleData
  211. strcpy((char *) ShmRelayModuleData[0]->version, ver.Version_FW);
  212. // SystemInfo
  213. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
  214. LOG_INFO("GetFwAndHwVersion_RC1 s1 = %s", ver.Version_FW);
  215. }
  216. if (Query_HW_Ver(Uart5Fd, Addr.DO360_RC1, &ver) == PASS)
  217. {
  218. // SystemInfo
  219. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  220. LOG_INFO("GetFwAndHwVersion_RC1 s2 = %s", ver.Version_HW);
  221. }
  222. }
  223. void GetFwAndHwVersion_Relay2()
  224. {
  225. // DO360 RC2
  226. if (Query_FW_Ver(Uart5Fd, Addr.DO360_RC2, &ver) == PASS)
  227. {
  228. // RelayModuleData
  229. strcpy((char *) ShmRelayModuleData[1]->version, ver.Version_FW);
  230. // SystemInfo
  231. strcpy((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, ver.Version_FW);
  232. LOG_INFO("GetFwAndHwVersion_RC2 s1 = %s", ver.Version_FW);
  233. }
  234. if (Query_HW_Ver(Uart5Fd, Addr.DO360_RC2, &ver) == PASS)
  235. {
  236. // SystemInfo
  237. strcpy((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, ver.Version_FW);
  238. LOG_INFO("GetFwAndHwVersion_RC2 s2 = %s", ver.Version_HW);
  239. }
  240. }
  241. void GetFwAndHwVersion_Led()
  242. {
  243. if (Query_FW_Ver(Uart5Fd, Addr.Led, &ver) == PASS)
  244. {
  245. // LedModuleData
  246. strcpy((char *) ShmLedModuleData->version, ver.Version_FW);
  247. // SystemInfo
  248. strcpy((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, ver.Version_FW);
  249. LOG_INFO("GetFwAndHwVersion_Led s1 = %s", ver.Version_FW);
  250. ShmLedModuleData->SelfTest_Comp = YES;
  251. }
  252. else
  253. {
  254. //LOG_INFO("GetFwAndHwVersion_Led fail");
  255. }
  256. // if (Query_HW_Ver(Uart5Fd, Addr.Led, &ver) == PASS)
  257. // {
  258. // // SystemInfo
  259. // strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  260. // //LOG_INFO("GetFwAndHwVersion_Relay s2 = %s", ver.Version_HW);
  261. // }
  262. }
  263. void GetFwVersion_AC()
  264. {
  265. if (Query_FW_Ver(Uart5Fd, Addr.AcPlug, &ver) == PASS)
  266. {
  267. ac_chargingInfo[0]->SelfTest_Comp = YES;
  268. strcpy((char *) ac_chargingInfo[0]->version, ver.Version_FW);
  269. }
  270. }
  271. void GetAcModelName()
  272. {
  273. memset(ShmSysConfigAndInfo->SysConfig.AcModelName, 0x00, sizeof(ShmSysConfigAndInfo->SysConfig.AcModelName));
  274. if (Query_Model_Name(Uart5Fd, Addr.AcPlug, ShmSysConfigAndInfo->SysConfig.AcModelName) == PASS)
  275. {
  276. LOG_INFO("ac model name = %s", ShmSysConfigAndInfo->SysConfig.AcModelName);
  277. }
  278. }
  279. void SetRtcData_Relay(unsigned char index)
  280. {
  281. struct timeb csuTime;
  282. struct tm *tmCSU;
  283. ftime(&csuTime);
  284. tmCSU = localtime(&csuTime.time);
  285. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  286. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  287. // tmCSU->tm_sec);
  288. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  289. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  290. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  291. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  292. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  293. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  294. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  295. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  296. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  297. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  298. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  299. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  300. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  301. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  302. if(index == 0)
  303. {
  304. if (Config_Rtc_Data(Uart5Fd, Addr.DO360_RC1, &rtc) == PASS)
  305. {
  306. //LOG_INFO("SetRtc (RB) sucessfully.");
  307. }
  308. }
  309. else
  310. {
  311. if (Config_Rtc_Data(Uart5Fd, Addr.DO360_RC2, &rtc) == PASS)
  312. {
  313. //LOG_INFO("SetRtc (RB) sucessfully.");
  314. }
  315. }
  316. }
  317. void SetRtcData_Fan()
  318. {
  319. struct timeb csuTime;
  320. struct tm *tmCSU;
  321. ftime(&csuTime);
  322. tmCSU = localtime(&csuTime.time);
  323. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  324. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  325. // tmCSU->tm_sec);
  326. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  327. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  328. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  329. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  330. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  331. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  332. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  333. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  334. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  335. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  336. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  337. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  338. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  339. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  340. if (Config_Rtc_Data(Uart5Fd, Addr.Fan, &rtc) == PASS)
  341. {
  342. //LOG_INFO("SetRtc (FB) sucessfully.");
  343. }
  344. }
  345. void SetModelName_Fan()
  346. {
  347. if (Config_Model_Name(Uart5Fd, Addr.Fan, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  348. {
  349. LOG_INFO("Set Model name PASS = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  350. }
  351. }
  352. // AC 三相輸入電壓
  353. void GetPresentInputVol()
  354. {
  355. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == YES)
  356. {
  357. if(Query_DC_InputVoltage(Uart5Fd, Addr.DO360_RC1, &dcInputVoltage) == PASS)
  358. {
  359. ShmSysConfigAndInfo->SysInfo.InputVoltageDc = dcInputVoltage.DC_Input_1;
  360. // DC Input OVP
  361. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == NO)
  362. {
  363. if(dcInputVoltage.DC_Input_1 > DCIN_OVP_THRESHOLD_VOL)
  364. {
  365. _dcOvpCnt++;
  366. if(_dcOvpCnt >= OVP_UVP_CHK_COUNT)
  367. {
  368. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP = YES;
  369. LOG_INFO("Dc Input OVP: %.1f V", dcInputVoltage.DC_Input_1);
  370. }
  371. }
  372. else
  373. {
  374. _dcOvpCnt = 0;
  375. }
  376. }
  377. else
  378. {
  379. if(dcInputVoltage.DC_Input_1 <= DCIN_OVP_RECOVERY_VOL)
  380. {
  381. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP = NO;
  382. LOG_INFO("Dc Input OVP Recovery: %.1f V", dcInputVoltage.DC_Input_1);
  383. }
  384. _dcOvpCnt = 0;
  385. }
  386. // DC Input UVP
  387. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == NO)
  388. {
  389. if(dcInputVoltage.DC_Input_1 < DCIN_UVP_THRESHOLD_VOL)
  390. {
  391. _dcUvpCnt++;
  392. if(_dcUvpCnt >= OVP_UVP_CHK_COUNT)
  393. {
  394. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP = YES;
  395. LOG_INFO("Dc Input UVP: %.1f V", dcInputVoltage.DC_Input_1);
  396. }
  397. }
  398. else
  399. {
  400. _dcUvpCnt = 0;
  401. }
  402. }
  403. else
  404. {
  405. if(dcInputVoltage.DC_Input_1 >= DCIN_UVP_RECOVERY_VOL)
  406. {
  407. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP = NO;
  408. LOG_INFO("Dc Input UVP Recovery: %.1f V", dcInputVoltage.DC_Input_1);
  409. }
  410. _dcUvpCnt = 0;
  411. }
  412. }
  413. }
  414. else
  415. {
  416. if (Query_Present_InputVoltage(Uart5Fd, Addr.DO360_RC1, &inputVoltage) == PASS)
  417. {
  418. // resolution : 0.1
  419. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmRelayModuleData[0]->InputL1Volt = inputVoltage.L1N_L12;
  420. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmRelayModuleData[0]->InputL2Volt = inputVoltage.L2N_L23;
  421. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmRelayModuleData[0]->InputL3Volt = inputVoltage.L3N_L31;
  422. //********************************************************************************************************//
  423. // Vin (UVP)
  424. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  425. {
  426. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  427. {
  428. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_IEC)
  429. {
  430. LOG_INFO("In Uvp L1N_L12 = %f", inputVoltage.L1N_L12);
  431. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  432. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  433. else
  434. _threePhaseUvp[0] += 1;
  435. }
  436. }
  437. else
  438. {
  439. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_IEC)
  440. {
  441. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  442. _threePhaseUvp[0] = 0;
  443. }
  444. }
  445. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  446. {
  447. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_IEC)
  448. {
  449. LOG_INFO("In Uvp L2N_L23 = %f", inputVoltage.L2N_L23);
  450. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  451. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  452. else
  453. _threePhaseUvp[1] += 1;
  454. }
  455. }
  456. else
  457. {
  458. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_IEC)
  459. {
  460. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  461. _threePhaseUvp[1] = 0;
  462. }
  463. }
  464. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  465. {
  466. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_IEC)
  467. {
  468. LOG_INFO("In Uvp L3N_L31 = %f", inputVoltage.L3N_L31);
  469. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  470. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  471. else
  472. _threePhaseUvp[2] += 1;
  473. }
  474. }
  475. else
  476. {
  477. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_IEC)
  478. {
  479. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  480. _threePhaseUvp[2] = 0;
  481. }
  482. }
  483. }
  484. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_UL)
  485. {
  486. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  487. {
  488. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_UL)
  489. {
  490. LOG_INFO("In Uvp L1N_L12 = %f", inputVoltage.L1N_L12);
  491. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  492. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  493. else
  494. _threePhaseUvp[0] += 1;
  495. }
  496. }
  497. else
  498. {
  499. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_UL)
  500. {
  501. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  502. _threePhaseUvp[0] = 0;
  503. }
  504. }
  505. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  506. {
  507. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_UL)
  508. {
  509. LOG_INFO("In Uvp L2N_L23 = %f", inputVoltage.L2N_L23);
  510. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  511. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  512. else
  513. _threePhaseUvp[1] += 1;
  514. }
  515. }
  516. else
  517. {
  518. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_UL)
  519. {
  520. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  521. _threePhaseUvp[1] = 0;
  522. }
  523. }
  524. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  525. {
  526. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_UL)
  527. {
  528. LOG_INFO("In Uvp L3N_L31 = %f", inputVoltage.L3N_L31);
  529. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  530. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  531. else
  532. _threePhaseUvp[2] += 1;
  533. }
  534. }
  535. else
  536. {
  537. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_UL)
  538. {
  539. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  540. _threePhaseUvp[2] = 0;
  541. }
  542. }
  543. }
  544. //********************************************************************************************************//
  545. // Vin (OVP)
  546. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  547. {
  548. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  549. {
  550. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_IEC)
  551. {
  552. LOG_INFO("In Ovp L1N_L12 = %f", inputVoltage.L1N_L12);
  553. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  554. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  555. else
  556. _threePhaseOvp[0] += 1;
  557. }
  558. }
  559. else
  560. {
  561. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_IEC)
  562. {
  563. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  564. _threePhaseOvp[0] = 0;
  565. }
  566. }
  567. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  568. {
  569. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_IEC)
  570. {
  571. LOG_INFO("In Ovp L2N_L23 = %f", inputVoltage.L2N_L23);
  572. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  573. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  574. else
  575. _threePhaseOvp[1] += 1;
  576. }
  577. }
  578. else
  579. {
  580. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_IEC)
  581. {
  582. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  583. _threePhaseOvp[1] = 0;
  584. }
  585. }
  586. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  587. {
  588. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_IEC)
  589. {
  590. LOG_INFO("In Ovp L3N_L31 = %f", inputVoltage.L3N_L31);
  591. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  592. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  593. else
  594. _threePhaseOvp[2] += 1;
  595. }
  596. }
  597. else
  598. {
  599. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_IEC)
  600. {
  601. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  602. _threePhaseOvp[2] = 0;
  603. }
  604. }
  605. }
  606. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_UL)
  607. {
  608. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  609. {
  610. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_UL)
  611. {
  612. LOG_INFO("In Ovp L1N_L12 = %f", inputVoltage.L1N_L12);
  613. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  614. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  615. else
  616. _threePhaseOvp[0] += 0;
  617. }
  618. }
  619. else
  620. {
  621. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_UL)
  622. {
  623. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  624. _threePhaseOvp[0] = 0;
  625. }
  626. }
  627. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  628. {
  629. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_UL)
  630. {
  631. LOG_INFO("In Ovp L2N_L23 = %f", inputVoltage.L2N_L23);
  632. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  633. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  634. else
  635. _threePhaseOvp[1] += 0;
  636. }
  637. }
  638. else
  639. {
  640. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_UL)
  641. {
  642. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  643. _threePhaseOvp[1] = 0;
  644. }
  645. }
  646. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  647. {
  648. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_UL)
  649. {
  650. LOG_INFO("In Ovp L3N_L31 = %f", inputVoltage.L3N_L31);
  651. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  652. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  653. else
  654. _threePhaseOvp[2] += 1;
  655. }
  656. }
  657. else
  658. {
  659. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_UL)
  660. {
  661. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  662. _threePhaseOvp[2] = 0;
  663. }
  664. }
  665. }
  666. }
  667. }
  668. }
  669. // 左右槍的 Relay 前後的輸出電壓
  670. void GetPersentOutputVol()
  671. {
  672. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  673. {
  674. // two relay board
  675. if (Query_Present_OutputVoltage(Uart5Fd, Addr.DO360_RC1, &outputVoltage) == PASS)
  676. {
  677. ShmRelayModuleData[0]->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  678. ShmRelayModuleData[0]->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  679. ShmRelayModuleData[0]->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  680. ShmRelayModuleData[0]->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  681. _chargingData[0]->FuseChargingVoltage = ShmRelayModuleData[0]->Gun1FuseOutputVolt;
  682. _chargingData[0]->FireChargingVoltage = ShmRelayModuleData[0]->Gun1RelayOutputVolt;
  683. _chargingData[3]->FuseChargingVoltage = ShmRelayModuleData[0]->Gun2FuseOutputVolt;
  684. _chargingData[3]->FireChargingVoltage = ShmRelayModuleData[0]->Gun2RelayOutputVolt;
  685. }
  686. // DO360 RC2
  687. if (Query_Present_OutputVoltage(Uart5Fd, Addr.DO360_RC2, &outputVoltage) == PASS)
  688. {
  689. ShmRelayModuleData[1]->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  690. ShmRelayModuleData[1]->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  691. ShmRelayModuleData[1]->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  692. ShmRelayModuleData[1]->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  693. _chargingData[1]->FuseChargingVoltage = ShmRelayModuleData[1]->Gun2FuseOutputVolt;
  694. _chargingData[1]->FireChargingVoltage = ShmRelayModuleData[1]->Gun2RelayOutputVolt;
  695. _chargingData[2]->FuseChargingVoltage = ShmRelayModuleData[1]->Gun1FuseOutputVolt;
  696. _chargingData[2]->FireChargingVoltage = ShmRelayModuleData[1]->Gun1RelayOutputVolt;
  697. }
  698. }
  699. else
  700. {
  701. // only one relay board
  702. if (Query_Present_OutputVoltage(Uart5Fd, Addr.DO360_RC1, &outputVoltage) == PASS)
  703. {
  704. ShmRelayModuleData[0]->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  705. ShmRelayModuleData[0]->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  706. ShmRelayModuleData[0]->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  707. ShmRelayModuleData[0]->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  708. _chargingData[0]->FuseChargingVoltage = ShmRelayModuleData[0]->Gun1FuseOutputVolt;
  709. _chargingData[0]->FireChargingVoltage = ShmRelayModuleData[0]->Gun1RelayOutputVolt;
  710. _chargingData[1]->FuseChargingVoltage = ShmRelayModuleData[0]->Gun2FuseOutputVolt;
  711. _chargingData[1]->FireChargingVoltage = ShmRelayModuleData[0]->Gun2RelayOutputVolt;
  712. }
  713. }
  714. }
  715. // 風扇速度
  716. void GetFanSpeed()
  717. {
  718. //LOG_INFO("Get fan board speed");
  719. if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
  720. {
  721. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  722. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  723. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  724. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  725. // LOG_INFO("SystemFanRotaSpeed_1 = %d", fanSpeed.speed[0]);
  726. // LOG_INFO("SystemFanRotaSpeed_2 = %d", fanSpeed.speed[1]);
  727. // LOG_INFO("SystemFanRotaSpeed_3 = %d", fanSpeed.speed[2]);
  728. // LOG_INFO("SystemFanRotaSpeed_4 = %d", fanSpeed.speed[3]);
  729. // Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
  730. //SysInfoData (SystemFanRotaSpeed)
  731. }
  732. }
  733. // 讀取 Relay 狀態
  734. void GetRelayOutputStatus(void)
  735. {
  736. unsigned char location = 0;
  737. if(Query_Relay_Output(Uart5Fd, Addr.DO360_RC1, &regRelay[0]) == PASS)
  738. {
  739. regRelay[0].relay_event.bits.AC_Contactor = outputRelay[0].relay_event.bits.AC_Contactor;
  740. }
  741. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  742. {
  743. if(Query_Relay_Output(Uart5Fd, Addr.DO360_RC2, &regRelay[1]) == PASS)
  744. {
  745. regRelay[1].relay_event.bits.AC_Contactor = outputRelay[1].relay_event.bits.AC_Contactor;
  746. }
  747. }
  748. // update output relay feedback status
  749. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  750. {
  751. location = ShmPsuGrouping->GroupCollection[i].Location;
  752. if(ShmOutputRelayConfirmed[i]->bits.Output_N != LocationOutputRelayResponse[location]->bits.Gun_N)
  753. {
  754. LOG_INFO("Gun %d Get K1K2 N %s at Location %d",
  755. i + 1, LocationOutputRelayResponse[location]->bits.Gun_N ? "On" : "Off", location + 1);
  756. }
  757. ShmOutputRelayConfirmed[i]->bits.Output_N = LocationOutputRelayResponse[location]->bits.Gun_N;
  758. if(ShmOutputRelayConfirmed[i]->bits.Output_P != LocationOutputRelayResponse[location]->bits.Gun_P)
  759. {
  760. LOG_INFO("Gun %d Get K1K2 P %s at Location %d",
  761. i + 1, LocationOutputRelayResponse[location]->bits.Gun_P ? "On" : "Off", location + 1);
  762. }
  763. ShmOutputRelayConfirmed[i]->bits.Output_P = LocationOutputRelayResponse[location]->bits.Gun_P;
  764. }
  765. // update parallel relay feedback status
  766. int parallelCnt = ShmChargerInfo->Control.MaxConnector == GENERAL_GUN_QUANTITY ? PARALLEL_RELAY_COUNT : 1;
  767. for(int i = 0; i < parallelCnt; i++)
  768. {
  769. bool relayOnOff = 0;
  770. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  771. {
  772. switch(i)
  773. {
  774. case 0:
  775. LocationParallelRelayResponse[i] = regRelay[0].relay_event.bits.Gun1_Parallel_N;
  776. break;
  777. case 1:
  778. LocationParallelRelayResponse[i] = regRelay[0].relay_event.bits.Gun2_Parallel_N;
  779. break;
  780. case 2:
  781. LocationParallelRelayResponse[i] = regRelay[1].relay_event.bits.Gun1_Parallel_N;
  782. break;
  783. case 3:
  784. LocationParallelRelayResponse[i] = regRelay[1].relay_event.bits.Gun2_Parallel_N;
  785. break;
  786. case 4:
  787. LocationParallelRelayResponse[i] = regRelay[0].relay_event.bits.CCS_Precharge;
  788. break;
  789. case 5:
  790. LocationParallelRelayResponse[i] = regRelay[1].relay_event.bits.CCS_Precharge;
  791. break;
  792. }
  793. relayOnOff = LocationParallelRelayResponse[i];
  794. }
  795. else
  796. {
  797. bool original = ShmParallelRelayConfig->CtrlValue & (1 << i) ? false : true;
  798. relayOnOff = regRelay[0].relay_event.bits.Gun1_Parallel_N == regRelay[0].relay_event.bits.Gun2_Parallel_N ?
  799. regRelay[0].relay_event.bits.Gun1_Parallel_N : original;
  800. #if RELAY_DEBUG_MSG
  801. if(regRelay[0].relay_event.bits.Gun1_Parallel_N != regRelay[0].relay_event.bits.Gun2_Parallel_N)
  802. {
  803. LOG_INFO("Parallel Relay N & P at Location %d is Not Match: %d, %d",
  804. i + 1, regRelay[0].relay_event.bits.Gun1_Parallel_N, regRelay[0].relay_event.bits.Gun2_Parallel_N);
  805. }
  806. #endif
  807. }
  808. if((ShmParallelRelayConfirmed->CtrlValue & (1 << i)) != (relayOnOff << i))
  809. {
  810. LOG_INFO("Get Parallel Relay N & P %s at Location %d", relayOnOff ? "On" : "Off", i + 1);
  811. }
  812. if(relayOnOff)
  813. {
  814. ShmParallelRelayConfirmed->CtrlValue |= 1 << i;
  815. }
  816. else
  817. {
  818. ShmParallelRelayConfirmed->CtrlValue &= ~(1 << i);
  819. }
  820. }
  821. }
  822. // 確認 K1 K2 relay 的狀態
  823. void CheckK1K2RelayOutput(byte index)
  824. {
  825. unsigned char location = 0;
  826. if(index < MAX_GROUP_QUANTITY && index < ShmChargerInfo->Control.MaxConnector)
  827. {
  828. location = ShmPsuGrouping->GroupCollection[index].Location;
  829. if(LocationOutputRelayCtrl[location]->bits.Gun_N == LocationOutputRelayCtrl[location]->bits.Gun_P)
  830. {
  831. _chargingData[index]->RelayK1K2Status = LocationOutputRelayCtrl[location]->bits.Gun_N ? YES : NO;
  832. }
  833. else
  834. {
  835. _chargingData[index]->RelayK1K2Status = NO;
  836. }
  837. }
  838. }
  839. void GetGfdAdc(void)
  840. {
  841. unsigned char location = 0, result = 0;
  842. char *str_gfd[] = {"Idle", "Pass", "Fail", "Warning"};
  843. if(!_isGfdEnable)
  844. {
  845. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  846. {
  847. if(_chargingData[i]->PantographFlag == YES)
  848. {
  849. _isGfdEnable = true;
  850. LOG_INFO("Enable Power Cabinet GFD Function");
  851. }
  852. }
  853. }
  854. if(_isGfdEnable)
  855. {
  856. // define : 每 0.2 ~ 1 秒一次
  857. // occur : <= 75k 歐姆 @ 150 - 750 Vdc
  858. // warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
  859. if(Query_Gfd_Adc(Uart5Fd, Addr.DO360_RC1, &gfd_adc[0]) == PASS)
  860. {
  861. // if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].Parameter.bits.GfdDetection ||
  862. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].Parameter.bits.GfdDetection)
  863. // {
  864. // LOG_INFO("Query Relay1 GFD ADC1 Status = %d, ADC2 Status = %d", gfd_adc[0].result_conn1, gfd_adc[0].result_conn2);
  865. // }
  866. }
  867. // else
  868. // {
  869. // if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].Parameter.bits.GfdDetection ||
  870. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].Parameter.bits.GfdDetection)
  871. // {
  872. // LOG_INFO("Query Relay1 GFD ADC Fail");
  873. // }
  874. // }
  875. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  876. {
  877. if(Query_Gfd_Adc(Uart5Fd, Addr.DO360_RC2, &gfd_adc[1]) == PASS)
  878. {
  879. }
  880. }
  881. // update output relay feedback status
  882. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  883. {
  884. location = ShmPsuGrouping->GroupCollection[i].Location;
  885. result = LocaltionGfd[location]->bits.GFD_Result;
  886. if(_chargingData[i]->GroundFaultStatus != result)
  887. {
  888. if(result <= GFD_WARNING)
  889. {
  890. LOG_INFO("Gun %d GFD Result %s at Location %d", i + 1, str_gfd[result], location + 1);
  891. if(result == GFD_FAIL || result == GFD_WARNING)
  892. {
  893. LOG_INFO("Gun %d GFD Step = %d, R = %d, Vol = %d", i + 1, LocaltionGfd[location]->bits.rb_step,
  894. LocaltionGfd[location]->bits.Resister, LocaltionGfd[location]->bits.Voltage);
  895. }
  896. }
  897. else
  898. {
  899. LOG_INFO("Gun %d Unknown GFD Result %d at Location %d", i + 1, result, location + 1);
  900. }
  901. }
  902. _chargingData[i]->GroundFaultStatus = result;
  903. }
  904. }
  905. }
  906. void SetFanModuleSpeed()
  907. {
  908. {
  909. FanSpeed _fanSpeed;
  910. _setFanSpeed += fanSpeedSmoothValue;
  911. if (_setFanSpeed >= ShmFanModuleData->SetFan1Speed)
  912. _setFanSpeed = ShmFanModuleData->SetFan1Speed;
  913. //printf("_setFanSpeed = %d \n", _setFanSpeed);
  914. _fanSpeed.speed[0] = _setFanSpeed;
  915. _fanSpeed.speed[1] = _setFanSpeed;
  916. _fanSpeed.speed[2] = _setFanSpeed;
  917. _fanSpeed.speed[3] = _setFanSpeed;
  918. if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
  919. {
  920. //LOG_INFO("successfully Fan");
  921. }
  922. }
  923. }
  924. void SetPCabinetOutputRelayOff(byte index)
  925. {
  926. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  927. {
  928. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != NO)
  929. {
  930. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay OFF", index + 1);
  931. }
  932. ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] = NO;
  933. }
  934. }
  935. void SetMCabinetOutputRelay(byte index)
  936. {
  937. if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  938. _chargingData[index]->SystemStatus <= S_CHARGING))
  939. {
  940. if(_chargingData[index]->GroundFaultStatus == GFD_FAIL)
  941. {
  942. if(ShmOutputRelayConfig[index]->bits.Output_N || ShmOutputRelayConfig[index]->bits.Output_P)
  943. {
  944. LOG_INFO("Gun %d Set K1K2 Open By GFD Fail", index + 1);
  945. }
  946. ShmOutputRelayConfig[index]->bits.Output_N = false;
  947. ShmOutputRelayConfig[index]->bits.Output_P = false;
  948. SetPCabinetOutputRelayOff(index);
  949. }
  950. else
  951. {
  952. #if RELAY_OPEN_AT_PRECHARGE
  953. if(_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE)
  954. {
  955. if(ShmPsuGrouping->GroupCollection[index].GroupCtrl.bits.CableCheckDone == false &&
  956. ShmPsuGrouping->GroupCollection[index].GroupCtrl.bits.InPrechargeMode == false)
  957. {
  958. if(!ShmOutputRelayConfig[index]->bits.Output_N || !ShmOutputRelayConfig[index]->bits.Output_P)
  959. {
  960. LOG_INFO("Gun %d Set K1K2 Close And Prepare To Cable Check", index + 1);
  961. }
  962. ShmOutputRelayConfig[index]->bits.Output_N = true;
  963. ShmOutputRelayConfig[index]->bits.Output_P = true;
  964. }
  965. else if(ShmPsuGrouping->GroupCollection[index].GroupCtrl.bits.CableCheckDone == true &&
  966. ShmPsuGrouping->GroupCollection[index].GroupCtrl.bits.InPrechargeMode == false)
  967. {
  968. if(_chargingData[index]->FireChargingVoltage <= SELF_TO_CHANGE_RELAY_STATUS)
  969. {
  970. if(ShmOutputRelayConfig[index]->bits.Output_N || ShmOutputRelayConfig[index]->bits.Output_P)
  971. {
  972. LOG_INFO("Gun %d Set K1K2 Open By Cable Check Done", index + 1);
  973. }
  974. ShmOutputRelayConfig[index]->bits.Output_N = false;
  975. ShmOutputRelayConfig[index]->bits.Output_P = false;
  976. }
  977. }
  978. else if(ShmPsuGrouping->GroupCollection[index].GroupCtrl.bits.CableCheckDone == true &&
  979. ShmPsuGrouping->GroupCollection[index].GroupCtrl.bits.InPrechargeMode == true)
  980. {
  981. unsigned short voltage = 0, diffVol = 0;
  982. voltage = (int)(_chargingData[index]->PresentChargingVoltage * 10);
  983. diffVol = voltage >= ShmPsuGrouping->GroupOutput[index].GTargetVoltage ?
  984. voltage - ShmPsuGrouping->GroupOutput[index].GTargetVoltage :
  985. ShmPsuGrouping->GroupOutput[index].GTargetVoltage - voltage;
  986. if(diffVol <= 30)
  987. {
  988. if(!ShmOutputRelayConfig[index]->bits.Output_N || !ShmOutputRelayConfig[index]->bits.Output_P)
  989. {
  990. LOG_INFO("Gun %d Set K1K2 Close And Voltage Is Balance", index + 1);
  991. }
  992. ShmOutputRelayConfig[index]->bits.Output_N = true;
  993. ShmOutputRelayConfig[index]->bits.Output_P = true;
  994. }
  995. }
  996. }
  997. else
  998. {
  999. if(!ShmOutputRelayConfig[index]->bits.Output_N || !ShmOutputRelayConfig[index]->bits.Output_P)
  1000. {
  1001. LOG_INFO("Gun %d Set K1K2 Close In Charging Status", index + 1);
  1002. }
  1003. ShmOutputRelayConfig[index]->bits.Output_N = true;
  1004. ShmOutputRelayConfig[index]->bits.Output_P = true;
  1005. }
  1006. #else
  1007. if(!ShmOutputRelayConfig[index]->bits.Output_N || !ShmOutputRelayConfig[index]->bits.Output_P)
  1008. {
  1009. LOG_INFO("Gun %d Set K1K2 Close And Prepare To Charging", index + 1);
  1010. }
  1011. ShmOutputRelayConfig[index]->bits.Output_N = true;
  1012. ShmOutputRelayConfig[index]->bits.Output_P = true;
  1013. #endif
  1014. }
  1015. }
  1016. else if ((_chargingData[index]->SystemStatus >= S_TERMINATING &&
  1017. _chargingData[index]->SystemStatus <= S_COMPLETE) ||
  1018. _chargingData[index]->SystemStatus == S_ALARM)
  1019. {
  1020. if ((_chargingData[index]->PresentChargingCurrent * 10) <= SEFETY_SWITCH_RELAY_CUR ||
  1021. _chargingData[index]->GroundFaultStatus == GFD_FAIL)
  1022. {
  1023. if(ShmOutputRelayConfig[index]->bits.Output_N || ShmOutputRelayConfig[index]->bits.Output_P)
  1024. {
  1025. LOG_INFO("Gun %d Set K1K2 Open And Charging Stop", index + 1);
  1026. }
  1027. ShmOutputRelayConfig[index]->bits.Output_N = false;
  1028. ShmOutputRelayConfig[index]->bits.Output_P = false;
  1029. SetPCabinetOutputRelayOff(index);
  1030. }
  1031. }
  1032. else
  1033. {
  1034. if(ShmOutputRelayConfig[index]->bits.Output_N || ShmOutputRelayConfig[index]->bits.Output_P)
  1035. {
  1036. LOG_INFO("Gun %d Set K1K2 Open At Idle Mode", index + 1);
  1037. }
  1038. ShmOutputRelayConfig[index]->bits.Output_N = false;
  1039. ShmOutputRelayConfig[index]->bits.Output_P = false;
  1040. if(_chargingData[index]->SystemStatus == S_IDLE ||
  1041. _chargingData[index]->SystemStatus == S_MAINTAIN ||
  1042. _chargingData[index]->SystemStatus == S_FAULT)
  1043. {
  1044. SetPCabinetOutputRelayOff(index);
  1045. }
  1046. }
  1047. }
  1048. void SetSCabinetOutputRelay(byte index)
  1049. {
  1050. ShmOutputRelayConfig[index]->bits.Output_N = ShmChargerInfo->SCabinetControl.SOutputRelay[index] > 0 ? true : false;
  1051. ShmOutputRelayConfig[index]->bits.Output_P = ShmChargerInfo->SCabinetControl.SOutputRelay[index] > 0 ? true : false;
  1052. }
  1053. //==========================================
  1054. // Common Function
  1055. //==========================================
  1056. void SetK1K2RelayStatus(byte index)
  1057. {
  1058. unsigned char location = 0;
  1059. if(index < MAX_GROUP_QUANTITY && index < ShmChargerInfo->Control.MaxConnector)
  1060. {
  1061. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  1062. {
  1063. SetMCabinetOutputRelay(index);
  1064. }
  1065. else
  1066. {
  1067. SetSCabinetOutputRelay(index);
  1068. }
  1069. location = ShmPsuGrouping->GroupCollection[index].Location;
  1070. if(ShmOutputRelayConfig[index]->bits.Output_N != LocationOutputRelayCtrl[location]->bits.Gun_N)
  1071. {
  1072. #if RELAY_DEBUG_MSG
  1073. LOG_INFO("Connector %d Set K1K2 N %s at Location %d",
  1074. index + 1, ShmOutputRelayConfig[index]->bits.Output_N ? "On" : "Off", location + 1);
  1075. #endif
  1076. }
  1077. LocationOutputRelayCtrl[location]->bits.Gun_N = ShmOutputRelayConfig[index]->bits.Output_N;
  1078. if(ShmOutputRelayConfig[index]->bits.Output_P != LocationOutputRelayCtrl[location]->bits.Gun_P)
  1079. {
  1080. #if RELAY_DEBUG_MSG
  1081. LOG_INFO("Connector %d Set K1K2 P %s at Location %d",
  1082. index + 1, ShmOutputRelayConfig[index]->bits.Output_P ? "On" : "Off", location + 1);
  1083. #endif
  1084. }
  1085. LocationOutputRelayCtrl[location]->bits.Gun_P = ShmOutputRelayConfig[index]->bits.Output_P;
  1086. }
  1087. }
  1088. void SetParalleRelayStatus()
  1089. {
  1090. int parallelCnt = ShmChargerInfo->Control.MaxConnector == GENERAL_GUN_QUANTITY ? PARALLEL_RELAY_COUNT : 1;
  1091. for(int i = 0; i < parallelCnt; i++)
  1092. {
  1093. if((ShmParallelRelayConfig->CtrlValue & (1 << i)) != (LocationParallelRelayCtrl[i] << i))
  1094. {
  1095. #if RELAY_DEBUG_MSG
  1096. LOG_INFO("Set Parallel Relay N & P %s at Location %d",
  1097. (ShmParallelRelayConfig->CtrlValue & (1 << i)) ? "On" : "Off", i + 1);
  1098. #endif
  1099. }
  1100. LocationParallelRelayCtrl[i] = (ShmParallelRelayConfig->CtrlValue & (1 << i)) ? YES : NO;
  1101. switch(i)
  1102. {
  1103. case 0:
  1104. outputRelay[0].relay_event.bits.Gun1_Parallel_N = LocationParallelRelayCtrl[i];
  1105. if(!ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1106. {
  1107. outputRelay[0].relay_event.bits.Gun2_Parallel_N = LocationParallelRelayCtrl[i];
  1108. }
  1109. break;
  1110. case 1:
  1111. outputRelay[0].relay_event.bits.Gun2_Parallel_N = LocationParallelRelayCtrl[i];
  1112. break;
  1113. case 2:
  1114. outputRelay[1].relay_event.bits.Gun1_Parallel_N = LocationParallelRelayCtrl[i];
  1115. break;
  1116. case 3:
  1117. outputRelay[1].relay_event.bits.Gun2_Parallel_N = LocationParallelRelayCtrl[i];
  1118. break;
  1119. case 4:
  1120. outputRelay[0].relay_event.bits.CCS_Precharge = LocationParallelRelayCtrl[i];
  1121. break;
  1122. case 5:
  1123. outputRelay[1].relay_event.bits.CCS_Precharge = LocationParallelRelayCtrl[i];
  1124. break;
  1125. }
  1126. }
  1127. }
  1128. void SetAcContactorStatus(void)
  1129. {
  1130. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor == YES &&
  1131. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO &&
  1132. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency == NO)
  1133. {
  1134. outputRelay[0].relay_event.bits.AC_Contactor = YES;
  1135. }
  1136. else
  1137. {
  1138. outputRelay[0].relay_event.bits.AC_Contactor = NO;
  1139. }
  1140. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1141. {
  1142. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor == YES &&
  1143. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO &&
  1144. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency == NO)
  1145. {
  1146. outputRelay[1].relay_event.bits.AC_Contactor = YES;
  1147. }
  1148. else
  1149. {
  1150. outputRelay[1].relay_event.bits.AC_Contactor = NO;
  1151. }
  1152. }
  1153. }
  1154. bool IsNoneMatchLedColor()
  1155. {
  1156. bool result = false;
  1157. if (cur_led_color.Connect_1_Red != led_color.Connect_1_Red ||
  1158. cur_led_color.Connect_1_Green != led_color.Connect_1_Green ||
  1159. cur_led_color.Connect_1_Blue != led_color.Connect_1_Blue ||
  1160. cur_led_color.Connect_2_Red != led_color.Connect_2_Red ||
  1161. cur_led_color.Connect_2_Green != led_color.Connect_2_Green ||
  1162. cur_led_color.Connect_2_Blue != led_color.Connect_2_Blue)
  1163. {
  1164. result = true;
  1165. }
  1166. return result;
  1167. }
  1168. void SetLedColor(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
  1169. {
  1170. byte _colorBuf = COLOR_MAX_LV * LED_INTENSITY_BRIGHTEST;
  1171. if (ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity == _LED_INTENSITY_DARKEST)
  1172. _colorBuf = COLOR_MAX_LV * LED_INTENSITY_DARKEST;
  1173. else if (ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity == _LED_INTENSITY_MEDIUM)
  1174. _colorBuf = COLOR_MAX_LV * LED_INTENSITY_MEDIUM;
  1175. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  1176. {
  1177. if ((chargingData_1->SystemStatus == S_BOOTING || chargingData_1->SystemStatus == S_IDLE || chargingData_1->SystemStatus == S_RESERVATION) &&
  1178. (chargingData_2->SystemStatus == S_BOOTING || chargingData_2->SystemStatus == S_IDLE || chargingData_2->SystemStatus == S_RESERVATION))
  1179. {
  1180. led_color.Connect_1_Green = _colorBuf;
  1181. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1182. led_color.Connect_1_Red = COLOR_MIN_LV;
  1183. led_color.Connect_2_Green = _colorBuf;
  1184. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1185. led_color.Connect_2_Red = COLOR_MIN_LV;
  1186. }
  1187. else if ((chargingData_1->SystemStatus >= S_AUTHORIZING && chargingData_1->SystemStatus <= S_COMPLETE) ||
  1188. (chargingData_1->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_1->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  1189. (chargingData_2->SystemStatus >= S_AUTHORIZING && chargingData_2->SystemStatus <= S_COMPLETE) ||
  1190. (chargingData_2->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_2->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1191. {
  1192. led_color.Connect_1_Green = COLOR_MIN_LV;
  1193. led_color.Connect_1_Blue = _colorBuf;
  1194. led_color.Connect_1_Red = COLOR_MIN_LV;
  1195. led_color.Connect_2_Green = COLOR_MIN_LV;
  1196. led_color.Connect_2_Blue = _colorBuf;
  1197. led_color.Connect_2_Red = COLOR_MIN_LV;
  1198. }
  1199. }
  1200. else
  1201. {
  1202. if (chargingData_1->SystemStatus == S_BOOTING || chargingData_1->SystemStatus == S_IDLE || chargingData_1->SystemStatus == S_RESERVATION)
  1203. {
  1204. led_color.Connect_1_Green = _colorBuf;
  1205. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1206. led_color.Connect_1_Red = COLOR_MIN_LV;
  1207. }
  1208. else if ((chargingData_1->SystemStatus >= S_AUTHORIZING && chargingData_1->SystemStatus <= S_COMPLETE) ||
  1209. (chargingData_1->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_1->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1210. {
  1211. led_color.Connect_1_Green = COLOR_MIN_LV;
  1212. led_color.Connect_1_Blue = _colorBuf;
  1213. led_color.Connect_1_Red = COLOR_MIN_LV;
  1214. }
  1215. // --------------------------------------------------------------------------
  1216. if (chargingData_2->SystemStatus == S_BOOTING || chargingData_2->SystemStatus == S_IDLE || chargingData_2->SystemStatus == S_RESERVATION)
  1217. {
  1218. led_color.Connect_2_Green = _colorBuf;
  1219. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1220. led_color.Connect_2_Red = COLOR_MIN_LV;
  1221. }
  1222. else if ((chargingData_2->SystemStatus >= S_AUTHORIZING && chargingData_2->SystemStatus <= S_COMPLETE) ||
  1223. (chargingData_2->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_2->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1224. {
  1225. led_color.Connect_2_Green = COLOR_MIN_LV;
  1226. led_color.Connect_2_Blue = _colorBuf;
  1227. led_color.Connect_2_Red = COLOR_MIN_LV;
  1228. }
  1229. }
  1230. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  1231. {
  1232. led_color.Connect_1_Green = COLOR_MIN_LV;
  1233. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1234. led_color.Connect_1_Red = _colorBuf;
  1235. led_color.Connect_2_Green = COLOR_MIN_LV;
  1236. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1237. led_color.Connect_2_Red = _colorBuf;
  1238. }
  1239. if (_checkLedChanged > 0)
  1240. {
  1241. if (Config_Led_Color(Uart5Fd, Addr.Led, &led_color) == PASS)
  1242. {
  1243. _checkLedChanged--;
  1244. cur_led_color.Connect_1_Red = led_color.Connect_1_Red;
  1245. cur_led_color.Connect_1_Green = led_color.Connect_1_Green;
  1246. cur_led_color.Connect_1_Blue = led_color.Connect_1_Blue;
  1247. cur_led_color.Connect_2_Red = led_color.Connect_2_Red;
  1248. cur_led_color.Connect_2_Green = led_color.Connect_2_Green;
  1249. cur_led_color.Connect_2_Blue = led_color.Connect_2_Blue;
  1250. }
  1251. }
  1252. else if (IsNoneMatchLedColor())
  1253. _checkLedChanged = 3;
  1254. }
  1255. //==========================================
  1256. // Init all share memory
  1257. //==========================================
  1258. int InitShareMemory()
  1259. {
  1260. int result = PASS;
  1261. int MeterSMId;
  1262. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  1263. {
  1264. #ifdef SystemLogMessage
  1265. LOG_ERROR("shmget ShmSysConfigAndInfo NG");
  1266. #endif
  1267. result = FAIL;
  1268. }
  1269. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1270. {
  1271. #ifdef SystemLogMessage
  1272. LOG_ERROR("[shmat ShmSysConfigAndInfo NG");
  1273. #endif
  1274. result = FAIL;
  1275. }
  1276. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  1277. {
  1278. #ifdef SystemLogMessage
  1279. LOG_ERROR("shmget ShmStatusCodeData NG");
  1280. #endif
  1281. result = FAIL;
  1282. }
  1283. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1284. {
  1285. #ifdef SystemLogMessage
  1286. LOG_ERROR("shmat ShmStatusCodeData NG");
  1287. #endif
  1288. result = FAIL;
  1289. }
  1290. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
  1291. {
  1292. #ifdef SystemLogMessage
  1293. LOG_ERROR("shmget ShmFanModuleData NG");
  1294. #endif
  1295. result = FAIL;
  1296. }
  1297. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1298. {
  1299. #ifdef SystemLogMessage
  1300. LOG_ERROR("shmat ShmFanModuleData NG");
  1301. #endif
  1302. result = FAIL;
  1303. }
  1304. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  1305. {
  1306. #ifdef SystemLogMessage
  1307. LOG_ERROR("shmget ShmRelayModuleData NG");
  1308. #endif
  1309. result = FAIL;
  1310. }
  1311. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1312. {
  1313. #ifdef SystemLogMessage
  1314. LOG_ERROR("shmat ShmRelayModuleData NG");
  1315. #endif
  1316. result = FAIL;
  1317. }
  1318. // DO360 RC2
  1319. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  1320. {
  1321. #ifdef SystemLogMessage
  1322. LOG_ERROR("shmget ShmRelay2ModuleData NG");
  1323. #endif
  1324. result = FAIL;
  1325. }
  1326. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1327. {
  1328. #ifdef SystemLogMessage
  1329. LOG_ERROR("shmat ShmRelay2ModuleData NG");
  1330. #endif
  1331. result = FAIL;
  1332. }
  1333. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), 0777)) < 0)
  1334. {
  1335. #ifdef SystemLogMessage
  1336. LOG_ERROR("shmget ShmLedModuleData NG");
  1337. #endif
  1338. result = FAIL;
  1339. }
  1340. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1341. {
  1342. #ifdef SystemLogMessage
  1343. LOG_ERROR("shmat ShmLedModuleData NG");
  1344. #endif
  1345. result = FAIL;
  1346. }
  1347. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  1348. {
  1349. #ifdef SystemLogMessage
  1350. LOG_ERROR("shmget ShmPsuData NG");
  1351. #endif
  1352. result = FAIL;
  1353. }
  1354. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1355. {
  1356. #ifdef SystemLogMessage
  1357. LOG_ERROR("shmat ShmPsuData NG");
  1358. #endif
  1359. result = FAIL;
  1360. }
  1361. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  1362. {
  1363. #ifdef SystemLogMessage
  1364. LOG_ERROR("shmat ShmOCPP16Data NG");
  1365. #endif
  1366. result = FAIL;
  1367. }
  1368. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1369. {
  1370. #ifdef SystemLogMessage
  1371. LOG_ERROR("shmat ShmOCPP16Data NG");
  1372. #endif
  1373. result = FAIL;
  1374. }
  1375. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  1376. {
  1377. #ifdef SystemLogMessage
  1378. LOG_ERROR("shmat ChargerInfoData NG");
  1379. #endif
  1380. result = FAIL;
  1381. }
  1382. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1383. {
  1384. #ifdef SystemLogMessage
  1385. LOG_ERROR("shmat ChargerInfoData NG");
  1386. #endif
  1387. result = FAIL;
  1388. }
  1389. if(result == PASS)
  1390. {
  1391. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  1392. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  1393. {
  1394. ShmOutputRelayConfig[i] = &ShmChargerInfo->PsuGrouping.OutputRelayConfig[i];
  1395. ShmOutputRelayConfirmed[i] = &ShmChargerInfo->PsuGrouping.OutputRelayConfirmed[i];
  1396. }
  1397. ShmParallelRelayConfig = &ShmChargerInfo->PsuGrouping.ParallelRelayConfig;
  1398. ShmParallelRelayConfirmed = &ShmChargerInfo->PsuGrouping.ParallelRelayConfirmed;
  1399. if(ShmChargerInfo->Control.SysCtrl.bits.RelayBoardDisable == false)
  1400. {
  1401. LocationOutputRelayCtrl[0] = (OutputRelayControl *)&outputRelay[0].relay_event.relay_status[1];
  1402. LocationOutputRelayCtrl[1] = (OutputRelayControl *)&outputRelay[0].relay_event.relay_status[2];
  1403. LocationOutputRelayResponse[0] = (OutputRelayControl *)&regRelay[0].relay_event.relay_status[1];
  1404. LocationOutputRelayResponse[1] = (OutputRelayControl *)&regRelay[0].relay_event.relay_status[2];
  1405. memset(LocationParallelRelayCtrl, 0x00, sizeof(LocationParallelRelayCtrl));
  1406. memset(LocationParallelRelayResponse, 0x00, sizeof(LocationParallelRelayResponse));
  1407. LocaltionGfd[0] = (Connector_GFD *)&gfd_adc[0].Resister_conn1;
  1408. LocaltionGfd[1] = (Connector_GFD *)&gfd_adc[0].Resister_conn2;
  1409. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1410. {
  1411. LocationOutputRelayCtrl[2] = (OutputRelayControl *)&outputRelay[1].relay_event.relay_status[1];
  1412. LocationOutputRelayCtrl[3] = (OutputRelayControl *)&outputRelay[1].relay_event.relay_status[2];
  1413. LocationOutputRelayResponse[2] = (OutputRelayControl *)&regRelay[1].relay_event.relay_status[1];
  1414. LocationOutputRelayResponse[3] = (OutputRelayControl *)&regRelay[1].relay_event.relay_status[2];
  1415. LocaltionGfd[2] = (Connector_GFD *)&gfd_adc[1].Resister_conn1;
  1416. LocaltionGfd[3] = (Connector_GFD *)&gfd_adc[1].Resister_conn2;
  1417. }
  1418. }
  1419. }
  1420. return result;
  1421. }
  1422. int InitComPort()
  1423. {
  1424. int fd;
  1425. struct termios tios;
  1426. fd = open(relayRs485PortName, O_RDWR);
  1427. if(fd <= 0)
  1428. {
  1429. #ifdef SystemLogMessage
  1430. LOG_ERROR("Module_InternalComm. InitComPort NG");
  1431. #endif
  1432. if(ShmStatusCodeData!=NULL)
  1433. {
  1434. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1435. }
  1436. sleep(5);
  1437. return -1;
  1438. }
  1439. ioctl (fd, TCGETS, &tios);
  1440. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  1441. tios.c_lflag = 0;
  1442. tios.c_iflag = 0;
  1443. tios.c_oflag = 0;
  1444. tios.c_cc[VMIN]=0;
  1445. tios.c_cc[VTIME]=(byte)0; // timeout 0.5 second
  1446. tios.c_lflag=0;
  1447. tcflush(fd, TCIFLUSH);
  1448. ioctl (fd, TCSETS, &tios);
  1449. return fd;
  1450. }
  1451. //================================================
  1452. // Main process
  1453. //================================================
  1454. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1455. {
  1456. for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
  1457. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
  1458. == target) {
  1459. chargingData[target] =
  1460. &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1461. return true;
  1462. }
  1463. }
  1464. for (byte index = 0; index < CCS_QUANTITY; index++) {
  1465. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
  1466. == target) {
  1467. chargingData[target] =
  1468. &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1469. return true;
  1470. }
  1471. }
  1472. for (byte index = 0; index < GB_QUANTITY; index++) {
  1473. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
  1474. == target) {
  1475. chargingData[target] =
  1476. &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1477. return true;
  1478. }
  1479. }
  1480. //DO360
  1481. if(GENERAL_GUN_QUANTITY > 0 && target < GENERAL_GUN_QUANTITY)
  1482. {
  1483. //ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].GeneralChargingData.Index = target;
  1484. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].GeneralChargingData;
  1485. return true;
  1486. }
  1487. return false;
  1488. }
  1489. bool FindAcChargingInfoData(byte target, struct ChargingInfoData **acChargingData)
  1490. {
  1491. if (target < AC_QUANTITY)
  1492. {
  1493. acChargingData[target] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[target];
  1494. return true;
  1495. }
  1496. return false;
  1497. }
  1498. void Initialization()
  1499. {
  1500. bool isPass = false;
  1501. for (byte index = 0; index < ARRAY_SIZE(outputRelay[0].relay_event.relay_status); index++)
  1502. {
  1503. outputRelay[0].relay_event.relay_status[index] = 0x00;
  1504. outputRelay[1].relay_event.relay_status[index] = 0x00;
  1505. }
  1506. while(!isPass)
  1507. {
  1508. isPass = true;
  1509. for (byte _index = 0; _index < CONNECTOR_QUANTITY; _index++)
  1510. {
  1511. if (!FindChargingInfoData(_index, &_chargingData[0]))
  1512. {
  1513. LOG_ERROR("InternalComm : FindChargingInfoData false");
  1514. isPass = false;
  1515. break;
  1516. }
  1517. }
  1518. sleep(1);
  1519. }
  1520. isPass = false;
  1521. if (acgunCount > 0)
  1522. {
  1523. while(!isPass)
  1524. {
  1525. isPass = true;
  1526. for (byte _index = 0; _index < acgunCount; _index++)
  1527. {
  1528. if (!FindAcChargingInfoData(_index, &ac_chargingInfo[0]))
  1529. {
  1530. LOG_ERROR("EvComm : FindAcChargingInfoData false");
  1531. isPass = false;
  1532. break;
  1533. }
  1534. }
  1535. sleep(1);
  1536. }
  1537. }
  1538. }
  1539. bool IsNoneMatchRelayStatus(byte index)
  1540. {
  1541. bool result = false;
  1542. if(regRelay[index].relay_event.relay_status[0] != outputRelay[index].relay_event.relay_status[0] ||
  1543. regRelay[index].relay_event.relay_status[1] != outputRelay[index].relay_event.relay_status[1] ||
  1544. regRelay[index].relay_event.relay_status[2] != outputRelay[index].relay_event.relay_status[2])
  1545. {
  1546. if(TempRegRelay[index].relay_event.bits.AC_Contactor != outputRelay[index].relay_event.bits.AC_Contactor)
  1547. {
  1548. LOG_INFO("[%d]AC Contact Relay none match, need to %s", index,
  1549. outputRelay[index].relay_event.bits.AC_Contactor == YES ? "On" : "Off");
  1550. }
  1551. if(TempRegRelay[index].relay_event.bits.CCS_Precharge != outputRelay[index].relay_event.bits.CCS_Precharge)
  1552. {
  1553. #if RELAY_DEBUG_MSG
  1554. LOG_INFO("[%d]CCS Precharge Relay none match, need to %s", index,
  1555. outputRelay[index].relay_event.bits.CCS_Precharge == YES ? "On" : "Off");
  1556. #endif
  1557. }
  1558. if(TempRegRelay[index].relay_event.bits.Gun1_P != outputRelay[index].relay_event.bits.Gun1_P)
  1559. {
  1560. #if RELAY_DEBUG_MSG
  1561. LOG_INFO("[%d]SMR1:D+ Relay none match, need to %s", index,
  1562. outputRelay[index].relay_event.bits.Gun1_P == YES ? "On" : "Off");
  1563. #endif
  1564. }
  1565. if(TempRegRelay[index].relay_event.bits.Gun1_N != outputRelay[index].relay_event.bits.Gun1_N)
  1566. {
  1567. #if RELAY_DEBUG_MSG
  1568. LOG_INFO("[%d]SMR1:D- Relay none match, need to %s", index,
  1569. outputRelay[index].relay_event.bits.Gun1_N == YES ? "On" : "Off");
  1570. #endif
  1571. }
  1572. if(TempRegRelay[index].relay_event.bits.Gun2_P != outputRelay[index].relay_event.bits.Gun2_P)
  1573. {
  1574. #if RELAY_DEBUG_MSG
  1575. LOG_INFO("[%d]SMR2:D+ Relay none match, need to %s", index,
  1576. outputRelay[index].relay_event.bits.Gun2_P == YES ? "On" : "Off");
  1577. #endif
  1578. }
  1579. if(TempRegRelay[index].relay_event.bits.Gun2_N != outputRelay[index].relay_event.bits.Gun2_N)
  1580. {
  1581. #if RELAY_DEBUG_MSG
  1582. LOG_INFO("[%d]SMR2:D- Relay none match, need to %s", index,
  1583. outputRelay[index].relay_event.bits.Gun2_N == YES ? "On" : "Off");
  1584. #endif
  1585. }
  1586. if(TempRegRelay[index].relay_event.bits.Gun1_Parallel_P != outputRelay[index].relay_event.bits.Gun1_Parallel_P)
  1587. {
  1588. #if RELAY_DEBUG_MSG
  1589. LOG_INFO("[%d]Parallel:D+ Relay none match, need to %s", index,
  1590. outputRelay[index].relay_event.bits.Gun1_Parallel_P == YES ? "On" : "Off");
  1591. #endif
  1592. }
  1593. if(TempRegRelay[index].relay_event.bits.Gun1_Parallel_N != outputRelay[index].relay_event.bits.Gun1_Parallel_N)
  1594. {
  1595. #if RELAY_DEBUG_MSG
  1596. LOG_INFO("[%d]Parallel:D- Relay none match, need to %s", index,
  1597. outputRelay[index].relay_event.bits.Gun1_Parallel_N == YES ? "On" : "Off");
  1598. #endif
  1599. }
  1600. if(TempRegRelay[index].relay_event.bits.Gun2_Parallel_P != outputRelay[index].relay_event.bits.Gun2_Parallel_P)
  1601. {
  1602. #if RELAY_DEBUG_MSG
  1603. LOG_INFO("[%d]Parallel2:D+ Relay none match, need to %s", index,
  1604. outputRelay[index].relay_event.bits.Gun2_Parallel_P == YES ? "On" : "Off");
  1605. #endif
  1606. }
  1607. if(TempRegRelay[index].relay_event.bits.Gun2_Parallel_N != outputRelay[index].relay_event.bits.Gun2_Parallel_N)
  1608. {
  1609. #if RELAY_DEBUG_MSG
  1610. LOG_INFO("[%d]Parallel2:D- Relay none match, need to %s", index,
  1611. outputRelay[index].relay_event.bits.Gun2_Parallel_N == YES ? "On" : "Off");
  1612. #endif
  1613. }
  1614. TempRegRelay[index].relay_event.relay_status[0] = outputRelay[index].relay_event.relay_status[0];
  1615. TempRegRelay[index].relay_event.relay_status[1] = outputRelay[index].relay_event.relay_status[1];
  1616. TempRegRelay[index].relay_event.relay_status[2] = outputRelay[index].relay_event.relay_status[2];
  1617. result = true;
  1618. }
  1619. return result;
  1620. }
  1621. void CheckRelayStatusByADC()
  1622. {
  1623. if (ShmRelayModuleData[0]->Gun1FuseOutputVolt > 0 && ShmRelayModuleData[0]->Gun1RelayOutputVolt > 0 &&
  1624. (ShmRelayModuleData[0]->Gun1FuseOutputVolt == ShmRelayModuleData[0]->Gun1RelayOutputVolt))
  1625. {
  1626. // Relay 前後電壓一致
  1627. _chargingData[0]->RelayK1K2Status = 0x01;
  1628. }
  1629. else
  1630. _chargingData[0]->RelayK1K2Status = 0x00;
  1631. if (ShmRelayModuleData[1]->Gun2FuseOutputVolt > 0 && ShmRelayModuleData[1]->Gun2RelayOutputVolt > 0 &&
  1632. (ShmRelayModuleData[1]->Gun2FuseOutputVolt == ShmRelayModuleData[1]->Gun2RelayOutputVolt))
  1633. {
  1634. // Relay 前後電壓一致
  1635. _chargingData[1]->RelayK1K2Status = 0x01;
  1636. }
  1637. else
  1638. _chargingData[1]->RelayK1K2Status = 0x00;
  1639. }
  1640. void SetGfdConfig(byte index, byte resister)
  1641. {
  1642. unsigned char add = 0;
  1643. gfd_config.index = (index % 2);
  1644. gfd_config.state = resister;
  1645. add = index < 2 ? Addr.DO360_RC1 : Addr.DO360_RC2;
  1646. //LOG_INFO("************************GFD Vol = %d, GFD Res = %d", gfd_config.reqVol, gfd_config.resister);
  1647. if (Config_Gfd_Value(Uart5Fd, add, &gfd_config) == PASS)
  1648. {
  1649. // LOG_INFO("Set reqVol = %f, resister = %d",
  1650. // gfd_config.reqVol,
  1651. // gfd_config.resister);
  1652. // if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].Parameter.bits.GfdDetection ||
  1653. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].Parameter.bits.GfdDetection)
  1654. // {
  1655. // LOG_INFO("Set Relay %02X GFD Config index = %d, state = %d OK", add, gfd_config.index, gfd_config.state);
  1656. // }
  1657. }
  1658. // else
  1659. // {
  1660. // if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].Parameter.bits.GfdDetection ||
  1661. // ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].Parameter.bits.GfdDetection)
  1662. // {
  1663. // LOG_INFO("Set Relay %02X GFD Config index = %d, state = %d NG", add, gfd_config.index, gfd_config.state);
  1664. // }
  1665. // }
  1666. }
  1667. char GfdConfig[4];
  1668. void CableCheckDetected(byte index)
  1669. {
  1670. unsigned char location = 0;
  1671. char *strGfdConfig[] = {"Idle", "CableCheck", "PreCharge", "Charging"};
  1672. // Cable Check
  1673. // 當火線上的電壓 = 車端要求的電壓電流
  1674. // _chargingData[targetGun]->EvBatterytargetVoltage
  1675. // 才可以開始偵測 1s
  1676. // Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
  1677. // Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
  1678. // SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
  1679. if(_chargingData[index]->PantographFlag == YES)
  1680. {
  1681. location = ShmPsuGrouping->GroupCollection[index].Location;
  1682. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.GfdDetection &&
  1683. _chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus < S_TERMINATING)
  1684. {
  1685. if(_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE && _chargingData[index]->RelayWeldingCheck == YES)
  1686. {
  1687. SetGfdConfig(location, GFD_CABLECHK);
  1688. if(GfdConfig[index] != GFD_CABLECHK)
  1689. {
  1690. LOG_INFO("Gun %d Set GFD = %s", index + 1, strGfdConfig[GFD_CABLECHK]);
  1691. }
  1692. GfdConfig[index] = GFD_CABLECHK;
  1693. }
  1694. else
  1695. {
  1696. SetGfdConfig(location, GFD_CHARGING);
  1697. if(GfdConfig[index] != GFD_CHARGING)
  1698. {
  1699. LOG_INFO("Gun %d Set GFD = %s", index + 1, strGfdConfig[GFD_CHARGING]);
  1700. }
  1701. GfdConfig[index] = GFD_CHARGING;
  1702. }
  1703. }
  1704. else
  1705. {
  1706. SetGfdConfig(location, GFD_IDLE);
  1707. if(GfdConfig[index] != GFD_IDLE)
  1708. {
  1709. LOG_INFO("Gun %d Set GFD = %s", index + 1, strGfdConfig[GFD_IDLE]);
  1710. }
  1711. GfdConfig[index] = GFD_IDLE;
  1712. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.GfdDetection = 0;
  1713. }
  1714. }
  1715. }
  1716. void CheckOutputPowerOverCarReq(byte index)
  1717. {
  1718. float fireV = _chargingData[index]->FireChargingVoltage;
  1719. float carV = _chargingData[index]->EvBatterytargetVoltage * 10;
  1720. if ((_chargingData[index]->EvBatterytargetVoltage * 10) > 1500 &&
  1721. (_chargingData[index]->Type == _Type_Chademo ||
  1722. _chargingData[index]->Type == _Type_CCS_2 ||
  1723. _chargingData[index]->Type == _Type_GB))
  1724. {
  1725. if (fireV >= (carV + (carV * 0.1)))
  1726. {
  1727. LOG_INFO("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f",
  1728. _chargingData[index]->FireChargingVoltage, (_chargingData[index]->EvBatterytargetVoltage * 10));
  1729. LOG_INFO("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f",
  1730. _chargingData[index]->FireChargingVoltage, (_chargingData[index]->EvBatterytargetVoltage * 10));
  1731. _chargingData[index]->StopChargeFlag = YES;
  1732. }
  1733. }
  1734. }
  1735. void CheckOutputVolNoneMatchFire(byte index)
  1736. {
  1737. if ((_chargingData[index]->EvBatterytargetVoltage * 10) > 1500 &&
  1738. (_chargingData[index]->Type == _Type_Chademo ||
  1739. _chargingData[index]->Type == _Type_CCS_2 ||
  1740. _chargingData[index]->Type == _Type_GB))
  1741. {
  1742. if (((_chargingData[index]->PresentChargingVoltage * 10) < _chargingData[index]->FireChargingVoltage - 300) ||
  1743. ((_chargingData[index]->PresentChargingVoltage * 10) > _chargingData[index]->FireChargingVoltage + 300))
  1744. {
  1745. if (!_isOutputNoneMatch[index])
  1746. {
  1747. _isOutputNoneMatch[index] = YES;
  1748. GetClockTime(&_checkOutputNoneMatchTimer[index]);
  1749. }
  1750. else
  1751. {
  1752. if ((GetTimeoutValue(_checkOutputNoneMatchTimer[index]) / 1000) >= 5000)
  1753. {
  1754. LOG_INFO("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d) : pre = %f, fire = %f",
  1755. index, (_chargingData[index]->PresentChargingVoltage * 10), _chargingData[index]->FireChargingVoltage);
  1756. LOG_INFO("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d): pre = %f, fire = %f",
  1757. index, (_chargingData[index]->PresentChargingVoltage * 10), _chargingData[index]->FireChargingVoltage);
  1758. _chargingData[index]->StopChargeFlag = YES;
  1759. }
  1760. }
  1761. }
  1762. else
  1763. _isOutputNoneMatch[index] = NO;
  1764. }
  1765. }
  1766. void CheckRelayWeldingStatus(byte index)
  1767. {
  1768. if (!_isRelayWelding[index])
  1769. {
  1770. if ((_chargingData[index]->PresentChargingVoltage * 10) >= VOUT_MIN_VOLTAGE * 10)
  1771. {
  1772. GetClockTime(&_checkRelayWeldingTimer[index]);
  1773. _isRelayWelding[index] = YES;
  1774. }
  1775. }
  1776. else
  1777. {
  1778. if ((GetTimeoutValue(_checkRelayWeldingTimer[index]) / 1000) >= 1000)
  1779. {
  1780. _chargingData[index]->RelayWeldingCheck = YES;
  1781. return;
  1782. }
  1783. if (_chargingData[index]->FireChargingVoltage >= VOUT_MIN_VOLTAGE)
  1784. {
  1785. if (_chargingData[index]->Type == _Type_Chademo)
  1786. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  1787. else if (_chargingData[index]->Type == _Type_GB)
  1788. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  1789. else if (_chargingData[index]->Type == _Type_CCS_2)
  1790. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  1791. LOG_INFO("CheckRelayWeldingStatus : fail");
  1792. _chargingData[index]->StopChargeFlag = YES;
  1793. }
  1794. }
  1795. }
  1796. void GetPsuTempForFanSpeed()
  1797. {
  1798. char temp = 0;
  1799. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1800. {
  1801. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  1802. {
  1803. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  1804. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  1805. }
  1806. }
  1807. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = temp;
  1808. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  1809. {
  1810. if (ShmFanModuleData->TestFanSpeed == NORMAL_FAN_SPEED)
  1811. {
  1812. if (temp >= ENV_TEMP_MAX)
  1813. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  1814. }
  1815. else if (ShmFanModuleData->TestFanSpeed == MAX_FAN_SPEED)
  1816. {
  1817. if (temp <= ENV_TEMP_MIN)
  1818. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1819. }
  1820. else
  1821. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1822. }
  1823. }
  1824. void GetFanSpeedByFunction()
  1825. {
  1826. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  1827. return;
  1828. // 風控修改 :
  1829. // ******************************************************* //
  1830. //
  1831. // 當前PSU輸出總 KW PSU Temp
  1832. // 50 x -------------------- x ---------- + 0.5 x (PSU Temp - 70)
  1833. // 當前樁最大功率 KW 50
  1834. //
  1835. // ******************************************************* //
  1836. // 當前樁最大功率 KW : ShmPsuData->SystemAvailablePower
  1837. unsigned int _maxPower = ShmPsuData->SystemAvailablePower;
  1838. // 當前PSU輸出總 KW & PSU Temp :
  1839. unsigned char temp = 0;
  1840. float power = 0;
  1841. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1842. {
  1843. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  1844. {
  1845. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  1846. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  1847. }
  1848. power += (_chargingData[index]->PresentChargingPower * 10);
  1849. }
  1850. double _pw_rate = 0;
  1851. if (_maxPower > 0)
  1852. _pw_rate = power / (double)_maxPower;
  1853. double _temp_rate = 0;
  1854. if (temp > 0)
  1855. _temp_rate = (double)temp / 50;
  1856. unsigned char _temp_diff = 0;
  1857. if (temp > 70)
  1858. _temp_diff = temp - 70;
  1859. ShmFanModuleData->TestFanSpeed = (((50 * _pw_rate * _temp_rate) + (0.5 * _temp_diff)) / 100) * MAX_FAN_SPEED;
  1860. if (ShmFanModuleData->TestFanSpeed > MAX_FAN_SPEED)
  1861. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  1862. if (ShmFanModuleData->TestFanSpeed < 0)
  1863. ShmFanModuleData->TestFanSpeed = 0;
  1864. // -----------------------------------------------------------------------
  1865. // printf("power = %f \n", power);
  1866. // printf("_maxPower = %d \n", _maxPower);
  1867. // printf("temp = %d \n", temp);
  1868. //
  1869. // printf("_pw_rate = %f \n", _pw_rate);
  1870. // printf("_temp_rate = %f \n", _temp_rate);
  1871. // printf("_temp_diff = %d \n", _temp_diff);
  1872. // printf("fan rate = %f \n", (30 * _pw_rate * _temp_rate + 14 * _temp_diff));
  1873. // printf("ShmFanModuleData->TestFanSpeed = %d \n", ShmFanModuleData->TestFanSpeed);
  1874. }
  1875. int main(void)
  1876. {
  1877. if(InitShareMemory() == FAIL)
  1878. {
  1879. #ifdef SystemLogMessage
  1880. LOG_ERROR("InitShareMemory NG");
  1881. #endif
  1882. if(ShmStatusCodeData!=NULL)
  1883. {
  1884. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1885. }
  1886. sleep(5);
  1887. return 0;
  1888. }
  1889. gunCount = ShmChargerInfo->Control.MaxConnector;
  1890. acgunCount = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  1891. // Open Uart5 for RB
  1892. Uart5Fd = InitComPort();
  1893. Initialization();
  1894. sleep(1);
  1895. if(Uart5Fd < 0)
  1896. {
  1897. LOG_ERROR("(Internal) open port error.");
  1898. return 0;
  1899. }
  1900. _RelaySelfTestOK = NO;
  1901. memset(&outputRelay[0], 0x00, sizeof(Relay));
  1902. memset(&outputRelay[1], 0x00, sizeof(Relay));
  1903. if(ShmChargerInfo->Control.SysCtrl.bits.RelayBoardDisable == false)
  1904. {
  1905. if(ShmRelayModuleData[0]->SelfTest_Comp == NO)
  1906. {
  1907. if(Config_Relay_Output(Uart5Fd, Addr.DO360_RC1, &outputRelay[0]) != PASS)
  1908. LOG_INFO("Config_Relay1_Output fail");
  1909. }
  1910. else
  1911. {
  1912. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  1913. {
  1914. _RelaySelfTestOK = YES;
  1915. }
  1916. }
  1917. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1918. {
  1919. if(ShmRelayModuleData[1]->SelfTest_Comp == NO)
  1920. {
  1921. if(Config_Relay_Output(Uart5Fd, Addr.DO360_RC2, &outputRelay[1]) != PASS)
  1922. LOG_INFO("Config_Relay2_Output fail");
  1923. }
  1924. else
  1925. {
  1926. _RelaySelfTestOK = YES;
  1927. }
  1928. }
  1929. }
  1930. cur_led_color.Connect_1_Red = COLOR_MIN_LV;
  1931. cur_led_color.Connect_1_Green = COLOR_MIN_LV;
  1932. cur_led_color.Connect_1_Blue = COLOR_MIN_LV;
  1933. cur_led_color.Connect_2_Red = COLOR_MIN_LV;
  1934. cur_led_color.Connect_2_Green = COLOR_MIN_LV;
  1935. cur_led_color.Connect_2_Blue = COLOR_MIN_LV;
  1936. //bool printRelayStatus = true;
  1937. for(;;)
  1938. {
  1939. if(!ShmChargerInfo->Control.RelayCtrl.bits.Paused)
  1940. {
  1941. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  1942. if(ShmChargerInfo->Control.SysCtrl.bits.RelayBoardDisable == false)
  1943. {
  1944. if (ShmRelayModuleData[0]->SelfTest_Comp == NO && !ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  1945. {
  1946. // clena fw version
  1947. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0x00, 32);
  1948. GetFwAndHwVersion_Relay();
  1949. SetRtcData_Relay(0);
  1950. sleep(1);
  1951. if(strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev))
  1952. {
  1953. ShmRelayModuleData[0]->SelfTest_Comp = YES;
  1954. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  1955. {
  1956. _RelaySelfTestOK = YES;
  1957. }
  1958. }
  1959. }
  1960. // DO360 RC2
  1961. if (ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == true &&
  1962. ShmRelayModuleData[1]->SelfTest_Comp == NO && !ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  1963. {
  1964. // clena fw version
  1965. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0x00, 32);
  1966. GetFwAndHwVersion_Relay2();
  1967. SetRtcData_Relay(1);
  1968. sleep(1);
  1969. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev) != 0)
  1970. {
  1971. ShmRelayModuleData[1]->SelfTest_Comp = YES;
  1972. if(ShmRelayModuleData[0]->SelfTest_Comp == YES)
  1973. {
  1974. _RelaySelfTestOK = YES;
  1975. }
  1976. }
  1977. }
  1978. }
  1979. if(ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable == false)
  1980. {
  1981. if (ShmFanModuleData->SelfTest_Comp == NO && !ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  1982. {
  1983. // clena fw version
  1984. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0x00, 32);
  1985. GetFwAndHwVersion_Fan();
  1986. SetModelName_Fan();
  1987. SetRtcData_Fan();
  1988. sleep(1);
  1989. GetClockTime(&_priority_time);
  1990. if(strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0)
  1991. {
  1992. ShmFanModuleData->SelfTest_Comp = YES;
  1993. }
  1994. }
  1995. }
  1996. if(_RelaySelfTestOK == YES || ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  1997. {
  1998. // ==============優先權最高 10 ms ==============
  1999. // 輸出電壓
  2000. GetPersentOutputVol();
  2001. // 三相輸入電壓
  2002. GetPresentInputVol();
  2003. GetRelayOutputStatus();
  2004. GetGfdAdc();
  2005. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  2006. {
  2007. // Cable check (Set)
  2008. CableCheckDetected(i);
  2009. // check k1 k2 relay 狀態
  2010. CheckK1K2RelayOutput(i);
  2011. // 依據當前各槍的狀態選擇 搭上/放開 Relay
  2012. SetK1K2RelayStatus(i);
  2013. if (_chargingData[i]->SystemStatus == S_IDLE)
  2014. {
  2015. _chargingData[i]->RelayWeldingCheck = NO;
  2016. _isRelayWelding[i] = NO;
  2017. }
  2018. if (_chargingData[i]->SystemStatus == S_BOOTING ||
  2019. (_chargingData[i]->SystemStatus >= S_REASSIGN_CHECK && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
  2020. (_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  2021. ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES ||
  2022. (ShmSysConfigAndInfo->SysInfo.PageIndex >= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.PageIndex <= _LCM_WAIT_FOR_PLUG))
  2023. {
  2024. _chargingData[i]->IsReadyToCharging = YES;
  2025. // 限定只有在槍類別為 GBT 的時候才做 relay welding 的判斷
  2026. // if (_chargingData[i]->Type == _Type_GB)
  2027. // {
  2028. // if (_chargingData[i]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  2029. // _chargingData[i]->RelayWeldingCheck == NO)
  2030. // CheckRelayWeldingStatus(i);
  2031. // }
  2032. // else
  2033. _chargingData[i]->RelayWeldingCheck = YES;
  2034. if (_chargingData[i]->SystemStatus == S_CHARGING)
  2035. {
  2036. // DO360 do not check under voltage output & any voltage difference
  2037. //CheckOutputPowerOverCarReq(i);
  2038. //CheckOutputVolNoneMatchFire(i);
  2039. }
  2040. else
  2041. _isOutputNoneMatch[i] = NO;
  2042. }
  2043. else
  2044. _chargingData[i]->IsReadyToCharging = NO;
  2045. }
  2046. // 橋接 relay
  2047. SetParalleRelayStatus();
  2048. SetAcContactorStatus();
  2049. // 搭上/鬆開 Relay
  2050. if(IsNoneMatchRelayStatus(0))
  2051. {
  2052. if (Config_Relay_Output(Uart5Fd, Addr.DO360_RC1, &outputRelay[0]))
  2053. {
  2054. //regRelay[0].relay_event.relay_status[0] = outputRelay[0].relay_event.relay_status[0];
  2055. //regRelay[0].relay_event.relay_status[1] = outputRelay[0].relay_event.relay_status[1];
  2056. //regRelay[0].relay_event.relay_status[2] = outputRelay[0].relay_event.relay_status[2];
  2057. }
  2058. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2059. {
  2060. regRelay[0].relay_event.relay_status[0] = outputRelay[0].relay_event.relay_status[0];
  2061. regRelay[0].relay_event.relay_status[1] = outputRelay[0].relay_event.relay_status[1];
  2062. regRelay[0].relay_event.relay_status[2] = outputRelay[0].relay_event.relay_status[2];
  2063. }
  2064. }
  2065. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2066. {
  2067. // 搭上/鬆開 Relay
  2068. if(IsNoneMatchRelayStatus(1))
  2069. {
  2070. if (Config_Relay_Output(Uart5Fd, Addr.DO360_RC2, &outputRelay[1]))
  2071. {
  2072. //regRelay[1].relay_event.relay_status[0] = outputRelay[1].relay_event.relay_status[0];
  2073. //regRelay[1].relay_event.relay_status[1] = outputRelay[1].relay_event.relay_status[1];
  2074. //regRelay[1].relay_event.relay_status[2] = outputRelay[1].relay_event.relay_status[2];
  2075. }
  2076. }
  2077. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2078. {
  2079. regRelay[1].relay_event.relay_status[0] = outputRelay[1].relay_event.relay_status[0];
  2080. regRelay[1].relay_event.relay_status[1] = outputRelay[1].relay_event.relay_status[1];
  2081. regRelay[1].relay_event.relay_status[2] = outputRelay[1].relay_event.relay_status[2];
  2082. }
  2083. }
  2084. }
  2085. }
  2086. if(!ShmChargerInfo->Control.FanCtrl.bits.Paused)
  2087. {
  2088. if (ShmFanModuleData->SelfTest_Comp == YES ||
  2089. strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  2090. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  2091. {
  2092. if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
  2093. {
  2094. //GetPsuTempForFanSpeed();
  2095. GetFanSpeedByFunction();
  2096. GetFanSpeed();
  2097. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = _setFanSpeed;
  2098. GetClockTime(&_priority_time);
  2099. unsigned short TargetSpeed = ShmFanModuleData->TestFanSpeed;
  2100. if(TargetSpeed != 0 && TargetSpeed < MIN_FAN_SPEED)
  2101. {
  2102. TargetSpeed = MIN_FAN_SPEED;
  2103. }
  2104. ShmFanModuleData->SetFan1Speed = TargetSpeed;
  2105. ShmFanModuleData->SetFan2Speed = TargetSpeed;
  2106. ShmFanModuleData->SetFan3Speed = TargetSpeed;
  2107. ShmFanModuleData->SetFan4Speed = TargetSpeed;
  2108. //LOG_INFO("set fan = %d", ShmFanModuleData->SetFan1Speed);
  2109. SetFanModuleSpeed();
  2110. }
  2111. }
  2112. }
  2113. usleep(10000);
  2114. }
  2115. return FAIL;
  2116. }