Module_PsuComm.c 83 KB

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