Module_PsuComm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. #include "Module_PsuComm.h"
  2. #define Debug
  3. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  4. #define PASS 1
  5. #define FAIL -1
  6. #define YES 1
  7. #define NO 0
  8. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  9. struct StatusCodeData *ShmStatusCodeData;
  10. struct PsuData *ShmPsuData;
  11. void trim(char *s);
  12. int mystrcmp(char *p1,char *p2);
  13. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  14. void split(char **arr, char *str, const char *del);
  15. bool libInitialize = false;
  16. byte gun_count = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  17. byte getAvailableCapOffset = 5;
  18. float carReqVol = 0;
  19. float carReqCur = 0;
  20. float evseOutVol = 0;
  21. float evseOutCur = 0;
  22. int DiffTimeb(struct timeb ST, struct timeb ET)
  23. {
  24. //return milli-second
  25. unsigned int StartTime,StopTime;
  26. StartTime=(unsigned int)ST.time;
  27. StopTime=(unsigned int)ET.time;
  28. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  29. }
  30. //=================================
  31. // Common routine
  32. //=================================
  33. char* getTimeString(void)
  34. {
  35. char *result=malloc(21);
  36. time_t timep;
  37. struct tm *p;
  38. time(&timep);
  39. p=gmtime(&timep);
  40. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
  41. return result;
  42. }
  43. void trim(char *s)
  44. {
  45. int i=0, j, k, l=0;
  46. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  47. i++;
  48. j = strlen(s)-1;
  49. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  50. j--;
  51. if(i==0 && j==strlen(s)-1) { }
  52. else if(i==0) s[j+1] = '\0';
  53. else {
  54. for(k=i; k<=j; k++) s[l++] = s[k];
  55. s[l] = '\0';
  56. }
  57. }
  58. int mystrcmp(char *p1,char *p2)
  59. {
  60. while(*p1==*p2)
  61. {
  62. if(*p1=='\0' || *p2=='\0')
  63. break;
  64. p1++;
  65. p2++;
  66. }
  67. if(*p1=='\0' && *p2=='\0')
  68. return(PASS);
  69. else
  70. return(FAIL);
  71. }
  72. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  73. {
  74. strncpy(dest, src + start, cnt);
  75. dest[cnt] = 0;
  76. }
  77. void split(char **arr, char *str, const char *del)
  78. {
  79. char *s = strtok(str, del);
  80. while(s != NULL)
  81. {
  82. *arr++ = s;
  83. s = strtok(NULL, del);
  84. }
  85. }
  86. //=================================
  87. // Save data to share memory Function
  88. //=================================
  89. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  90. {
  91. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  92. {
  93. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  94. {
  95. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  96. return true;
  97. }
  98. }
  99. for (byte index = 0; index < CCS_QUANTITY; index++)
  100. {
  101. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  102. {
  103. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  104. return true;
  105. }
  106. }
  107. for (byte index = 0; index < GB_QUANTITY; index++)
  108. {
  109. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  110. {
  111. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. void GetPsuRequestCallback(byte phy_id, char *serial_number)
  118. {
  119. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  120. return;
  121. // ********************** 單槍的狀態,分配同一個 **********************
  122. byte group = 0;
  123. if(ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING || gun_count == 1)
  124. {
  125. // 初始化狀態,則直接先分配到同個群
  126. bool isNewPsu = true;
  127. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  128. {
  129. if (ShmPsuData->PsuGroup[group].PsuModule[index].PhysicalID == phy_id &&
  130. strncmp((char *)ShmPsuData->PsuGroup[group].PsuModule[index].SerialNumber, serial_number, 7) == 0)
  131. {
  132. isNewPsu = false;
  133. }
  134. }
  135. if (isNewPsu)
  136. {
  137. ShmPsuData->SystemPresentPsuQuantity++;
  138. printf("get psu********id = %d, group = %d \n", ShmPsuData->SystemPresentPsuQuantity, group);
  139. if (ShmPsuData->Work_Step >= _TEST_LINE_STEP && ShmPsuData->Work_Step <= _TEST_COMPLETE)
  140. {
  141. // 已經進入火線上的驗證動作
  142. ShmPsuData->NeedBackTest = YES;
  143. }
  144. else if (ShmPsuData->Work_Step == _WORK_CHARGING)
  145. {
  146. // 一旦進入火線,分配一個不會用到的給該模塊
  147. group++;
  148. }
  149. ShmPsuData->PsuGroup[group].PsuModule[ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity].Address = ShmPsuData->SystemPresentPsuQuantity;
  150. ShmPsuData->PsuGroup[group].PsuModule[ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity].PhysicalID = phy_id;
  151. ShmPsuData->PsuGroup[group].PsuModule[ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity].AssignID = (group >> 6) + ShmPsuData->SystemPresentPsuQuantity;
  152. strcpy((char *)ShmPsuData->PsuGroup[group].PsuModule[ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity].SerialNumber, serial_number);
  153. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity++;
  154. PsuAddressAssignment(phy_id, serial_number, ShmPsuData->SystemPresentPsuQuantity, group);
  155. if (ShmPsuData->Work_Step != _WORK_CHARGING)
  156. {
  157. ShmPsuData->GroupCount = group + 1;
  158. }
  159. }
  160. }
  161. }
  162. void SaveStatusCallback(byte group, byte address, int alarm, int fault)
  163. {
  164. //EVSE
  165. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  166. {
  167. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  168. {
  169. ShmPsuData->PsuGroup[group].PsuModule[index].AlarmCode = alarm;
  170. ShmPsuData->PsuGroup[group].PsuModule[index].FaultCode = fault;
  171. break;
  172. }
  173. }
  174. }
  175. void SaveAlarmNotifyCallback(byte group, byte address, byte *alarm)
  176. {
  177. //EVSE
  178. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  179. {
  180. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  181. {
  182. printf("PSU Alarm = %s \n", alarm);
  183. //ShmPsuData->PsuGroup[group].PsuModule[index].AlarmCode = alarm;
  184. break;
  185. }
  186. }
  187. }
  188. void SaveFaultNotifyCallback(byte group, byte address, byte *fault)
  189. {
  190. //EVSE
  191. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  192. {
  193. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  194. {
  195. printf("PSU Fault = %s \n", fault);
  196. //ShmPsuData->PsuGroup[group].PsuModule[index].FaultCode = fault;
  197. break;
  198. }
  199. }
  200. }
  201. void SaveFirmwareVersion(byte group, byte address, unsigned char packageIndex, unsigned char type , unsigned char *data)
  202. {
  203. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  204. {
  205. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  206. {
  207. if (packageIndex == 0 || packageIndex == 1)
  208. strncpy((char *)ShmPsuData->PsuGroup[group].PsuModule[index].FwVersion + (packageIndex * 6), (char *)data, 6);
  209. else
  210. strncpy((char *)ShmPsuData->PsuGroup[group].PsuModule[index].FwVersion + (packageIndex * 6), (char *)data, 4);
  211. }
  212. }
  213. if (packageIndex == 1)
  214. {
  215. char string[3];
  216. memcpy(string, (data + 2), 2);
  217. string[2] = '\0';
  218. // if (maxChargingVol != 0)
  219. // {
  220. // chargingInfo[group]->MaximumChargingVoltage = maxChargingVol;
  221. // }
  222. // else
  223. {
  224. if (strcmp(string, "50") == 0)
  225. chargingInfo[group]->MaximumChargingVoltage = 5000;
  226. else if (strcmp(string, "70") == 0)
  227. chargingInfo[group]->MaximumChargingVoltage = 7000;
  228. else if (strcmp(string, "75") == 0)
  229. chargingInfo[group]->MaximumChargingVoltage = 7500;
  230. else if (strcmp(string, "80") == 0)
  231. chargingInfo[group]->MaximumChargingVoltage = 8000;
  232. else if (strcmp(string, "95") == 0)
  233. chargingInfo[group]->MaximumChargingVoltage = 9500;
  234. else if (strcmp(string, "A0") == 0)
  235. chargingInfo[group]->MaximumChargingVoltage = 10000;
  236. else if (strcmp(string, "C0") == 0)
  237. chargingInfo[group]->MaximumChargingVoltage = 12000;
  238. else if (strcmp(string, "F0") == 0)
  239. chargingInfo[group]->MaximumChargingVoltage = 15000;
  240. }
  241. //printf("index = %d, max vol = %f \n", group, chargingInfo[group]->MaximumChargingVoltage);
  242. }
  243. }
  244. void SaveFanSpeedCallback(byte group, byte address, byte fan1, byte fan2, byte fan3, byte fan4)
  245. {
  246. //EVSE
  247. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  248. {
  249. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  250. {
  251. ShmPsuData->PsuGroup[group].PsuModule[index].FanSpeed_1 = fan1;
  252. ShmPsuData->PsuGroup[group].PsuModule[index].FanSpeed_2 = fan2;
  253. ShmPsuData->PsuGroup[group].PsuModule[index].FanSpeed_3 = fan3;
  254. ShmPsuData->PsuGroup[group].PsuModule[index].FanSpeed_4 = fan4;
  255. break;
  256. }
  257. }
  258. }
  259. void SaveTemperatureCallback(byte group, byte address, char cri_temp1, char cri_temp2, char cri_temp3, char ex_temp, char in_temp1, char in_temp2, char out_temp)
  260. {
  261. //EVSE
  262. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  263. {
  264. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  265. {
  266. ShmPsuData->PsuGroup[group].PsuModule[index].CriticalTemp1 = cri_temp1;
  267. ShmPsuData->PsuGroup[group].PsuModule[index].CriticalTemp2 = cri_temp2;
  268. ShmPsuData->PsuGroup[group].PsuModule[index].CriticalTemp3 = cri_temp3;
  269. ShmPsuData->PsuGroup[group].PsuModule[index].ExletTemp = ex_temp;
  270. ShmPsuData->PsuGroup[group].PsuModule[index].InletTemp_1 = in_temp1;
  271. ShmPsuData->PsuGroup[group].PsuModule[index].InletTemp_2 = in_temp2;
  272. ShmPsuData->PsuGroup[group].PsuModule[index].OutletTemp = out_temp;
  273. break;
  274. }
  275. }
  276. }
  277. void SavePresentInputVoltageCallback(byte group, byte address, byte vol_type, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  278. {
  279. //EVSE
  280. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  281. {
  282. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  283. {
  284. ShmPsuData->PsuGroup[group].PsuModule[index].InputVoltage_Type = vol_type;
  285. ShmPsuData->PsuGroup[group].PsuModule[index].InputVoltageL1 = vol1;
  286. ShmPsuData->PsuGroup[group].PsuModule[index].InputVoltageL2 = vol2;
  287. ShmPsuData->PsuGroup[group].PsuModule[index].InputVoltageL3 = vol3;
  288. break;
  289. }
  290. }
  291. }
  292. // 模塊輸出的電壓電流
  293. void SavePresentOutputCallback(byte group, byte address, unsigned short out_vol, unsigned short out_cur)
  294. {
  295. unsigned short outputVol = 0;
  296. unsigned short outputCur = 0;
  297. bool isChange = false;
  298. // PSU
  299. for (int index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  300. {
  301. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  302. {
  303. ShmPsuData->PsuGroup[group].PsuModule[index].PresentOutputVoltage = out_vol;
  304. ShmPsuData->PsuGroup[group].PsuModule[index].PresentOutputCurrent = out_cur;
  305. isChange = true;
  306. }
  307. if (ShmPsuData->PsuGroup[group].PsuModule[index].PresentOutputVoltage > outputVol)
  308. outputVol = ShmPsuData->PsuGroup[group].PsuModule[index].PresentOutputVoltage;
  309. outputCur += ShmPsuData->PsuGroup[group].PsuModule[index].PresentOutputCurrent;
  310. }
  311. if (isChange)
  312. {
  313. // PSU Group
  314. // 電壓
  315. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outputVol;
  316. // 電流
  317. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outputCur;
  318. //EVSE - 槍端的輸出電壓
  319. chargingInfo[group]->PresentChargingVoltage = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  320. //EVSE - 槍端的輸出電流
  321. chargingInfo[group]->PresentChargingCurrent = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  322. }
  323. printf("GroupPresentOutputVoltage = %d \n", ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage);
  324. printf("GroupPresentOutputCurrent = %d \n", ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent);
  325. }
  326. void SaveAvailableCapCallback(byte group, byte address, unsigned short able_power, unsigned short able_cur)
  327. {
  328. unsigned int power = 0;
  329. unsigned int current = 0;
  330. bool isChange = false;
  331. //printf("SaveAvailableCapCallback : index = %d, cur = %d \n", address, able_cur);
  332. // PSU
  333. for (int index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  334. {
  335. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  336. {
  337. ShmPsuData->PsuGroup[group].PsuModule[index].AvailablePower = able_power;
  338. ShmPsuData->PsuGroup[group].PsuModule[index].AvailableCurrent = able_cur;
  339. isChange = true;
  340. }
  341. power += ShmPsuData->PsuGroup[group].PsuModule[index].AvailablePower;
  342. current += ShmPsuData->PsuGroup[group].PsuModule[index].AvailableCurrent;
  343. }
  344. if (isChange)
  345. {
  346. // PSU Group
  347. // Available Power
  348. ShmPsuData->PsuGroup[group].GroupAvailablePower = power;
  349. // Available Current
  350. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = current;
  351. //EVSE
  352. if (ShmPsuData->PsuGroup[group].GroupAvailablePower < chargingInfo[group]->AvailableChargingPower)
  353. {
  354. chargingInfo[group]->CurrentDerating = 0x01;
  355. printf("Power derating old = %f, new = %d ***************************************************************** \n",
  356. chargingInfo[group]->AvailableChargingPower,
  357. ShmPsuData->PsuGroup[group].GroupAvailablePower);
  358. }
  359. chargingInfo[group]->AvailableChargingCurrent = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  360. chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  361. }
  362. }
  363. void SavePresentInputCurrentCallback(byte group, byte address, unsigned short in_cur1, unsigned short in_cur2, unsigned short in_cur3)
  364. {
  365. //EVSE
  366. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  367. {
  368. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  369. {
  370. ShmPsuData->PsuGroup[group].PsuModule[index].InputCurrentL1 = in_cur1;
  371. ShmPsuData->PsuGroup[group].PsuModule[index].InputCurrentL2 = in_cur2;
  372. ShmPsuData->PsuGroup[group].PsuModule[index].InputCurrentL3 = in_cur3;
  373. break;
  374. }
  375. }
  376. }
  377. void SaveHardwareVersion(byte group, byte address, int hw_ver)
  378. {
  379. //EVSE
  380. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  381. {
  382. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  383. {
  384. ShmPsuData->PsuGroup[group].PsuModule[index].FwVersion[0] = (hw_ver >> 24) & 0xFF;
  385. ShmPsuData->PsuGroup[group].PsuModule[index].FwVersion[1] = (hw_ver >> 16) & 0xFF;
  386. ShmPsuData->PsuGroup[group].PsuModule[index].FwVersion[2] = (hw_ver >> 8) & 0xFF;
  387. ShmPsuData->PsuGroup[group].PsuModule[index].FwVersion[3] = hw_ver & 0xFF;
  388. break;
  389. }
  390. }
  391. }
  392. void SaveStatusNotifyCallback(byte group, byte address, byte st_machine, unsigned short out_vol, unsigned short out_cur)
  393. {
  394. //EVSE
  395. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  396. {
  397. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  398. {
  399. ShmPsuData->PsuGroup[group].PsuModule[index].StateMachine = st_machine;
  400. //ShmPsuData->PsuGroup[group].PsuModule[index].PresentOutputVoltage = out_vol;
  401. //ShmPsuData->PsuGroup[group].PsuModule[index].PresentOutputCurrent = out_cur;
  402. //printf("psu state = %d, vol = %d, cur = %d \n", st_machine, out_vol, out_cur);
  403. break;
  404. }
  405. }
  406. }
  407. void GetSerialNumberCallback(byte group, byte address, unsigned char packageIndex, unsigned char *data)
  408. {
  409. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  410. {
  411. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  412. {
  413. strcpy((char *)ShmPsuData->PsuGroup[group].PsuModule[index].SerialNumber + (packageIndex * 7), (char *)data);
  414. break;
  415. }
  416. }
  417. }
  418. void GetOutputPowerSwitchStatusCallback(byte group, byte address, unsigned char value)
  419. {
  420. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  421. {
  422. if (ShmPsuData->PsuGroup[group].PsuModule[index].Address == address)
  423. {
  424. //printf("PowerSwitch = %d, group = %d, address = %d \n", value, group, address);
  425. ShmPsuData->PsuGroup[group].PsuModule[index].OutputPowerSwitch = value;
  426. break;
  427. }
  428. }
  429. }
  430. //==========================================
  431. // Init all share memory
  432. //==========================================
  433. int InitShareMemory()
  434. {
  435. int result = PASS;
  436. int MeterSMId;
  437. //creat ShmSysConfigAndInfo
  438. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  439. {
  440. #ifdef SystemLogMessage
  441. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG %d \n");
  442. #endif
  443. result = FAIL;
  444. }
  445. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  446. {
  447. #ifdef SystemLogMessage
  448. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG \n");
  449. #endif
  450. result = FAIL;
  451. }
  452. else
  453. {}
  454. //creat ShmStatusCodeData
  455. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  456. {
  457. #ifdef SystemLogMessage
  458. DEBUG_ERROR("shmget ShmStatusCodeData NG \n");
  459. #endif
  460. result = FAIL;
  461. }
  462. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  463. {
  464. #ifdef SystemLogMessage
  465. DEBUG_ERROR("shmat ShmStatusCodeData NG \n");
  466. #endif
  467. result = FAIL;
  468. }
  469. else
  470. {}
  471. //creat ShmPsuData
  472. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  473. {
  474. #ifdef SystemLogMessage
  475. DEBUG_ERROR("shmget ShmPsuData NG \n");
  476. #endif
  477. result = FAIL;
  478. }
  479. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  480. {
  481. #ifdef SystemLogMessage
  482. DEBUG_ERROR("shmat ShmPsuData NG \n");
  483. #endif
  484. result = FAIL;
  485. }
  486. memset(ShmPsuData,0,sizeof(struct PsuData));
  487. return result;
  488. }
  489. //================================================
  490. // Main process
  491. //================================================
  492. void InitialPsuData()
  493. {
  494. ShmPsuData->SystemPresentPsuQuantity = 0;
  495. for (byte _groupCount = 0; _groupCount < ARRAY_SIZE(ShmPsuData->PsuGroup); _groupCount++)
  496. {
  497. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  498. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  499. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  500. }
  501. }
  502. void Initialization()
  503. {
  504. bool isPass = false;
  505. while(!isPass)
  506. {
  507. isPass = true;
  508. for (byte _index = 0; _index < _gunCount; _index++)
  509. {
  510. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  511. {
  512. DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
  513. isPass = false;
  514. break;
  515. }
  516. }
  517. }
  518. }
  519. int main(void)
  520. {
  521. printf("Psu Task boot .... \n");
  522. if(InitShareMemory() == FAIL)
  523. {
  524. #ifdef SystemLogMessage
  525. DEBUG_ERROR("InitShareMemory NG\n");
  526. #endif
  527. if(ShmStatusCodeData != NULL)
  528. {
  529. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  530. }
  531. sleep(5);
  532. return 0;
  533. }
  534. printf("InitShareMemory OK\n");
  535. // register callback function
  536. GetPsuAddressReq(&GetPsuRequestCallback);
  537. RefreshStatus(&SaveStatusCallback);
  538. RefreshFanSpeed(&SaveFanSpeedCallback);
  539. RefreshTemp(&SaveTemperatureCallback);
  540. RefreshInputVol(&SavePresentInputVoltageCallback);
  541. RefreshGetOutput(&SavePresentOutputCallback);
  542. RefreshAvailableCap(&SaveAvailableCapCallback);
  543. RefreshInputCur(&SavePresentInputCurrentCallback);
  544. RefreshAlarmNotify(&SaveAlarmNotifyCallback);
  545. RefreshFaultNotify(&SaveFaultNotifyCallback);
  546. RefreshStatusNotify(&SaveStatusNotifyCallback);
  547. RefreshSerialNumber(&GetSerialNumberCallback);
  548. RefreshOutputPowerSwitch(&GetOutputPowerSwitchStatusCallback);
  549. RefreshFWVersion(&SaveFirmwareVersion);
  550. //RefreshHWVersion(&SaveHardwareVersion);
  551. // initial object
  552. InitialPsuData();
  553. Initialization();
  554. libInitialize = InitialCommunication();
  555. byte priorityLow = 1;
  556. //main loop
  557. while (libInitialize)
  558. {
  559. // 斷電狀態
  560. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  561. {
  562. InitialPsuData();
  563. sleep(1);
  564. ShmPsuData->Work_Step = ASSIGN_START;
  565. continue;
  566. }
  567. // update psu fw req
  568. // if(psu update req ?)
  569. // {
  570. //
  571. // continue;
  572. // }
  573. // 自檢失敗
  574. if (ShmPsuData->Work_Step == _NO_WORKING)
  575. {
  576. DEBUG_ERROR("(PSU) self test fail. \n");
  577. printf("(PSU) self test fail. \n");
  578. sleep(5);
  579. }
  580. switch(ShmPsuData->Work_Step)
  581. {
  582. // 開始分配模塊的 Group & Address
  583. case ASSIGN_START:
  584. {
  585. gettimeofday(&_id_assign_time, NULL);
  586. ShmPsuData->Work_Step = ASSIGN_COMP;
  587. }
  588. break;
  589. case ASSIGN_COMP:
  590. {
  591. if (priorityLow == 1)
  592. {
  593. // 一旦被分配好的模塊,將透過以下指令保持通訊,並取得一些資訊
  594. for (byte psuIndex = 0; psuIndex < ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity; psuIndex++)
  595. {
  596. if (ShmPsuData->PsuGroup[0].PsuModule[psuIndex].Address == NONE_CARE_ADDRESS)
  597. continue;
  598. GetStatus(0, ShmPsuData->PsuGroup[0].PsuModule[psuIndex].Address);
  599. usleep(50000);
  600. if (strlen((char *)ShmPsuData->PsuGroup[0].PsuModule[psuIndex].FwVersion) == 0 &&
  601. ShmPsuData->PsuGroup[0].PsuModule[psuIndex].FwVersion[0] == '\0')
  602. {
  603. GetFwVersion(0, ShmPsuData->PsuGroup[0].PsuModule[psuIndex].Address, 0x02);
  604. usleep(50000);
  605. }
  606. if (strlen((char *)ShmPsuData->PsuGroup[0].PsuModule[psuIndex].SerialNumber) == 0 &&
  607. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] == '\0')
  608. {
  609. GetSerialNumber(0, ShmPsuData->PsuGroup[0].PsuModule[psuIndex].Address);
  610. usleep(50000);
  611. }
  612. }
  613. }
  614. priorityLow >= 20 ? priorityLow = 1 : priorityLow++;
  615. // 最多等待 15 秒 (在這個時間內的模塊都將被分配好並進入下一個狀態)
  616. if (GetTimeoutValue(_id_assign_time) >= 15000000)
  617. {
  618. ShmPsuData->Work_Step = ENABLE_POW;
  619. }
  620. }
  621. break;
  622. case ENABLE_POW:
  623. {
  624. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  625. {
  626. // 電樁在 Booting 的狀態 - 自檢
  627. ShmPsuData->Work_Step = _TEST_LINE_STEP;
  628. }
  629. else
  630. {
  631. ShmPsuData->Work_Step = _WORK_CHARGING;
  632. gettimeofday(&_workModePriority_time, NULL);
  633. }
  634. }
  635. break;
  636. case _TEST_LINE_STEP:
  637. {
  638. // 自檢開始 :
  639. // 1. 取得模塊火線上的位置
  640. // (假設有兩個模塊對應到兩把槍,線路上一定是一個模塊對應到一把槍,需要找出對應的槍與模塊 Index)
  641. printf("cur total psu count = %d \n", ShmPsuData->SystemPresentPsuQuantity);
  642. if (ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity <= 0)
  643. {
  644. sleep(1);
  645. continue;
  646. }
  647. // 對整個 Group 保持通訊
  648. bool isFind = false;
  649. while(ShmPsuData->Work_Step != _NO_WORKING &&
  650. _curCheckPsuIndexForFireLine < ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity)
  651. {
  652. GetStatus(0, NONE_CARE_ADDRESS);
  653. usleep(50000);
  654. GetAvailableCap(0, NONE_CARE_ADDRESS, getAvailableCapOffset);
  655. usleep(50000);
  656. EnableOutputPower(0, NONE_CARE_ADDRESS, OUTPUT_POWER_SWITCH_ON);
  657. usleep(50000);
  658. if (ShmPsuData->NeedBackTest == YES)
  659. {
  660. ShmPsuData->NeedBackTest = NO;
  661. _curCheckPsuIndexForFireLine = 0x00;
  662. }
  663. if (isFind)
  664. {
  665. GetPresentOutput(0, ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].Address);
  666. usleep(50000);
  667. //printf("stop vor = %d \n", ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].PresentOutputVoltage);
  668. if (ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].PresentOutputVoltage <= 1000)
  669. {
  670. // 檢查下一個
  671. _curCheckPsuIndexForFireLine++;
  672. isFind = false;
  673. }
  674. SetPresentOutput(0, ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].Address,
  675. ZERO_VOL, ZERO_CUR, chargingInfo[0]->AvailableChargingCurrent);
  676. usleep(50000);
  677. }
  678. else
  679. {
  680. printf("AvailableCurrent = %d \n", ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].AvailableCurrent);
  681. if (ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].AvailableCurrent > 0)
  682. {
  683. if (ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].PresentOutputVoltage == 0)
  684. {
  685. //printf("set output vol = %d, cur = %d \n", SELF_TEST_VOL, SELF_TEST_CUR);
  686. SetPresentOutput(0, ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].Address,
  687. SELF_TEST_VOL, SELF_TEST_CUR, chargingInfo[0]->AvailableChargingCurrent);
  688. usleep(50000);
  689. }
  690. if(!isCheckOutputTimeStart)
  691. {
  692. gettimeofday(&_chk_output_time, NULL);
  693. isCheckOutputTimeStart = true;
  694. }
  695. else
  696. {
  697. // 如果五秒內火線上都沒有偵測到電壓,則代表異常
  698. if (GetTimeoutValue(_chk_output_time) >= 5000000)
  699. {
  700. // 自檢失敗
  701. printf("self test timeout \n");
  702. EnableOutputPower(0, NONE_CARE_ADDRESS, OUTPUT_POWER_SWITCH_OFF);
  703. usleep(50000);
  704. ShmPsuData->Work_Step = _NO_WORKING;
  705. continue;
  706. }
  707. }
  708. for (byte gunIndex = 0; gunIndex < _gunCount; gunIndex ++)
  709. {
  710. GetPresentOutput(0, ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].Address);
  711. usleep(50000);
  712. printf("Cur psu output voltage = %d \n", ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].PresentOutputVoltage);
  713. printf("Fire voltage = %f \n", chargingInfo[gunIndex]->FuseChargingVoltage);
  714. // 該模組的輸出電壓與火線上的電壓一致
  715. if (chargingInfo[gunIndex]->FuseChargingVoltage >= 1200 &&
  716. ((chargingInfo[gunIndex]->FuseChargingVoltage >= ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].PresentOutputVoltage - 300) &&
  717. (chargingInfo[gunIndex]->FuseChargingVoltage <= ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].PresentOutputVoltage + 300)))
  718. {
  719. // 找到火線上的電壓了,這邊紀錄火線是紀錄屬於哪一把槍的火線
  720. if (_curCheckPsuIndexForFireLine < ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity)
  721. {
  722. // 紀錄當前 PSU 是哪個火線上的
  723. recordPsuData[_curCheckPsuIndexForFireLine]._fire_index = gunIndex;
  724. recordPsuData[_curCheckPsuIndexForFireLine]._phy_addr = ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].PhysicalID;
  725. strcpy(recordPsuData[_curCheckPsuIndexForFireLine]._serial_num, (char *)ShmPsuData->PsuGroup[0].PsuModule[_curCheckPsuIndexForFireLine].SerialNumber);
  726. printf("Find Fire Line Number end~~~~~~~~~~~~~~~ = %d \n", gunIndex);
  727. usleep(100000);
  728. isCheckOutputTimeStart = false;
  729. isFind = true;
  730. break;
  731. }
  732. }
  733. }
  734. }
  735. }
  736. usleep(100000);
  737. }
  738. if (ShmPsuData->Work_Step != _NO_WORKING)
  739. ShmPsuData->Work_Step = _TEST_POWER_STEP;
  740. EnableOutputPower(0, NONE_CARE_ADDRESS, OUTPUT_POWER_SWITCH_OFF);
  741. usleep(50000);
  742. }
  743. break;
  744. case _TEST_POWER_STEP:
  745. {
  746. // 2. 取得火線上最大輸出能量
  747. if(!_chkTotalCapStart)
  748. {
  749. _chkTotalCapStart = true;
  750. gettimeofday(&_chk_cap_time, NULL);
  751. }
  752. for (byte groupIndex = 0; groupIndex < ShmPsuData->GroupCount; groupIndex++)
  753. {
  754. GetStatus(groupIndex, NONE_CARE_ADDRESS);
  755. usleep(50000);
  756. GetAvailableCap(groupIndex, NONE_CARE_ADDRESS, getAvailableCapOffset);
  757. usleep(50000);
  758. }
  759. if (GetTimeoutValue(_chk_cap_time) >= 2000000)
  760. {
  761. printf("AvailableChargingCurrent = %f, AvailableChargingPower = %f \n",
  762. chargingInfo[0]->AvailableChargingCurrent, chargingInfo[0]->AvailableChargingPower);
  763. for (byte index = 0; index < ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity; index++)
  764. {
  765. printf("index = %d, fire index = %d, phy addr = %d \n",
  766. index, recordPsuData[index]._fire_index, recordPsuData[index]._phy_addr);
  767. }
  768. ShmPsuData->Work_Step = _TEST_COMPLETE;
  769. }
  770. }
  771. break;
  772. case _TEST_COMPLETE:
  773. {
  774. // PSU 自檢結束
  775. priorityLow = 1;
  776. sleep(1);
  777. }
  778. break;
  779. case _WORK_CHARGING:
  780. {
  781. // 一旦自檢結束後,重新分配模塊結束即會跳到該狀態,等待輸出
  782. int time = GetTimeoutValue(_workModePriority_time) / 1000;
  783. //printf("cur total psu count = %d \n", ShmPsuData->SystemPresentPsuQuantity);
  784. for (byte groupIndex = 0; groupIndex < ShmPsuData->GroupCount; groupIndex++)
  785. {
  786. if (time > 5000)
  787. {
  788. // GetStatus(groupIndex, NONE_CARE_ADDRESS);
  789. // usleep(50000);
  790. GetAvailableCap(groupIndex, NONE_CARE_ADDRESS, getAvailableCapOffset);
  791. usleep(50000);
  792. gettimeofday(&_workModePriority_time, NULL);
  793. }
  794. GetPresentOutput(groupIndex, NONE_CARE_ADDRESS);
  795. usleep(50000);
  796. if (carReqVol != chargingInfo[groupIndex]->EvBatterytargetVoltage)
  797. {
  798. carReqVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  799. DEBUG_INFO("ev need vol = %f \n", chargingInfo[groupIndex]->EvBatterytargetVoltage);
  800. }
  801. if (carReqCur != chargingInfo[groupIndex]->EvBatterytargetCurrent)
  802. {
  803. carReqCur = chargingInfo[groupIndex]->EvBatterytargetCurrent;
  804. DEBUG_INFO("ev need cur = %f \n", chargingInfo[groupIndex]->EvBatterytargetCurrent);
  805. }
  806. if (evseOutVol != chargingInfo[groupIndex]->FireChargingVoltage)
  807. {
  808. evseOutVol = chargingInfo[groupIndex]->FireChargingVoltage;
  809. printf("evse output vol = %f \n", chargingInfo[groupIndex]->FireChargingVoltage);
  810. }
  811. if (evseOutCur != chargingInfo[groupIndex]->PresentChargingCurrent)
  812. {
  813. evseOutCur = chargingInfo[groupIndex]->PresentChargingCurrent;
  814. printf("evse output cur = %f \n", chargingInfo[groupIndex]->PresentChargingCurrent);
  815. }
  816. // 針對各槍當前狀態,傳送需要回傳的資料指令
  817. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  818. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  819. {
  820. if (ShmPsuData->PsuGroup[groupIndex].GroupAvailableCurrent > 0)
  821. {
  822. EnableOutputPower(groupIndex, NONE_CARE_ADDRESS, OUTPUT_POWER_SWITCH_ON);
  823. usleep(50000);
  824. float targetVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  825. float targetCur = chargingInfo[groupIndex]->EvBatterytargetCurrent;
  826. if (targetVol != 0)
  827. {
  828. if (targetCur <= 10)
  829. targetCur = 10;
  830. }
  831. else
  832. {
  833. targetVol = 0;
  834. targetCur = 0;
  835. }
  836. //printf("Charging : Set Present Output V = %f, C = %f \n",
  837. // chargingInfo[groupIndex]->EvBatterytargetVoltage, targetCur);
  838. // 該充電槍的目標電壓與目標電流
  839. SetPresentOutput(groupIndex, NONE_CARE_ADDRESS,
  840. targetVol,
  841. targetCur,
  842. chargingInfo[groupIndex]->AvailableChargingCurrent);
  843. usleep(50000);
  844. }
  845. }
  846. else if (chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  847. chargingInfo[groupIndex]->SystemStatus <= S_COMPLETE)
  848. {
  849. SetPresentOutput(groupIndex, NONE_CARE_ADDRESS, ZERO_VOL, ZERO_CUR, chargingInfo[groupIndex]->AvailableChargingCurrent);
  850. usleep(50000);
  851. // if (chargingInfo[groupIndex]->RelayK1K2Status == NO)
  852. // {
  853. // //printf("DD OFF ---------------------------------------------------\n");
  854. // // EnableOutputPower(groupIndex, NONE_CARE_ADDRESS, OUTPUT_POWER_SWITCH_OFF);
  855. // usleep(50000);
  856. // }
  857. }
  858. }
  859. priorityLow >= 200 ? priorityLow = 1 : priorityLow++;
  860. break;
  861. }
  862. }
  863. usleep(45000);
  864. }
  865. return FAIL;
  866. }