Module_PsuComm.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  1. #include "Module_PsuComm.h"
  2. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  3. #define PASS 1
  4. #define FAIL -1
  5. #define YES 1
  6. #define NO 0
  7. #define DERATING_COUNT 30
  8. #define DERATING_GAP 30
  9. #define ELEMENT_NOT_FIND 255
  10. #define CHK_VOL_RANGE 50
  11. #define CHK_CUR_RANGE 10
  12. #define DERATING_RANGE 100
  13. #define ZERO_CURRENT 10 // 該值須保持最小為 1A
  14. #define ZERO_VOLTAGE 50
  15. #define STOP_CURRENT 30
  16. #define PSU_MIN_CUR 1000
  17. #define PSU_MIN_VOL 1500
  18. #define PRE_CHARG_STEP_CUR 30
  19. #define PRE_CHARG_RANGE 50
  20. #define EQUAL 0
  21. #define CMD_DELAY_TIME 25000
  22. #define LOG_VOL_GAP 50
  23. #define LOG_CUR_GAP 5
  24. #define PSU_MIN_OUTPUT_CUR 1
  25. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  26. struct StatusCodeData *ShmStatusCodeData;
  27. struct PsuData *ShmPsuData;
  28. struct DcCommonInformation *ShmDcCommonData;
  29. bool libInitialize = false;
  30. byte getAvailableCapOffset = 5;
  31. byte deratingKeepCount = 0;
  32. byte psuCmdSeq = _PSU_CMD_CAP;
  33. byte startModuleFlag = false;
  34. float chargingOutputLogInfo[2][4];
  35. void PRINTF_FUNC(char *string, ...);
  36. int StoreLogMsg(const char *fmt, ...);
  37. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  38. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  39. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  40. unsigned long GetTimeoutValue(struct timeval _sour_time);
  41. unsigned long GetTimeoutValue(struct timeval _sour_time)
  42. {
  43. struct timeval _end_time;
  44. gettimeofday(&_end_time, NULL);
  45. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  46. }
  47. int StoreLogMsg(const char *fmt, ...)
  48. {
  49. char Buf[4096+256];
  50. char buffer[4096];
  51. va_list args;
  52. struct timeb SeqEndTime;
  53. struct tm *tm;
  54. va_start(args, fmt);
  55. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  56. va_end(args);
  57. memset(Buf,0,sizeof(Buf));
  58. ftime(&SeqEndTime);
  59. SeqEndTime.time = time(NULL);
  60. tm=localtime(&SeqEndTime.time);
  61. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  62. {
  63. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  64. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  65. printf("%s \n", Buf);
  66. }
  67. else
  68. {
  69. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s",
  70. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  71. buffer,
  72. tm->tm_year+1900,tm->tm_mon+1,
  73. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  74. system(Buf);
  75. }
  76. return rc;
  77. }
  78. void PRINTF_FUNC(char *string, ...)
  79. {
  80. va_list args;
  81. char buffer[4096];
  82. va_start(args, string);
  83. vsnprintf(buffer, sizeof(buffer), string, args);
  84. va_end(args);
  85. DEBUG_INFO("%s ", buffer);
  86. }
  87. //=================================
  88. // Common routine
  89. //=================================
  90. size_t FindIndex(const int a[], size_t size, int value, byte group)
  91. {
  92. size_t index = 0;
  93. while ( index < size && a[index] != value ) ++index;
  94. return (index == size ? ELEMENT_NOT_FIND : group);
  95. }
  96. byte FindTargetGroup(byte address)
  97. {
  98. byte _group = ELEMENT_NOT_FIND;
  99. if (ShmPsuData->GroupCount == 1)
  100. _group = 0;
  101. else
  102. {
  103. _group = FindIndex(connector_1, ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity, address, 0);
  104. if (_group == ELEMENT_NOT_FIND)
  105. _group = FindIndex(connector_2, ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity, address, 1);
  106. }
  107. return _group;
  108. }
  109. bool IsOverModuleCount(byte count)
  110. {
  111. bool result = false;
  112. if (count >= ShmPsuData->SystemPresentPsuQuantity)
  113. result = true;
  114. return result;
  115. }
  116. //=================================
  117. // Save data to share memory Function
  118. //=================================
  119. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  120. {
  121. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  122. {
  123. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  124. {
  125. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  126. return true;
  127. }
  128. }
  129. for (byte index = 0; index < CCS_QUANTITY; index++)
  130. {
  131. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  132. {
  133. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  134. return true;
  135. }
  136. }
  137. for (byte index = 0; index < GB_QUANTITY; index++)
  138. {
  139. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  140. {
  141. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. //=================================
  148. // Alarm code mapping to share memory Function
  149. //=================================
  150. // 檢查 Byte 中某個 Bit 的值
  151. // _byte : 欲改變的 byte
  152. // _bit : 該 byte 的第幾個 bit
  153. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  154. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  155. {
  156. return ( _byte & mask_table[_bit] ) != 0x00;
  157. }
  158. void AbnormalStopAnalysis(byte gun_index, int errCode)
  159. {
  160. for (char i = 0; i < 4; i++)
  161. {
  162. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  163. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  164. {
  165. if(DetectBitValue(byteIndex , bitIndex) == 1)
  166. {
  167. switch(i)
  168. {
  169. case 0:
  170. {
  171. if (bitIndex == 2)
  172. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFuseBurnOut = YES;
  173. else if (bitIndex == 3)
  174. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcAndDcdcCommFault = YES;
  175. else if (bitIndex == 6)
  176. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageUnbalance = YES;
  177. else if (bitIndex == 7)
  178. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusOverVoltage = YES;
  179. }
  180. break;
  181. case 1:
  182. {
  183. if (bitIndex == 0)
  184. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageAbnormal = YES;
  185. else if (bitIndex == 1)
  186. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  187. else if (bitIndex == 2)
  188. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  189. else if (bitIndex == 3)
  190. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusUnderVoltage = YES;
  191. else if (bitIndex == 4)
  192. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = YES;
  193. else if (bitIndex == 6)
  194. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  195. }
  196. break;
  197. case 2:
  198. {
  199. if (bitIndex == 0)
  200. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCommunicationFail = YES;
  201. else if (bitIndex == 1)
  202. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuSevereUnevenCurrent = YES;
  203. else if (bitIndex == 3)
  204. {
  205. // PFC power off
  206. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFfcSideShutDown = YES;
  207. }
  208. else if (bitIndex == 5)
  209. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFullSpeed = YES;
  210. else if (bitIndex == 6)
  211. {
  212. // DCDC power off
  213. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  214. }
  215. else if (bitIndex == 7)
  216. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  217. }
  218. break;
  219. case 3:
  220. {
  221. if (bitIndex == 0)
  222. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuTemperaturePowerLimit = YES;
  223. else if (bitIndex == 1)
  224. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuAcPowerLimit = YES;
  225. else if (bitIndex == 2)
  226. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcEepromFault = YES;
  227. else if (bitIndex == 3)
  228. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  229. else if (bitIndex == 4)
  230. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  231. else if (bitIndex == 5)
  232. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcEepromFault = YES;
  233. else if (bitIndex == 6)
  234. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  235. else if (bitIndex == 7)
  236. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = YES;
  237. }
  238. break;
  239. }
  240. }
  241. // else
  242. // {
  243. // switch (byteIndex) {
  244. // case 0: {
  245. // if (bitIndex == 0)
  246. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  247. // else if (bitIndex == 5)
  248. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  249. // }
  250. // break;
  251. // case 1: {
  252. // if (bitIndex == 1)
  253. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  254. // else if (bitIndex == 2)
  255. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  256. // else if (bitIndex == 3)
  257. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  258. // else if (bitIndex == 4)
  259. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  260. // else if (bitIndex == 5)
  261. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  262. // }
  263. // break;
  264. // case 2: {
  265. // if (bitIndex == 1)
  266. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  267. // if (bitIndex == 2)
  268. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  269. // else if (bitIndex == 3)
  270. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  271. // else if (bitIndex == 4)
  272. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  273. // else if (bitIndex == 5)
  274. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  275. // else if (bitIndex == 6)
  276. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  277. // }
  278. // break;
  279. // }
  280. // }
  281. }
  282. }
  283. }
  284. //=================================
  285. // Callback Function
  286. //=================================
  287. // no using -- GetOutputAndTempCallback
  288. void GetStatusCallback(byte group, byte SN, byte temp, int alarm)
  289. {
  290. bool isFind = false;
  291. if ((conn_1_count + conn_2_count) != ShmPsuData->SystemPresentPsuQuantity)
  292. {
  293. if (group == 0)
  294. {
  295. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  296. {
  297. if (connector_1[psuIndex] == SN)
  298. {
  299. isFind = true;
  300. break;
  301. }
  302. }
  303. if(!isFind)
  304. {
  305. connector_1[conn_1_count] = SN;
  306. conn_1_count++;
  307. }
  308. }
  309. else if (group == 1)
  310. {
  311. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  312. {
  313. if (connector_2[psuIndex] == SN)
  314. {
  315. isFind = true;
  316. break;
  317. }
  318. }
  319. if(!isFind)
  320. {
  321. connector_2[conn_2_count] = SN;
  322. conn_2_count++;
  323. }
  324. }
  325. }
  326. if ((conn_1_count + conn_2_count) == ShmPsuData->SystemPresentPsuQuantity)
  327. {
  328. // Arrangment
  329. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  330. {
  331. connector_1[psuIndex] = psuIndex;
  332. }
  333. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  334. {
  335. connector_2[psuIndex] = conn_1_count + psuIndex;
  336. }
  337. for(byte psuIndex = 0; psuIndex < conn_1_count; psuIndex++)
  338. PRINTF_FUNC("DC Left Gun - PSU Number = %d \n", connector_1[psuIndex]);
  339. for(byte psuIndex = 0; psuIndex < conn_2_count; psuIndex++)
  340. PRINTF_FUNC("DC Right Gun - PSU Number = %d \n", connector_2[psuIndex]);
  341. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  342. ShmDcCommonData->chargerType == CHARGER_TYPE_STANDARD)
  343. {
  344. // 雙槍才需要考慮是否模塊 switch 撥錯了
  345. char EvsePower[2];
  346. byte maxCount = 0;
  347. int power = 0;
  348. EvsePower[2] = '\0';
  349. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  350. {
  351. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  352. power = atoi(EvsePower);
  353. }
  354. // 超過 90 KW 或 360 KW
  355. if (power < 30 || power == 36)
  356. power *= 10;
  357. maxCount = ((power / 30) + 1) / 2;
  358. if (conn_1_count > maxCount ||
  359. conn_2_count > maxCount)
  360. {
  361. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail = YES;
  362. }
  363. }
  364. }
  365. }
  366. // no using -- GetOutputAndTempCallback End
  367. void GetModuleCountCallback(byte group, byte count)
  368. {
  369. if (group == SYSTEM_CMD)
  370. ShmPsuData->SystemPresentPsuQuantity = count;
  371. else
  372. {
  373. if (group >= ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  374. return;
  375. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  376. }
  377. }
  378. void GetMaxPowerAndCur(unsigned char mode, int ratingCur, int *pow, int *cur)
  379. {
  380. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  381. return;
  382. //unsigned short maxCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  383. unsigned short maxCurrent = ShmPsuData->SystemAvailableCurrent;
  384. //unsigned short maxPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  385. unsigned short maxPower = ShmPsuData->SystemAvailablePower;
  386. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10 != 0 &&
  387. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10 < maxCurrent)
  388. maxCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  389. if (ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10 != 0 &&
  390. ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10 < maxPower)
  391. maxPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  392. if (mode == _MAIN_CHARGING_MODE_AVER)
  393. {
  394. maxCurrent /= 2;
  395. maxPower /= 2;
  396. }
  397. if (maxPower != 0 && maxPower <= *pow)
  398. *pow = maxPower;
  399. if (maxCurrent != 0 && maxCurrent <= *cur)
  400. *cur = maxCurrent;
  401. if (ratingCur != 0 && ratingCur <= *cur)
  402. *cur = ratingCur;
  403. }
  404. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  405. {
  406. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  407. return;
  408. int _groupPower = 0, _groupCurrent = 0;
  409. byte group = FindTargetGroup(address);
  410. float _chargingVol = 0, _targetVol = 0;
  411. if (group == 1)
  412. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  413. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  414. {
  415. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  416. {
  417. if (chargingInfo[groupIndex]->EvBatteryMaxVoltage > 0)
  418. {
  419. _chargingVol = chargingInfo[groupIndex]->EvBatteryMaxVoltage;
  420. _targetVol = chargingInfo[groupIndex]->EvBatterytargetVoltage;
  421. break;
  422. }
  423. }
  424. }
  425. // PRINTF_FUNC("group = %d, DeratingChargingCurrent = %f, RealRatingPower = %d \n",
  426. // group, chargingInfo[group]->DeratingChargingCurrent, chargingInfo[group]->RealRatingPower);
  427. if (chargingInfo[group]->DeratingChargingCurrent == 0)
  428. {
  429. // 在還沒取得真正可輸出的電流前,依照 GFD 階段得到的真正 POWER / 模塊個數 / 車子電池最大電壓
  430. if (ShmPsuData->PsuGroup[group].GroupRealOutputPower > 0 && _chargingVol > 0)
  431. {
  432. // printf("GroupRealOutputPower = %d, GroupPresentPsuQuantity = %d\n",
  433. // ShmPsuData->PsuGroup[group].GroupRealOutputPower,
  434. // ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity);
  435. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent =
  436. ((ShmPsuData->PsuGroup[group].GroupRealOutputPower / ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity) * 1000 / (int)_chargingVol) * 10;
  437. // PRINTF_FUNC(" *1* AvailableCurrent = %d \n",
  438. // ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent);
  439. }
  440. else
  441. {
  442. // 注一 : 獲取模塊最大輸出能力 (忽視 Derating 狀態),所以這邊需要限制實際可輸出的電流
  443. if (ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent <= 0)
  444. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = PSU_MIN_CUR;
  445. // PRINTF_FUNC(" *2* group = %d, AvailableCurrent = %d \n",
  446. // group, ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent);
  447. }
  448. // PRINTF_FUNC("group = %d, address = %d, AvailableCurrent = %d \n",
  449. // group, address, ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent);
  450. }
  451. else
  452. {
  453. ShmPsuData->PsuGroup[group].PsuModule[address].AvailableCurrent = maxCur;
  454. }
  455. ShmPsuData->PsuGroup[group].PsuModule[address].AvailablePower = totalPow;
  456. // 總和該 Group 的可輸出電流
  457. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  458. {
  459. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[index].AvailableCurrent;
  460. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[index].AvailablePower;
  461. }
  462. // 各群得到最大輸出能力 (電流、Power)
  463. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  464. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  465. if (chargingInfo[group]->MaximumChargingVoltage != maxVol)
  466. {
  467. PRINTF_FUNC("G_%d -> Max Vol = %.1f \n", group, maxVol / 10);
  468. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  469. }
  470. int _power = 0, _current = 0, _ratingcurrent = 0, _sysRealPower = 0;
  471. bool isGetAllDeratingCurrent = true;
  472. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  473. {
  474. _power += ShmPsuData->PsuGroup[index].GroupAvailablePower;
  475. _current += ShmPsuData->PsuGroup[index].GroupAvailableCurrent;
  476. _ratingcurrent += chargingInfo[index]->DeratingChargingCurrent;
  477. _sysRealPower += ShmPsuData->PsuGroup[index].GroupRealOutputPower;
  478. if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage >= PSU_MIN_VOL &&
  479. chargingInfo[index]->DeratingChargingCurrent == 0)
  480. isGetAllDeratingCurrent = false;
  481. }
  482. // 如果不是所有群都得到 Derating current,則先不採樣該次的 ratingCurrent
  483. if (!isGetAllDeratingCurrent) _ratingcurrent = 0;
  484. // 因應注一,為避免一值改變通知車子電樁最大可輸出電流所做的限制
  485. // 而因為 rating value 一般都會小於模塊的最大可輸出電流
  486. // 所以如果該值大於在注一所限制的輸出電流,則以此值為主
  487. if (_ratingcurrent != 0) _current = _ratingcurrent;
  488. //printf("=============== _current ==================== %d \n", _current);
  489. //printf("=============== _ratingcurrent ==================== %d \n", _ratingcurrent);
  490. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  491. {
  492. ShmPsuData->SystemAvailableCurrent = _current;
  493. ShmPsuData->SystemAvailablePower = _power;
  494. }
  495. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER ||
  496. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  497. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  498. {
  499. int halfPow = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  500. int halfCur = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  501. int ratingCur = chargingInfo[group]->DeratingChargingCurrent;
  502. int gpRealPow = ShmPsuData->PsuGroup[group].GroupRealOutputPower;
  503. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  504. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  505. {
  506. if (chargingInfo[group]->DividChargingCurrent == 0)
  507. return;
  508. else
  509. {
  510. halfCur = chargingInfo[group]->DividChargingCurrent;
  511. ratingCur = 0;
  512. }
  513. }
  514. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_AVER, ratingCur, &halfPow, &halfCur);
  515. // if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  516. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A))
  517. // {
  518. // chargingInfo[group]->AvailableChargingCurrent = DERATING_RANGE;
  519. // chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  520. // }
  521. // else
  522. {
  523. // 以下狀況 -> 槍資訊中的最大輸出能力,為該群的輸出能力
  524. // 1. 如不是最大充
  525. // 2. 智能切換成均充過程
  526. chargingInfo[group]->AvailableChargingCurrent = halfCur;
  527. chargingInfo[group]->AvailableChargingPower = halfPow;
  528. chargingInfo[group]->RealRatingPower = gpRealPow;
  529. if(chargingInfo[group]->DeratingChargingCurrent > 0)
  530. {
  531. unsigned short _powBuf = 0;
  532. _powBuf = ((chargingInfo[group]->DeratingChargingCurrent / 10) * ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10) / 1000; // 單位是 KW
  533. if (_powBuf > ShmPsuData->PsuGroup[group].GroupRealOutputPower ||
  534. chargingInfo[group]->EvBatterytargetVoltage > 0)
  535. {
  536. ShmPsuData->PsuGroup[group].GroupRealOutputPower = _powBuf;
  537. // PRINTF_FUNC("group = %d, DeratingChargingCurrent = %f, RealRatingPower = %d \n",
  538. // group, chargingInfo[group]->DeratingChargingCurrent, chargingInfo[group]->RealRatingPower);
  539. }
  540. }
  541. //printf("(Aver.) RealRatingPower = %d \n", chargingInfo[group]->RealRatingPower);
  542. }
  543. }
  544. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  545. {
  546. //PRINTF_FUNC("group = %d, Final = %d \n", group, _current);
  547. GetMaxPowerAndCur(_MAIN_CHARGING_MODE_MAX, _ratingcurrent, &_power, &_current);
  548. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  549. {
  550. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  551. {
  552. chargingInfo[count]->MaximumChargingVoltage = maxVol;
  553. chargingInfo[count]->AvailableChargingCurrent = _current;
  554. chargingInfo[count]->AvailableChargingPower = _power;
  555. chargingInfo[count]->RealRatingPower = _sysRealPower;
  556. }
  557. }
  558. else
  559. {
  560. // 如果是最大充,該槍資訊中的輸出能力為各群輸出能力的和
  561. chargingInfo[group]->AvailableChargingCurrent = _current;
  562. chargingInfo[group]->AvailableChargingPower = _power;
  563. chargingInfo[group]->RealRatingPower = _sysRealPower;
  564. }
  565. if(chargingInfo[group]->DeratingChargingCurrent > 0)
  566. {
  567. unsigned short _powBuf = 0;
  568. _powBuf = ((chargingInfo[group]->DeratingChargingCurrent / 10) * ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10) / 1000; // 單位是 KW
  569. if (_powBuf > ShmPsuData->PsuGroup[group].GroupRealOutputPower ||
  570. chargingInfo[group]->EvBatterytargetVoltage > 0 ||
  571. _targetVol > 0)
  572. {
  573. ShmPsuData->PsuGroup[group].GroupRealOutputPower = _powBuf;
  574. }
  575. }
  576. }
  577. }
  578. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  579. {
  580. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  581. return;
  582. if (IsOverModuleCount(address))
  583. return;
  584. byte group = FindTargetGroup(address);
  585. sprintf((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  586. sprintf((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, "PFC %d.%02d", (pfcSwVer & 0xFF00) >> 8, pfcSwVer & 0xFF);
  587. if (group == 1)
  588. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  589. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  590. //DEBUG_INFO("fw Ver. = %s \n", ShmPsuData->PsuGroup[group].PsuModule[address].FwVersion);
  591. }
  592. // no using -- GetInputVoltageCallback
  593. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  594. {
  595. // if (ShmPsuData->Work_Step < GET_SYS_CAP)
  596. // return;
  597. //
  598. // if (IsOverModuleCount(address))
  599. // return;
  600. //
  601. // byte group = FindTargetGroup(address);
  602. //
  603. // if (group == 1)
  604. // address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  605. //
  606. // ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = vol1;
  607. // ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = vol2;
  608. // ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = vol3;
  609. //
  610. // PRINTF_FUNC("***Input*** address = %d, R = %d, S = %d, T = %d, gp = %d \n",
  611. // address, vol1, vol2, vol3, group);
  612. }
  613. // no using -- GetInputVoltageCallback End
  614. // no using -- GetOutputAndTempCallback
  615. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  616. {
  617. // if (ShmPsuData->Work_Step < GET_SYS_CAP)
  618. // return;
  619. //if (outCur != ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent)
  620. //{
  621. // PRINTF_FUNC("Gp_%d, gp output cur = %d \n", group, outCur);
  622. //}
  623. // // PSU Group - 電壓
  624. // ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outVol;
  625. // // PSU Group - 電流
  626. // ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outCur;
  627. //
  628. // if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  629. // (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  630. // (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  631. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  632. // )
  633. // {
  634. // unsigned short outputVol = 0;
  635. // unsigned short outputCur = 0;
  636. //
  637. // for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  638. // {
  639. // bool needtoAdd = true;
  640. //
  641. // if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  642. // outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  643. //
  644. // if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  645. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  646. // {
  647. //// PRINTF_FUNC("Gp_%d, DividChargingCurrent = %d \n", index,
  648. //// chargingInfo[index]->DividChargingCurrent);
  649. // if (chargingInfo[index]->DividChargingCurrent == 0)
  650. // needtoAdd = false;
  651. // }
  652. //
  653. // if (needtoAdd)
  654. // outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  655. // }
  656. //
  657. // // 黑白機
  658. // if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  659. // {
  660. // for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  661. // {
  662. // float _vol_buf = outputVol;
  663. // float _cur_buf = outputCur;
  664. //
  665. // // EVSE - 電壓
  666. // _vol_buf /= 10;
  667. // chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  668. // // EVSE - 電流
  669. // _cur_buf /= 10;
  670. // chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  671. // }
  672. // }
  673. //
  674. // if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  675. // (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  676. // {
  677. // float _vol_buf = outputVol;
  678. // float _cur_buf = outputCur;
  679. //
  680. // // EVSE - 電壓
  681. // _vol_buf /= 10;
  682. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  683. // // EVSE - 電流
  684. // _cur_buf /= 10;
  685. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  686. // }
  687. // }
  688. // else
  689. // {
  690. // float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  691. // float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  692. //
  693. // // EVSE - 電壓
  694. // _vol_buf /= 10;
  695. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  696. // // EVSE - 電流
  697. // _cur_buf /= 10;
  698. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  699. // }
  700. //
  701. // PRINTF_FUNC("Gun_%d, PresentChargingVoltage = %f, PresentChargingCurrent = %f \n", group,
  702. // chargingInfo[group]->PresentChargingVoltage,
  703. // chargingInfo[group]->PresentChargingCurrent);
  704. }
  705. // no using -- GetOutputAndTempCallback End
  706. void GetMisCallback(byte address, unsigned int value, byte type)
  707. {
  708. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  709. return;
  710. if (IsOverModuleCount(address))
  711. return;
  712. byte group = FindTargetGroup(address);
  713. if (group == 1)
  714. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  715. if (type == 1)
  716. {
  717. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_1 = value;
  718. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_2 = value;
  719. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_3 = value;
  720. ShmPsuData->PsuGroup[group].PsuModule[address].FanSpeed_4 = value;
  721. }
  722. else if (type == 2)
  723. {
  724. //printf("DC - group = %d, index = %d, value = %d \n", group, address, value);
  725. // ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp1 = value;
  726. // ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp2 = value;
  727. // ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp3 = value;
  728. ShmPsuData->PsuGroup[group].PsuModule[address].ExletTemp = value;
  729. }
  730. else if (type == 3)
  731. {
  732. printf("PFC - group = %d, index = %d, value = %d \n", group, address, value);
  733. }
  734. }
  735. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  736. {
  737. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  738. return;
  739. if (IsOverModuleCount(address))
  740. return;
  741. // 經度 0.1
  742. //ShmPsuData->PsuGroup[address].GroupTargetOutputVoltage = Vext;
  743. //PRINTF_FUNC("address = %d, Iavail = %d \n", address, Iavail);
  744. byte group = FindTargetGroup(address);
  745. if (group == 1)
  746. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  747. //PRINTF_FUNC("group = %d, address_%d, Iavail = %d \n", group, address, Iavail);
  748. ShmPsuData->PsuGroup[group].PsuModule[address].IAvailableCurrent = Iavail;
  749. bool isPass = true;
  750. int totalCur = 0;
  751. byte sampleCount = 8;
  752. if (Iavail == 0)
  753. {
  754. for (byte count = 0; count < sampleCount; count++)
  755. {
  756. chargingInfo[group]->SampleChargingCur[count] = Iavail;
  757. }
  758. }
  759. else
  760. {
  761. // 該群的可輸出電流
  762. for (byte index = 0; index < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; index++)
  763. {
  764. totalCur += ShmPsuData->PsuGroup[group].PsuModule[index].IAvailableCurrent;
  765. }
  766. for (byte count = 0; count < sampleCount; count++)
  767. {
  768. if (chargingInfo[group]->SampleChargingCur[count] == 0)
  769. {
  770. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  771. return;
  772. }
  773. else
  774. {
  775. if (chargingInfo[group]->SampleChargingCur[count] != totalCur)
  776. {
  777. chargingInfo[group]->SampleChargingCur[count] = totalCur;
  778. isPass = false;
  779. break;
  780. }
  781. }
  782. }
  783. }
  784. if (isPass)
  785. {
  786. // if (totalCur != 0)
  787. // PRINTF_FUNC("group = %d, rating pass value = %d \n", group, totalCur);
  788. chargingInfo[group]->DeratingChargingCurrent = totalCur;
  789. }
  790. }
  791. void GetPresentOutputFCallback(byte group, float outVol, float outCur)
  792. {
  793. // isinf : -1 = 負無窮,1 = 正無窮,0 = 其他
  794. if (isinf(outVol) == 0)
  795. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = (unsigned short)(outVol * 10);
  796. else
  797. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = 0;
  798. if (isinf(outCur) == 0)
  799. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = (unsigned short)(outCur * 10);
  800. else
  801. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = 0;
  802. //printf("group = %d, Current = %d \n", group, ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent);
  803. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  804. (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  805. (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  806. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  807. )
  808. {
  809. unsigned short outputVol = 0;
  810. unsigned short outputCur = 0;
  811. unsigned char _maxTOaver = 0;
  812. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  813. {
  814. bool needtoAdd = true;
  815. if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  816. outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  817. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  818. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  819. {
  820. if (chargingInfo[index]->DividChargingCurrent == 0)
  821. needtoAdd = false;
  822. else
  823. _maxTOaver = index;
  824. }
  825. if (needtoAdd)
  826. outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  827. }
  828. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  829. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  830. {
  831. if (chargingInfo[_maxTOaver]->DividChargingCurrent != 0)
  832. {
  833. float _cur_buf = outputCur;
  834. _cur_buf /= 10;
  835. chargingInfo[_maxTOaver]->PresentChargingCurrent = _cur_buf;
  836. }
  837. }
  838. // 黑白機
  839. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  840. {
  841. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  842. {
  843. float _vol_buf = outputVol;
  844. float _cur_buf = outputCur;
  845. // EVSE - 電壓
  846. _vol_buf /= 10;
  847. chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  848. _cur_buf /= 10;
  849. chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  850. }
  851. }
  852. if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  853. (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  854. chargingInfo[group]->SystemStatus == S_ALARM)
  855. {
  856. float _vol_buf = outputVol;
  857. float _cur_buf = outputCur;
  858. // EVSE - 電壓
  859. _vol_buf /= 10;
  860. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  861. _cur_buf /= 10;
  862. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  863. }
  864. }
  865. else
  866. {
  867. float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  868. float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  869. // EVSE - 電壓
  870. _vol_buf /= 10;
  871. chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  872. _cur_buf /= 10;
  873. chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  874. }
  875. }
  876. //==========================================
  877. // 特規用指令
  878. //==========================================
  879. void GetOutputAndTempCallback(byte address, unsigned short outputVol_s,
  880. unsigned short outputCur_s, unsigned short outputPower, unsigned char Temperature)
  881. {
  882. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  883. return;
  884. //unsigned short outVol = outputVol_s;
  885. //unsigned short outCur = outputCur_s;
  886. if (IsOverModuleCount(address))
  887. return;
  888. byte group = FindTargetGroup(address);
  889. if (group == 1)
  890. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  891. // // PSU Group - 電壓
  892. // ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = outVol;
  893. // // PSU Group - 電流
  894. // ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = outCur;
  895. // // PSU Group - 能量
  896. // ShmPsuData->PsuGroup[group].GroupPresentOutputPower = outVol * outCur;
  897. //
  898. // if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  899. // (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  900. // (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING &&
  901. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_COMP))
  902. // )
  903. // {
  904. // unsigned short outputVol = 0;
  905. // unsigned short outputCur = 0;
  906. // unsigned char _maxTOaver = 0;
  907. //
  908. // for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  909. // {
  910. // bool needtoAdd = true;
  911. //
  912. // if (ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage > outputVol)
  913. // outputVol = ShmPsuData->PsuGroup[index].GroupPresentOutputVoltage;
  914. //
  915. //// if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  916. //// ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  917. //// {
  918. //// if (chargingInfo[index]->DividChargingCurrent == 0)
  919. //// needtoAdd = false;
  920. //// else
  921. //// _maxTOaver = index;
  922. //// }
  923. //
  924. // if (needtoAdd)
  925. // outputCur += ShmPsuData->PsuGroup[index].GroupPresentOutputCurrent;
  926. // }
  927. //
  928. // if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A &&
  929. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  930. // {
  931. // if (chargingInfo[_maxTOaver]->DividChargingCurrent != 0)
  932. // {
  933. // float _cur_buf = outputCur;
  934. // _cur_buf /= 10;
  935. // chargingInfo[_maxTOaver]->PresentChargingCurrent = _cur_buf;
  936. // }
  937. // }
  938. //
  939. // // 黑白機
  940. // if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  941. // {
  942. // for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  943. // {
  944. // float _vol_buf = outputVol;
  945. // float _cur_buf = outputCur;
  946. //
  947. // // EVSE - 電壓
  948. // _vol_buf /= 10;
  949. // chargingInfo[count]->PresentChargingVoltage = _vol_buf;
  950. //
  951. // _cur_buf /= 10;
  952. // chargingInfo[count]->PresentChargingCurrent = _cur_buf;
  953. // }
  954. // }
  955. //
  956. // if ((chargingInfo[group]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[group]->SystemStatus <= S_COMPLETE) ||
  957. // (chargingInfo[group]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[group]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  958. // {
  959. // float _vol_buf = outputVol;
  960. // float _cur_buf = outputCur;
  961. //
  962. // // EVSE - 電壓
  963. // _vol_buf /= 10;
  964. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  965. //
  966. // _cur_buf /= 10;
  967. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  968. // }
  969. // }
  970. // else
  971. // {
  972. // float _vol_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage;
  973. // float _cur_buf = ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent;
  974. //
  975. // // EVSE - 電壓
  976. // _vol_buf /= 10;
  977. // chargingInfo[group]->PresentChargingVoltage = _vol_buf;
  978. //
  979. // _cur_buf /= 10;
  980. // chargingInfo[group]->PresentChargingCurrent = _cur_buf;
  981. // }
  982. ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp1 = Temperature;
  983. ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp2 = Temperature;
  984. ShmPsuData->PsuGroup[group].PsuModule[address].CriticalTemp3 = Temperature;
  985. // ShmPsuData->PsuGroup[group].PsuModule[address].ExletTemp = Temperature;
  986. // PRINTF_FUNC("***Output Value and Temp*** group = %d, Vol = %d, Cur = %d \n",
  987. // group, outputVol_s, outputCur_s);
  988. }
  989. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  990. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  991. {
  992. ShmDcCommonData->psuKeepCommunication = ShmDcCommonData->acContactSwitch;
  993. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  994. return;
  995. if (IsOverModuleCount(address))
  996. return;
  997. byte group1 = FindTargetGroup(address);
  998. if (group1 == 1)
  999. address -= ShmPsuData->PsuGroup[group1 - 1].GroupPresentPsuQuantity;
  1000. int alarm = (err2 << 24) | (err3 << 16) | (err4 << 8) | err1;
  1001. ShmPsuData->PsuGroup[group1].PsuModule[address].AlarmCode = alarm;
  1002. //AbnormalStopAnalysis(group1, alarm);
  1003. if(isErr)
  1004. {
  1005. // 012267 - need to test the variable (short circuit output)
  1006. if (!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm )
  1007. {
  1008. //ShmPsuData->PsuGroup[group1].GroupErrorFlag.bits.PsuFailure = YES;
  1009. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  1010. PRINTF_FUNC("Group_%d - Address_%d, PSU Error Occurred. \n", group1, address);
  1011. }
  1012. }
  1013. }
  1014. void GetModuleInputCallback(byte address, unsigned short inputR,
  1015. unsigned short inputS, unsigned short inputT)
  1016. {
  1017. if (ShmPsuData->Work_Step < GET_SYS_CAP)
  1018. return;
  1019. if (IsOverModuleCount(address))
  1020. return;
  1021. byte group = FindTargetGroup(address);
  1022. if (group == 1)
  1023. address -= ShmPsuData->PsuGroup[group - 1].GroupPresentPsuQuantity;
  1024. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL1 = inputR;
  1025. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL2 = inputS;
  1026. ShmPsuData->PsuGroup[group].PsuModule[address].InputVoltageL3 = inputT;
  1027. //PRINTF_FUNC("***Input*** address = %d, R = %d, S = %d, T = %d \n",
  1028. // address, inputR, inputS, inputT);
  1029. }
  1030. //==========================================
  1031. // Init all share memory
  1032. //==========================================
  1033. int InitShareMemory()
  1034. {
  1035. int result = PASS;
  1036. int MeterSMId;
  1037. //creat ShmSysConfigAndInfo
  1038. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  1039. {
  1040. #ifdef SystemLogMessage
  1041. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG %d \n");
  1042. #endif
  1043. result = FAIL;
  1044. }
  1045. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1046. {
  1047. #ifdef SystemLogMessage
  1048. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG \n");
  1049. #endif
  1050. result = FAIL;
  1051. }
  1052. else
  1053. {}
  1054. //creat ShmStatusCodeData
  1055. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  1056. {
  1057. #ifdef SystemLogMessage
  1058. DEBUG_ERROR("shmget ShmStatusCodeData NG \n");
  1059. #endif
  1060. result = FAIL;
  1061. }
  1062. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1063. {
  1064. #ifdef SystemLogMessage
  1065. DEBUG_ERROR("shmat ShmStatusCodeData NG \n");
  1066. #endif
  1067. result = FAIL;
  1068. }
  1069. else
  1070. {}
  1071. //creat ShmPsuData
  1072. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  1073. {
  1074. #ifdef SystemLogMessage
  1075. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1076. #endif
  1077. result = FAIL;
  1078. }
  1079. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1080. {
  1081. #ifdef SystemLogMessage
  1082. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1083. #endif
  1084. result = FAIL;
  1085. }
  1086. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  1087. {
  1088. #ifdef SystemLogMessage
  1089. DEBUG_ERROR("shmget ShmCommonKey NG \n");
  1090. #endif
  1091. result = FAIL;
  1092. }
  1093. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1094. {
  1095. #ifdef SystemLogMessage
  1096. DEBUG_ERROR("shmat ShmCommonKey NG \n");
  1097. #endif
  1098. result = FAIL;
  1099. }
  1100. return result;
  1101. }
  1102. //================================================
  1103. // Log function
  1104. //================================================
  1105. void OutputChargingLogFuncion(byte groupIndex)
  1106. {
  1107. // 列印時機 : 需求改變或輸出電壓與紀錄落差超過 5V 或者輸出電流與紀錄落差超過 0.5A
  1108. if (chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_VOL] != chargingInfo[groupIndex]->EvBatterytargetVoltage * 10 ||
  1109. (chargingInfo[groupIndex]->FireChargingVoltage <= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] - LOG_VOL_GAP ||
  1110. chargingInfo[groupIndex]->FireChargingVoltage >= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] + LOG_VOL_GAP))
  1111. {
  1112. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_VOL] = chargingInfo[groupIndex]->EvBatterytargetVoltage * 10;
  1113. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] = chargingInfo[groupIndex]->FireChargingVoltage;
  1114. PRINTF_FUNC("Conn %d, EV Req Voltage : %.1f, EVSE Output Voltage = %.1f \n", groupIndex,
  1115. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_VOL] / 10,
  1116. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_VOL] / 10);
  1117. }
  1118. if (chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_CUR] != chargingInfo[groupIndex]->EvBatterytargetCurrent * 10 ||
  1119. (chargingInfo[groupIndex]->PresentChargingCurrent <= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR] - LOG_CUR_GAP ||
  1120. chargingInfo[groupIndex]->PresentChargingCurrent >= chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR] + LOG_CUR_GAP))
  1121. {
  1122. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_CUR] = chargingInfo[groupIndex]->EvBatterytargetCurrent * 10;
  1123. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR] = chargingInfo[groupIndex]->PresentChargingCurrent;
  1124. PRINTF_FUNC("Conn %d, EV Req Current : %.1f, EVSE Output Current = %.1f \n", groupIndex,
  1125. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_NEED_CUR] / 10,
  1126. chargingOutputLogInfo[groupIndex][_CHARGING_LOG_OUTPUT_CUR]);
  1127. }
  1128. }
  1129. //================================================
  1130. // Main process
  1131. //================================================
  1132. void InitialPsuData()
  1133. {
  1134. ShmPsuData->SystemPresentPsuQuantity = 0;
  1135. PRINTF_FUNC("InitialPsuData : PSU Group = %d \n", ShmPsuData->GroupCount);
  1136. for (byte _groupCount = 0; _groupCount < ShmPsuData->GroupCount; _groupCount++)
  1137. {
  1138. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  1139. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  1140. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  1141. chargingInfo[_groupCount]->PresentChargingVoltage = 0;
  1142. chargingInfo[_groupCount]->PresentChargingCurrent = 0;
  1143. }
  1144. }
  1145. void Initialization()
  1146. {
  1147. bool isPass = false;
  1148. while(!isPass)
  1149. {
  1150. isPass = true;
  1151. for (byte _index = 0; _index < _gunCount; _index++)
  1152. {
  1153. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  1154. {
  1155. DEBUG_ERROR("Module_PsuComm : FindChargingInfoData false \n");
  1156. isPass = false;
  1157. break;
  1158. }
  1159. }
  1160. sleep(1);
  1161. }
  1162. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  1163. ShmPsuData->GroupCount = 1;
  1164. else
  1165. ShmPsuData->GroupCount = _gunCount;
  1166. ShmPsuData->SystemAvailableCurrent = 0;
  1167. ShmPsuData->SystemAvailablePower = 0;
  1168. }
  1169. void CheckSmartChargingStep(byte chargingStatus)
  1170. {
  1171. // 跑切換均充的邏輯
  1172. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1173. {
  1174. // 確認 A 槍是否已經進入充電階段
  1175. if (chargingStatus == _CHARGING_GUN_STATUS_CHARGING)
  1176. {
  1177. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  1178. {
  1179. PRINTF_FUNC("======= Max -> Aver : Update new charging capacity. (Step 2) ======= \n");
  1180. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  1181. }
  1182. }
  1183. else if (chargingStatus == _CHARGING_GUN_STATUS_STOP)
  1184. {
  1185. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  1186. {
  1187. PRINTF_FUNC("============= Max -> Aver : Charging mode = MAX. (Step 0) ============= \n");
  1188. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1189. }
  1190. }
  1191. }
  1192. // 跑切換最大充邏輯
  1193. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1194. {
  1195. if (chargingStatus == _CHARGING_GUN_STATUS_CHARGING)
  1196. {
  1197. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  1198. {
  1199. PRINTF_FUNC("======= Aver -> Max : switch on the psu voltage. (Step 12) ======= \n");
  1200. preChargingCur = preChargingTarget = 0;
  1201. gettimeofday(&_max_time, NULL);
  1202. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  1203. }
  1204. }
  1205. else if (chargingStatus == _CHARGING_GUN_STATUS_STOP)
  1206. {
  1207. PRINTF_FUNC("======= Aver -> Max : Charging mode = MAX. (Step 15) ======= \n");
  1208. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1209. }
  1210. }
  1211. // if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_M_TO_A)
  1212. // {
  1213. // if (isWaitingAver)
  1214. // {
  1215. // if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1216. // {
  1217. // ShmSysConfigAndInfo->SysInfo.CanAverageCharging = canAverageCharging;
  1218. //
  1219. // if (canAverageCharging)
  1220. // {
  1221. // PRINTF_FUNC("======= Only to get the charging side capacity (Step 2) ======= \n");
  1222. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_GET_NEW_CAP;
  1223. // }
  1224. // else
  1225. // {
  1226. // PRINTF_FUNC("=============Smart Charging : _REASSIGNED_NONE============= Step 0 \n");
  1227. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1228. // }
  1229. // }
  1230. // else
  1231. // {
  1232. // PRINTF_FUNC("=============Smart Charging Aver mode : _REASSIGNED_NONE============= Step 0 \n");
  1233. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1234. // }
  1235. // }
  1236. // }
  1237. // else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_PREPARE_A_TO_M)
  1238. // {
  1239. // if (isCharging)
  1240. // {
  1241. // if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1242. // {
  1243. // PRINTF_FUNC("======= To raise voltage of idle module to charging voltage (Step 12) ======= \n");
  1244. // preChargingCur = preChargingTarget = 0;
  1245. // gettimeofday(&_max_time, NULL);
  1246. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_A_TO_M;
  1247. // }
  1248. // else
  1249. // {
  1250. // PRINTF_FUNC("======= The Change to maximum charge mode is complete. (Step 15) ======= \n");
  1251. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1252. // }
  1253. // }
  1254. // else
  1255. // {
  1256. // PRINTF_FUNC("======= The Change to maximum charge mode is complete. (Step 15) ======= \n");
  1257. // ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1258. // }
  1259. // }
  1260. }
  1261. void PreCheckSmartChargingStep()
  1262. {
  1263. _maxChargingStatus = _CHARGING_GUN_STATUS_NONE;
  1264. isReadyToCharging = false;
  1265. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1266. {
  1267. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus < S_CHARGING) ||
  1268. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1269. {
  1270. _maxChargingStatus = _CHARGING_GUN_STATUS_COMM;
  1271. }
  1272. else if (chargingInfo[index]->SystemStatus == S_CHARGING)
  1273. {
  1274. _maxChargingStatus = _CHARGING_GUN_STATUS_CHARGING;
  1275. }
  1276. else if (chargingInfo[index]->SystemStatus == S_COMPLETE ||
  1277. chargingInfo[index]->SystemStatus == S_ALARM)
  1278. {
  1279. _maxChargingStatus = _CHARGING_GUN_STATUS_STOP;
  1280. }
  1281. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  1282. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1283. {
  1284. isReadyToCharging = true;
  1285. }
  1286. }
  1287. CheckSmartChargingStep(_maxChargingStatus);
  1288. }
  1289. void Await()
  1290. {
  1291. usleep(CMD_DELAY_TIME);
  1292. }
  1293. bool MaxToAverChargingProc(byte groupIndex)
  1294. {
  1295. // 智能判斷 Start -----------------------------------------------------------
  1296. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1297. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1298. {
  1299. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_GET_NEW_CAP)
  1300. {
  1301. if (chargingInfo[groupIndex]->DividChargingCurrent == 0)
  1302. {
  1303. chargingInfo[groupIndex]->DividChargingCurrent = ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent;
  1304. PRINTF_FUNC("Index = %d, DividChargingCurrent = %.1f \n", groupIndex, chargingInfo[groupIndex]->DividChargingCurrent);
  1305. }
  1306. // 車端要求電流為該充電槍的額定輸出電流的範圍內
  1307. if ((chargingInfo[groupIndex]->EvBatterytargetCurrent * 10) <= ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP ||
  1308. deratingKeepCount >= DERATING_COUNT)
  1309. {
  1310. // 車端降載完成
  1311. PRINTF_FUNC("Index = %d, newEvCurrent = %f \n", groupIndex, (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1312. PRINTF_FUNC("======= Max -> Aver : wait the target current is down. (Step 3) ======= \n");
  1313. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_ADJUST_M_TO_A;
  1314. gettimeofday(&_derating_time, NULL);
  1315. deratingKeepCount = 0;
  1316. }
  1317. else
  1318. {
  1319. deratingKeepCount++;
  1320. if (deratingKeepCount % 3 == 0)
  1321. {
  1322. PRINTF_FUNC("Max To Ava mode (2) : Index = %d, EvBatterytargetCurrent = %f, TargetCurrent = %d, Count = %d \n",
  1323. groupIndex,
  1324. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10),
  1325. (ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent + DERATING_GAP),
  1326. deratingKeepCount);
  1327. }
  1328. }
  1329. }
  1330. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_M_TO_A)
  1331. {
  1332. bool isChanged = false;
  1333. if (chargingInfo[groupIndex]->DividChargingCurrent == 0 ||
  1334. (chargingInfo[groupIndex]->DividChargingCurrent != 0 &&
  1335. (chargingInfo[groupIndex]->DividChargingCurrent == chargingInfo[groupIndex]->AvailableChargingCurrent) &&
  1336. chargingInfo[groupIndex]->AvailableChargingCurrent >= (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)))
  1337. {
  1338. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1339. {
  1340. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1341. {
  1342. // 當 B 模塊輸出電流小於 5A 及退開 relay
  1343. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= 50)
  1344. isChanged = true;
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. else if (chargingInfo[groupIndex]->PresentChargingCurrent == 0)
  1350. {
  1351. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1352. {
  1353. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1354. {
  1355. if ((ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent) <= CHK_CUR_RANGE)
  1356. isChanged = true;
  1357. break;
  1358. }
  1359. }
  1360. }
  1361. if (isChanged)
  1362. {
  1363. PRINTF_FUNC("Max To Ava mode (3-2) : Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d \n", groupIndex,
  1364. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1365. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent);
  1366. // 輸出端與車端要求電流接近
  1367. PRINTF_FUNC("======= Max -> Aver : off the Parallel relay. (Step 4) ======= \n");
  1368. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1369. }
  1370. else
  1371. {
  1372. int _t = GetTimeoutValue(_derating_time);
  1373. if ((_t / 1000) > 1000 || _t < 0)
  1374. {
  1375. PRINTF_FUNC("Max To Ava mode (3-1) : Gun_%d, AvailableChargingCurrent = %f, EvBatterytargetCurrent = %f \n",
  1376. groupIndex,
  1377. chargingInfo[groupIndex]->AvailableChargingCurrent,
  1378. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1379. PRINTF_FUNC("Max To Ava mode (3-1) : Gun_%d, PresentChargingCurrent = %f, GroupPresentOutputCurrent = %d, GroupCount = %d \n",
  1380. groupIndex,
  1381. (chargingInfo[groupIndex]->PresentChargingCurrent * 10),
  1382. ShmPsuData->PsuGroup[groupIndex].GroupPresentOutputCurrent,
  1383. ShmPsuData->GroupCount);
  1384. gettimeofday(&_derating_time, NULL);
  1385. }
  1386. }
  1387. }
  1388. }
  1389. else
  1390. {
  1391. chargingInfo[groupIndex]->DividChargingCurrent = 0;
  1392. chargingInfo[groupIndex]->MaxChargingToAverPassFlag = 0;
  1393. }
  1394. // 調整輸出電流 : 漸進調整方式
  1395. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_GET_NEW_CAP &&
  1396. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_M_TO_A)
  1397. {
  1398. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_M_TO_A)
  1399. {
  1400. // 當前充電中的目標電壓
  1401. float targetVol = (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10);
  1402. byte reassignIndex = ELEMENT_NOT_FIND;
  1403. // 找到等待分配的槍
  1404. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1405. {
  1406. if (chargingInfo[subIndex]->SystemStatus == S_REASSIGN)
  1407. {
  1408. reassignIndex = subIndex;
  1409. break;
  1410. }
  1411. }
  1412. if (reassignIndex != ELEMENT_NOT_FIND)
  1413. {
  1414. if ((GetTimeoutValue(_derating_time) / 1000) <= 150 ||
  1415. chargingInfo[groupIndex]->MaxChargingToAverPassFlag == 0)
  1416. {
  1417. chargingInfo[groupIndex]->MaxChargingToAverPassFlag = 1;
  1418. if(chargingInfo[groupIndex]->EvBatterytargetCurrent <= PSU_MIN_OUTPUT_CUR)
  1419. chargingInfo[groupIndex]->EvBatterytargetCurrent = PSU_MIN_OUTPUT_CUR;
  1420. PresentOutputVol(groupIndex, targetVol, (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1421. PresentOutputVol(reassignIndex, targetVol, CHK_CUR_RANGE); Await();
  1422. }
  1423. }
  1424. }
  1425. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1426. {
  1427. bool isNeedToClosePower = false;
  1428. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1429. {
  1430. if (isStartOutputSwitch[index])
  1431. {
  1432. isNeedToClosePower = true;
  1433. }
  1434. isStartOutputSwitch[index] = false;
  1435. }
  1436. if (isNeedToClosePower)
  1437. {
  1438. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1439. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1440. }
  1441. }
  1442. }
  1443. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY_M_TO_A)
  1444. {
  1445. //PRINTF_FUNC("set out (%d) value = %f******** 3 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1446. if(chargingInfo[groupIndex]->EvBatterytargetCurrent <= PSU_MIN_OUTPUT_CUR)
  1447. chargingInfo[groupIndex]->EvBatterytargetCurrent = PSU_MIN_OUTPUT_CUR;
  1448. PresentOutputVol(groupIndex,
  1449. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1450. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1451. }
  1452. else
  1453. {
  1454. return false;
  1455. }
  1456. return true;
  1457. }
  1458. bool AverToMaxChargingProc(byte groupIndex)
  1459. {
  1460. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_ADJUST_A_TO_M)
  1461. {
  1462. byte reassignIndex = ELEMENT_NOT_FIND;
  1463. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1464. {
  1465. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1466. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1467. chargingInfo[subIndex]->SystemStatus == S_FAULT ||
  1468. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK ||
  1469. chargingInfo[subIndex]->SystemStatus == S_COMPLETE ||
  1470. chargingInfo[subIndex]->SystemStatus == S_ALARM)
  1471. {
  1472. reassignIndex = subIndex;
  1473. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  1474. {
  1475. preChargingCur = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1476. }
  1477. else
  1478. preChargingCur = 0;
  1479. }
  1480. else
  1481. {
  1482. if ((ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING) &&
  1483. (preChargingCur >= preChargingTarget - ZERO_CURRENT))
  1484. preChargingTarget += PRE_CHARG_STEP_CUR;
  1485. if (preChargingTarget >= (chargingInfo[subIndex]->EvBatterytargetCurrent * 10) / 2)
  1486. preChargingTarget = (chargingInfo[subIndex]->EvBatterytargetCurrent * 10) / 2;
  1487. }
  1488. }
  1489. if (reassignIndex != ELEMENT_NOT_FIND)
  1490. {
  1491. if ((GetTimeoutValue(_max_time) / 1000) <= 150)
  1492. {
  1493. //PRINTF_FUNC("set out (%d) value = %d******** 5 \n", reassignIndex, ZERO_CURRENT + preChargingTarget);
  1494. // 閒置模塊升壓,另對剛分配近來的模塊,預上升電流值 (preChargingCur)
  1495. PresentOutputVol(reassignIndex,
  1496. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1497. ZERO_CURRENT + preChargingTarget); Await();
  1498. }
  1499. byte _ovCahrgingCur = 0;
  1500. if (preChargingCur > PRE_CHARG_STEP_CUR)
  1501. _ovCahrgingCur = PRE_CHARG_STEP_CUR;
  1502. PresentOutputVol(groupIndex,
  1503. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1504. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10) - preChargingCur - _ovCahrgingCur); Await();
  1505. }
  1506. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1507. {
  1508. bool isNeedToClosePower = false;
  1509. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1510. {
  1511. if (isStartOutputSwitch[index])
  1512. {
  1513. isNeedToClosePower = true;
  1514. }
  1515. isStartOutputSwitch[index] = false;
  1516. }
  1517. if (isNeedToClosePower)
  1518. {
  1519. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1520. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1521. }
  1522. }
  1523. else
  1524. {
  1525. bool isNeedToOpenPower = false;
  1526. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1527. {
  1528. if (!isStartOutputSwitch[index])
  1529. {
  1530. isNeedToOpenPower = true;
  1531. }
  1532. isStartOutputSwitch[index] = true;
  1533. }
  1534. if (isNeedToOpenPower)
  1535. {
  1536. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1537. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1538. }
  1539. }
  1540. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_ADJUST_A_TO_M)
  1541. {
  1542. bool balanceVol = true;
  1543. byte subIndex;
  1544. for (subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1545. {
  1546. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1547. chargingInfo[subIndex]->SystemStatus == S_FAULT ||
  1548. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1549. chargingInfo[subIndex]->SystemStatus == S_COMPLETE ||
  1550. chargingInfo[subIndex]->SystemStatus == S_ALARM)
  1551. {
  1552. // 各群電壓接近平衡
  1553. if (((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE) ||
  1554. ((chargingInfo[subIndex]->PresentChargingVoltage * 10) < (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - CHK_VOL_RANGE))
  1555. {
  1556. balanceVol = false;
  1557. }
  1558. break;
  1559. }
  1560. }
  1561. if (balanceVol)
  1562. {
  1563. // 閒置端與車端要求電壓接近
  1564. PRINTF_FUNC("======= Aver -> Max : switch on the Parallel relay. (Step 13) ======= \n");
  1565. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_A_TO_M;
  1566. }
  1567. else
  1568. {
  1569. int _tMax_1 = GetTimeoutValue(_max_time);
  1570. if ((_tMax_1 / 1000) > 500 || _tMax_1 < 0)
  1571. {
  1572. PRINTF_FUNC("Ava To Max mode (12) : Gun_%d, PresentChargingVoltage = %f, PresentChargingVoltage_V = %f, EvBatterytargetVoltage = %f \n", subIndex,
  1573. (chargingInfo[subIndex]->PresentChargingVoltage * 10),
  1574. ((chargingInfo[groupIndex]->PresentChargingVoltage * 10) - ZERO_VOLTAGE),
  1575. ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) - CHK_VOL_RANGE));
  1576. gettimeofday(&_max_time, NULL);
  1577. }
  1578. }
  1579. }
  1580. else if(ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_WAITING)
  1581. {
  1582. int idleCurrent = 0;
  1583. int chargingCurrent = 0;
  1584. for (byte subIndex = 0; subIndex < ShmPsuData->GroupCount; subIndex++)
  1585. {
  1586. if (chargingInfo[subIndex]->SystemStatus == S_IDLE ||
  1587. chargingInfo[subIndex]->SystemStatus == S_RESERVATION ||
  1588. chargingInfo[subIndex]->SystemStatus == S_FAULT ||
  1589. chargingInfo[subIndex]->SystemStatus == S_REASSIGN_CHECK)
  1590. idleCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1591. else
  1592. {
  1593. chargingCurrent = ShmPsuData->PsuGroup[subIndex].GroupPresentOutputCurrent;
  1594. }
  1595. }
  1596. if (idleCurrent >= chargingCurrent - PRE_CHARG_RANGE)
  1597. {
  1598. PRINTF_FUNC("======= Aver -> Max : Charging mode = MAX. (Step 15) ======= \n");
  1599. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  1600. }
  1601. else
  1602. {
  1603. int _tMax_2 = GetTimeoutValue(_max_time);
  1604. if ((_tMax_2 / 1000) > 300 || _tMax_2 < 0)
  1605. {
  1606. gettimeofday(&_max_time, NULL);
  1607. }
  1608. }
  1609. }
  1610. }
  1611. else
  1612. {
  1613. return false;
  1614. }
  1615. return true;
  1616. }
  1617. void SwitchOffPowerCheck(byte groupIndex)
  1618. {
  1619. _maxChargingStatus = _CHARGING_GUN_STATUS_NONE;
  1620. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1621. {
  1622. // 判斷另一把槍的狀態
  1623. if (index != groupIndex)
  1624. {
  1625. if ((chargingInfo[index]->SystemStatus >= S_PREPARING_FOR_EV && chargingInfo[index]->SystemStatus <= S_CHARGING) ||
  1626. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1627. _maxChargingStatus = _CHARGING_GUN_STATUS_CHARGING;
  1628. else
  1629. _maxChargingStatus = _CHARGING_GUN_STATUS_STOP;
  1630. }
  1631. }
  1632. }
  1633. int main(void)
  1634. {
  1635. if(InitShareMemory() == FAIL)
  1636. {
  1637. #ifdef SystemLogMessage
  1638. DEBUG_ERROR("InitShareMemory NG\n");
  1639. #endif
  1640. if(ShmStatusCodeData != NULL)
  1641. {
  1642. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  1643. }
  1644. sleep(5);
  1645. return 0;
  1646. }
  1647. PRINTF_FUNC("InitShareMemory OK\n");
  1648. // register callback function
  1649. RefreshStatus(&GetStatusCallback);
  1650. RefreshModuleCount(&GetModuleCountCallback);
  1651. RefreshAvailableCap(&GetAvailableCapCallback);
  1652. RefreshFwVersion(&GetFwCallback);
  1653. RefreshInputVol(&GetInputVoltageCallback);
  1654. RefreshGetOutput(&GetPresentOutputCallback);
  1655. RefreshMisInfo(&GetMisCallback);
  1656. RefreshIavailable(&GetIavailableCallback);
  1657. RefreshGetOutputF(&GetPresentOutputFCallback);
  1658. // GetPresentOutputCallback & GetStatusCallback
  1659. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  1660. // GetStatusCallback
  1661. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  1662. // GetInputVoltageCallback
  1663. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  1664. sleep(2);
  1665. _gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1666. // initial object
  1667. InitialPsuData();
  1668. Initialization();
  1669. libInitialize = InitialCommunication();
  1670. byte isInitialComp = NO;
  1671. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  1672. {
  1673. PRINTF_FUNC("Alter native mode. \n");
  1674. }
  1675. //main loop
  1676. while (libInitialize)
  1677. {
  1678. // 斷電狀態
  1679. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1680. {
  1681. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  1682. if (!isInitialComp)
  1683. {
  1684. ShmPsuData->Work_Step = INITIAL_START;
  1685. psuCmdSeq = _PSU_CMD_STATUS;
  1686. sleep(2);
  1687. InitialPsuData();
  1688. isInitialComp = YES;
  1689. }
  1690. ShmDcCommonData->psuKeepCommunication = ShmDcCommonData->acContactSwitch;
  1691. sleep(1);
  1692. continue;
  1693. }
  1694. else
  1695. isInitialComp = NO;
  1696. // 自檢失敗
  1697. if (ShmPsuData->Work_Step == _NO_WORKING)
  1698. {
  1699. PRINTF_FUNC("== PSU == self test fail. \n");
  1700. sleep(5);
  1701. }
  1702. switch(ShmPsuData->Work_Step)
  1703. {
  1704. case INITIAL_START:
  1705. {
  1706. PRINTF_FUNC("== PSU == INITIAL_START \n");
  1707. gettimeofday(&_cmdSubPriority_time, NULL);
  1708. sleep(5);
  1709. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1710. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  1711. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1712. isStartOutputSwitch[index] = false;
  1713. ShmPsuData->Work_Step = GET_PSU_COUNT;
  1714. }
  1715. break;
  1716. case GET_PSU_COUNT:
  1717. {
  1718. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1719. byte moduleCount = 0;
  1720. if (time < 0)
  1721. gettimeofday(&_cmdSubPriority_time, NULL);
  1722. // 發送取得目前全部模組數量
  1723. GetModuleCount(SYSTEM_CMD);
  1724. if (time > 2000)
  1725. {
  1726. // if (ShmPsuData->GroupCount == 0)
  1727. // ShmPsuData->GroupCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1728. // 分別取各群模組數量
  1729. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1730. {
  1731. // 總和各群模組數量
  1732. moduleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  1733. // 取各群模組數量
  1734. GetModuleCount(index);
  1735. }
  1736. PRINTF_FUNC("== PSU == Connector Count = %d, moduleCount = %d, sysCount = %d\n",
  1737. ShmPsuData->GroupCount, moduleCount, ShmPsuData->SystemPresentPsuQuantity);
  1738. // 判斷系統數量與各群數量一致
  1739. if(moduleCount == ShmPsuData->SystemPresentPsuQuantity && moduleCount > 0)
  1740. {
  1741. if (ShmSysConfigAndInfo->SysInfo.BootingStatus == BOOTTING)
  1742. {
  1743. // 電樁在 Booting 的狀態 - 自檢
  1744. PRINTF_FUNC("== PSU == GET_SYS_CAP \n");
  1745. ShmPsuData->Work_Step = GET_SYS_CAP;
  1746. }
  1747. else
  1748. {
  1749. PRINTF_FUNC("== PSU == _WORK_CHARGING \n");
  1750. ShmPsuData->Work_Step = _WORK_CHARGING;
  1751. }
  1752. }
  1753. gettimeofday(&_cmdSubPriority_time, NULL);
  1754. }
  1755. }
  1756. break;
  1757. case GET_SYS_CAP:
  1758. {
  1759. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1760. if (time < 0)
  1761. gettimeofday(&_cmdSubPriority_time, NULL);
  1762. if (time > 500)
  1763. {
  1764. bool isFinish = true;
  1765. for (byte psuCount = 0; psuCount < ShmPsuData->SystemPresentPsuQuantity; psuCount++)
  1766. {
  1767. if (strcmp((char *)ShmPsuData->PsuVersion[psuCount].FwPrimaryVersion, "") == EQUAL ||
  1768. ShmPsuData->SystemAvailablePower <= 0 || ShmPsuData->SystemAvailableCurrent <= 0)
  1769. {
  1770. isFinish = false;
  1771. break;
  1772. }
  1773. }
  1774. if (!isFinish)
  1775. {
  1776. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1777. {
  1778. if (psuCmdSeq == _PSU_CMD_STATUS)
  1779. {
  1780. // 取得狀態 : 支援模塊不須按照順序功能
  1781. GetStatus(index);
  1782. }
  1783. else if (psuCmdSeq == _PSU_CMD_CAP)
  1784. {
  1785. // 取系統總輸出能力
  1786. GetModuleCap(index);
  1787. }
  1788. else if (psuCmdSeq == _PSU_CMD_VERSION)
  1789. {
  1790. // 取版號
  1791. GetModuleVer(index);
  1792. }
  1793. }
  1794. if (psuCmdSeq == _PSU_CMD_STATUS)
  1795. psuCmdSeq = _PSU_CMD_CAP;
  1796. else if (psuCmdSeq == _PSU_CMD_CAP)
  1797. psuCmdSeq = _PSU_CMD_VERSION;
  1798. else
  1799. psuCmdSeq = _PSU_CMD_STATUS;
  1800. }
  1801. else
  1802. {
  1803. // 判斷系統輸出額定功率與電流
  1804. PRINTF_FUNC("SystemAvailableCurrent = %d, SystemAvailablePower = %d \n",
  1805. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  1806. PRINTF_FUNC("== PSU == BOOTING_COMPLETE \n");
  1807. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  1808. }
  1809. gettimeofday(&_cmdSubPriority_time, NULL);
  1810. gettimeofday(&_log_time, NULL);
  1811. }
  1812. }
  1813. break;
  1814. case BOOTING_COMPLETE:
  1815. {
  1816. bool isSelfTestPass = true;
  1817. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1818. {
  1819. if (chargingInfo[groupIndex]->SystemStatus == S_BOOTING)
  1820. {
  1821. isSelfTestPass = false;
  1822. }
  1823. }
  1824. if (isSelfTestPass)
  1825. ShmPsuData->Work_Step = _WORK_CHARGING;
  1826. sleep(1);
  1827. }
  1828. break;
  1829. case _WORK_CHARGING:
  1830. {
  1831. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  1832. unsigned long _timebuf = 0;
  1833. if (time < 0)
  1834. gettimeofday(&_cmdSubPriority_time, NULL);
  1835. // 低 Priority 的指令
  1836. if (time > 1500)
  1837. {
  1838. PreCheckSmartChargingStep();
  1839. startModuleFlag = true;
  1840. gettimeofday(&_cmdSubPriority_time, NULL);
  1841. }
  1842. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1843. {
  1844. if (psuCmdSeq == _PSU_CMD_CAP)
  1845. {
  1846. // 取系統總輸出能力
  1847. GetModuleCap(groupIndex);
  1848. }
  1849. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1850. {
  1851. // 取各群輸出電壓電流 (float)
  1852. GetModuleOutputF(groupIndex);
  1853. }
  1854. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1855. {
  1856. // 取得模塊輸出額定電流能力
  1857. GetModuleIavailable(groupIndex);
  1858. }
  1859. else if (psuCmdSeq == _PSU_CMD_TEMP)
  1860. {
  1861. // 取得模塊溫度
  1862. GetDcTemperature(groupIndex);
  1863. }
  1864. }
  1865. Await();
  1866. if (psuCmdSeq == _PSU_CMD_CAP)
  1867. psuCmdSeq = _PSU_CMD_OUTPUT;
  1868. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1869. psuCmdSeq = _PSU_CMD_IVAILIABLE;
  1870. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1871. psuCmdSeq = _PSU_CMD_TEMP;
  1872. else
  1873. psuCmdSeq = _PSU_CMD_CAP;
  1874. for (byte groupIndex = 0; groupIndex < _gunCount; groupIndex++)
  1875. {
  1876. // 針對各槍當前狀態,傳送需要回傳的資料指令
  1877. if (((chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING) && chargingInfo[groupIndex]->RelayK1K2Status) ||
  1878. (chargingInfo[groupIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[groupIndex]->SystemStatus <= S_CHARGING && chargingInfo[groupIndex]->Type == _Type_GB) ||
  1879. (chargingInfo[groupIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[groupIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1880. {
  1881. _timebuf = GetTimeoutValue(_log_time);
  1882. if (_timebuf < 0)
  1883. gettimeofday(&_log_time, NULL);
  1884. if (_timebuf / 1000 > 1000)
  1885. {
  1886. OutputChargingLogFuncion(groupIndex);
  1887. gettimeofday(&_log_time, NULL);
  1888. }
  1889. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1890. {
  1891. if (!MaxToAverChargingProc(groupIndex))
  1892. {
  1893. if(chargingInfo[groupIndex]->EvBatterytargetCurrent <= PSU_MIN_OUTPUT_CUR)
  1894. chargingInfo[groupIndex]->EvBatterytargetCurrent = PSU_MIN_OUTPUT_CUR;
  1895. PresentOutputVol(SYSTEM_CMD,
  1896. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1897. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10));
  1898. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1899. {
  1900. bool isNeedToClosePower = false;
  1901. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1902. {
  1903. if (isStartOutputSwitch[index])
  1904. {
  1905. isNeedToClosePower = true;
  1906. }
  1907. isStartOutputSwitch[index] = false;
  1908. }
  1909. if (isNeedToClosePower)
  1910. {
  1911. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1912. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1913. }
  1914. }
  1915. else
  1916. {
  1917. bool isNeedToOpenPower = false;
  1918. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1919. {
  1920. if (!isStartOutputSwitch[index])
  1921. {
  1922. isNeedToOpenPower = true;
  1923. }
  1924. isStartOutputSwitch[index] = true;
  1925. }
  1926. if (isNeedToOpenPower || startModuleFlag)
  1927. {
  1928. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  1929. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  1930. }
  1931. }
  1932. }
  1933. }
  1934. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1935. {
  1936. if (!AverToMaxChargingProc(groupIndex))
  1937. {
  1938. if (chargingInfo[groupIndex]->AvailableChargingCurrent > 0)
  1939. {
  1940. //PRINTF_FUNC("set out (%d) value = %f******** 7 \n", groupIndex, chargingInfo[groupIndex]->EvBatterytargetCurrent);
  1941. if(chargingInfo[groupIndex]->EvBatterytargetCurrent <= PSU_MIN_OUTPUT_CUR)
  1942. chargingInfo[groupIndex]->EvBatterytargetCurrent = PSU_MIN_OUTPUT_CUR;
  1943. PresentOutputVol(groupIndex,
  1944. (chargingInfo[groupIndex]->EvBatterytargetVoltage * 10),
  1945. (chargingInfo[groupIndex]->EvBatterytargetCurrent * 10)); Await();
  1946. if ((chargingInfo[groupIndex]->EvBatterytargetVoltage * 10) == 0)
  1947. {
  1948. if (isStartOutputSwitch[groupIndex])
  1949. {
  1950. isStartOutputSwitch[groupIndex] = false;
  1951. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  1952. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  1953. }
  1954. }
  1955. else
  1956. {
  1957. if (!isStartOutputSwitch[groupIndex] || startModuleFlag)
  1958. {
  1959. isStartOutputSwitch[groupIndex] = true;
  1960. SwitchPower(groupIndex, PSU_POWER_ON); Await();
  1961. FlashLed(groupIndex, PSU_FLASH_ON); Await();
  1962. }
  1963. }
  1964. }
  1965. }
  1966. }
  1967. }
  1968. else if (chargingInfo[groupIndex]->SystemStatus >= S_TERMINATING &&
  1969. chargingInfo[groupIndex]->SystemStatus <= S_ALARM)
  1970. {
  1971. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1972. {
  1973. SwitchOffPowerCheck(groupIndex);
  1974. if (_maxChargingStatus == _CHARGING_GUN_STATUS_STOP)
  1975. {
  1976. bool isNeedToClosePower = false;
  1977. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1978. {
  1979. if (isStartOutputSwitch[index])
  1980. {
  1981. isNeedToClosePower = true;
  1982. }
  1983. isStartOutputSwitch[index] = false;
  1984. }
  1985. if (isNeedToClosePower)
  1986. {
  1987. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  1988. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  1989. }
  1990. if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE ||
  1991. chargingInfo[groupIndex]->SystemStatus == S_ALARM)
  1992. {
  1993. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  1994. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  1995. {
  1996. // 代表在切換的過程中,停止充電了
  1997. if ((chargingInfo[groupIndex]->PresentChargingCurrent * 10) <= STOP_CURRENT)
  1998. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_RELAY_M_TO_A;
  1999. }
  2000. }
  2001. }
  2002. else if (chargingInfo[groupIndex]->SystemStatus == S_COMPLETE)
  2003. {
  2004. // 代表充電的槍依舊在充電,欲進入充電的槍取消充電了
  2005. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_PREPARE_M_TO_A &&
  2006. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag <= _REASSIGNED_RELAY_M_TO_A)
  2007. {
  2008. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  2009. }
  2010. }
  2011. }
  2012. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  2013. {
  2014. // if (!isReadyToCharging)
  2015. // {
  2016. // bool isNeedToClosePower = false;
  2017. // for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  2018. // {
  2019. // if (isStartOutputSwitch[index])
  2020. // {
  2021. // isNeedToClosePower = true;
  2022. // }
  2023. // isStartOutputSwitch[index] = false;
  2024. // }
  2025. //
  2026. // if (isNeedToClosePower)
  2027. // {
  2028. // SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  2029. // FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  2030. // }
  2031. // }
  2032. // else
  2033. // {
  2034. if (isStartOutputSwitch[groupIndex] &&
  2035. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  2036. {
  2037. isStartOutputSwitch[groupIndex] = false;
  2038. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  2039. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  2040. }
  2041. //}
  2042. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_WAITING)
  2043. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  2044. }
  2045. }
  2046. else if ((chargingInfo[groupIndex]->SystemStatus >= S_PREPARNING && chargingInfo[groupIndex]->SystemStatus <= S_PREPARING_FOR_EV) &&
  2047. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  2048. {
  2049. //PRINTF_FUNC("%d ******** 7 \n", groupIndex);
  2050. if (isStartOutputSwitch[groupIndex])
  2051. {
  2052. isStartOutputSwitch[groupIndex] = false;
  2053. SwitchPower(groupIndex, PSU_POWER_OFF); Await();
  2054. FlashLed(groupIndex, PSU_FLASH_NORMAL); Await();
  2055. }
  2056. }
  2057. }
  2058. startModuleFlag = false;
  2059. break;
  2060. }
  2061. case _TEST_MODE:
  2062. {
  2063. // 在測試模式中,保持與模塊的通訊
  2064. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  2065. if (time < 0)
  2066. gettimeofday(&_cmdSubPriority_time, NULL);
  2067. if (time > 1500)
  2068. {
  2069. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  2070. {
  2071. // 取系統總輸出能力
  2072. GetModuleCap(index); Await();
  2073. // 取各群輸出電壓電流 (float)
  2074. GetModuleOutputF(index); Await();
  2075. }
  2076. gettimeofday(&_cmdSubPriority_time, NULL);
  2077. }
  2078. byte _switch = 0x00;
  2079. if ((chargingInfo[0]->EvBatterytargetVoltage * 10) > 0 && (chargingInfo[0]->EvBatterytargetCurrent * 10) > 0)
  2080. _switch = 0x01;
  2081. for (byte _groupCount_1 = 0; _groupCount_1 < conn_1_count; _groupCount_1++)
  2082. {
  2083. SetDirModulePresentOutput(connector_1[_groupCount_1],
  2084. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  2085. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  2086. _switch, _switch); Await();
  2087. }
  2088. for (byte _groupCount_2 = 0; _groupCount_2 < conn_2_count; _groupCount_2++)
  2089. {
  2090. SetDirModulePresentOutput(connector_2[_groupCount_2],
  2091. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  2092. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  2093. _switch, _switch); Await();
  2094. }
  2095. }
  2096. break;
  2097. }
  2098. usleep(20000);
  2099. }
  2100. return FAIL;
  2101. }