Module_InternalComm.c 89 KB

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