Module_PsuComm.c 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275
  1. #include "Module_PsuComm.h"
  2. #include "Config.h"
  3. #include "Common.h"
  4. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  5. #define PASS 1
  6. #define FAIL -1
  7. #define YES 1
  8. #define NO 0
  9. #define DERATING_COUNT 30
  10. #define DERATING_GAP 30
  11. #define ELEMENT_NOT_FIND 255
  12. #define CHK_VOL_RANGE 50
  13. #define CHK_CUR_RANGE 10
  14. #define DERATING_RANGE 100
  15. #define ZERO_CURRENT 10 // 該值須保持最小為 1A
  16. #define ZERO_VOLTAGE 50
  17. #define STOP_CURRENT 30
  18. #define PSU_MAX_CUR 1000
  19. #define PSU_MIN_VOL 1500
  20. #define PRE_CHARG_STEP_CUR 30
  21. #define PRE_CHARG_RANGE 50
  22. #define EQUAL 0
  23. #define CMD_DELAY_TIME 25000 // 25us
  24. #define PSU_TASK_CHECK_TIME 1
  25. #define GET_PSU_COUNT_INTERVAL 1 // unit: second
  26. #define GET_PSU_COUNT_TIME 13 // unit: second
  27. #define GET_PSU_LOCATION_INTERVAL 200 // unit: millisecond
  28. #define PSU_COUNT_CONFIRM_INTERVAL 200 // unit: millisecond
  29. #define PSU_COUNT_CONFIRM_TIME 1 // unit: second
  30. #define GET_PSU_VERSION_INTERVAL 200 // unit: millisecond
  31. #define GET_PSU_CAP_INTERVAL 200 // unit: millisecond
  32. #define GET_PSU_CAP_TIME 1 // unit: second
  33. #define STABLE_CURRENT_TOLERANCE 10 // unit: 0.1A, 1A
  34. #define MAX_STABLE_COUNT 24
  35. #define WAIT_EV_DERATING_TIMEOUT 5 // unit: second
  36. #define WAIT_SLAVE_POWER_OFF_TIMEOUT 5 // unit: second
  37. #define WAIT_PARALLEL_RELAY_DELAY 1 // unit: second
  38. #define WAIT_RELAY_CONFIRMED_TIME 3 // unit: second
  39. #define MAX_PREPARE_SWITCH_OFF_TIME 10 // unit: second
  40. #define MIN_POWER_OFF_TIME 1 // unit: second
  41. #define WAIT_SLAVE_TO_CHARGING 1 // unit: second
  42. #define WAIT_SLAVE_TIMEOUT 5 // unit: second
  43. #define WAIT_GRAB_TIME 15 // unit: second
  44. #define MAX_PSU_POWER_OFF_CURRENT 50 // unit: 0.1A, 5A
  45. #define MAX_PSU_POWER_OFF_VOLTAGE 100 // unit: 0.1A, 10V
  46. #define PRECHARGE_OFFSET_VOLTAGE 20 // unit: 0.1V, 2V
  47. #define PRECHARGE_RANGE_VOLTAGE 50 // unit: 0.1V, 5V
  48. #define WAIT_PRECHARGE_TIME 10 // unit: second
  49. #define EXTEND_CAPABILITY_DELAY 60 // unit: second
  50. #define EXTEND_LOADING 8000 // unit: 0.01%, 80%
  51. #define RELEASE_LOADING 5000 // unit: 0.01%, 50%
  52. #define RELEASE_LOADING_OFFSET 1000 // unit: 0.01%, 10%
  53. #define BALANCE_CURRENT_INTERVAL 100 // unit: 1ms, 100ms
  54. #define CURRENT_REACH_TARGET_TIME 5 // unit: 1s, 5s
  55. #define CURRENT_STABLE_TIME 10 // unit: 1s, 10s
  56. #define REACH_CURRENT_TOLERANCE 10 // unit: 0.1A, 1A
  57. #define MAX_ADJ_BALANCE_CURRENT 30 // unit: 0.1A, 3A
  58. #define START_BALANCE_CRITERIA 100 // unit: 0.01%, 1%
  59. #define STOP_BALANCE_CRITERIA 10 // unit: 0.01%, 0.1%
  60. #define EV_MAX_CURRENT_DELAY 120 // unit: second
  61. #define WAIT_SLAVE_READY_TIME 15 // unit: second
  62. #define WAIT_SLAVE_DELAY 1 // unit: second
  63. #define SHOW_OUTPUT_DELAY 1
  64. #define CURRENT_BALANCE_CRITERIA 300 // unit: 0.1A, 30A
  65. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  66. struct StatusCodeData *ShmStatusCodeData;
  67. struct PsuData *ShmPsuData;
  68. ChargerInfoData *ShmChargerInfo;
  69. PsuPositionInfoData *ShmPsuPosition;
  70. PsuGroupingInfoData *ShmPsuGrouping;
  71. PsuGroupCollectionData *ShmGroupCollection;
  72. bool libInitialize = false;
  73. byte _gunCount = 0;
  74. byte _maxGroupCount = 0;
  75. byte getAvailableCapOffset = 5;
  76. byte deratingKeepCount = 0;
  77. byte psuCmdSeq = _PSU_CMD_CAP;
  78. byte startModuleFlag = false;
  79. bool psuReceiveRecovery = false;
  80. unsigned short evseOutVol[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  81. unsigned short evseOutCur[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  82. unsigned short evseOutputDelay[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  83. struct timespec _PsuReceiveRecoveryCheck_time;
  84. struct timespec _PsuWorkStep_time;
  85. struct timespec _cmdSubPriority_time;
  86. struct timespec _PsuGroupRole_time[CONNECTOR_QUANTITY];
  87. struct timespec _ChargingRequest_time[CONNECTOR_QUANTITY];
  88. struct timespec _PsuGroupDerating_time[CONNECTOR_QUANTITY];
  89. struct timespec _StopCharging_time[CONNECTOR_QUANTITY];
  90. struct timespec _ExtendCapability_time[CONNECTOR_QUANTITY];
  91. struct timespec _ReachCurrent_time[CONNECTOR_QUANTITY];
  92. struct timespec _BalanceCurrent_time[CONNECTOR_QUANTITY];
  93. struct timespec _MaxCurrent_time[CONNECTOR_QUANTITY];
  94. struct timespec _StageCurrent_time[CONNECTOR_QUANTITY];
  95. struct timespec _CheckSlaveReady_time[CONNECTOR_QUANTITY];
  96. unsigned short GCTargetVoltage[CONNECTOR_QUANTITY];
  97. unsigned short GCTargetCurrent[CONNECTOR_QUANTITY];
  98. unsigned short StableOutputCurrent[CONNECTOR_QUANTITY];
  99. unsigned short MaxCurrentDemand[CONNECTOR_QUANTITY];
  100. unsigned short StageMaxCurrent[CONNECTOR_QUANTITY];
  101. GroupOutputConfigInfo PreGroupOutput[MAX_GROUP_QUANTITY];
  102. unsigned char OutputConfigStep[MAX_GROUP_QUANTITY];
  103. unsigned char _preOutputConfigStep[MAX_GROUP_QUANTITY];
  104. //=================================
  105. // Common routine
  106. //=================================
  107. // return psu group number
  108. // return -1 when out of TotalPsuQuantity
  109. int FindTargetGroup(byte address)
  110. {
  111. return address < ShmPsuPosition->TotalPsuQuantity ? ShmPsuPosition->PsuAddressInfo[address].GroupNo : -1;
  112. }
  113. // return psu index in the group
  114. // return -1 when out of TotalPsuQuantity
  115. int FindGroupIndex(byte address)
  116. {
  117. return address < ShmPsuPosition->TotalPsuQuantity ? ShmPsuPosition->PsuAddressInfo[address].GIndex : -1;
  118. }
  119. //=================================
  120. // Save data to share memory Function
  121. //=================================
  122. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  123. {
  124. if(GENERAL_GUN_QUANTITY > 0 && target < GENERAL_GUN_QUANTITY)
  125. {
  126. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].GeneralChargingData;
  127. return true;
  128. }
  129. return false;
  130. }
  131. //=================================
  132. // Alarm code mapping to share memory Function
  133. //=================================
  134. // 檢查 Byte 中某個 Bit 的值
  135. // _byte : 欲改變的 byte
  136. // _bit : 該 byte 的第幾個 bit
  137. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  138. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  139. {
  140. return ( _byte & mask_table[_bit] ) != 0x00;
  141. }
  142. void AbnormalStopAnalysis(byte gun_index, int errCode)
  143. {
  144. for (char i = 0; i < 4; i++)
  145. {
  146. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  147. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  148. {
  149. if(DetectBitValue(byteIndex , bitIndex) == 1)
  150. {
  151. switch(i)
  152. {
  153. case 0:
  154. {
  155. // err 1
  156. if (bitIndex == 2)
  157. {
  158. // 012307
  159. // fuse burn-out
  160. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFuseBurnOut = YES;
  161. }
  162. else if (bitIndex == 3)
  163. {
  164. // 012308
  165. // Communication fault between PFC and DCDC
  166. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcAndDcdcCommFault = YES;
  167. }
  168. else if (bitIndex == 6)
  169. {
  170. // 012309
  171. // Unbalance positive and negative BUS voltage
  172. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageUnbalance = YES;
  173. }
  174. else if (bitIndex == 7)
  175. {
  176. // 012310
  177. // BUS over voltage
  178. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusOverVoltage = YES;
  179. }
  180. }
  181. break;
  182. case 1:
  183. {
  184. // err 2
  185. if (bitIndex == 0)
  186. {
  187. // 012311
  188. // BUS voltage abnormal
  189. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageAbnormal = YES;
  190. }
  191. else if (bitIndex == 1)
  192. {
  193. // 012270
  194. // Over voltage of any phase
  195. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  196. }
  197. else if (bitIndex == 2)
  198. {
  199. // 012263
  200. // ID number repetition
  201. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  202. }
  203. else if (bitIndex == 3)
  204. {
  205. // 012312
  206. // BUS under voltage
  207. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusUnderVoltage = YES;
  208. }
  209. else if (bitIndex == 4)
  210. {
  211. // 012313
  212. // Phase loss
  213. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = YES;
  214. }
  215. else if (bitIndex == 6)
  216. {
  217. // 012271
  218. // Under voltage of any phase
  219. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  220. }
  221. }
  222. break;
  223. case 2:
  224. {
  225. // err3
  226. if (bitIndex == 0)
  227. {
  228. // 012240
  229. // CAN communication fault
  230. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCommunicationFail = YES;
  231. }
  232. else if (bitIndex == 1)
  233. {
  234. // 012275
  235. // DCDC uneven current sharing
  236. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuSevereUnevenCurrent = YES;
  237. }
  238. else if (bitIndex == 3)
  239. {
  240. // 012278
  241. // PFC power off
  242. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFfcSideShutDown = YES;
  243. }
  244. else if (bitIndex == 5)
  245. {
  246. // 012314
  247. // Full speed of fan
  248. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFullSpeed = YES;
  249. }
  250. else if (bitIndex == 6)
  251. {
  252. // 012266
  253. // DCDC power off
  254. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  255. }
  256. else if (bitIndex == 7)
  257. {
  258. // 012273
  259. // Module unders power limiting status
  260. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  261. }
  262. }
  263. break;
  264. case 3:
  265. {
  266. // err 4
  267. if (bitIndex == 0)
  268. {
  269. // 012315
  270. // Temperature power limiting
  271. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuTemperaturePowerLimit = YES;
  272. }
  273. else if (bitIndex == 1)
  274. {
  275. // 012316
  276. // AC power limiting
  277. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuAcPowerLimit = YES;
  278. }
  279. else if (bitIndex == 2)
  280. {
  281. // 012317
  282. // DCDC eeprom faults
  283. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcEepromFault = YES;
  284. }
  285. else if (bitIndex == 3)
  286. {
  287. // 012269
  288. // Fan faults
  289. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  290. }
  291. else if (bitIndex == 4)
  292. {
  293. // 012264
  294. // DCDC short circuit
  295. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  296. }
  297. else if (bitIndex == 5)
  298. {
  299. // 012318
  300. // PFC eeprom faults
  301. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcEepromFault = YES;
  302. }
  303. else if (bitIndex == 6)
  304. {
  305. // 012226
  306. // DCDC over temperature
  307. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  308. }
  309. else if (bitIndex == 7)
  310. {
  311. // 012319
  312. // DCDC output over voltage
  313. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = YES;
  314. }
  315. }
  316. break;
  317. }
  318. }
  319. // else
  320. // {
  321. // switch (byteIndex) {
  322. // case 0: {
  323. // if (bitIndex == 0)
  324. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  325. // else if (bitIndex == 5)
  326. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  327. // }
  328. // break;
  329. // case 1: {
  330. // if (bitIndex == 1)
  331. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  332. // else if (bitIndex == 2)
  333. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  334. // else if (bitIndex == 3)
  335. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  336. // else if (bitIndex == 4)
  337. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  338. // else if (bitIndex == 5)
  339. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  340. // }
  341. // break;
  342. // case 2: {
  343. // if (bitIndex == 1)
  344. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  345. // if (bitIndex == 2)
  346. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  347. // else if (bitIndex == 3)
  348. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  349. // else if (bitIndex == 4)
  350. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  351. // else if (bitIndex == 5)
  352. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  353. // else if (bitIndex == 6)
  354. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  355. // }
  356. // break;
  357. // }
  358. // }
  359. }
  360. }
  361. }
  362. //=================================
  363. // Callback Function
  364. //=================================
  365. // 0x04: PSU_RCmd_ModuleStatus
  366. void GetStatusCallback(byte group, byte SN, byte temp, int alarm)
  367. {
  368. if(ShmPsuData->Work_Step != Get_PSU_LOCATION)
  369. {
  370. return;
  371. }
  372. if(group >= _maxGroupCount || SN >= MAX_MODULE_PER_GROUP)
  373. {
  374. return;
  375. }
  376. ShmPsuPosition->PsuAddressInfo[SN].Address = SN;
  377. ShmPsuPosition->PsuAddressInfo[SN].GroupNo = group;
  378. if(!ShmPsuPosition->PsuAddressInfo[SN].CheckIn)
  379. {
  380. ShmPsuPosition->PsuAddressInfo[SN].CheckIn = true;
  381. ShmPsuPosition->TotalPsuQuantity++;
  382. //LOG_INFO("SN = %d At Group %02X", SN, group);
  383. }
  384. if(ShmPsuPosition->TotalPsuQuantity == ShmPsuData->SystemPresentPsuQuantity)
  385. {
  386. byte group = 0, quantity = 0;
  387. if(!ShmPsuPosition->PsuLocationInit)
  388. {
  389. memset(ShmPsuPosition->GroupLocationInfo, 0x00, sizeof(ShmPsuPosition->GroupLocationInfo));
  390. for(int index = 0; index < MAX_PSU_MODULE_QUANTITY; index++)
  391. {
  392. if(ShmPsuPosition->PsuAddressInfo[index].CheckIn)
  393. {
  394. group = ShmPsuPosition->PsuAddressInfo[index].GroupNo;
  395. quantity = ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity;
  396. ShmPsuPosition->PsuAddressInfo[index].GIndex = quantity;
  397. ShmPsuPosition->GroupLocationInfo[group].PsuSN[quantity] = ShmPsuPosition->PsuAddressInfo[index].Address;
  398. ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity++;
  399. //LOG_INFO("Psu %d Assign To Group %02X At %d", index, group, ShmPsuPosition->PsuAddressInfo[index].GIndex);
  400. }
  401. }
  402. bool match = true;
  403. for(int index = 0; index < _maxGroupCount; index++)
  404. {
  405. if(ShmPsuPosition->GroupLocationInfo[index].GroupPsuQuantity != ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity)
  406. {
  407. match = false;
  408. }
  409. }
  410. if(match)
  411. {
  412. ShmPsuPosition->PsuLocationInit = true;
  413. }
  414. else
  415. {
  416. // try to re-initial psu location
  417. ShmPsuPosition->ReInitPsuLocation = true;
  418. }
  419. }
  420. }
  421. }
  422. // 0x02: PSU_RCmd_SysModuleCount
  423. void GetModuleCountCallback(byte group, byte count)
  424. {
  425. if(group == SYSTEM_CMD)
  426. {
  427. ShmPsuData->SystemPresentPsuQuantity = count;
  428. }
  429. else
  430. {
  431. if(group < _maxGroupCount)
  432. {
  433. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  434. }
  435. }
  436. }
  437. void UpdateGunAvailableCapability(unsigned char group)
  438. {
  439. int _availableCurrent = 0, _availablePower = 0, _realPower = 0;
  440. unsigned char master = 0;
  441. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  442. // calculate output group capability
  443. if(master == 0)
  444. {
  445. chargingInfo[group]->AvailableChargingCurrent = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  446. chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  447. chargingInfo[group]->RealRatingPower = ShmPsuData->PsuGroup[group].GroupRealOutputPower * 10;
  448. }
  449. else
  450. {
  451. if(ShmPsuGrouping->GroupCollection[master - 1].Role == _GROLE_MASTER)
  452. {
  453. _availableCurrent = ShmPsuData->PsuGroup[master - 1].GroupAvailableCurrent;
  454. _availablePower = ShmPsuData->PsuGroup[master - 1].GroupAvailablePower;
  455. _realPower = ShmPsuData->PsuGroup[master - 1].GroupRealOutputPower * 10;
  456. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  457. {
  458. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  459. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  460. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  461. {
  462. _availableCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  463. _availablePower += ShmPsuData->PsuGroup[slave].GroupAvailablePower;
  464. _realPower += ShmPsuData->PsuGroup[slave].GroupRealOutputPower * 10;
  465. }
  466. }
  467. if(ShmPsuGrouping->GroupCollection[master - 1].GroupCtrl.bits.DeratingConfirmed)
  468. {
  469. chargingInfo[master - 1]->AvailableChargingCurrent = ShmPsuGrouping->GroupCollection[master - 1].ReAssignAvailableCurrent;
  470. }
  471. else
  472. {
  473. chargingInfo[master - 1]->AvailableChargingCurrent = _availableCurrent;
  474. }
  475. chargingInfo[master - 1]->AvailableChargingPower = _availablePower;
  476. chargingInfo[master - 1]->RealRatingPower = _realPower;
  477. // if((master - 1) != group)
  478. // {
  479. // chargingInfo[group]->AvailableChargingCurrent = 0;;
  480. // chargingInfo[group]->AvailableChargingPower = 0;
  481. // chargingInfo[group]->RealRatingPower = 0;
  482. // }
  483. }
  484. }
  485. }
  486. void UpdateSystemAvailable(void)
  487. {
  488. int _sysCurrent = 0, _sysPower = 0;
  489. // summation of system current & power
  490. for(byte i = 0; i < _maxGroupCount; i++)
  491. {
  492. _sysCurrent += ShmPsuData->PsuGroup[i].GroupAvailableCurrent;
  493. _sysPower += ShmPsuData->PsuGroup[i].GroupAvailablePower;
  494. }
  495. ShmPsuData->SystemAvailableCurrent = _sysCurrent;
  496. ShmPsuData->SystemAvailablePower = _sysPower;
  497. }
  498. // maxVol, minVol unit: 0.1V
  499. // maxCur unit: 0.1A
  500. // totalPow unit: 0.1kW
  501. // 0x0A: PSU_RCmd_ModuleCapability
  502. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  503. {
  504. int _groupCurrent = 0, _groupPower = 0;
  505. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemPresentPsuQuantity ||
  506. !ShmPsuPosition->PsuLocationInit)
  507. {
  508. return;
  509. }
  510. byte master = 0;
  511. int group = FindTargetGroup(address);
  512. int gIndex = FindGroupIndex(address);
  513. if(group < 0 || gIndex < 0)
  514. {
  515. return;
  516. }
  517. // calculate psu module available power
  518. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailablePower = totalPow;
  519. //if(ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage >= PSU_MIN_VOL)
  520. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage >= PSU_MIN_VOL)
  521. {
  522. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailableCurrent =
  523. ((ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailablePower * 100) * 10) / (ShmPsuGrouping->GroupOutput[group].GTargetVoltage / 10);
  524. }
  525. else
  526. {
  527. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailableCurrent = maxCur > PSU_MAX_CUR ? PSU_MAX_CUR : maxCur;
  528. }
  529. // summation of psu group current & power
  530. for(byte i = 0; i < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; i++)
  531. {
  532. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[i].AvailableCurrent;
  533. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[i].AvailablePower;
  534. }
  535. if(ShmPsuData->PsuGroup[group].StableIAvailableCurrent != 0)
  536. {
  537. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = ShmPsuData->PsuGroup[group].StableIAvailableCurrent;
  538. }
  539. else
  540. {
  541. if(ShmPsuData->PsuGroup[group].GroupRealOutputPower != 0 &&
  542. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage >= PSU_MIN_VOL)
  543. {
  544. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = ((ShmPsuData->PsuGroup[group].GroupRealOutputPower * 1000) * 10) / (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10);
  545. }
  546. else
  547. {
  548. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  549. }
  550. }
  551. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  552. // update MaximumChargingVoltage
  553. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  554. if(master == 0)
  555. {
  556. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  557. }
  558. else
  559. {
  560. if(ShmPsuGrouping->GroupCollection[master - 1].Role == _GROLE_MASTER)
  561. {
  562. chargingInfo[master - 1]->MaximumChargingVoltage = maxVol;
  563. // if((master - 1) != group)
  564. // {
  565. // chargingInfo[group]->MaximumChargingVoltage = 0;
  566. // }
  567. }
  568. }
  569. UpdateGunAvailableCapability(group);
  570. UpdateSystemAvailable();
  571. //LOG_INFO("address = %d, maxVol = %d, minVol = %d, maxCur = %d, totalPow = %d", address, maxVol, minVol, maxCur, totalPow);
  572. }
  573. // 0x07: PSU_RCmd_ModuleVersion
  574. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  575. {
  576. if (ShmPsuData->Work_Step != Get_PSU_VERSION || address >= ShmPsuData->SystemPresentPsuQuantity ||
  577. !ShmPsuPosition->PsuLocationInit)
  578. {
  579. return;
  580. }
  581. int group = FindTargetGroup(address);
  582. int gIndex = FindGroupIndex(address);
  583. if(group < 0 || gIndex < 0)
  584. {
  585. return;
  586. }
  587. sprintf((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  588. sprintf((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, "PFC %d.%02d", (pfcSwVer & 0xFF00) >> 8, pfcSwVer & 0xFF);
  589. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[gIndex].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  590. //LOG_INFO("Psu %d %s %s", address, ShmPsuData->PsuVersion[address].FwPrimaryVersion, ShmPsuData->PsuVersion[address].FwSecondVersion);
  591. }
  592. // no using -- GetInputVoltageCallback
  593. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  594. {
  595. }
  596. // no using -- GetInputVoltageCallback End
  597. // no using -- GetPresentOutputCallback
  598. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  599. {
  600. }
  601. // no using -- GetPresentOutputCallback End
  602. // type 1: FAN_SPEED_CMD
  603. // type 2: TEMP_DC_CMD
  604. // type 3: TEMP_PFC_CMD
  605. // 0x0E: PSU_RCmd_ModuleMiscInfo
  606. void GetMisCallback(byte address, unsigned int value, byte type)
  607. {
  608. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemPresentPsuQuantity ||
  609. !ShmPsuPosition->PsuLocationInit)
  610. {
  611. return;
  612. }
  613. int group = FindTargetGroup(address);
  614. int gIndex = FindGroupIndex(address);
  615. if(group < 0 || gIndex < 0)
  616. {
  617. return;
  618. }
  619. if (type == 1)
  620. {
  621. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_1 = value;
  622. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_2 = value;
  623. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_3 = value;
  624. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_4 = value;
  625. }
  626. else if (type == 2)
  627. {
  628. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp1 = value;
  629. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp2 = value;
  630. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp3 = value;
  631. ShmPsuData->PsuGroup[group].PsuModule[gIndex].ExletTemp = value;
  632. }
  633. else if (type == 3)
  634. {
  635. //printf("PFC - group = %d, index = %d, value = %d \n", group, address, value);
  636. }
  637. }
  638. // Iavail unit: 0.1A
  639. // Vext unit: 0.1V
  640. // 0x0C: PSU_RCmd_ModuleIAvailable
  641. // 0x02CCF0XX
  642. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  643. {
  644. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemPresentPsuQuantity ||
  645. !ShmPsuPosition->PsuLocationInit)
  646. {
  647. return;
  648. }
  649. byte master = 0;
  650. unsigned short diffIAvailable = 0;
  651. int totalCurrent = 0;
  652. int group = FindTargetGroup(address);
  653. int gIndex = FindGroupIndex(address);
  654. if(group < 0 || gIndex < 0)
  655. {
  656. return;
  657. }
  658. ShmPsuData->PsuGroup[group].PsuModule[gIndex].IAvailableCurrent = Iavail;
  659. // summation of psu group i available current
  660. for (byte i = 0; i < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; i++)
  661. {
  662. totalCurrent += ShmPsuData->PsuGroup[group].PsuModule[i].IAvailableCurrent;
  663. }
  664. ShmPsuData->PsuGroup[group].TotalIAvailableCurrent = totalCurrent;
  665. diffIAvailable = totalCurrent >= ShmPsuData->PsuGroup[group].TempIAvailableCurrent ?
  666. totalCurrent - ShmPsuData->PsuGroup[group].TempIAvailableCurrent : ShmPsuData->PsuGroup[group].TempIAvailableCurrent - totalCurrent;
  667. if(diffIAvailable > STABLE_CURRENT_TOLERANCE)
  668. {
  669. ShmPsuData->PsuGroup[group].StableCurrentCounter = 0;
  670. ShmPsuData->PsuGroup[group].StableIAvailableCurrent = 0;
  671. }
  672. ShmPsuData->PsuGroup[group].TempIAvailableCurrent = ShmPsuData->PsuGroup[group].TotalIAvailableCurrent;
  673. ShmPsuData->PsuGroup[group].StableCurrentCounter++;
  674. if(ShmPsuData->PsuGroup[group].StableCurrentCounter >= MAX_STABLE_COUNT)
  675. {
  676. // get stable StableIAvailableCurrent
  677. ShmPsuData->PsuGroup[group].StableIAvailableCurrent = ShmPsuData->PsuGroup[group].TotalIAvailableCurrent;
  678. // get stable GroupRealOutputPower
  679. ShmPsuData->PsuGroup[group].GroupRealOutputPower = (ShmPsuData->PsuGroup[group].StableIAvailableCurrent * ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage) / 100 / 1000;
  680. ShmPsuData->PsuGroup[group].StableCurrentCounter = 0;
  681. }
  682. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  683. if(master == 0)
  684. {
  685. // unit: 0.1A
  686. chargingInfo[group]->DeratingChargingCurrent = ShmPsuData->PsuGroup[group].StableIAvailableCurrent;
  687. chargingInfo[group]->DeratingChargingPower = (chargingInfo[group]->DeratingChargingCurrent * chargingInfo[group]->PresentChargingVoltage) / 10 / 100;
  688. }
  689. else
  690. {
  691. // calculate DeratingChargingCurrent of master group
  692. totalCurrent = ShmPsuData->PsuGroup[master - 1].StableIAvailableCurrent;
  693. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  694. {
  695. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  696. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE)
  697. {
  698. totalCurrent += ShmPsuData->PsuGroup[slave].StableIAvailableCurrent;
  699. }
  700. }
  701. chargingInfo[master - 1]->DeratingChargingCurrent = totalCurrent;
  702. chargingInfo[master - 1]->DeratingChargingPower = (chargingInfo[master - 1]->DeratingChargingCurrent * chargingInfo[master - 1]->PresentChargingVoltage) / 10 / 100;
  703. if((master - 1) != group)
  704. {
  705. chargingInfo[group]->DeratingChargingCurrent = 0;
  706. chargingInfo[group]->DeratingChargingPower = 0;
  707. }
  708. }
  709. }
  710. // outVol unit: 1V
  711. // outCur unit: 1A
  712. // 0x01: PSU_RCmd_SysOutputVolCur_F
  713. void GetPresentOutputFCallback(byte group, float outVol, float outCur)
  714. {
  715. if (ShmPsuData->Work_Step < GET_SYS_CAP || group >= _maxGroupCount ||
  716. !ShmPsuPosition->PsuLocationInit)
  717. {
  718. return;
  719. }
  720. byte master = 0;
  721. unsigned short pVoltage = 0, pCurrent = 0;
  722. unsigned short gVoltage = 0, gCurrent = 0;
  723. pVoltage = isinf(outVol) == 0 ? (unsigned short)(outVol * 10) : 0;
  724. pCurrent = isinf(outCur) == 0 ? (unsigned short)(outCur * 10) : 0;
  725. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = pVoltage;
  726. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = pCurrent;
  727. ShmPsuData->PsuGroup[group].GroupPresentOutputPower = (pVoltage * pCurrent) / 100 / 100;
  728. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  729. if(master == 0)
  730. {
  731. // calculate PresentChargingVoltage and PresentChargingCurrent of self group
  732. chargingInfo[group]->PresentChargingVoltage = (float)ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10;
  733. chargingInfo[group]->PresentChargingCurrent = (float)ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10;
  734. }
  735. else
  736. {
  737. // calculate PresentChargingVoltage and PresentChargingCurrent of master group
  738. gVoltage = ShmPsuData->PsuGroup[master - 1].GroupPresentOutputVoltage;
  739. gCurrent = ShmPsuData->PsuGroup[master - 1].GroupPresentOutputCurrent;
  740. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  741. {
  742. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  743. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  744. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF ||
  745. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  746. {
  747. if(ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage > gVoltage)
  748. {
  749. gVoltage = ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage;
  750. }
  751. gCurrent += ShmPsuData->PsuGroup[slave].GroupPresentOutputCurrent;
  752. }
  753. }
  754. chargingInfo[master - 1]->PresentChargingVoltage = (float)gVoltage / 10;
  755. chargingInfo[master - 1]->PresentChargingCurrent = (float)gCurrent / 10;
  756. if((master - 1) != group)
  757. {
  758. chargingInfo[group]->PresentChargingVoltage = 0;
  759. chargingInfo[group]->PresentChargingCurrent = 0;
  760. }
  761. }
  762. }
  763. //==========================================
  764. // 特規用指令
  765. //==========================================
  766. // 0x1901: Nexton_PSU_DcOutputValue
  767. void GetOutputAndTempCallback(byte address, unsigned short outputVol_s,
  768. unsigned short outputCur_s, unsigned short outputPower, unsigned char Temperature)
  769. {
  770. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemPresentPsuQuantity ||
  771. !ShmPsuPosition->PsuLocationInit)
  772. {
  773. return;
  774. }
  775. int group = FindTargetGroup(address);
  776. int gIndex = FindGroupIndex(address);
  777. if(group < 0 || gIndex < 0)
  778. {
  779. return;
  780. }
  781. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp1 = Temperature;
  782. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp2 = Temperature;
  783. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp3 = Temperature;
  784. }
  785. // 0x1902: Nexton_PSU_StatusEvent
  786. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  787. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  788. {
  789. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemPresentPsuQuantity ||
  790. !ShmPsuPosition->PsuLocationInit)
  791. {
  792. return;
  793. }
  794. int group = FindTargetGroup(address);
  795. int gIndex = FindGroupIndex(address);
  796. if(group < 0 || gIndex < 0)
  797. {
  798. return;
  799. }
  800. int alarm = (err4 << 24) | (err3 << 16) | (err2 << 8) | err1;
  801. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AlarmCode = alarm;
  802. AbnormalStopAnalysis(group, alarm);
  803. if(isErr)
  804. {
  805. // 012267
  806. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  807. ShmPsuData->PsuGroup[group].GroupErrorFlag.bits.PsuFailure = true;
  808. }
  809. }
  810. // 0x1903: Nexton_PSU_AcInputValue
  811. void GetModuleInputCallback(byte address, unsigned short inputR,
  812. unsigned short inputS, unsigned short inputT)
  813. {
  814. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemPresentPsuQuantity ||
  815. !ShmPsuPosition->PsuLocationInit)
  816. {
  817. return;
  818. }
  819. int group = FindTargetGroup(address);
  820. int gIndex = FindGroupIndex(address);
  821. if(group < 0 || gIndex < 0)
  822. {
  823. return;
  824. }
  825. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL1 = inputR;
  826. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL2 = inputS;
  827. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL3 = inputT;
  828. }
  829. //==========================================
  830. // Init all share memory
  831. //==========================================
  832. int InitShareMemory()
  833. {
  834. int result = PASS;
  835. int MeterSMId;
  836. //creat ShmSysConfigAndInfo
  837. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  838. {
  839. #ifdef SystemLogMessage
  840. LOG_ERROR("shmget ShmSysConfigAndInfo NG %d");
  841. #endif
  842. result = FAIL;
  843. }
  844. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  845. {
  846. #ifdef SystemLogMessage
  847. LOG_ERROR("shmat ShmSysConfigAndInfo NG");
  848. #endif
  849. result = FAIL;
  850. }
  851. else
  852. {}
  853. //creat ShmStatusCodeData
  854. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  855. {
  856. #ifdef SystemLogMessage
  857. LOG_ERROR("shmget ShmStatusCodeData NG");
  858. #endif
  859. result = FAIL;
  860. }
  861. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  862. {
  863. #ifdef SystemLogMessage
  864. LOG_ERROR("shmat ShmStatusCodeData NG");
  865. #endif
  866. result = FAIL;
  867. }
  868. else
  869. {}
  870. //creat ShmPsuData
  871. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  872. {
  873. #ifdef SystemLogMessage
  874. LOG_ERROR("shmget ShmPsuData NG");
  875. #endif
  876. result = FAIL;
  877. }
  878. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  879. {
  880. #ifdef SystemLogMessage
  881. LOG_ERROR("shmat ShmPsuData NG");
  882. #endif
  883. result = FAIL;
  884. }
  885. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  886. {
  887. #ifdef SystemLogMessage
  888. LOG_ERROR("shmat ChargerInfoData NG");
  889. #endif
  890. result = FAIL;
  891. }
  892. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  893. {
  894. #ifdef SystemLogMessage
  895. LOG_ERROR("shmat ChargerInfoData NG");
  896. #endif
  897. result = FAIL;
  898. }
  899. if(result == PASS)
  900. {
  901. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  902. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  903. ShmGroupCollection = &ShmPsuGrouping->GroupCollection[0];
  904. }
  905. return result;
  906. }
  907. //================================================
  908. // Main process
  909. //================================================
  910. void InitialPsuData()
  911. {
  912. ShmPsuData->SystemPresentPsuQuantity = 0;
  913. ShmPsuData->SystemAvailablePower = 0;
  914. LOG_INFO("************ psu Group = %d", ShmPsuData->GroupCount);
  915. for (byte _groupCount = 0; _groupCount < ShmPsuData->GroupCount; _groupCount++)
  916. {
  917. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  918. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  919. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  920. ShmPsuData->PsuGroup[_groupCount].GroupErrorFlag.PsuGroupErrorValue = 0;
  921. }
  922. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFuseBurnOut = NO;
  923. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcAndDcdcCommFault = NO;
  924. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageUnbalance = NO;
  925. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusOverVoltage = NO;
  926. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageAbnormal = NO;
  927. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  928. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  929. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusUnderVoltage = NO;
  930. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = NO;
  931. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  932. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCommunicationFail = NO;
  933. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuSevereUnevenCurrent = NO;
  934. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFfcSideShutDown = NO;
  935. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFullSpeed = NO;
  936. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  937. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = NO;
  938. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuTemperaturePowerLimit = NO;
  939. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuAcPowerLimit = NO;
  940. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcEepromFault = NO;
  941. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  942. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  943. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcEepromFault = NO;
  944. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  945. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = NO;
  946. }
  947. void Initialization()
  948. {
  949. bool isPass = false;
  950. while(!isPass)
  951. {
  952. isPass = true;
  953. for (byte _index = 0; _index < _maxGroupCount; _index++)
  954. {
  955. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  956. {
  957. LOG_ERROR("Module_PsuComm : FindChargingInfoData false");
  958. isPass = false;
  959. break;
  960. }
  961. }
  962. sleep(1);
  963. }
  964. ShmPsuData->GroupCount = _maxGroupCount;
  965. }
  966. void Await()
  967. {
  968. usleep(CMD_DELAY_TIME);
  969. }
  970. void PsuReceiveRecoveryCheck(void)
  971. {
  972. char *ptrSave, *ptrToken;
  973. int fd, psuTaskCount = 0;
  974. char pathBuffer[64], psuTaskPidString[64];
  975. system("pidof Module_PsuComm > /tmp/Module_PsuTask");
  976. snprintf(pathBuffer, sizeof(pathBuffer), "/tmp/Module_PsuTask");
  977. fd = open(pathBuffer, O_RDWR);
  978. if(fd < 0)
  979. {
  980. return;
  981. }
  982. if(read(fd, psuTaskPidString, 64) < 0)
  983. {
  984. return;
  985. }
  986. ptrToken = strtok_r(psuTaskPidString, " ", &ptrSave);
  987. while(ptrToken != NULL)
  988. {
  989. int psuPid = atoi(ptrToken);
  990. if(psuPid > 0)
  991. {
  992. psuTaskCount++;
  993. }
  994. ptrToken = strtok_r(NULL, " ", &ptrSave);
  995. }
  996. close(fd);
  997. if(psuTaskCount == 1)
  998. {
  999. LOG_INFO("************ PSU Receive Task Need Recovery ************\n");
  1000. InitialCommunication();
  1001. psuReceiveRecovery = true;
  1002. }
  1003. }
  1004. void ShowGroupMember(unsigned char group)
  1005. {
  1006. if(group < MAX_GROUP_QUANTITY)
  1007. {
  1008. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1009. {
  1010. for(byte psu = 0; psu < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; psu++)
  1011. {
  1012. LOG_INFO("Group %d - Number = %d, SN = %02X", group + 1, psu,
  1013. ShmPsuPosition->GroupLocationInfo[group].PsuSN[psu]);
  1014. }
  1015. }
  1016. }
  1017. }
  1018. void ShowPsuVersion(unsigned char group)
  1019. {
  1020. if(group < MAX_GROUP_QUANTITY)
  1021. {
  1022. if(ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity > 0)
  1023. {
  1024. LOG_INFO("Group %d PSU Version Info", group + 1);
  1025. for(int i = 0; i < ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity; i++)
  1026. {
  1027. int psuIndex = ShmPsuPosition->GroupLocationInfo[group].PsuSN[i];
  1028. LOG_INFO("PSU Primary: %s, Second: %s",
  1029. ShmPsuData->PsuVersion[psuIndex].FwPrimaryVersion,
  1030. ShmPsuData->PsuVersion[psuIndex].FwSecondVersion);
  1031. }
  1032. }
  1033. }
  1034. }
  1035. void ShowGroupAvailableCurrentPower(unsigned char group)
  1036. {
  1037. if(group < MAX_GROUP_QUANTITY)
  1038. {
  1039. LOG_INFO("Group %d Available Current = %3d.%dA, Power = %3d.%dkW", group + 1,
  1040. (ShmPsuData->PsuGroup[group].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[group].GroupAvailableCurrent % 10),
  1041. (ShmPsuData->PsuGroup[group].GroupAvailablePower / 10), (ShmPsuData->PsuGroup[group].GroupAvailablePower % 10));
  1042. }
  1043. }
  1044. void PsuGroupRoutineQuery(void)
  1045. {
  1046. for(byte group = 0; group < GENERAL_GUN_QUANTITY; group++)
  1047. {
  1048. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1049. {
  1050. if (psuCmdSeq == _PSU_CMD_CAP)
  1051. {
  1052. // 取系統總輸出能力
  1053. GetModuleCap(group);
  1054. }
  1055. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1056. {
  1057. // 取各群輸出電壓電流 (float)
  1058. GetModuleOutputF(group);
  1059. }
  1060. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1061. {
  1062. // 取得模塊輸出額定電流能力
  1063. GetModuleIavailable(group);
  1064. }
  1065. else if (psuCmdSeq == _PSU_CMD_TEMP)
  1066. {
  1067. // 取得模塊溫度
  1068. GetDcTemperature(group);
  1069. }
  1070. }
  1071. }
  1072. if(psuCmdSeq == _PSU_CMD_CAP)
  1073. {
  1074. psuCmdSeq = _PSU_CMD_OUTPUT;
  1075. }
  1076. else if(psuCmdSeq == _PSU_CMD_OUTPUT)
  1077. {
  1078. psuCmdSeq = _PSU_CMD_IVAILIABLE;
  1079. }
  1080. else if(psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1081. {
  1082. psuCmdSeq = _PSU_CMD_TEMP;
  1083. }
  1084. else
  1085. {
  1086. psuCmdSeq = _PSU_CMD_CAP;
  1087. }
  1088. Await();
  1089. }
  1090. void SetPsuGroupRole(byte group, byte role)
  1091. {
  1092. if(group < GENERAL_GUN_QUANTITY)
  1093. {
  1094. ShmGroupCollection[group].Role = role;
  1095. }
  1096. }
  1097. void SetPsuGroupToIdle(byte group)
  1098. {
  1099. if(group < GENERAL_GUN_QUANTITY)
  1100. {
  1101. SetPsuGroupRole(group, _GROLE_IDLE);
  1102. memset(&ShmGroupCollection[group].Partner, 0x00, sizeof(PsuGroupPartner));
  1103. memset(&ShmGroupCollection[group].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  1104. ShmGroupCollection[group].TargetGroup = 0;
  1105. ShmGroupCollection[group].ReservedTarget = 0;
  1106. //LOG_INFO("Reset Group[%02X] To Idle", group);
  1107. }
  1108. }
  1109. // group: self group index
  1110. void SetPsuGroupToMaster(byte group)
  1111. {
  1112. if(group < GENERAL_GUN_QUANTITY)
  1113. {
  1114. SetPsuGroupRole(group, _GROLE_MASTER);
  1115. ShmGroupCollection[group].TargetGroup = group + 1;
  1116. //LOG_INFO("Set Group[%02X] As Master To Gun %d", group, group + 1);
  1117. }
  1118. }
  1119. // group: self group index
  1120. // target: target group index + 1
  1121. void SetPsuGroupToSlave(byte group, byte target)
  1122. {
  1123. if(group < GENERAL_GUN_QUANTITY && target <= GENERAL_GUN_QUANTITY)
  1124. {
  1125. SetPsuGroupRole(group, _GROLE_SLAVE);
  1126. ShmGroupCollection[group].TargetGroup = target;
  1127. ShmGroupCollection[group].ReservedTarget = 0;
  1128. memset(&ShmGroupCollection[group].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  1129. ShmGroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  1130. //LOG_INFO("Set Group[%02X] As Slave To Gun %d", group, target);
  1131. }
  1132. }
  1133. // target: target group index + 1
  1134. // tPartner: set member to power off
  1135. void PrepareToPowerOff(byte target, PsuGroupPartner *tPartner)
  1136. {
  1137. unsigned char master = 0, slave = 0;
  1138. char strMember[64];
  1139. bool find = false;
  1140. sprintf(strMember, "Set Member:");
  1141. for(int i = 0; i < tPartner->Quantity; i++)
  1142. {
  1143. slave = tPartner->Member[i];
  1144. master = ShmGroupCollection[slave].TargetGroup - 1;
  1145. SetPsuGroupRole(slave, _GROLE_PREPARE_SWITCH_OFF);
  1146. ShmGroupCollection[slave].ReservedTarget = target;
  1147. ShmGroupCollection[master].GroupCtrl.bits.NeedDerating = true;
  1148. char strSlave[8];
  1149. sprintf(strSlave, " [%02X]", slave);
  1150. strcat(strMember, strSlave);
  1151. find = true;
  1152. }
  1153. if(find)
  1154. {
  1155. char strTemp[32];
  1156. if(target != 0)
  1157. {
  1158. sprintf(strTemp, " Prepare To Change To Gun %d", target);
  1159. }
  1160. else
  1161. {
  1162. sprintf(strTemp, " Prepare To Power Of");
  1163. }
  1164. strcat(strMember, strTemp);
  1165. LOG_INFO("%s", strMember);
  1166. }
  1167. }
  1168. // target: target group index + 1
  1169. // tPartner: set member to prepare to attach on
  1170. void PrepareToExtendCapability(byte target, PsuGroupPartner *tPartner)
  1171. {
  1172. char strMember[64];
  1173. sprintf(strMember, "Set Member:");
  1174. for(int i = 0; i < tPartner->Quantity; i++)
  1175. {
  1176. SetPsuGroupRole(tPartner->Member[i], _GROLE_PREPARE_ATTACH_ON);
  1177. ShmGroupCollection[tPartner->Member[i]].ReservedTarget = target;
  1178. char strSlave[8];
  1179. sprintf(strSlave, " [%02X]", tPartner->Member[i]);
  1180. strcat(strMember, strSlave);
  1181. }
  1182. if(tPartner->Quantity > 0)
  1183. {
  1184. char strTemp[32];
  1185. sprintf(strTemp, " Prepare To Attach To Gun %d", target);
  1186. strcat(strMember, strTemp);
  1187. LOG_INFO("%s", strMember);
  1188. }
  1189. }
  1190. void FindPsuGroupPartner(byte master, byte quantity, PsuGroupPartner *tPartner)
  1191. {
  1192. int slave = 0, location = 0;
  1193. PsuGroupPartner partner;
  1194. memset(&partner, 0x00, sizeof(PsuGroupPartner));
  1195. // search from left
  1196. location = ShmGroupCollection[master].Location - 1;
  1197. for(int i = location; i >= 0; i--)
  1198. {
  1199. if(partner.Quantity >= quantity)
  1200. {
  1201. break;
  1202. }
  1203. slave = ShmPsuGrouping->Layout[i];
  1204. if(ShmGroupCollection[slave].Role == _GROLE_IDLE ||
  1205. (ShmGroupCollection[slave].Role == _GROLE_WAIT_SLAVE && (ShmGroupCollection[slave].ReservedTarget - 1) == master))
  1206. {
  1207. partner.Member[partner.Quantity++] = slave;
  1208. }
  1209. else
  1210. {
  1211. if(ShmGroupCollection[slave].Role == _GROLE_SLAVE && master == (ShmGroupCollection[slave].TargetGroup - 1))
  1212. {
  1213. continue;
  1214. }
  1215. break;
  1216. }
  1217. }
  1218. // search from right
  1219. location = ShmGroupCollection[master].Location + 1;
  1220. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1221. {
  1222. if(partner.Quantity >= quantity)
  1223. {
  1224. break;
  1225. }
  1226. slave = ShmPsuGrouping->Layout[i];
  1227. if(ShmGroupCollection[slave].Role == _GROLE_IDLE ||
  1228. (ShmGroupCollection[slave].Role == _GROLE_WAIT_SLAVE && (ShmGroupCollection[slave].ReservedTarget - 1) == master))
  1229. {
  1230. partner.Member[partner.Quantity++] = slave;
  1231. }
  1232. else
  1233. {
  1234. if(ShmGroupCollection[slave].Role == _GROLE_SLAVE && master == (ShmGroupCollection[slave].TargetGroup - 1))
  1235. {
  1236. continue;
  1237. }
  1238. break;
  1239. }
  1240. }
  1241. memcpy(tPartner, &partner, sizeof(PsuGroupPartner));
  1242. }
  1243. int GetPsuGroupAvailable(byte group)
  1244. {
  1245. PsuGroupPartner partner;
  1246. FindPsuGroupPartner(group, MAX_GROUP_QUANTITY, &partner);
  1247. return partner.Quantity;
  1248. }
  1249. // return grab success or fail
  1250. // return tPartner with possible member
  1251. bool PsuGroupGrabCheck(byte group, PsuGroupPartner *tPartner)
  1252. {
  1253. int slave = 0, target = 0, location = 0, share = 0, average = 0, total = 0;
  1254. if(ShmGroupCollection[group].Role != _GROLE_SLAVE && ShmGroupCollection[group].Role != _GROLE_REQUEST_TO_CHARGING)
  1255. {
  1256. return share > 0 ? true : false;
  1257. }
  1258. total = ShmGroupCollection[group].Role == _GROLE_REQUEST_TO_CHARGING ? GetPsuGroupAvailable(group) + 1 : 0;
  1259. // search from left
  1260. location = ShmGroupCollection[group].Location - 1;
  1261. for(int i = location; i >= 0; i--)
  1262. {
  1263. slave = ShmPsuGrouping->Layout[i];
  1264. if(ShmGroupCollection[slave].Role == _GROLE_SLAVE &&
  1265. (ShmGroupCollection[group].Role == _GROLE_REQUEST_TO_CHARGING ||
  1266. ShmGroupCollection[slave].TargetGroup == ShmGroupCollection[group].TargetGroup))
  1267. {
  1268. target = ShmGroupCollection[slave].TargetGroup - 1;
  1269. if((ShmGroupCollection[target].Partner.Quantity + 1) > total)
  1270. {
  1271. average = (ShmGroupCollection[target].Partner.Quantity + 1 + total) / 2;
  1272. share = average > 0 ? average - 1 : 0;
  1273. }
  1274. break;
  1275. }
  1276. else
  1277. {
  1278. break;
  1279. }
  1280. }
  1281. if(share == 0)
  1282. {
  1283. // search from right
  1284. location = ShmGroupCollection[group].Location + 1;
  1285. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1286. {
  1287. slave = ShmPsuGrouping->Layout[i];
  1288. if(ShmGroupCollection[slave].Role == _GROLE_SLAVE &&
  1289. (ShmGroupCollection[group].Role == _GROLE_REQUEST_TO_CHARGING ||
  1290. ShmGroupCollection[slave].TargetGroup == ShmGroupCollection[group].TargetGroup))
  1291. {
  1292. target = ShmGroupCollection[slave].TargetGroup - 1;
  1293. if((ShmGroupCollection[target].Partner.Quantity + 1) > total)
  1294. {
  1295. average = (ShmGroupCollection[target].Partner.Quantity + 1 + total) / 2;
  1296. share = average > 0 ? average - 1 : 0;
  1297. }
  1298. break;
  1299. }
  1300. else
  1301. {
  1302. break;
  1303. }
  1304. }
  1305. }
  1306. tPartner->Quantity = share;
  1307. tPartner->Member[0] = share > 0 ? slave : 0;
  1308. return share > 0 ? true : false;
  1309. }
  1310. // return tPartner with possible member when group power off
  1311. void PsuGroupPowerOffCheck(byte group, PsuGroupPartner *tPartner)
  1312. {
  1313. int master = 0, quantity = 0, location = 0;
  1314. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  1315. if(ShmGroupCollection[group].Role != _GROLE_SLAVE)
  1316. {
  1317. return;
  1318. }
  1319. master = ShmGroupCollection[group].TargetGroup - 1;
  1320. quantity = ShmGroupCollection[master].Partner.Quantity;
  1321. for(int i = 0; i < quantity; i++)
  1322. {
  1323. if(tPartner->Quantity == 0)
  1324. {
  1325. if(group == ShmGroupCollection[master].Partner.Member[i])
  1326. {
  1327. location = i;
  1328. tPartner->Member[tPartner->Quantity] = group;
  1329. tPartner->Quantity++;
  1330. }
  1331. }
  1332. else
  1333. {
  1334. // find other group in the same direction
  1335. if(ShmPsuGrouping->Location[ShmGroupCollection[master].Partner.Member[location]] < ShmPsuGrouping->Location[master])
  1336. {
  1337. if(ShmPsuGrouping->Location[ShmGroupCollection[master].Partner.Member[i]] < ShmPsuGrouping->Location[master])
  1338. {
  1339. tPartner->Member[tPartner->Quantity] = ShmGroupCollection[master].Partner.Member[i];
  1340. tPartner->Quantity++;
  1341. //printf("\r\n Find Other Group %02X In The Same Direction", ShmGroupCollection[master].Partner.Member[i]);
  1342. }
  1343. }
  1344. if(ShmPsuGrouping->Location[ShmGroupCollection[master].Partner.Member[location]] > ShmPsuGrouping->Location[master])
  1345. {
  1346. if(ShmPsuGrouping->Location[ShmGroupCollection[master].Partner.Member[i]] > ShmPsuGrouping->Location[master])
  1347. {
  1348. tPartner->Member[tPartner->Quantity] = ShmGroupCollection[master].Partner.Member[i];
  1349. tPartner->Quantity++;
  1350. //printf("\r\n Find Other Group %02X In The Same Direction", ShmGroupCollection[master].Partner.Member[i]);
  1351. }
  1352. }
  1353. }
  1354. }
  1355. }
  1356. // group: group index, target: target index
  1357. // master: master group index
  1358. void AddMember(byte group, byte master)
  1359. {
  1360. if(group < GENERAL_GUN_QUANTITY && master < GENERAL_GUN_QUANTITY)
  1361. {
  1362. if(ShmGroupCollection[master].Role != _GROLE_MASTER &&
  1363. ShmGroupCollection[master].Role != _GROLE_REQUEST_TO_CHARGING)
  1364. {
  1365. return;
  1366. }
  1367. if(ShmGroupCollection[group].Role != _GROLE_IDLE &&
  1368. ShmGroupCollection[group].Role != _GROLE_WAIT_SLAVE &&
  1369. ShmGroupCollection[group].Role != _GROLE_PRECHARGE_READY)
  1370. {
  1371. return;
  1372. }
  1373. SetPsuGroupToSlave(group, master + 1);
  1374. ShmGroupCollection[master].Partner.Member[ShmGroupCollection[master].Partner.Quantity++] = group;
  1375. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1376. {
  1377. ShmGroupCollection[master].Partner.RealQuantity++;
  1378. }
  1379. //printf("\r\n Add Group %02X To Gun %d (Quantity %d), Set Parallel Relay %d On", group, target + 1, ShmGroupCollection[target].Partner.Quantity, ParallelConfig);
  1380. }
  1381. }
  1382. // master: master group index
  1383. void AddAvailableMember(unsigned char master)
  1384. {
  1385. PsuGroupPartner partner;
  1386. char strMember[64];
  1387. FindPsuGroupPartner(master, MAX_GROUP_QUANTITY, &partner);
  1388. sprintf(strMember, "Gun %d Add Available Member:", master + 1);
  1389. for(int i = 0; i < partner.Quantity; i++)
  1390. {
  1391. AddMember(partner.Member[i], master);
  1392. char strSlave[8];
  1393. sprintf(strSlave, " [%02X]", partner.Member[i]);
  1394. strcat(strMember, strSlave);
  1395. }
  1396. if(partner.Quantity > 0)
  1397. {
  1398. LOG_INFO("%s", strMember);
  1399. }
  1400. }
  1401. // master: master group index
  1402. // slave: slave group index
  1403. void RemoveMember(unsigned char master, unsigned char slave)
  1404. {
  1405. bool find = false;
  1406. int location = 0;
  1407. unsigned char other = 0;
  1408. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1409. {
  1410. if(slave == ShmGroupCollection[master].Partner.Member[i])
  1411. {
  1412. ShmGroupCollection[master].Partner.Member[i] = 0;
  1413. location = i;
  1414. find = true;
  1415. break;
  1416. }
  1417. }
  1418. if(find)
  1419. {
  1420. for(int i = location + 1; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1421. {
  1422. other = ShmGroupCollection[master].Partner.Member[i];
  1423. ShmGroupCollection[master].Partner.Member[i] = 0;
  1424. ShmGroupCollection[master].Partner.Member[i - 1] = other;
  1425. }
  1426. ShmGroupCollection[master].Partner.Quantity--;
  1427. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  1428. {
  1429. ShmGroupCollection[master].Partner.RealQuantity--;
  1430. }
  1431. }
  1432. }
  1433. // master: master group index
  1434. void RemoveNonGroupMember(unsigned char master)
  1435. {
  1436. unsigned char slave = 0;
  1437. char strMember[64];
  1438. PsuGroupPartner partner;
  1439. bool find = false;
  1440. if(ShmGroupCollection[master].Role == _GROLE_MASTER)
  1441. {
  1442. sprintf(strMember, "Gun %d Remove Member:", master + 1);
  1443. memcpy(&partner, &ShmGroupCollection[master].Partner, sizeof(PsuGroupPartner));
  1444. for(int i = 0; i < partner.Quantity; i++)
  1445. {
  1446. slave = partner.Member[i];
  1447. if(ShmGroupCollection[slave].Role != _GROLE_SLAVE)
  1448. {
  1449. RemoveMember(master, slave);
  1450. if(ShmGroupCollection[slave].ReservedTarget == 0)
  1451. {
  1452. SetPsuGroupToIdle(slave);
  1453. }
  1454. else
  1455. {
  1456. SetPsuGroupRole(slave, _GROLE_WAIT_SLAVE);
  1457. }
  1458. char strSlave[8];
  1459. sprintf(strSlave, " [%02X]", slave);
  1460. strcat(strMember, strSlave);
  1461. find = true;
  1462. }
  1463. }
  1464. if(find)
  1465. {
  1466. LOG_INFO("%s", strMember);
  1467. }
  1468. }
  1469. }
  1470. void SetPsuGroupPowerOnOff(unsigned char group, unsigned char power_on_off)
  1471. {
  1472. if(group < CONNECTOR_QUANTITY)
  1473. {
  1474. enum PSU_POWER_CMD power_cmd = power_on_off == PSU_POWER_ON ? PSU_POWER_ON : PSU_POWER_OFF;
  1475. enum PSU_FLASH_CMD led_cmd = power_on_off == PSU_POWER_ON ? PSU_FLASH_ON : PSU_FLASH_NORMAL;
  1476. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1477. {
  1478. SwitchPower(group, power_cmd);
  1479. Await();
  1480. FlashLed(group, led_cmd);
  1481. Await();
  1482. }
  1483. isStartOutputSwitch[group] = power_on_off == PSU_POWER_ON ? true : false;
  1484. }
  1485. }
  1486. // master: master group index
  1487. unsigned short GetPresentTargetCurrent(unsigned char master, unsigned char role_condition)
  1488. {
  1489. unsigned char slave = 0;
  1490. unsigned short current = 0;
  1491. current = ShmPsuGrouping->GroupOutput[master].GTargetCurrent;
  1492. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1493. {
  1494. slave = ShmGroupCollection[master].Partner.Member[i];
  1495. if(role_condition == _GROLE_MASTER || ShmGroupCollection[slave].Role == _GROLE_SLAVE)
  1496. {
  1497. current += ShmPsuGrouping->GroupOutput[slave].GTargetCurrent;
  1498. }
  1499. }
  1500. return current;
  1501. }
  1502. // group: group index
  1503. int GetPsuModuleQuantity(unsigned char group)
  1504. {
  1505. int quantity = 0;
  1506. unsigned char slave = 0;
  1507. quantity = ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity;
  1508. for(int i = 0; i < ShmGroupCollection[group].Partner.Quantity; i++)
  1509. {
  1510. slave = ShmGroupCollection[group].Partner.Member[i];
  1511. quantity += ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity;
  1512. }
  1513. return quantity;
  1514. }
  1515. // group: group index
  1516. bool IsAvailableGroup(unsigned char group)
  1517. {
  1518. return ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0 ? true : false;
  1519. }
  1520. // master: master group index
  1521. int GetMasterAvailableGroup(unsigned char master)
  1522. {
  1523. int quantity = 0;
  1524. unsigned char slave = 0;
  1525. quantity = IsAvailableGroup(master) ? quantity + 1 : quantity;
  1526. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1527. {
  1528. slave = ShmGroupCollection[master].Partner.Member[i];
  1529. quantity = IsAvailableGroup(slave) ? quantity + 1 : quantity;
  1530. }
  1531. return quantity;
  1532. }
  1533. // group: group index
  1534. void UpdateGunLoading(unsigned char group)
  1535. {
  1536. if(ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup != 0 &&
  1537. group == ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup - 1)
  1538. {
  1539. unsigned short TargetCurrent = (int)(chargingInfo[group]->EvBatterytargetCurrent * 10);
  1540. ShmGroupCollection[group].GunLoading = chargingInfo[group]->AvailableChargingCurrent != 0 ?
  1541. (TargetCurrent * 10000 / (int)chargingInfo[group]->AvailableChargingCurrent) : 0;
  1542. }
  1543. else
  1544. {
  1545. ShmGroupCollection[group].GunLoading = 0;
  1546. }
  1547. }
  1548. // group: group index
  1549. void UpdatePsuGroupLoading(unsigned char group)
  1550. {
  1551. ShmPsuGrouping->GroupOutput[group].OutputLoading = ShmPsuData->PsuGroup[group].GroupAvailableCurrent != 0 ?
  1552. (ShmPsuGrouping->GroupOutput[group].GTargetCurrent * 10000 / ShmPsuData->PsuGroup[group].GroupAvailableCurrent) : 0;
  1553. }
  1554. // master: master group index
  1555. void UpdateMasterPsuGroupLoading(unsigned char master)
  1556. {
  1557. unsigned char slave = 0;
  1558. UpdatePsuGroupLoading(master);
  1559. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1560. {
  1561. slave = ShmGroupCollection[master].Partner.Member[i];
  1562. UpdatePsuGroupLoading(slave);
  1563. }
  1564. }
  1565. void PsuGroupIncreaseCurrent(unsigned char group, unsigned short needIncrease, unsigned short *realIncreaseCurrent)
  1566. {
  1567. unsigned short increase = needIncrease;
  1568. if((ShmPsuGrouping->GroupOutput[group].GTargetCurrent + increase) <= ShmPsuData->PsuGroup[group].GroupAvailableCurrent)
  1569. {
  1570. ShmPsuGrouping->GroupOutput[group].GTargetCurrent += increase;
  1571. }
  1572. else
  1573. {
  1574. increase = ShmPsuData->PsuGroup[group].GroupAvailableCurrent - ShmPsuGrouping->GroupOutput[group].GTargetCurrent;
  1575. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  1576. }
  1577. *realIncreaseCurrent += increase;
  1578. }
  1579. void PsuGroupDecreaseCurrent(unsigned char group, unsigned short needDecrease, unsigned short *realDecreaseCurrent)
  1580. {
  1581. unsigned short decrease = needDecrease;
  1582. if(ShmPsuGrouping->GroupOutput[group].GTargetCurrent >= (decrease + ZERO_CURRENT))
  1583. {
  1584. ShmPsuGrouping->GroupOutput[group].GTargetCurrent -= decrease;
  1585. }
  1586. else
  1587. {
  1588. decrease = ShmPsuGrouping->GroupOutput[group].GTargetCurrent - ZERO_CURRENT;
  1589. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = ZERO_CURRENT;
  1590. }
  1591. *realDecreaseCurrent += decrease;
  1592. }
  1593. void IncreaseCurrentByGunLoading(unsigned char group, unsigned short loading, unsigned short *realIncrease, unsigned short MaxIncrease)
  1594. {
  1595. unsigned short target = 0, need = 0;
  1596. if(*realIncrease >= MaxIncrease)
  1597. {
  1598. return;
  1599. }
  1600. target = (loading * ShmPsuData->PsuGroup[group].GroupAvailableCurrent) / 10000;
  1601. need = target >= ShmPsuGrouping->GroupOutput[group].GTargetCurrent ?
  1602. (target - ShmPsuGrouping->GroupOutput[group].GTargetCurrent) : 0;
  1603. need = need > (MaxIncrease - *realIncrease) ? (MaxIncrease - *realIncrease) : need;
  1604. //LOG_INFO("Group [%02X] Target Current %d.%d A, Increase Current: %d.%d A",
  1605. // group, (target / 10), (target % 10), (need / 10), (need % 10));
  1606. if(need > 0)
  1607. {
  1608. PsuGroupIncreaseCurrent(group, need, realIncrease);
  1609. }
  1610. }
  1611. void DecreaseCurrentByGunLoading(unsigned char group, unsigned short loading, unsigned short *realDecrease, unsigned short MaxDecrease)
  1612. {
  1613. unsigned short target = 0, need = 0;
  1614. if(*realDecrease >= MaxDecrease)
  1615. {
  1616. return;
  1617. }
  1618. target = (loading * ShmPsuData->PsuGroup[group].GroupAvailableCurrent) / 10000;
  1619. need = ShmPsuGrouping->GroupOutput[group].GTargetCurrent >= target ?
  1620. (ShmPsuGrouping->GroupOutput[group].GTargetCurrent - target) : 0;
  1621. need = need > (MaxDecrease - *realDecrease) ? (MaxDecrease - *realDecrease) : need;
  1622. //LOG_INFO("Group [%02X] Target Current %d.%d A, Decrease Current: %d.%d A",
  1623. // group, (target / 10), (target % 10), (need / 10), (need % 10));
  1624. if(need > 0)
  1625. {
  1626. PsuGroupDecreaseCurrent(group, need, realDecrease);
  1627. }
  1628. }
  1629. // master: master group index
  1630. void AverageIncreaseCurrent(unsigned char master, unsigned short NeedIncreaseCurrent)
  1631. {
  1632. unsigned char availableGroup = 0, slave = 0;
  1633. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realIncrease = 0;
  1634. #if 0
  1635. PSU_LOG("Gun %d Increase By GunLoading %d.%02d Percent",
  1636. master + 1, (ShmGroupCollection[master].GunLoading / 100), (ShmGroupCollection[master].GunLoading % 100));
  1637. #endif
  1638. IncreaseCurrentByGunLoading(master, ShmGroupCollection[master].GunLoading, &realIncrease, NeedIncreaseCurrent);
  1639. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1640. {
  1641. slave = ShmGroupCollection[master].Partner.Member[i];
  1642. IncreaseCurrentByGunLoading(slave, ShmGroupCollection[master].GunLoading, &realIncrease, NeedIncreaseCurrent);
  1643. }
  1644. availableGroup = GetMasterAvailableGroup(master);
  1645. if(NeedIncreaseCurrent > realIncrease && availableGroup > 0)
  1646. {
  1647. // increase current by group quantity
  1648. avgGroupCurrent = (NeedIncreaseCurrent - realIncrease) / availableGroup;
  1649. #if 0
  1650. PSU_LOG("Gun %d Increase By Group, AvgGroupCurrent %d.%d A",
  1651. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  1652. #endif
  1653. PsuGroupIncreaseCurrent(master, avgGroupCurrent, &realIncrease);
  1654. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1655. {
  1656. slave = ShmGroupCollection[master].Partner.Member[i];
  1657. PsuGroupIncreaseCurrent(slave, avgGroupCurrent, &realIncrease);
  1658. }
  1659. }
  1660. if(NeedIncreaseCurrent > realIncrease)
  1661. {
  1662. // increase excess current to master group
  1663. excessCurrent = NeedIncreaseCurrent - realIncrease;
  1664. #if 0
  1665. PSU_LOG("Gun %d Increase, ExcessCurrent %d.%d A", master + 1, (excessCurrent / 10), (excessCurrent % 10));
  1666. #endif
  1667. PsuGroupIncreaseCurrent(master, excessCurrent, &realIncrease);
  1668. }
  1669. }
  1670. // master: master group index
  1671. void AverageDecreaseCurrent(unsigned char master, unsigned short NeedDecreaseCurrent)
  1672. {
  1673. unsigned char availableGroup = 0, slave = 0;
  1674. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realDecrease = 0;
  1675. #if 0
  1676. PSU_LOG("Gun %d Decrease By GunLoading %d.%02d Percent",
  1677. master + 1, (ShmGroupCollection[master].GunLoading / 100), (ShmGroupCollection[master].GunLoading % 100));
  1678. #endif
  1679. DecreaseCurrentByGunLoading(master, ShmGroupCollection[master].GunLoading, &realDecrease, NeedDecreaseCurrent);
  1680. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1681. {
  1682. slave = ShmGroupCollection[master].Partner.Member[i];
  1683. DecreaseCurrentByGunLoading(slave, ShmGroupCollection[master].GunLoading, &realDecrease, NeedDecreaseCurrent);
  1684. }
  1685. availableGroup = GetMasterAvailableGroup(master);
  1686. if(NeedDecreaseCurrent > realDecrease)
  1687. {
  1688. // decrease current by group quantity
  1689. avgGroupCurrent = (NeedDecreaseCurrent - realDecrease) / availableGroup;
  1690. #if 0
  1691. PSU_LOG("Gun %d Decrease By Group, AvgGroupCurrent %d.%d A",
  1692. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  1693. #endif
  1694. PsuGroupDecreaseCurrent(master, avgGroupCurrent, &realDecrease);
  1695. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1696. {
  1697. slave = ShmGroupCollection[master].Partner.Member[i];
  1698. PsuGroupDecreaseCurrent(slave, avgGroupCurrent, &realDecrease);
  1699. }
  1700. }
  1701. if(NeedDecreaseCurrent > realDecrease)
  1702. {
  1703. // decrease excess current
  1704. excessCurrent = NeedDecreaseCurrent - realDecrease;
  1705. #if 0
  1706. PSU_LOG("Gun %d Decrease, ExcessCurrent %d.%d A", master + 1, (excessCurrent / 10), (excessCurrent % 10));
  1707. #endif
  1708. PsuGroupDecreaseCurrent(master, excessCurrent, &realDecrease);
  1709. }
  1710. }
  1711. void MasterIncreaseCurrent(unsigned char master, unsigned short NeedIncreaseCurrent)
  1712. {
  1713. unsigned short excessCurrent = 0, realIncrease = 0;
  1714. PsuGroupIncreaseCurrent(master, NeedIncreaseCurrent, &realIncrease);
  1715. if(NeedIncreaseCurrent > realIncrease)
  1716. {
  1717. excessCurrent = NeedIncreaseCurrent - realIncrease;
  1718. AverageIncreaseCurrent(master, excessCurrent);
  1719. }
  1720. }
  1721. void MasterDecreaseCurrent(unsigned char master, unsigned short NeedDecreaseCurrent)
  1722. {
  1723. unsigned char slave = 0;
  1724. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realDecrease = 0;
  1725. if(ShmGroupCollection[master].Partner.RealQuantity > 0)
  1726. {
  1727. // decrease current by group quantity
  1728. avgGroupCurrent = NeedDecreaseCurrent / ShmGroupCollection[master].Partner.RealQuantity;
  1729. #if 0
  1730. PSU_LOG("Gun %d Decrease By Group, AvgGroupCurrent %d.%d A",
  1731. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  1732. #endif
  1733. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1734. {
  1735. slave = ShmGroupCollection[master].Partner.Member[i];
  1736. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  1737. {
  1738. PsuGroupDecreaseCurrent(slave, avgGroupCurrent, &realDecrease);
  1739. }
  1740. }
  1741. }
  1742. if(NeedDecreaseCurrent > realDecrease)
  1743. {
  1744. excessCurrent = NeedDecreaseCurrent - realDecrease;
  1745. PsuGroupDecreaseCurrent(master, excessCurrent, &realDecrease);
  1746. }
  1747. }
  1748. void UpdateMaxCurrent(unsigned char master, unsigned short current)
  1749. {
  1750. int time = 0;
  1751. // update max stage current
  1752. if(current > StageMaxCurrent[master])
  1753. {
  1754. StageMaxCurrent[master] = current;
  1755. ShmGroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  1756. GetClockTime(&_StageCurrent_time[master]);
  1757. }
  1758. if(!ShmGroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent)
  1759. {
  1760. time = GetTimeoutValue(_StageCurrent_time[master]) / uSEC_VAL;
  1761. if(time >= EV_MAX_CURRENT_DELAY)
  1762. {
  1763. ShmGroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = true;
  1764. LOG_INFO("Gun %d Reach Max Stage Current", master + 1);
  1765. }
  1766. }
  1767. // update max target current and reset max current time
  1768. if(current > MaxCurrentDemand[master])
  1769. {
  1770. if(ShmGroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  1771. {
  1772. LOG_INFO("Gun %d Max Current Demand Timer Reset", master + 1);
  1773. }
  1774. MaxCurrentDemand[master] = current;
  1775. ShmGroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand = false;
  1776. GetClockTime(&_MaxCurrent_time[master]);
  1777. }
  1778. if(!ShmGroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  1779. {
  1780. time = GetTimeoutValue(_MaxCurrent_time[master]) / uSEC_VAL;
  1781. if(time >= EV_MAX_CURRENT_DELAY)
  1782. {
  1783. ShmGroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand = true;
  1784. LOG_INFO("Gun %d Reach Max Current Demand", master + 1);
  1785. }
  1786. }
  1787. }
  1788. // master: master group index
  1789. bool IsMasterOutputCurrentStable(unsigned char master)
  1790. {
  1791. int time = 0;
  1792. bool stable = false, reachTarget = false;
  1793. unsigned short presentOutput = (int)chargingInfo[master]->PresentChargingCurrent * 10;
  1794. unsigned short TargetCurrent = 0;
  1795. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  1796. reachTarget = presentOutput > (TargetCurrent - REACH_CURRENT_TOLERANCE) && presentOutput < (TargetCurrent + REACH_CURRENT_TOLERANCE);
  1797. if((presentOutput >= StableOutputCurrent[master] && presentOutput - StableOutputCurrent[master] > REACH_CURRENT_TOLERANCE) ||
  1798. (presentOutput < StableOutputCurrent[master] && StableOutputCurrent[master] - presentOutput > REACH_CURRENT_TOLERANCE))
  1799. {
  1800. GetClockTime(&_ReachCurrent_time[master]);
  1801. StableOutputCurrent[master] = presentOutput;
  1802. }
  1803. time = GetTimeoutValue(_ReachCurrent_time[master]) / uSEC_VAL;
  1804. if(reachTarget || time >= CURRENT_STABLE_TIME ||
  1805. (ShmChargerInfo->Control.FCharging[master].FCtrl.bits.EnableForceCharging && time >= CURRENT_REACH_TARGET_TIME))
  1806. {
  1807. stable = true;
  1808. }
  1809. return stable;
  1810. }
  1811. // master: master group index
  1812. void MasterBalanceCurrent(unsigned char master)
  1813. {
  1814. int time = 0;
  1815. unsigned char slave = 0;
  1816. unsigned short realBalance = 0, grabCurrent = 0;
  1817. time = GetTimeoutValue(_BalanceCurrent_time[master]) / mSEC_VAL;
  1818. if(time >= BALANCE_CURRENT_INTERVAL)
  1819. {
  1820. //LOG_INFO("Gun %d Start Balance Current", master + 1);
  1821. DecreaseCurrentByGunLoading(master, ShmGroupCollection[master].GunLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  1822. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1823. {
  1824. slave = ShmGroupCollection[master].Partner.Member[i];
  1825. DecreaseCurrentByGunLoading(slave, ShmGroupCollection[master].GunLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  1826. }
  1827. //LOG_INFO("Gun %d Grab Balance Current %d.%d A", master + 1, (grabCurrent / 10), (grabCurrent % 10));
  1828. IncreaseCurrentByGunLoading(master, ShmGroupCollection[master].GunLoading, &realBalance, grabCurrent);
  1829. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1830. {
  1831. slave = ShmGroupCollection[master].Partner.Member[i];
  1832. IncreaseCurrentByGunLoading(slave, ShmGroupCollection[master].GunLoading, &realBalance, grabCurrent);
  1833. }
  1834. //LOG_INFO("Gun %d Increase Balance Current %d.%d A", master + 1, (realBalance / 10), (realBalance % 10));
  1835. UpdateMasterPsuGroupLoading(master);
  1836. GetClockTime(&_BalanceCurrent_time[master]);
  1837. }
  1838. }
  1839. unsigned short GetLoadingDiff(unsigned char master)
  1840. {
  1841. unsigned char slave = 0;
  1842. unsigned short maxLoading = 0, minLoading = 0, diffLoading = 0;
  1843. maxLoading = ShmPsuGrouping->GroupOutput[master].OutputLoading;
  1844. minLoading = ShmPsuGrouping->GroupOutput[master].OutputLoading;
  1845. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1846. {
  1847. slave = ShmGroupCollection[master].Partner.Member[i];
  1848. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity == 0 || ShmPsuData->PsuGroup[slave].GroupAvailableCurrent == 0)
  1849. {
  1850. continue;
  1851. }
  1852. maxLoading = ShmPsuGrouping->GroupOutput[slave].OutputLoading > maxLoading ?
  1853. ShmPsuGrouping->GroupOutput[slave].OutputLoading : maxLoading;
  1854. minLoading = ShmPsuGrouping->GroupOutput[slave].OutputLoading < minLoading ?
  1855. ShmPsuGrouping->GroupOutput[slave].OutputLoading : minLoading;
  1856. }
  1857. diffLoading = maxLoading - minLoading;
  1858. return diffLoading;
  1859. }
  1860. void CheckCurrentBalance(unsigned char master)
  1861. {
  1862. unsigned short diffLoading = 0;
  1863. diffLoading = GetLoadingDiff(master);
  1864. if(!ShmGroupCollection[master].GroupCtrl.bits.NeedCurrentBalance)
  1865. {
  1866. if(diffLoading >= START_BALANCE_CRITERIA)
  1867. {
  1868. ShmGroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = true;
  1869. PSU_LOG("Gun %d Output Current Is Unbalance, diffLoading = %d.%02d", master + 1, (diffLoading / 100), (diffLoading % 100));
  1870. GetClockTime(&_BalanceCurrent_time[master]);
  1871. }
  1872. }
  1873. if(ShmGroupCollection[master].GroupCtrl.bits.NeedCurrentBalance)
  1874. {
  1875. MasterBalanceCurrent(master);
  1876. diffLoading = GetLoadingDiff(master);
  1877. if(diffLoading <= STOP_BALANCE_CRITERIA)
  1878. {
  1879. ShmGroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  1880. PSU_LOG("Gun %d Output Current Is Balance", master + 1);
  1881. }
  1882. }
  1883. }
  1884. bool IsOtherCharging(unsigned char group)
  1885. {
  1886. bool find = false;
  1887. int other = 0, selfTarget = 0, location = 0;
  1888. if(ShmGroupCollection[group].TargetGroup == 0)
  1889. {
  1890. return find;
  1891. }
  1892. selfTarget = ShmGroupCollection[group].TargetGroup - 1;
  1893. // search from left
  1894. location = ShmGroupCollection[group].Location - 1;
  1895. for(int i = location; i >= 0; i--)
  1896. {
  1897. other = ShmPsuGrouping->Layout[i];
  1898. if(ShmGroupCollection[other].Role == _GROLE_MASTER)
  1899. {
  1900. find = other != selfTarget ? true : false;
  1901. break;
  1902. }
  1903. }
  1904. if(!find)
  1905. {
  1906. // search from right
  1907. location = ShmGroupCollection[group].Location + 1;
  1908. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1909. {
  1910. other = ShmPsuGrouping->Layout[i];
  1911. if(ShmGroupCollection[other].Role == _GROLE_MASTER)
  1912. {
  1913. find = other != selfTarget ? true : false;
  1914. break;
  1915. }
  1916. }
  1917. }
  1918. return find;
  1919. }
  1920. // master: master group index
  1921. void MasterReleasePsuGroup(unsigned char master)
  1922. {
  1923. bool findOtherInCharging = false;
  1924. unsigned char slave = 0, member = 0, releaseTarget = 0;
  1925. unsigned char otherCharging[MAX_GROUP_QUANTITY] = {0};
  1926. unsigned char releaseList[MAX_GROUP_QUANTITY] = {0};
  1927. unsigned short releaseLoading[MAX_GROUP_QUANTITY] = {0};
  1928. PsuGroupPartner ReleaseMember;
  1929. unsigned short target = 0, releaseAvailableCurrent = 0, originalAvailableCurrent = 0, originalLoading = 0, maxReleaseLoading = 0;
  1930. if(ShmChargerInfo->PsuGrouping.GroupCollection[master].Role != _GROLE_MASTER ||
  1931. ShmGroupCollection[master].Partner.Quantity == 0)
  1932. {
  1933. return;
  1934. }
  1935. target = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  1936. originalAvailableCurrent = (int)chargingInfo[master]->AvailableChargingCurrent;
  1937. originalLoading = ShmGroupCollection[master].GunLoading;
  1938. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1939. {
  1940. slave = ShmGroupCollection[master].Partner.Member[i];
  1941. releaseAvailableCurrent = 0;
  1942. PsuGroupPowerOffCheck(slave, &ReleaseMember);
  1943. otherCharging[i] = IsOtherCharging(slave);
  1944. findOtherInCharging = otherCharging[i] ? otherCharging[i] : findOtherInCharging;
  1945. releaseList[i] = slave;
  1946. for(int j = 0; j < ReleaseMember.Quantity; j++)
  1947. {
  1948. member = ReleaseMember.Member[j];
  1949. releaseAvailableCurrent += ShmPsuData->PsuGroup[member].GroupAvailableCurrent;
  1950. }
  1951. releaseLoading[i] = originalAvailableCurrent > releaseAvailableCurrent ?
  1952. (target * 10000 / (originalAvailableCurrent - releaseAvailableCurrent)) : 0;
  1953. //LOG_INFO("Gun %d Release [%02X], Total Release %d Group, Gun Loading %d.%02d >> %d.%02d", master + 1, slave, ReleaseMember.Quantity,
  1954. // (originalLoading / 100), (originalLoading % 100),
  1955. // (releaseLoading[i] / 100), (releaseLoading[i] % 100));
  1956. }
  1957. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  1958. {
  1959. if((otherCharging[i] || !findOtherInCharging) &&
  1960. releaseLoading[i] > maxReleaseLoading && releaseLoading[i] < (EXTEND_LOADING - RELEASE_LOADING_OFFSET))
  1961. {
  1962. maxReleaseLoading = releaseLoading[i];
  1963. releaseTarget = releaseList[i];
  1964. }
  1965. }
  1966. if(maxReleaseLoading > 0)
  1967. {
  1968. ShmGroupCollection[releaseTarget].GroupCtrl.bits.SlavePowerOffRequest = true;
  1969. LOG_INFO("Gun %d Set [%02X] Release(%s), Gun Loading %d.%02d >> %d.%02d",
  1970. master + 1, releaseTarget, findOtherInCharging ? "Other In Charging" : "Normal",
  1971. (originalLoading / 100), (originalLoading % 100), (maxReleaseLoading / 100), (maxReleaseLoading % 100));
  1972. }
  1973. }
  1974. void CheckReleaseOrExtend(unsigned char master)
  1975. {
  1976. if((ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation ||
  1977. !ShmChargerInfo->Control.FCharging[master].FCtrl.bits.EnableForceCharging) &&
  1978. ShmGroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent &&
  1979. ShmGroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue == 0 &&
  1980. ShmGroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue == 0)
  1981. {
  1982. if(ShmGroupCollection[master].Partner.Quantity > 0 &&
  1983. ShmGroupCollection[master].GunLoading < RELEASE_LOADING)
  1984. {
  1985. MasterReleasePsuGroup(master);
  1986. }
  1987. else if(ShmGroupCollection[master].GunLoading >= EXTEND_LOADING)
  1988. {
  1989. int available = GetPsuGroupAvailable(master);
  1990. if(available > 0)
  1991. {
  1992. if(!ShmGroupCollection[master].GroupCtrl.bits.ExtendAvailable)
  1993. {
  1994. LOG_INFO("Gun %d Extend Capability Available", master + 1);
  1995. GetClockTime(&_ExtendCapability_time[master]);
  1996. }
  1997. ShmGroupCollection[master].GroupCtrl.bits.ExtendAvailable = true;
  1998. }
  1999. }
  2000. }
  2001. }
  2002. bool IsPsuGroupOutputConfigDifferent(unsigned char group)
  2003. {
  2004. bool different = false;
  2005. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage != PreGroupOutput[group].GTargetVoltage ||
  2006. ShmPsuGrouping->GroupOutput[group].GTargetCurrent != PreGroupOutput[group].GTargetCurrent)
  2007. {
  2008. different = true;
  2009. }
  2010. PreGroupOutput[group].GTargetVoltage = ShmPsuGrouping->GroupOutput[group].GTargetVoltage;
  2011. PreGroupOutput[group].GTargetCurrent = ShmPsuGrouping->GroupOutput[group].GTargetCurrent;
  2012. return different;
  2013. }
  2014. // Gun X Demand Change -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  2015. // Master [XX] -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  2016. // Member [XX] -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  2017. void ShowPsuGroupOutputConfig(unsigned char master)
  2018. {
  2019. unsigned char slave = 0;
  2020. PSU_LOG("Gun %d Demand Change -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  2021. master + 1, (ShmPsuGrouping->GroupOutput[master].GTargetVoltage / 10),
  2022. ((int)chargingInfo[master]->EvBatterytargetCurrent), (((int)(chargingInfo[master]->EvBatterytargetCurrent * 10)) % 10),
  2023. ((int)chargingInfo[master]->AvailableChargingCurrent / 10), ((int)chargingInfo[master]->AvailableChargingCurrent % 10),
  2024. (ShmGroupCollection[master].GunLoading / 100), (ShmGroupCollection[master].GunLoading % 100));
  2025. PSU_LOG(" Master [%02X] -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  2026. master, (ShmPsuGrouping->GroupOutput[master].GTargetVoltage / 10),
  2027. (ShmPsuGrouping->GroupOutput[master].GTargetCurrent / 10), (ShmPsuGrouping->GroupOutput[master].GTargetCurrent % 10),
  2028. (ShmPsuData->PsuGroup[master].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[master].GroupAvailableCurrent % 10),
  2029. (ShmPsuGrouping->GroupOutput[master].OutputLoading / 100), (ShmPsuGrouping->GroupOutput[master].OutputLoading % 100));
  2030. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2031. {
  2032. slave = ShmGroupCollection[master].Partner.Member[i];
  2033. PSU_LOG(" Member [%02X] -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  2034. slave, (ShmPsuGrouping->GroupOutput[slave].GTargetVoltage / 10),
  2035. (ShmPsuGrouping->GroupOutput[slave].GTargetCurrent / 10), (ShmPsuGrouping->GroupOutput[slave].GTargetCurrent % 10),
  2036. (ShmPsuData->PsuGroup[slave].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[slave].GroupAvailableCurrent % 10),
  2037. (ShmPsuGrouping->GroupOutput[slave].OutputLoading / 100), (ShmPsuGrouping->GroupOutput[slave].OutputLoading % 100));
  2038. }
  2039. }
  2040. void PsuGroupOutputConfigCheck(unsigned char master)
  2041. {
  2042. unsigned char slave = 0;
  2043. bool show = false;
  2044. show = IsPsuGroupOutputConfigDifferent(master);
  2045. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2046. {
  2047. slave = ShmGroupCollection[master].Partner.Member[i];
  2048. if(IsPsuGroupOutputConfigDifferent(slave))
  2049. {
  2050. show = true;
  2051. }
  2052. }
  2053. if(show)
  2054. {
  2055. ShowPsuGroupOutputConfig(master);
  2056. }
  2057. }
  2058. // master: master group index
  2059. // group: self group index
  2060. void UpdatePsuGroupOutputConfig(unsigned char master)
  2061. {
  2062. unsigned char slave = 0;
  2063. unsigned short TargetVoltage = 0, TargetCurrent = 0, PresentTargetCurrent = 0;
  2064. unsigned short current = 0;
  2065. TargetVoltage = (int)(chargingInfo[master]->EvBatterytargetVoltage * 10);
  2066. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2067. if(chargingInfo[master]->RelayK1K2Status)
  2068. {
  2069. // update target voltage
  2070. if(!ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  2071. {
  2072. GCTargetVoltage[master] = TargetVoltage;
  2073. ShmPsuGrouping->GroupOutput[master].GTargetVoltage = GCTargetVoltage[master];
  2074. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2075. {
  2076. slave = ShmGroupCollection[master].Partner.Member[i];
  2077. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = GCTargetVoltage[master];
  2078. }
  2079. }
  2080. // update target current
  2081. if(((ShmGroupCollection[master].Partner.RealQuantity + 1) * ZERO_CURRENT) > TargetCurrent ||
  2082. !ShmGroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode)
  2083. {
  2084. current = ZERO_CURRENT;
  2085. #if 0
  2086. if(ShmPsuGrouping->GroupOutput[master].GTargetCurrent != current)
  2087. {
  2088. LOG_INFO("Gun %d Need Minimum Target Current = %d", master + 1, current);
  2089. }
  2090. #endif
  2091. ShmPsuGrouping->GroupOutput[master].GTargetCurrent = current;
  2092. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2093. {
  2094. slave = ShmGroupCollection[master].Partner.Member[i];
  2095. if(ShmGroupCollection[slave].Role == _GROLE_SLAVE || ShmGroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  2096. {
  2097. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent =
  2098. ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0 ? ZERO_CURRENT : 0;
  2099. }
  2100. }
  2101. }
  2102. else
  2103. {
  2104. if(!ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  2105. {
  2106. // master in normal output mode
  2107. PresentTargetCurrent = GetPresentTargetCurrent(master, _GROLE_MASTER);
  2108. current = TargetCurrent <= (int)chargingInfo[master]->AvailableChargingCurrent ?
  2109. TargetCurrent : (int)chargingInfo[master]->AvailableChargingCurrent;
  2110. #if 0
  2111. if(GCTargetCurrent[master] != current)
  2112. {
  2113. LOG_INFO("Gun %d Need Normal Target Current = %d", master + 1, current);
  2114. }
  2115. #endif
  2116. GCTargetCurrent[master] = current;
  2117. UpdateMaxCurrent(master, GCTargetCurrent[master]);
  2118. if(GCTargetCurrent[master] > PresentTargetCurrent)
  2119. {
  2120. // increase current
  2121. OutputConfigStep[master] = _CURRENT_MODE_INCREASE;
  2122. PSU_LOG("Gun %d Increase Current: %d.%d A, %d.%d A -> %d.%d A", master + 1,
  2123. ((GCTargetCurrent[master] - PresentTargetCurrent) / 10), ((GCTargetCurrent[master] - PresentTargetCurrent) % 10),
  2124. (PresentTargetCurrent / 10), (PresentTargetCurrent % 10), (GCTargetCurrent[master] / 10), (GCTargetCurrent[master] % 10));
  2125. #if 0
  2126. // for safety test
  2127. if(GCTargetCurrent[master] <= CURRENT_BALANCE_CRITERIA)
  2128. {
  2129. MasterIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  2130. }
  2131. else
  2132. {
  2133. AverageIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  2134. }
  2135. #else
  2136. AverageIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  2137. #endif
  2138. }
  2139. else if(GCTargetCurrent[master] < PresentTargetCurrent)
  2140. {
  2141. // decrease current
  2142. OutputConfigStep[master] = _CURRENT_MODE_DECREASE;
  2143. PSU_LOG("Gun %d Decrease Current: %d.%d A, %d.%d A -> %d.%d A", master + 1,
  2144. ((PresentTargetCurrent - GCTargetCurrent[master]) / 10), ((PresentTargetCurrent - GCTargetCurrent[master]) % 10),
  2145. (PresentTargetCurrent / 10), (PresentTargetCurrent % 10), (GCTargetCurrent[master] / 10), (GCTargetCurrent[master] % 10));
  2146. #if 0
  2147. // for safety test
  2148. if(GCTargetCurrent[master] <= ShmGroupCollection[master].Partner.RealQuantity * CURRENT_BALANCE_CRITERIA)
  2149. {
  2150. MasterDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  2151. }
  2152. else
  2153. {
  2154. AverageDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  2155. }
  2156. #else
  2157. AverageDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  2158. #endif
  2159. }
  2160. else
  2161. {
  2162. // balance current or do not change
  2163. OutputConfigStep[master] = _CURRENT_MODE_BALANCE;
  2164. if(OutputConfigStep[master] != _preOutputConfigStep[master])
  2165. {
  2166. GetClockTime(&_ReachCurrent_time[master]);
  2167. LOG_INFO("Gun %d In Balance Mode", master + 1);
  2168. StableOutputCurrent[master] = (int)chargingInfo[master]->PresentChargingCurrent * 10;
  2169. ShmGroupCollection[master].GroupCtrl.bits.OutputCurrentStable = false;
  2170. ShmGroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  2171. }
  2172. if(!ShmGroupCollection[master].GroupCtrl.bits.OutputCurrentStable)
  2173. {
  2174. if(IsMasterOutputCurrentStable(master))
  2175. {
  2176. ShmGroupCollection[master].GroupCtrl.bits.OutputCurrentStable = true;
  2177. LOG_INFO("Gun %d Output Current = %5.1f A Stable", master + 1, chargingInfo[master]->PresentChargingCurrent);
  2178. }
  2179. }
  2180. if(ShmGroupCollection[master].GroupCtrl.bits.OutputCurrentStable)
  2181. {
  2182. #if 0
  2183. if(GCTargetCurrent[master] > ShmGroupCollection[master].Partner.RealQuantity * CURRENT_BALANCE_CRITERIA)
  2184. {
  2185. CheckCurrentBalance(master);
  2186. }
  2187. else
  2188. {
  2189. ShmGroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  2190. }
  2191. #else
  2192. CheckCurrentBalance(master);
  2193. #endif
  2194. if(!ShmGroupCollection[master].GroupCtrl.bits.NeedCurrentBalance)
  2195. {
  2196. CheckReleaseOrExtend(master);
  2197. }
  2198. }
  2199. }
  2200. }
  2201. else
  2202. {
  2203. // master in derating mode
  2204. OutputConfigStep[master] = _CURRENT_MODE_DERATING;
  2205. if(OutputConfigStep[master] != _preOutputConfigStep[master])
  2206. {
  2207. LOG_INFO("Gun %d In Derating Mode", master + 1);
  2208. }
  2209. }
  2210. _preOutputConfigStep[master] = OutputConfigStep[master];
  2211. }
  2212. }
  2213. else
  2214. {
  2215. ShmPsuGrouping->GroupOutput[master].GTargetVoltage = 0;
  2216. ShmPsuGrouping->GroupOutput[master].GTargetCurrent = 0;
  2217. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2218. {
  2219. slave = ShmGroupCollection[master].Partner.Member[i];
  2220. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = 0;
  2221. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = 0;
  2222. }
  2223. }
  2224. }
  2225. // group: self group index
  2226. void SetPsuGroupOutput(unsigned char group)
  2227. {
  2228. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  2229. {
  2230. //if(ShmPsuGrouping->GroupOutput[group].GTargetCurrent > 100)
  2231. //{
  2232. PresentOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  2233. //}
  2234. //else
  2235. //{
  2236. // int moduleCurrent = (ShmPsuGrouping->GroupOutput[group].GTargetCurrent * 100) / ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity;
  2237. // SetModuleOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage * 100, moduleCurrent);
  2238. //}
  2239. Await();
  2240. }
  2241. //UpdatePsuGroupLoading(group);
  2242. UpdateGunLoading(group);
  2243. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage >= PSU_MIN_VOL)
  2244. {
  2245. if(!isStartOutputSwitch[group])
  2246. {
  2247. SetPsuGroupPowerOnOff(group, PSU_POWER_ON);
  2248. }
  2249. }
  2250. else
  2251. {
  2252. if(isStartOutputSwitch[group])
  2253. {
  2254. SetPsuGroupPowerOnOff(group, PSU_POWER_OFF);
  2255. }
  2256. }
  2257. }
  2258. // master: master group index
  2259. bool CanMasterStartDerating(unsigned char master)
  2260. {
  2261. bool start = false;
  2262. unsigned short TargetCurrent = 0;
  2263. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2264. if(TargetCurrent <= ShmGroupCollection[master].ReAssignAvailableCurrent)
  2265. {
  2266. start = true;
  2267. }
  2268. return start;
  2269. }
  2270. // master: master group index
  2271. // role_condition: when role_condition = _GROLE_MASTER, stop all member
  2272. void SetMemberStartPowerOff(unsigned char master, unsigned char role_condition)
  2273. {
  2274. unsigned char slave = 0;
  2275. char strMember[64];
  2276. bool find = false;
  2277. if(ShmGroupCollection[master].Role == _GROLE_MASTER)
  2278. {
  2279. sprintf(strMember, "Gun %d Set Member:", master + 1);
  2280. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2281. {
  2282. slave = ShmGroupCollection[master].Partner.Member[i];
  2283. if(role_condition == _GROLE_MASTER || ShmGroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  2284. {
  2285. SetPsuGroupRole(slave, _GROLE_SLAVE_POWER_OFF);
  2286. char strSlave[8];
  2287. sprintf(strSlave, " [%02X]", slave);
  2288. strcat(strMember, strSlave);
  2289. find = true;
  2290. }
  2291. }
  2292. if(find)
  2293. {
  2294. strcat(strMember, " Power Off");
  2295. LOG_INFO("%s", strMember);
  2296. }
  2297. }
  2298. }
  2299. // master: master group index
  2300. bool IsMemberPowerOffOK(unsigned char master)
  2301. {
  2302. bool done = true;
  2303. unsigned char slave = 0;
  2304. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2305. {
  2306. slave = ShmGroupCollection[master].Partner.Member[i];
  2307. if(ShmGroupCollection[slave].Role != _GROLE_SLAVE && ShmGroupCollection[slave].Role != _GROLE_SWITCH_OFF_OK)
  2308. {
  2309. done = false;
  2310. }
  2311. }
  2312. return done;
  2313. }
  2314. // master: master group index
  2315. void SetMemberPowerOffDone(unsigned char master)
  2316. {
  2317. unsigned char slave = 0;
  2318. if(ShmGroupCollection[master].Role == _GROLE_MASTER)
  2319. {
  2320. for(int i = 0; i < ShmGroupCollection[master].Partner.Quantity; i++)
  2321. {
  2322. slave = ShmGroupCollection[master].Partner.Member[i];
  2323. if(ShmGroupCollection[slave].Role == _GROLE_SWITCH_OFF_OK)
  2324. {
  2325. SetPsuGroupRole(slave, _GROLE_WAIT_IDLE);
  2326. }
  2327. else if(ShmGroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  2328. {
  2329. // can not power off normally
  2330. SetPsuGroupRole(slave, _GROLE_TERMINATE);
  2331. }
  2332. }
  2333. }
  2334. }
  2335. // master: master group index
  2336. // Yes_No: Yes: relay on, No: relay off
  2337. // role_condition: when role_condition = _GROLE_MASTER, parallel relay of all member do the action(on or off)
  2338. void SetParallelRelayOnOff(unsigned char master, unsigned char Yes_No, unsigned char role_condition)
  2339. {
  2340. int ParallelConfig = 0;
  2341. unsigned char slave = 0;
  2342. PsuGroupPartner *partner;
  2343. if(ShmGroupCollection[master].Role == _GROLE_MASTER ||
  2344. (ShmGroupCollection[master].Role == _GROLE_REQUEST_TO_CHARGING && Yes_No))
  2345. {
  2346. ShmGroupCollection[master].ParallelCheck = 0;
  2347. partner = role_condition == _GROLE_PRECHARGE_READY ? &ShmGroupCollection[master].PossibleMember : &ShmGroupCollection[master].Partner;
  2348. for(int i = 0; i < partner->Quantity; i++)
  2349. {
  2350. slave = partner->Member[i];
  2351. if(role_condition == _GROLE_MASTER || ShmGroupCollection[slave].Role != _GROLE_SLAVE)
  2352. {
  2353. ParallelConfig = ShmGroupCollection[master].ParallelConfig[slave];
  2354. if(ParallelConfig != 0)
  2355. {
  2356. ShmGroupCollection[master].ParallelCheck |= (1 << (ParallelConfig - 1));
  2357. }
  2358. }
  2359. }
  2360. if(Yes_No)
  2361. {
  2362. ShmPsuGrouping->ParallelRelayConfig.CtrlValue |= ShmGroupCollection[master].ParallelCheck;
  2363. }
  2364. else
  2365. {
  2366. ShmPsuGrouping->ParallelRelayConfig.CtrlValue &= ~ShmGroupCollection[master].ParallelCheck;
  2367. }
  2368. }
  2369. }
  2370. // master: master group index
  2371. bool IsParallelRelayConfirmed(unsigned char master)
  2372. {
  2373. bool confirmed = true;
  2374. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2375. {
  2376. if((1 << i) & ShmGroupCollection[master].ParallelCheck)
  2377. {
  2378. if((ShmPsuGrouping->ParallelRelayConfig.CtrlValue & (1 << i)) != (ShmPsuGrouping->ParallelRelayConfirmed.CtrlValue & (1 << i)))
  2379. {
  2380. confirmed = false;
  2381. break;
  2382. }
  2383. }
  2384. }
  2385. return confirmed;
  2386. }
  2387. // master: master group index
  2388. bool IsPossibleMemberReady(unsigned char master)
  2389. {
  2390. bool ready = true;
  2391. unsigned char slave = 0;
  2392. if(ShmGroupCollection[master].PossibleMember.Quantity > 0)
  2393. {
  2394. for(int i = 0; i < ShmGroupCollection[master].PossibleMember.Quantity; i++)
  2395. {
  2396. slave = ShmGroupCollection[master].PossibleMember.Member[i];
  2397. if(ShmGroupCollection[slave].Role != _GROLE_WAIT_SLAVE)
  2398. {
  2399. ready = false;
  2400. }
  2401. }
  2402. }
  2403. return ready;
  2404. }
  2405. // master: master group index
  2406. bool IsExtendPrechargeReady(unsigned char master)
  2407. {
  2408. bool ready = true;
  2409. unsigned char slave = 0;
  2410. for(int i = 0; i < ShmGroupCollection[master].PossibleMember.Quantity; i++)
  2411. {
  2412. slave = ShmGroupCollection[master].PossibleMember.Member[i];
  2413. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity == 0 || ShmPsuData->PsuGroup[slave].GroupAvailableCurrent == 0)
  2414. {
  2415. continue;
  2416. }
  2417. if(ShmGroupCollection[slave].Role != _GROLE_PREPARE_ATTACH_ON ||
  2418. ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage > (ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage + PRECHARGE_RANGE_VOLTAGE) ||
  2419. ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage < (ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage - PRECHARGE_OFFSET_VOLTAGE- PRECHARGE_RANGE_VOLTAGE))
  2420. {
  2421. ready = false;
  2422. }
  2423. }
  2424. return ready;
  2425. }
  2426. // master: master group index
  2427. void SetExtendPrechargeCompleted(unsigned char master)
  2428. {
  2429. unsigned char slave = 0;
  2430. for(int i = 0; i < ShmGroupCollection[master].PossibleMember.Quantity; i++)
  2431. {
  2432. slave = ShmGroupCollection[master].PossibleMember.Member[i];
  2433. SetPsuGroupRole(slave, _GROLE_PRECHARGE_READY);
  2434. }
  2435. }
  2436. // master: master group index
  2437. void SetExtendPrechargeStop(unsigned char master)
  2438. {
  2439. unsigned char slave = 0;
  2440. for(int i = 0; i < ShmGroupCollection[master].PossibleMember.Quantity; i++)
  2441. {
  2442. slave = ShmGroupCollection[master].PossibleMember.Member[i];
  2443. SetPsuGroupRole(slave, _GROLE_EXTEND_STOP);
  2444. }
  2445. memset(&ShmGroupCollection[master].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  2446. }
  2447. // master: master group index
  2448. void AddExtendMember(unsigned char master)
  2449. {
  2450. unsigned char slave = 0;
  2451. for(int i = 0; i < ShmGroupCollection[master].PossibleMember.Quantity; i++)
  2452. {
  2453. slave = ShmGroupCollection[master].PossibleMember.Member[i];
  2454. if(ShmGroupCollection[slave].Role == _GROLE_PRECHARGE_READY)
  2455. {
  2456. AddMember(slave, master);
  2457. }
  2458. }
  2459. }
  2460. // group: self group index
  2461. void CheckChargingRequest(unsigned char group)
  2462. {
  2463. if(ShmGroupCollection[group].Role != _GROLE_IDLE)
  2464. {
  2465. return;
  2466. }
  2467. if(ShmGroupCollection[group].GroupCtrl.bits.ChargingRequest)
  2468. {
  2469. SetPsuGroupRole(group, _GROLE_REQUEST_TO_CHARGING);
  2470. }
  2471. }
  2472. // group: self group index
  2473. void ChargingRequestProcess(unsigned char group)
  2474. {
  2475. int time = 0;
  2476. PsuGroupPartner partner;
  2477. if(ShmGroupCollection[group].Role != _GROLE_REQUEST_TO_CHARGING)
  2478. {
  2479. return;
  2480. }
  2481. if(ShmGroupCollection[group].GroupCtrl.bits.ChargingRequest && !ShmGroupCollection[group].GroupCtrl.bits.ChargingRequestConfirmed)
  2482. {
  2483. GetClockTime(&_ChargingRequest_time[group]);
  2484. ShmGroupCollection[group].GroupCtrl.bits.ChargingRequest = false;
  2485. ShmGroupCollection[group].GroupCtrl.bits.ChargingRequestConfirmed = true;
  2486. ShmGroupCollection[group].GroupCtrl.bits.GroupShareCheck = true;
  2487. }
  2488. if(ShmGroupCollection[group].GroupCtrl.bits.GroupShareCheck && !ShmGroupCollection[group].GroupCtrl.bits.ShareConfirmed)
  2489. {
  2490. // check is there psu group to grab
  2491. if(PsuGroupGrabCheck(group, &partner))
  2492. {
  2493. GetClockTime(&_ChargingRequest_time[group]);
  2494. ShmGroupCollection[group].GroupCtrl.bits.GrabGroupWait = true;
  2495. PrepareToPowerOff(group + 1, &partner);
  2496. memcpy(&ShmGroupCollection[group].PossibleMember, &partner, sizeof(PsuGroupPartner));
  2497. }
  2498. else
  2499. {
  2500. ShmGroupCollection[group].GroupCtrl.bits.ShareCheckDone = true;
  2501. LOG_INFO("Gun %d Grab Nothing", group + 1);
  2502. }
  2503. ShmGroupCollection[group].GroupCtrl.bits.ShareConfirmed = true;
  2504. }
  2505. if(ShmGroupCollection[group].GroupCtrl.bits.GrabGroupWait && !ShmGroupCollection[group].GroupCtrl.bits.ShareCheckDone)
  2506. {
  2507. // wait until grab psu ready or timeout
  2508. bool ready = IsPossibleMemberReady(group);
  2509. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  2510. if(ready || time >= WAIT_GRAB_TIME)
  2511. {
  2512. ShmGroupCollection[group].GroupCtrl.bits.ShareCheckDone = true;
  2513. LOG_INFO("Gun %d Grab %s", group + 1, ready ? "OK" : "Timeout");
  2514. }
  2515. }
  2516. if(ShmGroupCollection[group].GroupCtrl.bits.ShareCheckDone && !ShmGroupCollection[group].GroupCtrl.bits.FindGroupPartner)
  2517. {
  2518. GetClockTime(&_ChargingRequest_time[group]);
  2519. AddAvailableMember(group);
  2520. ShmGroupCollection[group].GroupCtrl.bits.FindGroupPartner = true;
  2521. }
  2522. if(ShmGroupCollection[group].GroupCtrl.bits.FindGroupPartner && !ShmGroupCollection[group].GroupCtrl.bits.ParallelRelayOn)
  2523. {
  2524. // after a little delay, set parallel relay on
  2525. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  2526. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  2527. {
  2528. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  2529. GetClockTime(&_ChargingRequest_time[group]);
  2530. SetParallelRelayOnOff(group, YES, _GROLE_MASTER);
  2531. ShmGroupCollection[group].GroupCtrl.bits.ParallelRelayOn = true;
  2532. LOG_INFO("Gun %d Charging Request Set All Member Parallel Relay On %X -> %X", group + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  2533. }
  2534. }
  2535. if(ShmGroupCollection[group].GroupCtrl.bits.ParallelRelayOn && !ShmGroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed)
  2536. {
  2537. // wait until parallel relay on confirmed
  2538. bool confirmed = IsParallelRelayConfirmed(group);
  2539. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  2540. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  2541. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  2542. {
  2543. GetClockTime(&_ChargingRequest_time[group]);
  2544. ShmGroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed = true;
  2545. LOG_INFO("Gun %d Charging Request Parallel Relay Confirmed %s", group + 1, confirmed ? "OK" : "Timeout");
  2546. }
  2547. }
  2548. if(ShmGroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed && !ShmGroupCollection[group].GroupCtrl.bits.GroupingDone)
  2549. {
  2550. ShmGroupCollection[group].GroupCtrl.bits.GroupingDone = true;
  2551. }
  2552. if(ShmGroupCollection[group].GroupCtrl.bits.GroupingDone)
  2553. {
  2554. SetPsuGroupToMaster(group);
  2555. ShmGroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue = 0;
  2556. LOG_INFO("Gun %d Grouping Completed", group + 1);
  2557. }
  2558. }
  2559. // master: master group index
  2560. void PsuGroupDeratingProcess(unsigned char master)
  2561. {
  2562. int time = 0;
  2563. // slave group set NeedDerating flag to start re-assign
  2564. if(ShmGroupCollection[master].GroupCtrl.bits.NeedDerating)
  2565. {
  2566. // reduce output current capability start
  2567. GetClockTime(&_PsuGroupDerating_time[master]);
  2568. unsigned short ReAssignCurrent = GetPresentTargetCurrent(master, _GROLE_SLAVE);
  2569. ShmGroupCollection[master].ReAssignAvailableCurrent = ReAssignCurrent;
  2570. ShmGroupCollection[master].GroupCtrl.bits.NeedDerating = false;
  2571. LOG_INFO("Gun %d DeratingConfirmed%s, ReAssignAvailableCurrent = %d.%d A",
  2572. master + 1,
  2573. ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed ? " Again" : "",
  2574. (ReAssignCurrent / 10), (ReAssignCurrent % 10));
  2575. if(ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  2576. {
  2577. ShmGroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  2578. }
  2579. ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed = true;
  2580. }
  2581. if(ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed && !ShmGroupCollection[master].GroupCtrl.bits.DeratingStart)
  2582. {
  2583. // wait until derating from ev or timeout
  2584. bool start = CanMasterStartDerating(master);
  2585. bool bypass = ShmGroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode ? false : true;
  2586. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  2587. if(start || time >= WAIT_EV_DERATING_TIMEOUT || bypass)
  2588. {
  2589. GetClockTime(&_PsuGroupDerating_time[master]);
  2590. ShmGroupCollection[master].GroupCtrl.bits.DeratingStart = true;
  2591. LOG_INFO("Gun %d %s Start Derating%s", master + 1, start ? "Normal" : "Force", bypass ? " (Bypass)" : "");
  2592. SetMemberStartPowerOff(master, _GROLE_PREPARE_SWITCH_OFF);
  2593. if(!bypass)
  2594. {
  2595. // update stage max current and reset max current time
  2596. StageMaxCurrent[master] = start ?
  2597. (int)(chargingInfo[master]->EvBatterytargetCurrent * 10) :
  2598. ShmGroupCollection[master].ReAssignAvailableCurrent;
  2599. ShmGroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  2600. GetClockTime(&_StageCurrent_time[master]);
  2601. }
  2602. }
  2603. }
  2604. if(ShmGroupCollection[master].GroupCtrl.bits.DeratingStart && !ShmGroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone)
  2605. {
  2606. // wait until psu member power off ok or timeout
  2607. bool power_off_ok = IsMemberPowerOffOK(master);
  2608. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  2609. if(power_off_ok || time >= WAIT_SLAVE_POWER_OFF_TIMEOUT)
  2610. {
  2611. GetClockTime(&_PsuGroupDerating_time[master]);
  2612. SetMemberPowerOffDone(master);
  2613. ShmGroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone = true;
  2614. LOG_INFO("Gun %d Set Derating Member Power Off %s", master + 1, power_off_ok ? "OK" : "Timeout");
  2615. }
  2616. }
  2617. if(ShmGroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone && !ShmGroupCollection[master].GroupCtrl.bits.DeratingRelayOff)
  2618. {
  2619. // after a little delay, set parallel relay off
  2620. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  2621. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  2622. {
  2623. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  2624. GetClockTime(&_PsuGroupDerating_time[master]);
  2625. SetParallelRelayOnOff(master, NO, _GROLE_SWITCH_OFF_OK);
  2626. ShmGroupCollection[master].GroupCtrl.bits.DeratingRelayOff = true;
  2627. LOG_INFO("Gun %d Set Parallel Relay Off %X -> %X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  2628. }
  2629. }
  2630. if(ShmGroupCollection[master].GroupCtrl.bits.DeratingRelayOff && !ShmGroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed)
  2631. {
  2632. // wait until parallel relay off confirmed
  2633. bool confirmed = IsParallelRelayConfirmed(master);
  2634. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  2635. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  2636. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  2637. {
  2638. GetClockTime(&_PsuGroupDerating_time[master]);
  2639. ShmGroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed = true;
  2640. LOG_INFO("Gun %d Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  2641. }
  2642. }
  2643. if(ShmGroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed && !ShmGroupCollection[master].GroupCtrl.bits.DeratingCompleted)
  2644. {
  2645. // remove all non group member
  2646. RemoveNonGroupMember(master);
  2647. ShmGroupCollection[master].GroupCtrl.bits.DeratingCompleted = true;
  2648. }
  2649. if(ShmGroupCollection[master].GroupCtrl.bits.DeratingCompleted)
  2650. {
  2651. ShmGroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  2652. UpdateGunAvailableCapability(master);
  2653. LOG_INFO("Gun %d Derating Completed", master + 1);
  2654. }
  2655. }
  2656. void MasterStopChargingProcess(unsigned char master)
  2657. {
  2658. int time = 0;
  2659. if(ShmGroupCollection[master].GroupCtrl.bits.StopChargingRequest && !ShmGroupCollection[master].GroupCtrl.bits.StopChargingConfirmed)
  2660. {
  2661. // set all member to power off
  2662. GetClockTime(&_StopCharging_time[master]);
  2663. SetMemberStartPowerOff(master, _GROLE_MASTER);
  2664. ShmGroupCollection[master].GroupCtrl.bits.StopChargingConfirmed = true;
  2665. }
  2666. if(ShmGroupCollection[master].GroupCtrl.bits.StopChargingConfirmed && !ShmGroupCollection[master].GroupCtrl.bits.AllPowerOffDone)
  2667. {
  2668. // wait until psu member power off ok or timeout
  2669. bool power_off_ok = IsMemberPowerOffOK(master);
  2670. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  2671. if(power_off_ok || time >= WAIT_SLAVE_POWER_OFF_TIMEOUT)
  2672. {
  2673. GetClockTime(&_StopCharging_time[master]);
  2674. SetMemberPowerOffDone(master);
  2675. ShmGroupCollection[master].GroupCtrl.bits.AllPowerOffDone = true;
  2676. LOG_INFO("Gun %d Set All Member Power Off %s", master + 1, power_off_ok ? "OK" : "Timeout");
  2677. }
  2678. }
  2679. if(ShmGroupCollection[master].GroupCtrl.bits.AllPowerOffDone && !ShmGroupCollection[master].GroupCtrl.bits.AllParallelRelayOff)
  2680. {
  2681. // after a little delay, set all member parallel relay off
  2682. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  2683. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  2684. {
  2685. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  2686. GetClockTime(&_StopCharging_time[master]);
  2687. SetParallelRelayOnOff(master, NO, _GROLE_MASTER);
  2688. ShmGroupCollection[master].GroupCtrl.bits.AllParallelRelayOff = true;
  2689. LOG_INFO("Gun %d Set All Member Parallel Relay Off %X -> %X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  2690. }
  2691. }
  2692. if(ShmGroupCollection[master].GroupCtrl.bits.AllParallelRelayOff && !ShmGroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed)
  2693. {
  2694. // wait until parallel relay off confirmed
  2695. bool confirmed = IsParallelRelayConfirmed(master);
  2696. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  2697. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  2698. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  2699. {
  2700. GetClockTime(&_StopCharging_time[master]);
  2701. ShmGroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed = true;
  2702. LOG_INFO("Gun %d All Member Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  2703. }
  2704. }
  2705. if(ShmGroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed && !ShmGroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted)
  2706. {
  2707. // remove all non group member
  2708. RemoveNonGroupMember(master);
  2709. ShmGroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted = true;
  2710. LOG_INFO("Gun %d All Member Stop Completed", master + 1);
  2711. }
  2712. if(ShmGroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted && !ShmGroupCollection[master].GroupCtrl.bits.StopChargingCompleted)
  2713. {
  2714. // check self group power down or not
  2715. if(ShmPsuData->PsuGroup[master].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT)
  2716. {
  2717. ShmGroupCollection[master].GroupCtrl.bits.StopChargingCompleted = true;
  2718. }
  2719. }
  2720. if(ShmGroupCollection[master].GroupCtrl.bits.StopChargingCompleted)
  2721. {
  2722. ShmGroupCollection[master].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  2723. ShmGroupCollection[master].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  2724. SetPsuGroupToIdle(master);
  2725. LOG_INFO("Gun %d Stop Charging Completed", master + 1);
  2726. }
  2727. }
  2728. void SlaveStopChargingProcess(unsigned char slave)
  2729. {
  2730. int time = 0;
  2731. unsigned char master = 0;
  2732. PsuGroupPartner PowerOffMember, GrabMember;
  2733. if(ShmGroupCollection[slave].Role != _GROLE_SLAVE)
  2734. {
  2735. return;
  2736. }
  2737. master = ShmGroupCollection[slave].TargetGroup - 1;
  2738. if(ShmGroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest && !ShmGroupCollection[slave].GroupCtrl.bits.CheckSlaveReady)
  2739. {
  2740. ShmGroupCollection[slave].GroupCtrl.bits.CheckSlaveReady = true;
  2741. if(ShmGroupCollection[master].Role == _GROLE_REQUEST_TO_CHARGING)
  2742. {
  2743. LOG_INFO("Gun %d Need Wait Gun %d Grouping Completed", slave + 1, master + 1);
  2744. GetClockTime(&_CheckSlaveReady_time[slave]);
  2745. }
  2746. else if(ShmGroupCollection[master].Role == _GROLE_MASTER && ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  2747. {
  2748. LOG_INFO("Gun %d Need Wait Gun %d Derating Completed", slave + 1, master + 1);
  2749. GetClockTime(&_CheckSlaveReady_time[slave]);
  2750. }
  2751. else
  2752. {
  2753. ShmGroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest = true;
  2754. ShmGroupCollection[slave].GroupCtrl.bits.WaitSlaveReady = true;
  2755. LOG_INFO("Gun %d Need Power Off And Request To Charging", slave + 1);
  2756. }
  2757. }
  2758. // wait until master derating completed
  2759. if(ShmGroupCollection[slave].GroupCtrl.bits.CheckSlaveReady && !ShmGroupCollection[slave].GroupCtrl.bits.WaitSlaveReady)
  2760. {
  2761. time = GetTimeoutValue(_CheckSlaveReady_time[slave]) / uSEC_VAL;
  2762. if(time >= WAIT_SLAVE_READY_TIME ||
  2763. (ShmGroupCollection[master].Role == _GROLE_MASTER && !ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed))
  2764. {
  2765. if(!(ShmGroupCollection[master].Role == _GROLE_MASTER && !ShmGroupCollection[master].GroupCtrl.bits.DeratingConfirmed))
  2766. {
  2767. ShmGroupCollection[slave].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  2768. LOG_INFO("Gun %d Wait Gun %d Timeout", slave + 1);
  2769. }
  2770. else
  2771. {
  2772. ShmGroupCollection[slave].GroupCtrl.bits.WaitSlaveReady = true;
  2773. LOG_INFO("Gun %d Is Ready", slave + 1);
  2774. GetClockTime(&_CheckSlaveReady_time[slave]);
  2775. }
  2776. }
  2777. }
  2778. if(ShmGroupCollection[slave].GroupCtrl.bits.WaitSlaveReady && !ShmGroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest)
  2779. {
  2780. time = GetTimeoutValue(_CheckSlaveReady_time[slave]) / uSEC_VAL;
  2781. if(time >= WAIT_SLAVE_DELAY)
  2782. {
  2783. ShmGroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest = true;
  2784. }
  2785. }
  2786. // self group, other slave or master set SlavePowerOffRequest flag to power off
  2787. if(ShmGroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest && !ShmGroupCollection[slave].GroupCtrl.bits.SlavePowerOffConfirmed)
  2788. {
  2789. PsuGroupPowerOffCheck(slave, &PowerOffMember);
  2790. if(PowerOffMember.Quantity == 1 && ShmGroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest)
  2791. {
  2792. // grab here
  2793. if(PsuGroupGrabCheck(slave, &GrabMember))
  2794. {
  2795. for(int i = 0; i < GrabMember.Quantity; i++)
  2796. {
  2797. PowerOffMember.Member[PowerOffMember.Quantity++] = GrabMember.Member[i];
  2798. }
  2799. }
  2800. }
  2801. if(PowerOffMember.Quantity > 0)
  2802. {
  2803. unsigned char target = ShmGroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest ? (slave + 1) : 0;
  2804. PrepareToPowerOff(target, &PowerOffMember);
  2805. if(PowerOffMember.Quantity > 1 && ShmGroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest)
  2806. {
  2807. ShmGroupCollection[slave].PossibleMember.Quantity = PowerOffMember.Quantity - 1;
  2808. memcpy(ShmGroupCollection[slave].PossibleMember.Member, &PowerOffMember.Member[1], PowerOffMember.Quantity - 1);
  2809. }
  2810. ShmGroupCollection[slave].GroupCtrl.bits.SlavePowerOffConfirmed = true;
  2811. }
  2812. else
  2813. {
  2814. ShmGroupCollection[slave].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  2815. }
  2816. }
  2817. }
  2818. void MasterExtendCapabilityProcess(unsigned char master)
  2819. {
  2820. int time = 0;
  2821. if(ShmGroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue != 0 ||
  2822. ShmGroupCollection[master].GroupCtrl.bits.StopChargingRequest)
  2823. {
  2824. if(ShmGroupCollection[master].GroupCtrl.bits.MorePowerConfirmed)
  2825. {
  2826. SetExtendPrechargeStop(master);
  2827. }
  2828. ShmGroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  2829. return;
  2830. }
  2831. if(ShmGroupCollection[master].GroupCtrl.bits.MorePowerRequest && !ShmGroupCollection[master].GroupCtrl.bits.MorePowerConfirmed)
  2832. {
  2833. GetClockTime(&_ExtendCapability_time[master]);
  2834. memset(&ShmGroupCollection[master].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  2835. FindPsuGroupPartner(master, MAX_GROUP_QUANTITY, &ShmGroupCollection[master].PossibleMember);
  2836. if(ShmGroupCollection[master].PossibleMember.Quantity > 0)
  2837. {
  2838. PrepareToExtendCapability(master + 1, &ShmGroupCollection[master].PossibleMember);
  2839. ShmGroupCollection[master].GroupCtrl.bits.MorePowerConfirmed = true;
  2840. }
  2841. else
  2842. {
  2843. ShmGroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  2844. }
  2845. }
  2846. if(ShmGroupCollection[master].GroupCtrl.bits.MorePowerConfirmed && !ShmGroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone)
  2847. {
  2848. unsigned short voltage = ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage >= PRECHARGE_OFFSET_VOLTAGE ?
  2849. ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage - PRECHARGE_OFFSET_VOLTAGE : 0;
  2850. for(int i = 0; i < ShmGroupCollection[master].PossibleMember.Quantity; i++)
  2851. {
  2852. unsigned char slave = ShmGroupCollection[master].PossibleMember.Member[i];
  2853. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0 && ShmPsuData->PsuGroup[slave].GroupAvailableCurrent > 0)
  2854. {
  2855. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = voltage;
  2856. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = ZERO_CURRENT;
  2857. }
  2858. }
  2859. if(!ShmGroupCollection[master].GroupCtrl.bits.ExtendPrecharge)
  2860. {
  2861. GetClockTime(&_ExtendCapability_time[master]);
  2862. LOG_INFO("Gun %d Set ExtendPrecharge Voltage %d.%d V", master + 1, (voltage / 10), (voltage % 10));
  2863. }
  2864. ShmGroupCollection[master].GroupCtrl.bits.ExtendPrecharge = true;
  2865. }
  2866. if(ShmGroupCollection[master].GroupCtrl.bits.ExtendPrecharge && !ShmGroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone)
  2867. {
  2868. bool ready = IsExtendPrechargeReady(master);
  2869. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  2870. if(ready || time >= WAIT_PRECHARGE_TIME)
  2871. {
  2872. LOG_INFO("Gun %d ExtendPrecharge %s", master + 1, ready ? "Ready" : "Timeout");
  2873. if(ready)
  2874. {
  2875. GetClockTime(&_ExtendCapability_time[master]);
  2876. SetExtendPrechargeCompleted(master);
  2877. ShmGroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone = true;
  2878. }
  2879. else
  2880. {
  2881. SetExtendPrechargeStop(master);
  2882. ShmGroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  2883. }
  2884. }
  2885. }
  2886. if(ShmGroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone && !ShmGroupCollection[master].GroupCtrl.bits.ExtendRelayOn)
  2887. {
  2888. // after a little delay, set extend parallel relay on
  2889. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  2890. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  2891. {
  2892. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  2893. GetClockTime(&_ExtendCapability_time[master]);
  2894. SetParallelRelayOnOff(master, YES, _GROLE_PRECHARGE_READY);
  2895. ShmGroupCollection[master].GroupCtrl.bits.ExtendRelayOn = true;
  2896. LOG_INFO("Gun %d Set Extend Parallel Relay On %X -> %X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  2897. }
  2898. }
  2899. if(ShmGroupCollection[master].GroupCtrl.bits.ExtendRelayOn && !ShmGroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed)
  2900. {
  2901. // wait until extend parallel relay on confirmed
  2902. bool confirmed = IsParallelRelayConfirmed(master);
  2903. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  2904. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  2905. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  2906. {
  2907. ShmGroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed = true;
  2908. LOG_INFO("Gun %d Extend Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  2909. }
  2910. }
  2911. if(ShmGroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed && !ShmGroupCollection[master].GroupCtrl.bits.ExtendCompleted)
  2912. {
  2913. AddExtendMember(master);
  2914. ShmGroupCollection[master].GroupCtrl.bits.ExtendCompleted = true;
  2915. }
  2916. if(ShmGroupCollection[master].GroupCtrl.bits.ExtendCompleted)
  2917. {
  2918. ShmGroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  2919. ShmGroupCollection[master].GroupCtrl.bits.OutputCurrentStable = false;
  2920. GetClockTime(&_ReachCurrent_time[master]);
  2921. UpdateGunAvailableCapability(master);
  2922. ShmGroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  2923. GetClockTime(&_StageCurrent_time[master]);
  2924. LOG_INFO("Gun %d Extend Capability Completed", master + 1);
  2925. }
  2926. }
  2927. void ShowGunVoltageCurrent(unsigned char master)
  2928. {
  2929. unsigned short voltage = 0, current = 0, fireVoltage = 0;
  2930. unsigned short diffVoltage = 0, diffCurrent = 0;
  2931. if(ShmGroupCollection[master].Role == _GROLE_MASTER)
  2932. {
  2933. voltage = (int)(chargingInfo[master]->PresentChargingVoltage * 10);
  2934. current = (int)(chargingInfo[master]->PresentChargingCurrent * 10);
  2935. fireVoltage = chargingInfo[master]->FireChargingVoltage;
  2936. diffVoltage = voltage >= evseOutVol[master] ? voltage - evseOutVol[master] : evseOutVol[master] - voltage;
  2937. diffCurrent = current >= evseOutCur[master] ? current - evseOutCur[master] : evseOutCur[master] - current;
  2938. if(diffVoltage >= 10 || diffCurrent >= 10)
  2939. {
  2940. evseOutputDelay[master] = SHOW_OUTPUT_DELAY;
  2941. evseOutVol[master] = voltage;
  2942. evseOutCur[master] = current;
  2943. }
  2944. if(evseOutputDelay[master] != 0)
  2945. {
  2946. LOG_INFO("Gun %d Need Voltage: %4d V, Current: %3d A, Output Voltage: %4d.%d V, Current: %3d.%d A, Fire Voltage: %4d V",
  2947. master + 1, (int)chargingInfo[master]->EvBatterytargetVoltage, (int)chargingInfo[master]->EvBatterytargetCurrent,
  2948. (voltage / 10), (voltage % 10), (current / 10), (current % 10), fireVoltage / 10);
  2949. evseOutputDelay[master]--;
  2950. }
  2951. }
  2952. }
  2953. void PsuGroupControlProcess(void)
  2954. {
  2955. int time = 0;
  2956. unsigned char role = 0;
  2957. //unsigned short TargetVoltage = 0, TargetCurrent = 0;
  2958. for(byte group = 0; group < GENERAL_GUN_QUANTITY; group++)
  2959. {
  2960. role = ShmGroupCollection[group].Role;
  2961. switch(role)
  2962. {
  2963. case _GROLE_IDLE:
  2964. if(ShmGroupCollection[group].PreRole != role)
  2965. {
  2966. ShmGroupCollection[group].PreRole = role;
  2967. ShmGroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue = 0;
  2968. ShmGroupCollection[group].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  2969. ShmGroupCollection[group].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  2970. ShmGroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  2971. ShmGroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  2972. ShmGroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  2973. PSU_LOG("===== PSU Group[%02X] ===== Idle", group);
  2974. GetClockTime(&_PsuGroupRole_time[group]);
  2975. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  2976. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  2977. PreGroupOutput[group].GTargetVoltage = 0;
  2978. PreGroupOutput[group].GTargetCurrent = 0;
  2979. SetPsuGroupOutput(group);
  2980. }
  2981. if(isStartOutputSwitch[group])
  2982. {
  2983. SetPsuGroupPowerOnOff(group, PSU_POWER_OFF);
  2984. }
  2985. if(ShmGroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue != 0 &&
  2986. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  2987. {
  2988. CheckChargingRequest(group);
  2989. }
  2990. break;
  2991. case _GROLE_MASTER:
  2992. if(ShmGroupCollection[group].PreRole != role)
  2993. {
  2994. ShmGroupCollection[group].PreRole = role;
  2995. ShmGroupCollection[group].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  2996. ShmGroupCollection[group].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  2997. ShmGroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  2998. PSU_LOG("===== PSU Group[%02X] ===== Master", group);
  2999. GetClockTime(&_PsuGroupRole_time[group]);
  3000. OutputConfigStep[group] = _CURRENT_MODE_NONE;
  3001. _preOutputConfigStep[group] = _CURRENT_MODE_NONE;
  3002. MaxCurrentDemand[group] = 0;
  3003. StageMaxCurrent[group] = 0;
  3004. }
  3005. if(!ShmGroupCollection[group].GroupCtrl.bits.AlreadyInChargingMode)
  3006. {
  3007. if(chargingInfo[group]->SystemStatus == S_CHARGING)
  3008. {
  3009. LOG_INFO("Gun %d Enter Charging Mode", group + 1);
  3010. ShmGroupCollection[group].GroupCtrl.bits.AlreadyInChargingMode = true;
  3011. ShmGroupCollection[group].GroupCtrl.bits.ReachMaxCurrentDemand = false;
  3012. ShmGroupCollection[group].GroupCtrl.bits.ReachMaxStageCurrent = false;
  3013. GetClockTime(&_MaxCurrent_time[group]);
  3014. GetClockTime(&_StageCurrent_time[group]);
  3015. }
  3016. }
  3017. if((chargingInfo[group]->SystemStatus <= S_IDLE) ||
  3018. (chargingInfo[group]->SystemStatus >= S_TERMINATING && chargingInfo[group]->SystemStatus <= S_FAULT))
  3019. {
  3020. if(!ShmGroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  3021. {
  3022. LOG_INFO("Gun %d SystemStatus(%d) Need Stop Charging", group + 1, chargingInfo[group]->SystemStatus);
  3023. }
  3024. ShmGroupCollection[group].GroupCtrl.bits.StopChargingRequest = true;
  3025. }
  3026. if(ShmGroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue == 0)
  3027. {
  3028. if(ShmGroupCollection[group].GroupCtrl.bits.ExtendAvailable)
  3029. {
  3030. time = GetTimeoutValue(_ExtendCapability_time[group]) / uSEC_VAL;
  3031. if(time >= EXTEND_CAPABILITY_DELAY)
  3032. {
  3033. int available = GetPsuGroupAvailable(group);
  3034. if(available > 0)
  3035. {
  3036. ShmGroupCollection[group].GroupCtrl.bits.MorePowerRequest = true;
  3037. LOG_INFO("Gun %d Start Extend Capability", group + 1);
  3038. }
  3039. ShmGroupCollection[group].GroupCtrl.bits.ExtendAvailable = false;
  3040. }
  3041. }
  3042. }
  3043. if(ShmGroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue != 0)
  3044. {
  3045. MasterExtendCapabilityProcess(group);
  3046. }
  3047. // need derating
  3048. if(ShmGroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue != 0 &&
  3049. !ShmGroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  3050. {
  3051. PsuGroupDeratingProcess(group);
  3052. }
  3053. if(ShmGroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  3054. {
  3055. ShmGroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  3056. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  3057. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  3058. MasterStopChargingProcess(group);
  3059. }
  3060. else
  3061. {
  3062. UpdateGunLoading(group);
  3063. UpdatePsuGroupOutputConfig(group);
  3064. UpdateMasterPsuGroupLoading(group);
  3065. PsuGroupOutputConfigCheck(group);
  3066. }
  3067. ShowGunVoltageCurrent(group);
  3068. SetPsuGroupOutput(group);
  3069. break;
  3070. case _GROLE_SLAVE:
  3071. if(ShmGroupCollection[group].PreRole != role)
  3072. {
  3073. ShmGroupCollection[group].PreRole = role;
  3074. PSU_LOG("===== PSU Group[%02X] ===== Slave", group);
  3075. GetClockTime(&_PsuGroupRole_time[group]);
  3076. }
  3077. if(ShmGroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue != 0)
  3078. {
  3079. SlaveStopChargingProcess(group);
  3080. }
  3081. else
  3082. {
  3083. SetPsuGroupOutput(group);
  3084. }
  3085. break;
  3086. case _GROLE_PREPARE_SWITCH_OFF:
  3087. if(ShmGroupCollection[group].PreRole != role)
  3088. {
  3089. ShmGroupCollection[group].PreRole = role;
  3090. PSU_LOG("===== PSU Group[%02X] ===== Prepare Off", group);
  3091. GetClockTime(&_PsuGroupRole_time[group]);
  3092. }
  3093. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  3094. if(time >= MAX_PREPARE_SWITCH_OFF_TIME)
  3095. {
  3096. // timeout shall not happen
  3097. SetPsuGroupRole(group, _GROLE_SLAVE_POWER_OFF);
  3098. }
  3099. else
  3100. {
  3101. SetPsuGroupOutput(group);
  3102. }
  3103. break;
  3104. case _GROLE_SLAVE_POWER_OFF:
  3105. if(ShmGroupCollection[group].PreRole != role)
  3106. {
  3107. ShmGroupCollection[group].PreRole = role;
  3108. PSU_LOG("===== PSU Group[%02X] ===== Slave Power Off", group);
  3109. GetClockTime(&_PsuGroupRole_time[group]);
  3110. }
  3111. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  3112. if(time >= MIN_POWER_OFF_TIME &&
  3113. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT)
  3114. {
  3115. SetPsuGroupRole(group, _GROLE_SWITCH_OFF_OK);
  3116. }
  3117. else
  3118. {
  3119. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  3120. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  3121. SetPsuGroupOutput(group);
  3122. }
  3123. break;
  3124. case _GROLE_SWITCH_OFF_OK:
  3125. if(ShmGroupCollection[group].PreRole != role)
  3126. {
  3127. ShmGroupCollection[group].PreRole = role;
  3128. PSU_LOG("===== PSU Group[%02X] ===== Switch Off OK, %d.%d V, %d.%d A", group,
  3129. (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10),
  3130. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent % 10));
  3131. GetClockTime(&_PsuGroupRole_time[group]);
  3132. }
  3133. break;
  3134. case _GROLE_WAIT_IDLE:
  3135. if(ShmGroupCollection[group].PreRole != role)
  3136. {
  3137. ShmGroupCollection[group].PreRole = role;
  3138. PSU_LOG("===== PSU Group[%02X] ===== Wait Idle, %d.%d V, %d.%d A", group,
  3139. (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10),
  3140. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent % 10));
  3141. GetClockTime(&_PsuGroupRole_time[group]);
  3142. }
  3143. break;
  3144. case _GROLE_WAIT_SLAVE:
  3145. if(ShmGroupCollection[group].PreRole != role)
  3146. {
  3147. ShmGroupCollection[group].PreRole = role;
  3148. PSU_LOG("===== PSU Group[%02X] ===== Wait Slave", group);
  3149. GetClockTime(&_PsuGroupRole_time[group]);
  3150. }
  3151. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  3152. if(ShmGroupCollection[group].ReservedTarget != 0)
  3153. {
  3154. if(time >= WAIT_SLAVE_TO_CHARGING && (ShmGroupCollection[group].ReservedTarget - 1) == group)
  3155. {
  3156. SetPsuGroupRole(group, _GROLE_REQUEST_TO_CHARGING);
  3157. ShmGroupCollection[group].GroupCtrl.bits.ChargingRequest = true;
  3158. ShmGroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  3159. break;
  3160. }
  3161. }
  3162. if(time >= WAIT_SLAVE_TIMEOUT)
  3163. {
  3164. PSU_LOG("Group[%02X] Wait Slave Timeout", group);
  3165. SetPsuGroupToIdle(group);
  3166. }
  3167. break;
  3168. case _GROLE_PREPARE_ATTACH_ON:
  3169. if(ShmGroupCollection[group].PreRole != role)
  3170. {
  3171. ShmGroupCollection[group].PreRole = role;
  3172. PSU_LOG("===== PSU Group[%02X] ===== Prepare Attach On", group);
  3173. GetClockTime(&_PsuGroupRole_time[group]);
  3174. }
  3175. SetPsuGroupOutput(group);
  3176. break;
  3177. case _GROLE_PRECHARGE_READY:
  3178. if(ShmGroupCollection[group].PreRole != role)
  3179. {
  3180. ShmGroupCollection[group].PreRole = role;
  3181. PSU_LOG("===== PSU Group[%02X] ===== Precharge %d.%d V Ready",
  3182. group, (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10));
  3183. GetClockTime(&_PsuGroupRole_time[group]);
  3184. }
  3185. break;
  3186. case _GROLE_EXTEND_STOP:
  3187. if(ShmGroupCollection[group].PreRole != role)
  3188. {
  3189. ShmGroupCollection[group].PreRole = role;
  3190. PSU_LOG("===== PSU Group[%02X] ===== Extend Stop", group);
  3191. GetClockTime(&_PsuGroupRole_time[group]);
  3192. }
  3193. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  3194. if(time >= MIN_POWER_OFF_TIME ||
  3195. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT &&
  3196. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage <= MAX_PSU_POWER_OFF_VOLTAGE))
  3197. {
  3198. SetPsuGroupToIdle(group);
  3199. }
  3200. else
  3201. {
  3202. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  3203. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  3204. SetPsuGroupOutput(group);
  3205. }
  3206. break;
  3207. case _GROLE_REQUEST_TO_CHARGING:
  3208. if(ShmGroupCollection[group].PreRole != role)
  3209. {
  3210. ShmGroupCollection[group].PreRole = role;
  3211. PSU_LOG("===== PSU Group[%02X] ===== Request To Charging", group);
  3212. GetClockTime(&_PsuGroupRole_time[group]);
  3213. }
  3214. if(ShmGroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue != 0)
  3215. {
  3216. ChargingRequestProcess(group);
  3217. }
  3218. break;
  3219. case _GROLE_TERMINATE:
  3220. if(ShmGroupCollection[group].PreRole != role)
  3221. {
  3222. ShmGroupCollection[group].PreRole = role;
  3223. PSU_LOG("===== PSU Group[%02X] ===== Terminate", group);
  3224. GetClockTime(&_PsuGroupRole_time[group]);
  3225. }
  3226. break;
  3227. case _GROLE_WAIT_TERMINATED:
  3228. if(ShmGroupCollection[group].PreRole != role)
  3229. {
  3230. ShmGroupCollection[group].PreRole = role;
  3231. PSU_LOG("===== PSU Group[%02X] ===== Wait Terminated", group);
  3232. GetClockTime(&_PsuGroupRole_time[group]);
  3233. }
  3234. break;
  3235. case _GROLE_NONE:
  3236. if(ShmGroupCollection[group].PreRole != role)
  3237. {
  3238. ShmGroupCollection[group].PreRole = role;
  3239. PSU_LOG("===== PSU Group[%02X] ===== None", group);
  3240. GetClockTime(&_PsuGroupRole_time[group]);
  3241. }
  3242. break;
  3243. default:
  3244. if(ShmGroupCollection[group].PreRole != role)
  3245. {
  3246. ShmGroupCollection[group].PreRole = role;
  3247. PSU_LOG("===== PSU Group[%02X] ===== Unknown Role = %d", group, role);
  3248. GetClockTime(&_PsuGroupRole_time[group]);
  3249. }
  3250. SetPsuGroupRole(group, _GROLE_NONE);
  3251. break;
  3252. }
  3253. }
  3254. }
  3255. // only for test & debug purpose
  3256. void PsuGroupSimulation(void)
  3257. {
  3258. unsigned char master = 0;
  3259. int _groupCurrent = 0, _groupPower = 0;
  3260. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  3261. {
  3262. master = ShmChargerInfo->PsuGrouping.GroupCollection[i].TargetGroup;
  3263. // calculate output group capability
  3264. if(master == 0)
  3265. {
  3266. chargingInfo[i]->AvailableChargingCurrent = ShmPsuData->PsuGroup[i].GroupAvailableCurrent;
  3267. chargingInfo[i]->AvailableChargingPower = ShmPsuData->PsuGroup[i].GroupAvailablePower;
  3268. }
  3269. else
  3270. {
  3271. _groupCurrent = ShmPsuData->PsuGroup[master - 1].GroupAvailableCurrent;
  3272. _groupPower = ShmPsuData->PsuGroup[master - 1].GroupAvailablePower;
  3273. for(byte j = 0; j < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; j++)
  3274. {
  3275. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[j];
  3276. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  3277. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  3278. {
  3279. _groupCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  3280. _groupPower += ShmPsuData->PsuGroup[slave].GroupAvailablePower;
  3281. }
  3282. }
  3283. if(ShmPsuGrouping->GroupCollection[master - 1].GroupCtrl.bits.DeratingConfirmed)
  3284. {
  3285. chargingInfo[master - 1]->AvailableChargingCurrent = ShmPsuGrouping->GroupCollection[master - 1].ReAssignAvailableCurrent;
  3286. }
  3287. else
  3288. {
  3289. chargingInfo[master - 1]->AvailableChargingCurrent = _groupCurrent;
  3290. }
  3291. chargingInfo[master - 1]->AvailableChargingPower = _groupPower;
  3292. if((master - 1) != i)
  3293. {
  3294. chargingInfo[i]->AvailableChargingCurrent = 0;;
  3295. chargingInfo[i]->AvailableChargingPower = 0;
  3296. chargingInfo[i]->RealRatingPower = 0;
  3297. chargingInfo[i]->MaximumChargingVoltage = 0;
  3298. }
  3299. }
  3300. }
  3301. }
  3302. void PsuGroupingInitial(void)
  3303. {
  3304. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  3305. {
  3306. ShmGroupCollection[i].Role = 0;
  3307. ShmGroupCollection[i].PreRole = 0xFF;
  3308. memset(&ShmGroupCollection[i].Partner, 0x00, sizeof(PsuGroupPartner));
  3309. memset(&ShmGroupCollection[i].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  3310. ShmGroupCollection[i].TargetGroup = 0;
  3311. ShmGroupCollection[i].ReservedTarget = 0;
  3312. memset(&ShmGroupCollection[i].GroupCtrl, 0x00, sizeof(PsuGroupControl));
  3313. ShmGroupCollection[i].ReAssignAvailableCurrent = 0;
  3314. ShmGroupCollection[i].ParallelCheck = 0;
  3315. ShmGroupCollection[i].GunLoading = 0;
  3316. memset(&ShmPsuGrouping->GroupOutput[i], 0x00, sizeof(GroupOutputConfigInfo));
  3317. }
  3318. }
  3319. int main(void)
  3320. {
  3321. byte _TotalModuleCount = 0;
  3322. byte _PrePsuWorkStep = 0;
  3323. byte isInitialComp = NO;
  3324. if(InitShareMemory() == FAIL)
  3325. {
  3326. #ifdef SystemLogMessage
  3327. LOG_ERROR("InitShareMemory NG");
  3328. #endif
  3329. if(ShmStatusCodeData != NULL)
  3330. {
  3331. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  3332. }
  3333. sleep(5);
  3334. return 0;
  3335. }
  3336. LOG_INFO("InitShareMemory OK");
  3337. signal(SIGCHLD, SIG_IGN);
  3338. // register callback function
  3339. RefreshStatus(&GetStatusCallback);
  3340. RefreshModuleCount(&GetModuleCountCallback);
  3341. RefreshAvailableCap(&GetAvailableCapCallback);
  3342. RefreshFwVersion(&GetFwCallback);
  3343. RefreshInputVol(&GetInputVoltageCallback);
  3344. RefreshGetOutput(&GetPresentOutputCallback);
  3345. RefreshMisInfo(&GetMisCallback);
  3346. RefreshIavailable(&GetIavailableCallback);
  3347. RefreshGetOutputF(&GetPresentOutputFCallback);
  3348. // GetPresentOutputCallback & GetStatusCallback
  3349. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  3350. // GetStatusCallback
  3351. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  3352. // GetInputVoltageCallback
  3353. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  3354. sleep(2);
  3355. _gunCount = GENERAL_GUN_QUANTITY;
  3356. _maxGroupCount = GENERAL_GUN_QUANTITY;
  3357. _PrePsuWorkStep = _INIT_PSU_STATUS;
  3358. ShmPsuData->Work_Step = INITIAL_START;
  3359. isInitialComp = NO;
  3360. LOG_INFO("PSU Work Status = %d", ShmPsuData->Work_Step);
  3361. // initial object
  3362. Initialization();
  3363. InitialPsuData();
  3364. libInitialize = InitialCommunication();
  3365. //main loop
  3366. while (libInitialize)
  3367. {
  3368. // 斷電狀態
  3369. if (ShmChargerInfo->Control.RelayCtrl.bits.AcContactor == NO ||
  3370. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == YES ||
  3371. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency == YES)
  3372. {
  3373. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  3374. if (!isInitialComp)
  3375. {
  3376. _PrePsuWorkStep = _INIT_PSU_STATUS;
  3377. ShmPsuData->Work_Step = INITIAL_START;
  3378. psuCmdSeq = _PSU_CMD_STATUS;
  3379. _TotalModuleCount = 0;
  3380. sleep(2);
  3381. InitialPsuData();
  3382. isInitialComp = YES;
  3383. }
  3384. sleep(1);
  3385. continue;
  3386. }
  3387. else
  3388. {
  3389. isInitialComp = NO;
  3390. }
  3391. // only for test & debug purpose
  3392. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  3393. {
  3394. ShmPsuData->Work_Step = _WORK_CHARGING;
  3395. ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity = 3;
  3396. ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity = 3;
  3397. ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity = 3;
  3398. ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity = 3;
  3399. ShmPsuData->PsuGroup[0].GroupAvailableCurrent = ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity * 1000;
  3400. ShmPsuData->PsuGroup[1].GroupAvailableCurrent = ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity * 1000;
  3401. ShmPsuData->PsuGroup[2].GroupAvailableCurrent = ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity * 1000;
  3402. ShmPsuData->PsuGroup[3].GroupAvailableCurrent = ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity * 1000;
  3403. ShmPsuData->PsuGroup[0].GroupAvailablePower = ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity * 300;
  3404. ShmPsuData->PsuGroup[1].GroupAvailablePower = ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity * 300;
  3405. ShmPsuData->PsuGroup[2].GroupAvailablePower = ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity * 300;
  3406. ShmPsuData->PsuGroup[3].GroupAvailablePower = ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity * 300;
  3407. PsuGroupSimulation();
  3408. }
  3409. // 自檢失敗
  3410. if (ShmPsuData->Work_Step == _NO_WORKING)
  3411. {
  3412. LOG_INFO("== PSU == self test fail.");
  3413. sleep(5);
  3414. }
  3415. if((GetTimeoutValue(_PsuReceiveRecoveryCheck_time) / uSEC_VAL) >= PSU_TASK_CHECK_TIME)
  3416. {
  3417. PsuReceiveRecoveryCheck();
  3418. GetClockTime(&_PsuReceiveRecoveryCheck_time);
  3419. }
  3420. switch(ShmPsuData->Work_Step)
  3421. {
  3422. case INITIAL_START:
  3423. {
  3424. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3425. {
  3426. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3427. LOG_INFO("== PSU == INITIAL_START");
  3428. }
  3429. sleep(5);
  3430. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  3431. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  3432. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  3433. {
  3434. isStartOutputSwitch[index] = false;
  3435. }
  3436. ShmPsuData->Work_Step = GET_PSU_COUNT;
  3437. }
  3438. break;
  3439. case GET_PSU_COUNT:
  3440. {
  3441. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3442. {
  3443. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3444. LOG_INFO("== PSU == GET_PSU_COUNT");
  3445. GetClockTime(&_PsuWorkStep_time);
  3446. GetClockTime(&_cmdSubPriority_time);
  3447. }
  3448. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  3449. int interval = GetTimeoutValue(_cmdSubPriority_time) / uSEC_VAL;
  3450. if(interval > GET_PSU_COUNT_INTERVAL)
  3451. {
  3452. _TotalModuleCount = 0;
  3453. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  3454. {
  3455. // 總和各群模組數量
  3456. _TotalModuleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  3457. }
  3458. LOG_INFO("== PSU == indexCount = %d, moduleCount = %d, sysCount = %d",
  3459. ShmPsuData->GroupCount, _TotalModuleCount, ShmPsuData->SystemPresentPsuQuantity);
  3460. // 判斷系統數量與各群數量一致
  3461. if(_TotalModuleCount == ShmPsuData->SystemPresentPsuQuantity && _TotalModuleCount > 0 &&
  3462. time > GET_PSU_COUNT_TIME)
  3463. {
  3464. LOG_INFO("Psu Count = %d", _TotalModuleCount);
  3465. ShmPsuData->Work_Step = Get_PSU_LOCATION;
  3466. }
  3467. else
  3468. {
  3469. // 發送取得目前全部模組數量
  3470. GetModuleCount(SYSTEM_CMD);
  3471. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  3472. {
  3473. // 取各群模組數量
  3474. GetModuleCount(index);
  3475. }
  3476. }
  3477. GetClockTime(&_cmdSubPriority_time);
  3478. }
  3479. }
  3480. break;
  3481. case Get_PSU_LOCATION:
  3482. {
  3483. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3484. {
  3485. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3486. LOG_INFO("== PSU == Get_PSU_LOCATION");
  3487. // clean psu location info
  3488. memset(ShmPsuPosition, 0x00, sizeof(PsuPositionInfoData));
  3489. GetClockTime(&_cmdSubPriority_time);
  3490. }
  3491. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  3492. if(interval > GET_PSU_LOCATION_INTERVAL)
  3493. {
  3494. for(byte index = 0; index < ShmPsuData->GroupCount; index++)
  3495. {
  3496. if(ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity > 0)
  3497. {
  3498. // 取得狀態 : 支援模塊不須按照順序功能
  3499. GetStatus(index);
  3500. }
  3501. }
  3502. }
  3503. if(ShmPsuPosition->PsuLocationInit)
  3504. {
  3505. #if 1
  3506. for(int i = 0; i < _maxGroupCount; i++)
  3507. {
  3508. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  3509. {
  3510. ShowGroupMember(i);
  3511. }
  3512. }
  3513. #endif
  3514. //ShmPsuData->Work_Step = Get_PSU_VERSION;
  3515. ShmPsuData->Work_Step = PSU_COUNT_CONFIRM;
  3516. }
  3517. if(ShmPsuPosition->ReInitPsuLocation)
  3518. {
  3519. LOG_INFO("Retry Psu Location Initialization");
  3520. ShmPsuData->Work_Step = GET_PSU_COUNT;
  3521. }
  3522. }
  3523. break;
  3524. case PSU_COUNT_CONFIRM:
  3525. {
  3526. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3527. {
  3528. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3529. LOG_INFO("== PSU == PSU_COUNT_CONFIRM");
  3530. GetClockTime(&_PsuWorkStep_time);
  3531. GetClockTime(&_cmdSubPriority_time);
  3532. }
  3533. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  3534. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  3535. if(interval > PSU_COUNT_CONFIRM_INTERVAL)
  3536. {
  3537. _TotalModuleCount = 0;
  3538. for (byte index = 0; index < _maxGroupCount; index++)
  3539. {
  3540. // 總和各群模組數量
  3541. _TotalModuleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  3542. }
  3543. if(time > PSU_COUNT_CONFIRM_TIME)
  3544. {
  3545. if(_TotalModuleCount == ShmPsuData->SystemPresentPsuQuantity && _TotalModuleCount > 0)
  3546. {
  3547. ShmPsuData->Work_Step = Get_PSU_VERSION;
  3548. }
  3549. else
  3550. {
  3551. LOG_INFO("Total PSU = %d, System PSU Quantity = %d", _TotalModuleCount, ShmPsuData->SystemPresentPsuQuantity);
  3552. LOG_INFO("PSU Quantity Confirm Fail");
  3553. ShmPsuData->Work_Step = GET_PSU_COUNT;
  3554. }
  3555. break;
  3556. }
  3557. // 發送取得目前全部模組數量
  3558. GetModuleCount(SYSTEM_CMD);
  3559. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  3560. {
  3561. // 取各群模組數量
  3562. GetModuleCount(index);
  3563. }
  3564. GetClockTime(&_cmdSubPriority_time);
  3565. }
  3566. }
  3567. break;
  3568. case Get_PSU_VERSION:
  3569. {
  3570. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3571. {
  3572. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3573. LOG_INFO("== PSU == Get_PSU_VERSION");
  3574. GetClockTime(&_cmdSubPriority_time);
  3575. // clean version info
  3576. memset(ShmPsuData->PsuVersion, 0x00, sizeof(ShmPsuData->PsuVersion));
  3577. }
  3578. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  3579. if (interval > GET_PSU_VERSION_INTERVAL)
  3580. {
  3581. bool isGetVersion = true;
  3582. for(byte psu = 0; psu < ShmPsuData->SystemPresentPsuQuantity; psu++)
  3583. {
  3584. if (strcmp((char *)ShmPsuData->PsuVersion[psu].FwPrimaryVersion, "") == EQUAL)
  3585. {
  3586. isGetVersion = false;
  3587. break;
  3588. }
  3589. }
  3590. if(isGetVersion)
  3591. {
  3592. #if 1
  3593. for(int i = 0; i < _maxGroupCount; i++)
  3594. {
  3595. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  3596. {
  3597. ShowPsuVersion(i);
  3598. }
  3599. }
  3600. #endif
  3601. ShmPsuData->Work_Step = GET_SYS_CAP;
  3602. }
  3603. else
  3604. {
  3605. for(byte group = 0; group < _maxGroupCount; group++)
  3606. {
  3607. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  3608. {
  3609. // 取版號
  3610. GetModuleVer(group);
  3611. }
  3612. }
  3613. }
  3614. GetClockTime(&_cmdSubPriority_time);
  3615. }
  3616. }
  3617. break;
  3618. case GET_SYS_CAP:
  3619. {
  3620. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3621. {
  3622. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3623. LOG_INFO("== PSU == GET_SYS_CAP");
  3624. GetClockTime(&_PsuWorkStep_time);
  3625. GetClockTime(&_cmdSubPriority_time);
  3626. }
  3627. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  3628. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  3629. if (interval > GET_PSU_CAP_INTERVAL)
  3630. {
  3631. if(time > GET_PSU_CAP_TIME && ShmPsuData->SystemAvailablePower > 0 &&
  3632. ShmPsuData->SystemAvailableCurrent > 0)
  3633. {
  3634. #if 1
  3635. for(int i = 0; i < _maxGroupCount; i++)
  3636. {
  3637. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  3638. {
  3639. ShowGroupAvailableCurrentPower(i);
  3640. }
  3641. }
  3642. #endif
  3643. // 判斷系統輸出額定功率與電流
  3644. LOG_INFO("SystemAvailableCurrent = %d, SystemAvailablePower = %d",
  3645. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  3646. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  3647. }
  3648. else
  3649. {
  3650. for(byte index = 0; index < ShmPsuData->GroupCount; index++)
  3651. {
  3652. if(ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity > 0)
  3653. {
  3654. // 取系統總輸出能力
  3655. GetModuleCap(index);
  3656. }
  3657. }
  3658. }
  3659. GetClockTime(&_cmdSubPriority_time);
  3660. }
  3661. }
  3662. break;
  3663. case BOOTING_COMPLETE:
  3664. {
  3665. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3666. {
  3667. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3668. LOG_INFO("== PSU == BOOTING_COMPLETE");
  3669. }
  3670. if(ShmChargerInfo->Control.SysCtrl.bits.SelfTestOK)
  3671. {
  3672. ShmPsuData->Work_Step = _WORK_CHARGING;
  3673. }
  3674. ShmChargerInfo->Control.SysCtrl.bits.SelfTestOK = true;
  3675. sleep(1);
  3676. }
  3677. break;
  3678. case _WORK_CHARGING:
  3679. {
  3680. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3681. {
  3682. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3683. LOG_INFO("== PSU == _WORK_CHARGING");
  3684. GetClockTime(&_PsuWorkStep_time);
  3685. GetClockTime(&_cmdSubPriority_time);
  3686. PsuGroupingInitial();
  3687. }
  3688. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  3689. // 低 Priority 的指令
  3690. if (time > 1000)
  3691. {
  3692. //PreCheckSmartChargingStep();
  3693. startModuleFlag = true;
  3694. GetClockTime(&_cmdSubPriority_time);
  3695. for(byte group = 0; group < GENERAL_GUN_QUANTITY; group++)
  3696. {
  3697. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  3698. {
  3699. GetStatus(group);
  3700. }
  3701. }
  3702. }
  3703. PsuGroupRoutineQuery();
  3704. PsuGroupControlProcess();
  3705. break;
  3706. }
  3707. case _TEST_MODE:
  3708. {
  3709. // 在測試模式中,保持與模塊的通訊
  3710. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  3711. if (time > 1500)
  3712. {
  3713. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  3714. {
  3715. // 取系統總輸出能力
  3716. GetModuleCap(index); Await();
  3717. // 取各群輸出電壓電流 (float)
  3718. GetModuleOutputF(index); Await();
  3719. }
  3720. GetClockTime(&_cmdSubPriority_time);
  3721. }
  3722. byte _switch = 0x00;
  3723. if ((chargingInfo[0]->EvBatterytargetVoltage * 10) > 0 && (chargingInfo[0]->EvBatterytargetCurrent * 10) > 0)
  3724. _switch = 0x01;
  3725. for (byte _groupCount_1 = 0; _groupCount_1 < conn_1_count; _groupCount_1++)
  3726. {
  3727. SetDirModulePresentOutput(connector_1[_groupCount_1],
  3728. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  3729. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  3730. _switch, _switch); Await();
  3731. }
  3732. for (byte _groupCount_2 = 0; _groupCount_2 < conn_2_count; _groupCount_2++)
  3733. {
  3734. SetDirModulePresentOutput(connector_2[_groupCount_2],
  3735. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  3736. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  3737. _switch, _switch); Await();
  3738. }
  3739. }
  3740. break;
  3741. case _INIT_PSU_STATUS:
  3742. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  3743. {
  3744. _PrePsuWorkStep = ShmPsuData->Work_Step;
  3745. LOG_INFO("== PSU == _INIT_PSU_STATUS");
  3746. GetClockTime(&_cmdSubPriority_time);
  3747. }
  3748. break;
  3749. default:
  3750. break;
  3751. }
  3752. usleep(20000);
  3753. }
  3754. return FAIL;
  3755. }