Module_PsuComm.c 78 KB

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