Module_InternalComm.c 82 KB

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