Module_InternalComm.c 81 KB

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