Module_PsuComm.c 222 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751
  1. #include "Module_PsuComm.h"
  2. #include "Config.h"
  3. #include "Common.h"
  4. #define DERATING_COUNT 30
  5. #define DERATING_GAP 30
  6. #define ELEMENT_NOT_FIND 255
  7. #define CHK_VOL_RANGE 50
  8. #define CHK_CUR_RANGE 10
  9. #define DERATING_RANGE 100
  10. #define ZERO_CURRENT 10 // 該值須保持最小為 1A
  11. #define ZERO_VOLTAGE 50
  12. #define STOP_CURRENT 30
  13. #define PSU_MAX_CUR 1000 // unit: 0.1A
  14. #define PSU_MIN_VOL 1000 // unit: 0.1V
  15. #define PRE_CHARG_STEP_CUR 30
  16. #define PRE_CHARG_RANGE 50
  17. #define CMD_DELAY_TIME 25000 // 25us
  18. #define PSU_TASK_CHECK_TIME 1
  19. #define PSU_COMM_CHECK_TIME 1
  20. #define GET_PSU_COUNT_INTERVAL 1 // unit: second
  21. #define GET_PSU_COUNT_TIME 13 // unit: second
  22. #define GET_PSU_LOCATION_INTERVAL 200 // unit: millisecond
  23. #define PSU_COUNT_CONFIRM_INTERVAL 200 // unit: millisecond
  24. #define PSU_COUNT_CONFIRM_TIME 1 // unit: second
  25. #define GET_PSU_VERSION_INTERVAL 200 // unit: millisecond
  26. #define GET_PSU_CAP_INTERVAL 200 // unit: millisecond
  27. #define GET_PSU_CAP_TIME 1 // unit: second
  28. #define STABLE_CURRENT_TOLERANCE 10 // unit: 0.1A, 1A
  29. #define MAX_STABLE_COUNT 24
  30. #define WAIT_EV_DERATING_TIMEOUT 5 // unit: second
  31. #define WAIT_SLAVE_POWER_OFF_TIMEOUT 5 // unit: second
  32. #define WAIT_PARALLEL_RELAY_DELAY 1 // unit: second
  33. #define WAIT_RELAY_CONFIRMED_TIME 3 // unit: second
  34. #define MAX_PREPARE_SWITCH_OFF_TIME 10 // unit: second
  35. #define MIN_POWER_OFF_TIME 1 // unit: second
  36. #define WAIT_SLAVE_TO_CHARGING 1 // unit: second
  37. #define WAIT_SLAVE_TIMEOUT 5 // unit: second
  38. #define WAIT_GRAB_TIME 15 // unit: second
  39. #define MAX_PSU_POWER_OFF_CURRENT 50 // unit: 0.1A, 5A
  40. #define MAX_PSU_POWER_OFF_VOLTAGE 100 // unit: 0.1A, 10V
  41. #define PRECHARGE_OFFSET_VOLTAGE 20 // unit: 0.1V, 2V
  42. #define PRECHARGE_RANGE_VOLTAGE 100 // unit: 0.1V, 10V
  43. #define WAIT_PRECHARGE_TIME 10 // unit: second
  44. #define EXTEND_CAPABILITY_DELAY 30 // unit: second
  45. #define EXTEND_LOADING 8000 // unit: 0.01%, 80%
  46. #define RELEASE_LOADING 5000 // unit: 0.01%, 50%
  47. #define RELEASE_LOADING_OFFSET 1000 // unit: 0.01%, 10%
  48. #define BALANCE_CURRENT_INTERVAL 100 // unit: 1ms, 100ms
  49. #define SMOOTH_DERATING_INTERVAL 100 // unit: 1ms, 100ms
  50. #define CURRENT_REACH_TARGET_TIME 5 // unit: 1s, 5s
  51. #define CURRENT_STABLE_TIME 10 // unit: 1s, 10s
  52. #define REACH_CURRENT_TOLERANCE 10 // unit: 0.1A, 1A
  53. #define MAX_ADJ_BALANCE_CURRENT 30 // unit: 0.1A, 3A
  54. #define START_BALANCE_CRITERIA 100 // unit: 0.01%, 1%
  55. #define STOP_BALANCE_CRITERIA 10 // unit: 0.01%, 0.1%
  56. #define EV_MAX_CURRENT_DELAY 60 // unit: second
  57. #define EV_MAX_STAGE_CURRENT_DELAY 30 // unit: second
  58. #define WAIT_SLAVE_READY_TIME 15 // unit: second
  59. #define WAIT_SLAVE_DELAY 1 // unit: second
  60. #define SHOW_OUTPUT_DELAY 1
  61. #define CURRENT_BALANCE_CRITERIA 400 // unit: 0.1A, 40A
  62. #define SAFETY_PRECHARGE_OFFSET 200 // unit: 0.1V, 20V
  63. #define CHARGING_DELAY_INTERVAL 200 // unit: 1ms, 400ms
  64. #define MAX_CHARGING_DELAY_COUNT 15
  65. #define VOLTAGE_RESUME_STEP 10 // unit: 0.1V, 1V
  66. #define POWER_ONOFF_RESEND_INTERVAL 1 // unit: 1s, 1s
  67. #define COMM_LOST_CRITICAL 0
  68. #define LOST_CNT_CRITICAL 10
  69. #if SAFETY_TEST_ENABLE
  70. #define PRECHARGE_OFFSET 1 // 0: normal output, 1: precharge voltage offset enable
  71. #define ONE_MODULE_OUTPUT 1 // 0: normal output, 1: only one module output enable
  72. #define MASTER_OUTPUT_FIRST 0 // 0: normal output, 1: master output first enable
  73. #else
  74. #define PRECHARGE_OFFSET 0 // 0: normal output, 1: precharge voltage offset enable
  75. #define ONE_MODULE_OUTPUT 0 // 0: normal output, 1: only one module output enable
  76. #define MASTER_OUTPUT_FIRST 0 // 0: normal output, 1: master output first enable
  77. #endif
  78. #define MIN_RELEASE_POWER 30 // unit: 1kw
  79. #define MAX_DIFF_POWER_TO_DERATING 5 // unit: 1kw
  80. #define MAX_DIFF_POWER_TO_EXTEND 3 // unit: 1kw
  81. #define MIN_DIFF_PHYSICAL_POWER 3 // unit: 1kw
  82. #define PSU_DEBUG_MSG 0
  83. #define ENABLE_SMOOTH_DERATING 1
  84. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  85. struct StatusCodeData *ShmStatusCodeData;
  86. struct PsuData *ShmPsuData;
  87. ChargerInfoData *ShmChargerInfo;
  88. PsuPositionInfoData *ShmPsuPosition;
  89. PsuGroupingInfoData *ShmPsuGrouping;
  90. bool libInitialize = false;
  91. byte _gunCount = 0;
  92. byte _maxGroupCount = 0;
  93. byte getAvailableCapOffset = 5;
  94. byte deratingKeepCount = 0;
  95. byte psuCmdSeq = _PSU_CMD_CAP;
  96. bool psuReceiveRecovery = false;
  97. bool isCommStart = false;
  98. int lostCnt = 0;
  99. bool _isTriggerShutdown = false;
  100. bool _isShutdownCompleted = false;
  101. pid_t _RxPsuPid = 0;
  102. unsigned short evseOutVol[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  103. unsigned short evseOutCur[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  104. unsigned short evseOutputDelay[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  105. struct timespec _PsuReceiveRecoveryCheck_time;
  106. struct timespec _PsuWorkStep_time;
  107. struct timespec _cmdSubPriority_time;
  108. struct timespec _PsuGroupRole_time[CONNECTOR_QUANTITY];
  109. struct timespec _ChargingRequest_time[CONNECTOR_QUANTITY];
  110. struct timespec _PsuGroupDerating_time[CONNECTOR_QUANTITY];
  111. struct timespec _StopCharging_time[CONNECTOR_QUANTITY];
  112. struct timespec _ExtendCapability_time[CONNECTOR_QUANTITY];
  113. struct timespec _ReachCurrent_time[CONNECTOR_QUANTITY];
  114. struct timespec _BalanceCurrent_time[CONNECTOR_QUANTITY];
  115. struct timespec _MaxCurrent_time[CONNECTOR_QUANTITY];
  116. struct timespec _StageCurrent_time[CONNECTOR_QUANTITY];
  117. struct timespec _CheckSlaveReady_time[CONNECTOR_QUANTITY];
  118. struct timespec _ChargingDelay_time[CONNECTOR_QUANTITY];
  119. struct timespec _PoweOnOff_time[CONNECTOR_QUANTITY];
  120. struct timespec _PsuCommCheck_time;
  121. struct timespec _SmoothDerating_time[CONNECTOR_QUANTITY];
  122. unsigned short GCTargetVoltage[CONNECTOR_QUANTITY];
  123. unsigned short GCTargetCurrent[CONNECTOR_QUANTITY];
  124. unsigned short StableOutputCurrent[CONNECTOR_QUANTITY];
  125. unsigned short MaxCurrentDemand[CONNECTOR_QUANTITY];
  126. unsigned short StageMaxCurrent[CONNECTOR_QUANTITY];
  127. GroupOutputConfigInfo PreGroupOutput[MAX_GROUP_QUANTITY];
  128. unsigned char OutputConfigStep[MAX_GROUP_QUANTITY];
  129. unsigned char _preOutputConfigStep[MAX_GROUP_QUANTITY];
  130. unsigned char _GfdStep[MAX_GROUP_QUANTITY];
  131. unsigned char _VoltageResumeCnt[MAX_GROUP_QUANTITY];
  132. unsigned char _GunIndexOfPrioritySequence[MAX_GROUP_QUANTITY];
  133. int GetPsuModuleQuantity(unsigned char group);
  134. //=================================
  135. // Common routine
  136. //=================================
  137. bool Is_OutputK1K2_Ready(int gunIndex)
  138. {
  139. bool ready = false;
  140. if(gunIndex < CONNECTOR_QUANTITY)
  141. {
  142. if(chargingInfo[gunIndex]->RelayK1K2Status || ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  143. {
  144. ready = true;
  145. }
  146. }
  147. return ready;
  148. }
  149. // default is return enable
  150. bool Is_EnableReleaseAndExtendWhenForceCharging(int gunIndex)
  151. {
  152. if(ShmChargerInfo->Control.FCharging[gunIndex].FCtrl.bits.EnableForceCharging)
  153. {
  154. return ShmChargerInfo->Control.FCharging[gunIndex].FCtrl.bits.EnableReleaseAndExtend;
  155. }
  156. return true;
  157. }
  158. // return psu group number
  159. // return -1 when out of TotalPsuQuantity
  160. int FindTargetGroup(byte address)
  161. {
  162. return address < ShmPsuPosition->TotalPsuQuantity ? ShmPsuPosition->PsuAddressInfo[address].GroupNo : -1;
  163. }
  164. // return psu index in the group
  165. // return -1 when out of TotalPsuQuantity
  166. int FindGroupIndex(byte address)
  167. {
  168. return address < ShmPsuPosition->TotalPsuQuantity ? ShmPsuPosition->PsuAddressInfo[address].GIndex : -1;
  169. }
  170. //=================================
  171. // Save data to share memory Function
  172. //=================================
  173. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  174. {
  175. if(GENERAL_GUN_QUANTITY > 0 && target < GENERAL_GUN_QUANTITY)
  176. {
  177. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].GeneralChargingData;
  178. return true;
  179. }
  180. return false;
  181. }
  182. //=================================
  183. // Alarm code mapping to share memory Function
  184. //=================================
  185. // 檢查 Byte 中某個 Bit 的值
  186. // _byte : 欲改變的 byte
  187. // _bit : 該 byte 的第幾個 bit
  188. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  189. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  190. {
  191. return ( _byte & mask_table[_bit] ) != 0x00;
  192. }
  193. void AbnormalStopAnalysis(byte gun_index, int errCode)
  194. {
  195. for (char i = 0; i < 4; i++)
  196. {
  197. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  198. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  199. {
  200. if(DetectBitValue(byteIndex , bitIndex) == 1)
  201. {
  202. switch(i)
  203. {
  204. case 0:
  205. {
  206. // err 1
  207. if (bitIndex == 2)
  208. {
  209. // 012307
  210. // fuse burn-out
  211. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFuseBurnOut = YES;
  212. }
  213. else if (bitIndex == 3)
  214. {
  215. // 012308
  216. // Communication fault between PFC and DCDC
  217. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcAndDcdcCommFault = YES;
  218. }
  219. else if (bitIndex == 6)
  220. {
  221. // 012309
  222. // Unbalance positive and negative BUS voltage
  223. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageUnbalance = YES;
  224. }
  225. else if (bitIndex == 7)
  226. {
  227. // 012310
  228. // BUS over voltage
  229. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusOverVoltage = YES;
  230. }
  231. }
  232. break;
  233. case 1:
  234. {
  235. // err 2
  236. if (bitIndex == 0)
  237. {
  238. // 012311
  239. // BUS voltage abnormal
  240. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageAbnormal = YES;
  241. }
  242. else if (bitIndex == 1)
  243. {
  244. // 012270
  245. // Over voltage of any phase
  246. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  247. }
  248. else if (bitIndex == 2)
  249. {
  250. // 012263
  251. // ID number repetition
  252. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  253. }
  254. else if (bitIndex == 3)
  255. {
  256. // 012312
  257. // BUS under voltage
  258. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusUnderVoltage = YES;
  259. }
  260. else if (bitIndex == 4)
  261. {
  262. // 012313
  263. // Phase loss
  264. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = YES;
  265. }
  266. else if (bitIndex == 6)
  267. {
  268. // 012271
  269. // Under voltage of any phase
  270. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  271. }
  272. }
  273. break;
  274. case 2:
  275. {
  276. // err3
  277. if (bitIndex == 0)
  278. {
  279. // 012240
  280. // CAN communication fault
  281. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCommunicationFail = YES;
  282. }
  283. else if (bitIndex == 1)
  284. {
  285. // 012275
  286. // DCDC uneven current sharing
  287. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuSevereUnevenCurrent = YES;
  288. }
  289. else if (bitIndex == 3)
  290. {
  291. // 012278
  292. // PFC power off
  293. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFfcSideShutDown = YES;
  294. }
  295. else if (bitIndex == 5)
  296. {
  297. // 012314
  298. // Full speed of fan
  299. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFullSpeed = YES;
  300. }
  301. else if (bitIndex == 6)
  302. {
  303. // 012266
  304. // DCDC power off
  305. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  306. }
  307. else if (bitIndex == 7)
  308. {
  309. // 012273
  310. // Module unders power limiting status
  311. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  312. }
  313. }
  314. break;
  315. case 3:
  316. {
  317. // err 4
  318. if (bitIndex == 0)
  319. {
  320. // 012315
  321. // Temperature power limiting
  322. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuTemperaturePowerLimit = YES;
  323. }
  324. else if (bitIndex == 1)
  325. {
  326. // 012316
  327. // AC power limiting
  328. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuAcPowerLimit = YES;
  329. }
  330. else if (bitIndex == 2)
  331. {
  332. // 012317
  333. // DCDC eeprom faults
  334. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcEepromFault = YES;
  335. }
  336. else if (bitIndex == 3)
  337. {
  338. // 012269
  339. // Fan faults
  340. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  341. }
  342. else if (bitIndex == 4)
  343. {
  344. // 012264
  345. // DCDC short circuit
  346. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  347. }
  348. else if (bitIndex == 5)
  349. {
  350. // 012318
  351. // PFC eeprom faults
  352. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcEepromFault = YES;
  353. }
  354. else if (bitIndex == 6)
  355. {
  356. // 012226
  357. // DCDC over temperature
  358. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  359. }
  360. else if (bitIndex == 7)
  361. {
  362. // 012319
  363. // DCDC output over voltage
  364. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = YES;
  365. }
  366. }
  367. break;
  368. }
  369. }
  370. // else
  371. // {
  372. // switch (byteIndex) {
  373. // case 0: {
  374. // if (bitIndex == 0)
  375. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  376. // else if (bitIndex == 5)
  377. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  378. // }
  379. // break;
  380. // case 1: {
  381. // if (bitIndex == 1)
  382. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  383. // else if (bitIndex == 2)
  384. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  385. // else if (bitIndex == 3)
  386. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  387. // else if (bitIndex == 4)
  388. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  389. // else if (bitIndex == 5)
  390. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  391. // }
  392. // break;
  393. // case 2: {
  394. // if (bitIndex == 1)
  395. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  396. // if (bitIndex == 2)
  397. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  398. // else if (bitIndex == 3)
  399. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  400. // else if (bitIndex == 4)
  401. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  402. // else if (bitIndex == 5)
  403. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  404. // else if (bitIndex == 6)
  405. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  406. // }
  407. // break;
  408. // }
  409. // }
  410. }
  411. }
  412. }
  413. //=================================
  414. // Callback Function
  415. //=================================
  416. // 0x04: PSU_RCmd_ModuleStatus
  417. void GetStatusCallback(byte group, byte SN, byte temp, int alarm)
  418. {
  419. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  420. if(ShmPsuData->Work_Step != Get_PSU_LOCATION)
  421. {
  422. return;
  423. }
  424. if(group >= _maxGroupCount || SN >= MAX_MODULE_PER_GROUP)
  425. {
  426. return;
  427. }
  428. ShmPsuPosition->PsuAddressInfo[SN].Address = SN;
  429. ShmPsuPosition->PsuAddressInfo[SN].GroupNo = group;
  430. if(!ShmPsuPosition->PsuAddressInfo[SN].CheckIn)
  431. {
  432. ShmPsuPosition->PsuAddressInfo[SN].CheckIn = true;
  433. ShmPsuPosition->TotalPsuQuantity++;
  434. //LOG_INFO("SN = %d At Group %02X", SN, group);
  435. }
  436. if(ShmPsuPosition->TotalPsuQuantity == ShmPsuData->SystemInitialPsuQuantity)
  437. {
  438. byte groupNo = 0, quantity = 0;
  439. if(!ShmPsuPosition->PsuLocationInit)
  440. {
  441. memset(ShmPsuPosition->GroupLocationInfo, 0x00, sizeof(ShmPsuPosition->GroupLocationInfo));
  442. for(int index = 0; index < MAX_PSU_MODULE_QUANTITY; index++)
  443. {
  444. if(ShmPsuPosition->PsuAddressInfo[index].CheckIn)
  445. {
  446. groupNo = ShmPsuPosition->PsuAddressInfo[index].GroupNo;
  447. quantity = ShmPsuPosition->GroupLocationInfo[groupNo].GroupPsuQuantity;
  448. ShmPsuPosition->PsuAddressInfo[index].GIndex = quantity;
  449. ShmPsuPosition->GroupLocationInfo[groupNo].PsuSN[quantity] = ShmPsuPosition->PsuAddressInfo[index].Address;
  450. ShmPsuPosition->GroupLocationInfo[groupNo].GroupPsuQuantity++;
  451. //LOG_INFO("Psu %d Assign To Group %02X At %d", index, group, ShmPsuPosition->PsuAddressInfo[index].GIndex);
  452. }
  453. }
  454. bool match = true;
  455. for(int index = 0; index < _maxGroupCount; index++)
  456. {
  457. if(ShmPsuPosition->GroupLocationInfo[index].GroupPsuQuantity != ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity)
  458. {
  459. match = false;
  460. }
  461. }
  462. if(match)
  463. {
  464. ShmPsuPosition->PsuLocationInit = true;
  465. }
  466. else
  467. {
  468. // try to re-initial psu location
  469. ShmPsuPosition->ReInitPsuLocation = true;
  470. }
  471. }
  472. else
  473. {
  474. ShmPsuData->PsuGroup[group].PsuModule[ShmPsuPosition->PsuAddressInfo[SN].GIndex].CriticalTemp1 = temp;
  475. }
  476. }
  477. }
  478. // 0x02: PSU_RCmd_SysModuleCount
  479. void GetModuleCountCallback(byte group, byte count)
  480. {
  481. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  482. if(group == SYSTEM_CMD)
  483. {
  484. if(ShmPsuData->Work_Step == GET_PSU_COUNT)
  485. {
  486. ShmPsuData->SystemInitialPsuQuantity = count;
  487. }
  488. ShmPsuData->SystemPresentPsuQuantity = count;
  489. }
  490. else
  491. {
  492. if(group < _maxGroupCount)
  493. {
  494. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  495. }
  496. }
  497. }
  498. void UpdateGunAvailableCapability(unsigned char group)
  499. {
  500. int _availableCurrent = 0, _availablePower = 0, _realPower = 0;
  501. unsigned char master = 0;
  502. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  503. // calculate output group capability
  504. if(master == 0)
  505. {
  506. chargingInfo[group]->AvailableChargingCurrent = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  507. chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  508. chargingInfo[group]->RealRatingPower = ShmPsuData->PsuGroup[group].GroupRealOutputPower * 10;
  509. }
  510. else
  511. {
  512. if(ShmPsuGrouping->GroupCollection[master - 1].Role == _GROLE_MASTER)
  513. {
  514. _availableCurrent = ShmPsuData->PsuGroup[master - 1].GroupAvailableCurrent;
  515. _availablePower = ShmPsuData->PsuGroup[master - 1].GroupAvailablePower;
  516. _realPower = ShmPsuData->PsuGroup[master - 1].GroupRealOutputPower * 10;
  517. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  518. {
  519. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  520. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  521. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  522. {
  523. _availableCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  524. _availablePower += ShmPsuData->PsuGroup[slave].GroupAvailablePower;
  525. _realPower += ShmPsuData->PsuGroup[slave].GroupRealOutputPower * 10;
  526. }
  527. }
  528. if(ShmPsuGrouping->GroupCollection[master - 1].GroupCtrl.bits.DeratingConfirmed)
  529. {
  530. chargingInfo[master - 1]->AvailableChargingCurrent = ShmPsuGrouping->GroupCollection[master - 1].ReAssignAvailableCurrent;
  531. }
  532. else
  533. {
  534. chargingInfo[master - 1]->AvailableChargingCurrent = _availableCurrent;
  535. }
  536. chargingInfo[master - 1]->AvailableChargingPower = _availablePower;
  537. chargingInfo[master - 1]->RealRatingPower = _realPower;
  538. // if((master - 1) != group)
  539. // {
  540. // chargingInfo[group]->AvailableChargingCurrent = 0;;
  541. // chargingInfo[group]->AvailableChargingPower = 0;
  542. // chargingInfo[group]->RealRatingPower = 0;
  543. // }
  544. }
  545. }
  546. }
  547. void UpdateSystemAvailable(void)
  548. {
  549. int _sysCurrent = 0, _sysPower = 0;
  550. // summation of system current & power
  551. for(byte i = 0; i < _maxGroupCount; i++)
  552. {
  553. _sysCurrent += ShmPsuData->PsuGroup[i].GroupAvailableCurrent;
  554. _sysPower += ShmPsuData->PsuGroup[i].GroupAvailablePower;
  555. }
  556. ShmPsuData->SystemAvailableCurrent = _sysCurrent;
  557. ShmPsuData->SystemAvailablePower = _sysPower;
  558. }
  559. // maxVol, minVol unit: 0.1V
  560. // maxCur unit: 0.1A
  561. // totalPow unit: 0.1kW
  562. // 0x0A: PSU_RCmd_ModuleCapability
  563. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  564. {
  565. int _groupCurrent = 0, _groupPower = 0;
  566. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  567. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  568. !ShmPsuPosition->PsuLocationInit)
  569. {
  570. return;
  571. }
  572. byte master = 0;
  573. int group = FindTargetGroup(address);
  574. int gIndex = FindGroupIndex(address);
  575. if(group < 0 || gIndex < 0)
  576. {
  577. return;
  578. }
  579. // calculate psu module available power
  580. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailablePower = totalPow;
  581. //if(ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage >= PSU_MIN_VOL)
  582. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage >= PSU_MIN_VOL)
  583. {
  584. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailableCurrent =
  585. ((ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailablePower * 100) * 10) / (ShmPsuGrouping->GroupOutput[group].GTargetVoltage / 10);
  586. }
  587. else
  588. {
  589. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailableCurrent = maxCur > PSU_MAX_CUR ? PSU_MAX_CUR : maxCur;
  590. }
  591. // summation of psu group current & power
  592. for(byte i = 0; i < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; i++)
  593. {
  594. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[i].AvailableCurrent;
  595. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[i].AvailablePower;
  596. }
  597. if(ShmPsuData->PsuGroup[group].StableIAvailableCurrent != 0)
  598. {
  599. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = ShmPsuData->PsuGroup[group].StableIAvailableCurrent;
  600. }
  601. else
  602. {
  603. if(ShmPsuData->PsuGroup[group].GroupRealOutputPower != 0 &&
  604. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage >= PSU_MIN_VOL)
  605. {
  606. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = ((ShmPsuData->PsuGroup[group].GroupRealOutputPower * 1000) * 10) / (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10);
  607. }
  608. else
  609. {
  610. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  611. }
  612. }
  613. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  614. // update MaximumChargingVoltage
  615. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  616. if(master == 0)
  617. {
  618. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  619. }
  620. else
  621. {
  622. if(ShmPsuGrouping->GroupCollection[master - 1].Role == _GROLE_MASTER)
  623. {
  624. chargingInfo[master - 1]->MaximumChargingVoltage = maxVol;
  625. // if((master - 1) != group)
  626. // {
  627. // chargingInfo[group]->MaximumChargingVoltage = 0;
  628. // }
  629. }
  630. }
  631. UpdateGunAvailableCapability(group);
  632. UpdateSystemAvailable();
  633. //LOG_INFO("address = %d, maxVol = %d, minVol = %d, maxCur = %d, totalPow = %d", address, maxVol, minVol, maxCur, totalPow);
  634. }
  635. // 0x07: PSU_RCmd_ModuleVersion
  636. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  637. {
  638. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  639. if (ShmPsuData->Work_Step != Get_PSU_VERSION || address >= ShmPsuData->SystemInitialPsuQuantity ||
  640. !ShmPsuPosition->PsuLocationInit)
  641. {
  642. return;
  643. }
  644. int group = FindTargetGroup(address);
  645. int gIndex = FindGroupIndex(address);
  646. if(group < 0 || gIndex < 0)
  647. {
  648. return;
  649. }
  650. sprintf((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  651. sprintf((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, "PFC %d.%02d", (pfcSwVer & 0xFF00) >> 8, pfcSwVer & 0xFF);
  652. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[gIndex].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  653. //LOG_INFO("Psu %d %s %s", address, ShmPsuData->PsuVersion[address].FwPrimaryVersion, ShmPsuData->PsuVersion[address].FwSecondVersion);
  654. }
  655. // no using -- GetInputVoltageCallback
  656. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  657. {
  658. }
  659. // no using -- GetInputVoltageCallback End
  660. // no using -- GetPresentOutputCallback
  661. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  662. {
  663. }
  664. // no using -- GetPresentOutputCallback End
  665. // type 1: FAN_SPEED_CMD
  666. // type 2: TEMP_DC_CMD
  667. // type 3: TEMP_PFC_CMD
  668. // 0x0E: PSU_RCmd_ModuleMiscInfo
  669. void GetMisCallback(byte address, unsigned int value, byte type)
  670. {
  671. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  672. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  673. !ShmPsuPosition->PsuLocationInit)
  674. {
  675. return;
  676. }
  677. int group = FindTargetGroup(address);
  678. int gIndex = FindGroupIndex(address);
  679. if(group < 0 || gIndex < 0)
  680. {
  681. return;
  682. }
  683. if (type == 1)
  684. {
  685. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_1 = value;
  686. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_2 = value;
  687. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_3 = value;
  688. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_4 = value;
  689. }
  690. else if (type == 2)
  691. {
  692. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp1 = value;
  693. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp2 = value;
  694. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp3 = value;
  695. ShmPsuData->PsuGroup[group].PsuModule[gIndex].ExletTemp = value;
  696. }
  697. else if (type == 3)
  698. {
  699. //printf("PFC - group = %d, index = %d, value = %d \n", group, address, value);
  700. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InletTemp = value;
  701. }
  702. }
  703. // Iavail unit: 0.1A
  704. // Vext unit: 0.1V
  705. // 0x0C: PSU_RCmd_ModuleIAvailable
  706. // 0x02CCF0XX
  707. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  708. {
  709. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  710. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  711. !ShmPsuPosition->PsuLocationInit)
  712. {
  713. return;
  714. }
  715. byte master = 0;
  716. unsigned short diffIAvailable = 0;
  717. int totalCurrent = 0;
  718. int group = FindTargetGroup(address);
  719. int gIndex = FindGroupIndex(address);
  720. if(group < 0 || gIndex < 0)
  721. {
  722. return;
  723. }
  724. ShmPsuData->PsuGroup[group].PsuModule[gIndex].IAvailableCurrent = Iavail;
  725. // summation of psu group i available current
  726. for (byte i = 0; i < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; i++)
  727. {
  728. totalCurrent += ShmPsuData->PsuGroup[group].PsuModule[i].IAvailableCurrent;
  729. }
  730. ShmPsuData->PsuGroup[group].TotalIAvailableCurrent = totalCurrent;
  731. diffIAvailable = totalCurrent >= ShmPsuData->PsuGroup[group].TempIAvailableCurrent ?
  732. totalCurrent - ShmPsuData->PsuGroup[group].TempIAvailableCurrent : ShmPsuData->PsuGroup[group].TempIAvailableCurrent - totalCurrent;
  733. if(diffIAvailable > STABLE_CURRENT_TOLERANCE)
  734. {
  735. ShmPsuData->PsuGroup[group].StableCurrentCounter = 0;
  736. ShmPsuData->PsuGroup[group].StableIAvailableCurrent = 0;
  737. }
  738. ShmPsuData->PsuGroup[group].TempIAvailableCurrent = ShmPsuData->PsuGroup[group].TotalIAvailableCurrent;
  739. ShmPsuData->PsuGroup[group].StableCurrentCounter++;
  740. if(ShmPsuData->PsuGroup[group].StableCurrentCounter >= MAX_STABLE_COUNT)
  741. {
  742. // get stable StableIAvailableCurrent
  743. ShmPsuData->PsuGroup[group].StableIAvailableCurrent = ShmPsuData->PsuGroup[group].TotalIAvailableCurrent;
  744. // get stable GroupRealOutputPower
  745. ShmPsuData->PsuGroup[group].GroupRealOutputPower = (ShmPsuData->PsuGroup[group].StableIAvailableCurrent * ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage) / 100 / 1000;
  746. ShmPsuData->PsuGroup[group].StableCurrentCounter = 0;
  747. }
  748. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  749. if(master == 0)
  750. {
  751. // unit: 0.1A
  752. chargingInfo[group]->DeratingChargingCurrent = ShmPsuData->PsuGroup[group].StableIAvailableCurrent;
  753. chargingInfo[group]->DeratingChargingPower = (chargingInfo[group]->DeratingChargingCurrent * chargingInfo[group]->PresentChargingVoltage) / 10 / 100;
  754. }
  755. else
  756. {
  757. // calculate DeratingChargingCurrent of master group
  758. totalCurrent = ShmPsuData->PsuGroup[master - 1].StableIAvailableCurrent;
  759. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  760. {
  761. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  762. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE)
  763. {
  764. totalCurrent += ShmPsuData->PsuGroup[slave].StableIAvailableCurrent;
  765. }
  766. }
  767. chargingInfo[master - 1]->DeratingChargingCurrent = totalCurrent;
  768. chargingInfo[master - 1]->DeratingChargingPower = (chargingInfo[master - 1]->DeratingChargingCurrent * chargingInfo[master - 1]->PresentChargingVoltage) / 10 / 100;
  769. if((master - 1) != group)
  770. {
  771. chargingInfo[group]->DeratingChargingCurrent = 0;
  772. chargingInfo[group]->DeratingChargingPower = 0;
  773. }
  774. }
  775. }
  776. // outVol unit: 1V
  777. // outCur unit: 1A
  778. // 0x01: PSU_RCmd_SysOutputVolCur_F
  779. void GetPresentOutputFCallback(byte group, float outVol, float outCur)
  780. {
  781. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  782. if (ShmPsuData->Work_Step < GET_SYS_CAP || group >= _maxGroupCount ||
  783. !ShmPsuPosition->PsuLocationInit)
  784. {
  785. return;
  786. }
  787. byte master = 0;
  788. unsigned short pVoltage = 0, pCurrent = 0;
  789. unsigned short gVoltage = 0, gCurrent = 0;
  790. pVoltage = isinf(outVol) == 0 ? (unsigned short)(outVol * 10) : 0;
  791. pCurrent = isinf(outCur) == 0 ? (unsigned short)(outCur * 10) : 0;
  792. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = pVoltage;
  793. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = pCurrent;
  794. ShmPsuData->PsuGroup[group].GroupPresentOutputPower = (pVoltage * pCurrent) / 100 / 100;
  795. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  796. if(master == 0)
  797. {
  798. // calculate PresentChargingVoltage and PresentChargingCurrent of self group
  799. chargingInfo[group]->PresentChargingVoltage = (float)ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10;
  800. chargingInfo[group]->PresentChargingCurrent = (float)ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10;
  801. }
  802. else
  803. {
  804. // calculate PresentChargingVoltage and PresentChargingCurrent of master group
  805. gVoltage = ShmPsuData->PsuGroup[master - 1].GroupPresentOutputVoltage;
  806. gCurrent = ShmPsuData->PsuGroup[master - 1].GroupPresentOutputCurrent;
  807. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  808. {
  809. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  810. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  811. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF ||
  812. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  813. {
  814. if(ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage > gVoltage)
  815. {
  816. gVoltage = ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage;
  817. }
  818. gCurrent += ShmPsuData->PsuGroup[slave].GroupPresentOutputCurrent;
  819. }
  820. }
  821. chargingInfo[master - 1]->PresentChargingVoltage = (float)gVoltage / 10;
  822. chargingInfo[master - 1]->PresentChargingCurrent = (float)gCurrent / 10;
  823. if((master - 1) != group)
  824. {
  825. chargingInfo[group]->PresentChargingVoltage = 0;
  826. chargingInfo[group]->PresentChargingCurrent = 0;
  827. }
  828. }
  829. }
  830. //==========================================
  831. // 特規用指令
  832. //==========================================
  833. // 0x1901: Nexton_PSU_DcOutputValue
  834. void GetOutputAndTempCallback(byte address, unsigned short outputVol_s,
  835. unsigned short outputCur_s, unsigned short outputPower, unsigned char Temperature)
  836. {
  837. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  838. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  839. !ShmPsuPosition->PsuLocationInit)
  840. {
  841. return;
  842. }
  843. int group = FindTargetGroup(address);
  844. int gIndex = FindGroupIndex(address);
  845. if(group < 0 || gIndex < 0)
  846. {
  847. return;
  848. }
  849. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp1 = Temperature;
  850. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp2 = Temperature;
  851. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp3 = Temperature;
  852. }
  853. // 0x1902: Nexton_PSU_StatusEvent
  854. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  855. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  856. {
  857. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  858. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  859. !ShmPsuPosition->PsuLocationInit)
  860. {
  861. return;
  862. }
  863. int group = FindTargetGroup(address);
  864. int gIndex = FindGroupIndex(address);
  865. if(group < 0 || gIndex < 0)
  866. {
  867. return;
  868. }
  869. int alarm = (err4 << 24) | (err3 << 16) | (err2 << 8) | err1;
  870. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AlarmCode = alarm;
  871. AbnormalStopAnalysis(group, alarm);
  872. if(isErr)
  873. {
  874. // 012267
  875. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  876. ShmPsuData->PsuGroup[group].GroupErrorFlag.bits.PsuFailure = true;
  877. }
  878. }
  879. // 0x1903: Nexton_PSU_AcInputValue
  880. void GetModuleInputCallback(byte address, unsigned short inputR,
  881. unsigned short inputS, unsigned short inputT)
  882. {
  883. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  884. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  885. !ShmPsuPosition->PsuLocationInit)
  886. {
  887. return;
  888. }
  889. int group = FindTargetGroup(address);
  890. int gIndex = FindGroupIndex(address);
  891. if(group < 0 || gIndex < 0)
  892. {
  893. return;
  894. }
  895. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL1 = inputR;
  896. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL2 = inputS;
  897. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL3 = inputT;
  898. }
  899. //==========================================
  900. // Init all share memory
  901. //==========================================
  902. int InitShareMemory()
  903. {
  904. int result = PASS;
  905. int MeterSMId;
  906. //creat ShmSysConfigAndInfo
  907. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  908. {
  909. #ifdef SystemLogMessage
  910. LOG_ERROR("shmget ShmSysConfigAndInfo NG %d");
  911. #endif
  912. result = FAIL;
  913. }
  914. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  915. {
  916. #ifdef SystemLogMessage
  917. LOG_ERROR("shmat ShmSysConfigAndInfo NG");
  918. #endif
  919. result = FAIL;
  920. }
  921. else
  922. {}
  923. //creat ShmStatusCodeData
  924. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  925. {
  926. #ifdef SystemLogMessage
  927. LOG_ERROR("shmget ShmStatusCodeData NG");
  928. #endif
  929. result = FAIL;
  930. }
  931. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  932. {
  933. #ifdef SystemLogMessage
  934. LOG_ERROR("shmat ShmStatusCodeData NG");
  935. #endif
  936. result = FAIL;
  937. }
  938. else
  939. {}
  940. //creat ShmPsuData
  941. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  942. {
  943. #ifdef SystemLogMessage
  944. LOG_ERROR("shmget ShmPsuData NG");
  945. #endif
  946. result = FAIL;
  947. }
  948. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  949. {
  950. #ifdef SystemLogMessage
  951. LOG_ERROR("shmat ShmPsuData NG");
  952. #endif
  953. result = FAIL;
  954. }
  955. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  956. {
  957. #ifdef SystemLogMessage
  958. LOG_ERROR("shmat ChargerInfoData NG");
  959. #endif
  960. result = FAIL;
  961. }
  962. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  963. {
  964. #ifdef SystemLogMessage
  965. LOG_ERROR("shmat ChargerInfoData NG");
  966. #endif
  967. result = FAIL;
  968. }
  969. if(result == PASS)
  970. {
  971. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  972. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  973. }
  974. return result;
  975. }
  976. //================================================
  977. // Main process
  978. //================================================
  979. void InitialPsuData(void)
  980. {
  981. ShmPsuData->SystemInitialPsuQuantity = 0;
  982. ShmPsuData->SystemPresentPsuQuantity = 0;
  983. ShmPsuData->SystemAvailablePower = 0;
  984. LOG_INFO("************ psu Group = %d", ShmPsuData->GroupCount);
  985. for (byte _groupCount = 0; _groupCount < ShmPsuData->GroupCount; _groupCount++)
  986. {
  987. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  988. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  989. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  990. ShmPsuData->PsuGroup[_groupCount].GroupErrorFlag.PsuGroupErrorValue = 0;
  991. }
  992. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFuseBurnOut = NO;
  993. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcAndDcdcCommFault = NO;
  994. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageUnbalance = NO;
  995. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusOverVoltage = NO;
  996. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageAbnormal = NO;
  997. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  998. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  999. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusUnderVoltage = NO;
  1000. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = NO;
  1001. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  1002. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCommunicationFail = NO;
  1003. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuSevereUnevenCurrent = NO;
  1004. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFfcSideShutDown = NO;
  1005. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFullSpeed = NO;
  1006. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  1007. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = NO;
  1008. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuTemperaturePowerLimit = NO;
  1009. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuAcPowerLimit = NO;
  1010. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcEepromFault = NO;
  1011. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  1012. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  1013. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcEepromFault = NO;
  1014. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  1015. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = NO;
  1016. }
  1017. void Initialization()
  1018. {
  1019. bool isPass = false;
  1020. while(!isPass)
  1021. {
  1022. isPass = true;
  1023. for (byte _index = 0; _index < _maxGroupCount; _index++)
  1024. {
  1025. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  1026. {
  1027. LOG_ERROR("Module_PsuComm : FindChargingInfoData false");
  1028. isPass = false;
  1029. break;
  1030. }
  1031. }
  1032. sleep(1);
  1033. }
  1034. ShmPsuData->GroupCount = _maxGroupCount;
  1035. }
  1036. void Await()
  1037. {
  1038. usleep(CMD_DELAY_TIME);
  1039. }
  1040. bool Is_PsuRxComm_Alive(void)
  1041. {
  1042. if(_RxPsuPid > 0)
  1043. {
  1044. if(kill(_RxPsuPid, 0) == 0)
  1045. {
  1046. // Process exists.
  1047. return true;
  1048. }
  1049. }
  1050. return false;
  1051. }
  1052. void Run_PsuRxCommProcess(void)
  1053. {
  1054. _RxPsuPid = InitialCommunication();
  1055. LOG_INFO("Create PsuRx PID = %d", _RxPsuPid);
  1056. }
  1057. void PsuReceiveRecoveryCheck(void)
  1058. {
  1059. char *ptrSave, *ptrToken;
  1060. int fd, psuTaskCount = 0;
  1061. char pathBuffer[64], psuTaskPidString[64];
  1062. system("pidof Module_PsuComm > /tmp/Module_PsuTask");
  1063. snprintf(pathBuffer, sizeof(pathBuffer), "/tmp/Module_PsuTask");
  1064. fd = open(pathBuffer, O_RDWR);
  1065. if(fd < 0)
  1066. {
  1067. close(fd);
  1068. return;
  1069. }
  1070. if(read(fd, psuTaskPidString, 64) < 0)
  1071. {
  1072. close(fd);
  1073. return;
  1074. }
  1075. ptrToken = strtok_r(psuTaskPidString, " ", &ptrSave);
  1076. while(ptrToken != NULL)
  1077. {
  1078. int psuPid = atoi(ptrToken);
  1079. if(psuPid > 0)
  1080. {
  1081. psuTaskCount++;
  1082. }
  1083. ptrToken = strtok_r(NULL, " ", &ptrSave);
  1084. }
  1085. close(fd);
  1086. if(psuTaskCount == 1)
  1087. {
  1088. LOG_INFO("************ PSU Receive Task Need Recovery ************\n");
  1089. Run_PsuRxCommProcess();
  1090. psuReceiveRecovery = true;
  1091. }
  1092. }
  1093. void ShowGroupMember(unsigned char group)
  1094. {
  1095. if(group < MAX_GROUP_QUANTITY)
  1096. {
  1097. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1098. {
  1099. for(byte psu = 0; psu < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; psu++)
  1100. {
  1101. LOG_INFO("Group %d - Number = %d, SN = %02X", group + 1, psu,
  1102. ShmPsuPosition->GroupLocationInfo[group].PsuSN[psu]);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. void ShowPsuVersion(unsigned char group)
  1108. {
  1109. if(group < MAX_GROUP_QUANTITY)
  1110. {
  1111. if(ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity > 0)
  1112. {
  1113. LOG_INFO("Group %d PSU Version Info", group + 1);
  1114. for(int i = 0; i < ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity; i++)
  1115. {
  1116. int psuIndex = ShmPsuPosition->GroupLocationInfo[group].PsuSN[i];
  1117. LOG_INFO("PSU Primary: %s, Second: %s",
  1118. ShmPsuData->PsuVersion[psuIndex].FwPrimaryVersion,
  1119. ShmPsuData->PsuVersion[psuIndex].FwSecondVersion);
  1120. }
  1121. }
  1122. }
  1123. }
  1124. void ShowGroupAvailableCurrentPower(unsigned char group)
  1125. {
  1126. if(group < MAX_GROUP_QUANTITY)
  1127. {
  1128. LOG_INFO("Group %d Available Current = %3d.%dA, Power = %3d.%dkW", group + 1,
  1129. (ShmPsuData->PsuGroup[group].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[group].GroupAvailableCurrent % 10),
  1130. (ShmPsuData->PsuGroup[group].GroupAvailablePower / 10), (ShmPsuData->PsuGroup[group].GroupAvailablePower % 10));
  1131. }
  1132. }
  1133. unsigned char _TempType = 0;
  1134. void PsuGroupRoutineQuery(void)
  1135. {
  1136. for(byte group = 0; group < GENERAL_GUN_QUANTITY; group++)
  1137. {
  1138. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1139. {
  1140. if (psuCmdSeq == _PSU_CMD_CAP)
  1141. {
  1142. // 取系統總輸出能力
  1143. GetModuleCap(group);
  1144. }
  1145. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1146. {
  1147. // 取各群輸出電壓電流 (float)
  1148. GetModuleOutputF(group);
  1149. }
  1150. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1151. {
  1152. // 取得模塊輸出額定電流能力
  1153. GetModuleIavailable(group);
  1154. }
  1155. else if (psuCmdSeq == _PSU_CMD_TEMP)
  1156. {
  1157. // 取得模塊溫度
  1158. if(_TempType == _PSU_TMP_DCDC)
  1159. {
  1160. GetDcTemperature(group);
  1161. }
  1162. else
  1163. {
  1164. GetPfcTemperature(group);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. if(psuCmdSeq == _PSU_CMD_CAP)
  1170. {
  1171. psuCmdSeq = _PSU_CMD_OUTPUT;
  1172. }
  1173. else if(psuCmdSeq == _PSU_CMD_OUTPUT)
  1174. {
  1175. psuCmdSeq = _PSU_CMD_IVAILIABLE;
  1176. }
  1177. else if(psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1178. {
  1179. psuCmdSeq = _PSU_CMD_TEMP;
  1180. _TempType = _TempType == _PSU_TMP_DCDC ? _PSU_TMP_PFC : _PSU_TMP_DCDC;
  1181. }
  1182. else
  1183. {
  1184. psuCmdSeq = _PSU_CMD_CAP;
  1185. }
  1186. Await();
  1187. }
  1188. void SetPsuGroupRole(byte group, byte role)
  1189. {
  1190. if(group < GENERAL_GUN_QUANTITY)
  1191. {
  1192. ShmPsuGrouping->GroupCollection[group].Role = role;
  1193. }
  1194. }
  1195. void SetPsuGroupToIdle(byte group)
  1196. {
  1197. if(group < GENERAL_GUN_QUANTITY)
  1198. {
  1199. SetPsuGroupRole(group, _GROLE_IDLE);
  1200. memset(&ShmPsuGrouping->GroupCollection[group].Partner, 0x00, sizeof(PsuGroupPartner));
  1201. memset(&ShmPsuGrouping->GroupCollection[group].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  1202. ShmPsuGrouping->GroupCollection[group].TargetGroup = 0;
  1203. ShmPsuGrouping->GroupCollection[group].ReservedTarget = 0;
  1204. //LOG_INFO("Reset Group[%02X] To Idle", group);
  1205. }
  1206. }
  1207. // group: self group index
  1208. void SetPsuGroupToMaster(byte group)
  1209. {
  1210. if(group < GENERAL_GUN_QUANTITY)
  1211. {
  1212. SetPsuGroupRole(group, _GROLE_MASTER);
  1213. ShmPsuGrouping->GroupCollection[group].TargetGroup = group + 1;
  1214. //LOG_INFO("Set Group[%02X] As Master To Gun %d", group, group + 1);
  1215. }
  1216. }
  1217. // group: self group index
  1218. // target: target group index + 1
  1219. void SetPsuGroupToSlave(byte group, byte target)
  1220. {
  1221. if(group < GENERAL_GUN_QUANTITY && target <= GENERAL_GUN_QUANTITY)
  1222. {
  1223. SetPsuGroupRole(group, _GROLE_SLAVE);
  1224. ShmPsuGrouping->GroupCollection[group].TargetGroup = target;
  1225. ShmPsuGrouping->GroupCollection[group].ReservedTarget = 0;
  1226. memset(&ShmPsuGrouping->GroupCollection[group].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  1227. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  1228. //LOG_INFO("Set Group[%02X] As Slave To Gun %d", group, target);
  1229. }
  1230. }
  1231. // target: target group index + 1
  1232. // tPartner: set member to power off
  1233. void PrepareToPowerOff(byte target, PsuGroupPartner *tPartner)
  1234. {
  1235. unsigned char master = 0, slave = 0;
  1236. char strMember[64];
  1237. bool find = false;
  1238. sprintf(strMember, "Set Member:");
  1239. for(int i = 0; i < tPartner->Quantity; i++)
  1240. {
  1241. slave = tPartner->Member[i];
  1242. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1243. SetPsuGroupRole(slave, _GROLE_PREPARE_SWITCH_OFF);
  1244. ShmPsuGrouping->GroupCollection[slave].ReservedTarget = target;
  1245. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedDerating = true;
  1246. char strSlave[8];
  1247. sprintf(strSlave, " [%02X]", slave);
  1248. strcat(strMember, strSlave);
  1249. find = true;
  1250. }
  1251. if(find)
  1252. {
  1253. char strTemp[32];
  1254. if(target != 0)
  1255. {
  1256. sprintf(strTemp, " Prepare To Change To Gun %d", target);
  1257. }
  1258. else
  1259. {
  1260. sprintf(strTemp, " Prepare To Power Of");
  1261. }
  1262. strcat(strMember, strTemp);
  1263. LOG_INFO("%s", strMember);
  1264. }
  1265. }
  1266. // target: target group index + 1
  1267. // tPartner: set member to prepare to attach on
  1268. void PrepareToExtendCapability(byte target, PsuGroupPartner *tPartner)
  1269. {
  1270. char strMember[64];
  1271. sprintf(strMember, "Set Member:");
  1272. for(int i = 0; i < tPartner->Quantity; i++)
  1273. {
  1274. SetPsuGroupRole(tPartner->Member[i], _GROLE_PREPARE_ATTACH_ON);
  1275. ShmPsuGrouping->GroupCollection[tPartner->Member[i]].ReservedTarget = target;
  1276. char strSlave[8];
  1277. sprintf(strSlave, " [%02X]", tPartner->Member[i]);
  1278. strcat(strMember, strSlave);
  1279. }
  1280. if(tPartner->Quantity > 0)
  1281. {
  1282. char strTemp[32];
  1283. sprintf(strTemp, " Prepare To Attach To Gun %d", target);
  1284. strcat(strMember, strTemp);
  1285. LOG_INFO("%s", strMember);
  1286. }
  1287. }
  1288. void FindPsuGroupPartner(byte master, int condition, PsuGroupPartner *tPartner, int type, int quantityLimit)
  1289. {
  1290. int slave = 0, location = 0;
  1291. PsuGroupPartner partner;
  1292. int extendPower = 0;
  1293. bool done = false;
  1294. int parallelRelay = 0;
  1295. unsigned short parallelFlag = 0;
  1296. memset(&partner, 0x00, sizeof(PsuGroupPartner));
  1297. // search from left
  1298. location = ShmPsuGrouping->GroupCollection[master].Location - 1;
  1299. for(int i = location; i >= 0; i--)
  1300. {
  1301. switch(type)
  1302. {
  1303. case _EXTEND_TYPE_QUANTITY:
  1304. if(partner.Quantity >= condition)
  1305. {
  1306. done = true;
  1307. }
  1308. break;
  1309. case _EXTEND_TYPE_POWER:
  1310. if(extendPower >= condition)
  1311. {
  1312. done = true;
  1313. }
  1314. break;
  1315. default:
  1316. done = true;
  1317. break;
  1318. }
  1319. if(done || partner.Quantity >= quantityLimit)
  1320. {
  1321. break;
  1322. }
  1323. slave = ShmPsuGrouping->Layout[i];
  1324. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_IDLE ||
  1325. (ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_WAIT_SLAVE && (ShmPsuGrouping->GroupCollection[slave].ReservedTarget - 1) == master))
  1326. {
  1327. parallelRelay = ShmPsuGrouping->GroupCollection[master].ParallelConfig[slave];
  1328. parallelFlag |= parallelRelay != 0 ? (1 << (parallelRelay - 1)) : 0;
  1329. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  1330. {
  1331. partner.Member[partner.Quantity] = slave;
  1332. partner.ParallelFlag[partner.Quantity] = parallelFlag;
  1333. partner.Quantity++;
  1334. extendPower += (ShmPsuData->PsuGroup[slave].GroupAvailablePower / 10);
  1335. parallelFlag = 0;
  1336. }
  1337. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay)
  1338. {
  1339. parallelFlag = 0;
  1340. }
  1341. }
  1342. else
  1343. {
  1344. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1345. {
  1346. // Enable6ParallelRelay = 0, need to check slave is the same target or not
  1347. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE && master == (ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1))
  1348. {
  1349. continue;
  1350. }
  1351. break;
  1352. }
  1353. }
  1354. }
  1355. parallelFlag = 0;
  1356. // search from right
  1357. location = ShmPsuGrouping->GroupCollection[master].Location + 1;
  1358. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1359. {
  1360. switch(type)
  1361. {
  1362. case _EXTEND_TYPE_QUANTITY:
  1363. if(partner.Quantity >= condition)
  1364. {
  1365. done = true;
  1366. }
  1367. break;
  1368. case _EXTEND_TYPE_POWER:
  1369. if(extendPower >= condition)
  1370. {
  1371. done = true;
  1372. }
  1373. break;
  1374. default:
  1375. done = true;
  1376. break;
  1377. }
  1378. if(done || partner.Quantity >= quantityLimit)
  1379. {
  1380. break;
  1381. }
  1382. slave = ShmPsuGrouping->Layout[i];
  1383. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_IDLE ||
  1384. (ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_WAIT_SLAVE && (ShmPsuGrouping->GroupCollection[slave].ReservedTarget - 1) == master))
  1385. {
  1386. parallelRelay = ShmPsuGrouping->GroupCollection[master].ParallelConfig[slave];
  1387. parallelFlag |= parallelRelay != 0 ? (1 << (parallelRelay - 1)) : 0;
  1388. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  1389. {
  1390. partner.Member[partner.Quantity] = slave;
  1391. partner.ParallelFlag[partner.Quantity] = parallelFlag;
  1392. partner.Quantity++;
  1393. extendPower += (ShmPsuData->PsuGroup[slave].GroupAvailablePower / 10);
  1394. parallelFlag = 0;
  1395. }
  1396. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay)
  1397. {
  1398. parallelFlag = 0;
  1399. }
  1400. }
  1401. else
  1402. {
  1403. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1404. {
  1405. // Enable6ParallelRelay = 0, need to check slave is the same target or not
  1406. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE && master == (ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1))
  1407. {
  1408. continue;
  1409. }
  1410. break;
  1411. }
  1412. }
  1413. }
  1414. memcpy(tPartner, &partner, sizeof(PsuGroupPartner));
  1415. }
  1416. int GetPsuGroupAvailable(byte group)
  1417. {
  1418. PsuGroupPartner partner;
  1419. FindPsuGroupPartner(group, MAX_GROUP_QUANTITY, &partner, _EXTEND_TYPE_QUANTITY, MAX_GROUP_QUANTITY);
  1420. return partner.Quantity;
  1421. }
  1422. // return quantity which role is _GROLE_MASTER
  1423. int FindChargingMasterQuantity(void)
  1424. {
  1425. int quantity = 0;
  1426. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1427. {
  1428. if(ShmPsuGrouping->GroupCollection[i].Role == _GROLE_MASTER)
  1429. {
  1430. quantity++;
  1431. }
  1432. }
  1433. return quantity;
  1434. }
  1435. // return total psu group quantity
  1436. int FindTotalPsuGroupQuantity(void)
  1437. {
  1438. int quantity = 0;
  1439. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  1440. {
  1441. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  1442. {
  1443. quantity++;
  1444. }
  1445. }
  1446. return quantity;
  1447. }
  1448. void ShowGunPrioritySequence(unsigned char *sequence, int quantity)
  1449. {
  1450. char strSequence[256];
  1451. sprintf(strSequence, "Priority Sequence:");
  1452. for(int i = 0; i < quantity; i++)
  1453. {
  1454. char strGun[32];
  1455. sprintf(strGun, " [Gun %d: %d kW]",
  1456. sequence[i] + 1,
  1457. (int)ShmPsuGrouping->GroupCollection[sequence[i]].DiffPower_PhysicalLimit);
  1458. strcat(strSequence, strGun);
  1459. }
  1460. PSU_LOG("%s", strSequence);
  1461. }
  1462. void CheckExtendConflictPriority(unsigned char *sequence, int quantity)
  1463. {
  1464. bool done = true;
  1465. int temp = 0;
  1466. for(int i = 0; i < quantity - 1; i++)
  1467. {
  1468. done = true;
  1469. for(int j = 0; j < quantity - i - 1; j++)
  1470. {
  1471. if(ShmPsuGrouping->GroupCollection[sequence[j + 1]].DiffPower_PhysicalLimit > ShmPsuGrouping->GroupCollection[sequence[j]].DiffPower_PhysicalLimit)
  1472. {
  1473. temp = sequence[j];
  1474. sequence[j] = sequence[j + 1];
  1475. sequence[j + 1] = temp;
  1476. done = false;
  1477. }
  1478. }
  1479. //ShowGunPrioritySequence(sequence, quantity);
  1480. if(done)
  1481. {
  1482. break;
  1483. }
  1484. }
  1485. }
  1486. // return conflict reason
  1487. int IsExtendConflict(byte master)
  1488. {
  1489. int available = 0, conflictMasterCnt = 0, conflictGroupCnt = 0, average = 0, remainder = 0;
  1490. available = GetPsuGroupAvailable(master);
  1491. if(available == 0)
  1492. {
  1493. return _CONFLICT_AVAILABLE;
  1494. }
  1495. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1496. {
  1497. if(ShmPsuGrouping->GroupCollection[i].GroupCtrl.bits.ExtendAvailable ||
  1498. ShmPsuGrouping->GroupCollection[i].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue != 0)
  1499. {
  1500. conflictMasterCnt++;
  1501. conflictGroupCnt += ShmPsuGrouping->GroupCollection[i].Partner.Quantity + 1;
  1502. }
  1503. }
  1504. average = conflictMasterCnt != 0 ? ((conflictGroupCnt + available) / conflictMasterCnt) : 0;
  1505. remainder = conflictMasterCnt != 0 ? ((conflictGroupCnt + available) % conflictMasterCnt) : 0;
  1506. if(ShmPsuGrouping->GroupCollection[master].Partner.Quantity + 1 < average)
  1507. {
  1508. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = average - ShmPsuGrouping->GroupCollection[master].Partner.Quantity - 1;
  1509. #if PSU_DEBUG_MSG
  1510. LOG_INFO("Gun %d Extend Average First, Quantity Limit: %d", master + 1, ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit);
  1511. #endif
  1512. return _CONFLICT_NONE;
  1513. }
  1514. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1515. {
  1516. if(ShmPsuGrouping->GroupCollection[i].GroupCtrl.bits.ExtendAvailable ||
  1517. ShmPsuGrouping->GroupCollection[i].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue != 0)
  1518. {
  1519. if(ShmPsuGrouping->GroupCollection[i].Partner.Quantity + 1 < average)
  1520. {
  1521. return _CONFLICT_AVERAGE;
  1522. }
  1523. }
  1524. }
  1525. if(remainder > 0)
  1526. {
  1527. int find = 0;
  1528. CheckExtendConflictPriority(_GunIndexOfPrioritySequence, MAX_GROUP_QUANTITY);
  1529. ShowGunPrioritySequence(_GunIndexOfPrioritySequence, MAX_GROUP_QUANTITY);
  1530. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1531. {
  1532. if(find >= remainder)
  1533. {
  1534. break;
  1535. }
  1536. if(ShmPsuGrouping->GroupCollection[_GunIndexOfPrioritySequence[i]].GroupCtrl.bits.ExtendAvailable)
  1537. {
  1538. find++;
  1539. if(master == _GunIndexOfPrioritySequence[i])
  1540. {
  1541. ShmPsuGrouping->GroupCollection[_GunIndexOfPrioritySequence[i]].ExtendQuantityLimit = 1;
  1542. #if PSU_DEBUG_MSG
  1543. LOG_INFO("Gun %d Extend Via Priority First, Quantity Limit: %d", master + 1, ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit);
  1544. #endif
  1545. return _CONFLICT_NONE;
  1546. }
  1547. }
  1548. }
  1549. }
  1550. return _CONFLICT_PRIORITY;
  1551. }
  1552. void FindGrabPartner(byte group, byte GrabMaster, PsuGroupPartner *tPartner)
  1553. {
  1554. bool find = false;
  1555. int location = 0, master = 0, slave = 0;
  1556. // find other group in the same direction
  1557. if(ShmPsuGrouping->Location[group] < ShmPsuGrouping->Location[GrabMaster])
  1558. {
  1559. // find other group far to GrabMaster in the same direction
  1560. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  1561. for(int i = location; i >= 0; i--)
  1562. {
  1563. slave = ShmPsuGrouping->Layout[i];
  1564. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1565. if(master == GrabMaster)
  1566. {
  1567. tPartner->Member[tPartner->Quantity] = slave;
  1568. tPartner->Quantity++;
  1569. find = true;
  1570. #if PSU_DEBUG_MSG
  1571. LOG_INFO("Find Priority Partner [%02X] Far To GrabMaster At Right Side", slave);
  1572. #endif
  1573. break;
  1574. }
  1575. }
  1576. if(!find)
  1577. {
  1578. // find other group near to GrabMaster in the same direction
  1579. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  1580. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1581. {
  1582. slave = ShmPsuGrouping->Layout[i];
  1583. if(slave == GrabMaster)
  1584. {
  1585. break;
  1586. }
  1587. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1588. if(master == GrabMaster)
  1589. {
  1590. tPartner->Member[tPartner->Quantity] = slave;
  1591. tPartner->Quantity++;
  1592. find = true;
  1593. #if PSU_DEBUG_MSG
  1594. LOG_INFO("Find Priority Partner [%02X] Near To GrabMaster At Right Side", slave);
  1595. #endif
  1596. break;
  1597. }
  1598. }
  1599. if(!find)
  1600. {
  1601. for(int i = ShmChargerInfo->Control.MaxConnector - 1; i >= 0; i--)
  1602. {
  1603. slave = ShmPsuGrouping->Layout[i];
  1604. if(slave == GrabMaster)
  1605. {
  1606. break;
  1607. }
  1608. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1609. if(master == GrabMaster)
  1610. {
  1611. tPartner->Member[tPartner->Quantity] = slave;
  1612. tPartner->Quantity++;
  1613. find = true;
  1614. #if PSU_DEBUG_MSG
  1615. LOG_INFO("Find Priority Partner [%02X] At Right Side At Ring Mode", slave);
  1616. #endif
  1617. break;
  1618. }
  1619. }
  1620. }
  1621. }
  1622. }
  1623. if(ShmPsuGrouping->Location[group] > ShmPsuGrouping->Location[GrabMaster])
  1624. {
  1625. // find other group far to GrabMaster in the same direction
  1626. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  1627. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1628. {
  1629. slave = ShmPsuGrouping->Layout[i];
  1630. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1631. if(master == GrabMaster)
  1632. {
  1633. tPartner->Member[tPartner->Quantity] = slave;
  1634. tPartner->Quantity++;
  1635. find = true;
  1636. #if PSU_DEBUG_MSG
  1637. LOG_INFO("Find Priority Partner [%02X] Far To GrabMaster At Left Side", slave);
  1638. #endif
  1639. break;
  1640. }
  1641. }
  1642. if(!find)
  1643. {
  1644. // find other group near to GrabMaster in the same direction
  1645. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  1646. for(int i = location; i >= 0; i--)
  1647. {
  1648. slave = ShmPsuGrouping->Layout[i];
  1649. if(slave == GrabMaster)
  1650. {
  1651. break;
  1652. }
  1653. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1654. if(master == GrabMaster)
  1655. {
  1656. tPartner->Member[tPartner->Quantity] = slave;
  1657. tPartner->Quantity++;
  1658. find = true;
  1659. #if PSU_DEBUG_MSG
  1660. LOG_INFO("Find Priority Partner [%02X] Near To GrabMaster At Left Side", slave);
  1661. #endif
  1662. break;
  1663. }
  1664. }
  1665. if(!find)
  1666. {
  1667. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1668. {
  1669. slave = ShmPsuGrouping->Layout[i];
  1670. if(slave == GrabMaster)
  1671. {
  1672. break;
  1673. }
  1674. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1675. if(master == GrabMaster)
  1676. {
  1677. tPartner->Member[tPartner->Quantity] = slave;
  1678. tPartner->Quantity++;
  1679. find = true;
  1680. #if PSU_DEBUG_MSG
  1681. LOG_INFO("Find Priority Partner [%02X] At Left Side At Ring Mode", slave);
  1682. #endif
  1683. break;
  1684. }
  1685. }
  1686. }
  1687. }
  1688. }
  1689. }
  1690. // return grab success or fail
  1691. // return tPartner with possible member
  1692. bool PsuGroupGrabCheck(byte group, PsuGroupPartner *tPartner)
  1693. {
  1694. int slave = 0, target = 0, location = 0, average = 0, total = 0;
  1695. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_SLAVE && ShmPsuGrouping->GroupCollection[group].Role != _GROLE_REQUEST_TO_CHARGING)
  1696. {
  1697. return false;
  1698. }
  1699. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  1700. total = GetPsuGroupAvailable(group) + 1;
  1701. average = FindTotalPsuGroupQuantity() / (1 + FindChargingMasterQuantity());
  1702. if(total < average)
  1703. {
  1704. // search from left
  1705. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  1706. for(int i = location; i >= 0; i--)
  1707. {
  1708. if(total + tPartner->Quantity < average)
  1709. {
  1710. slave = ShmPsuGrouping->Layout[i];
  1711. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE &&
  1712. (ShmPsuGrouping->GroupCollection[group].Role == _GROLE_REQUEST_TO_CHARGING ||
  1713. ShmPsuGrouping->GroupCollection[slave].TargetGroup == ShmPsuGrouping->GroupCollection[group].TargetGroup))
  1714. {
  1715. target = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1716. if((ShmPsuGrouping->GroupCollection[target].Partner.Quantity + 1) > average)
  1717. {
  1718. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1719. {
  1720. tPartner->Member[tPartner->Quantity] = slave;
  1721. tPartner->Quantity++;
  1722. }
  1723. else
  1724. {
  1725. FindGrabPartner(group, target, tPartner);
  1726. }
  1727. }
  1728. }
  1729. else
  1730. {
  1731. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1732. {
  1733. break;
  1734. }
  1735. }
  1736. }
  1737. }
  1738. // search from right
  1739. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  1740. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1741. {
  1742. if(total + tPartner->Quantity < average)
  1743. {
  1744. slave = ShmPsuGrouping->Layout[i];
  1745. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE &&
  1746. (ShmPsuGrouping->GroupCollection[group].Role == _GROLE_REQUEST_TO_CHARGING ||
  1747. ShmPsuGrouping->GroupCollection[slave].TargetGroup == ShmPsuGrouping->GroupCollection[group].TargetGroup))
  1748. {
  1749. target = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1750. if((ShmPsuGrouping->GroupCollection[target].Partner.Quantity + 1) > average)
  1751. {
  1752. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1753. {
  1754. tPartner->Member[tPartner->Quantity] = slave;
  1755. tPartner->Quantity++;
  1756. }
  1757. else
  1758. {
  1759. FindGrabPartner(group, target, tPartner);
  1760. }
  1761. }
  1762. }
  1763. else
  1764. {
  1765. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1766. {
  1767. break;
  1768. }
  1769. }
  1770. }
  1771. }
  1772. }
  1773. return tPartner->Quantity > 0 ? true : false;
  1774. }
  1775. // return tPartner with possible member when group power off
  1776. void PsuGroupPowerOffCheck(byte group, PsuGroupPartner *tPartner)
  1777. {
  1778. int master = 0, quantity = 0, location = 0;
  1779. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  1780. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_SLAVE)
  1781. {
  1782. return;
  1783. }
  1784. master = ShmPsuGrouping->GroupCollection[group].TargetGroup - 1;
  1785. quantity = ShmPsuGrouping->GroupCollection[master].Partner.Quantity;
  1786. for(int i = 0; i < quantity; i++)
  1787. {
  1788. if(tPartner->Quantity == 0)
  1789. {
  1790. if(group == ShmPsuGrouping->GroupCollection[master].Partner.Member[i])
  1791. {
  1792. location = i;
  1793. tPartner->Member[tPartner->Quantity] = group;
  1794. tPartner->Quantity++;
  1795. }
  1796. }
  1797. else
  1798. {
  1799. // find other group in the same direction
  1800. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[location]] < ShmPsuGrouping->Location[master])
  1801. {
  1802. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[i]] < ShmPsuGrouping->Location[master])
  1803. {
  1804. tPartner->Member[tPartner->Quantity] = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  1805. tPartner->Quantity++;
  1806. //printf("\r\n Find Other Group %02X In The Same Direction", ShmPsuGrouping->GroupCollection[master].Partner.Member[i]);
  1807. }
  1808. }
  1809. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[location]] > ShmPsuGrouping->Location[master])
  1810. {
  1811. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[i]] > ShmPsuGrouping->Location[master])
  1812. {
  1813. tPartner->Member[tPartner->Quantity] = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  1814. tPartner->Quantity++;
  1815. //printf("\r\n Find Other Group %02X In The Same Direction", ShmPsuGrouping->GroupCollection[master].Partner.Member[i]);
  1816. }
  1817. }
  1818. }
  1819. }
  1820. }
  1821. void UpdateGunTotalPsuQuantity(int master)
  1822. {
  1823. ShmPsuGrouping->GroupCollection[master].GunPsuQuantity = GetPsuModuleQuantity(master);
  1824. }
  1825. // group: group index, target: target index
  1826. // master: master group index
  1827. void AddMember(byte group, byte master, unsigned short flag)
  1828. {
  1829. if(group < GENERAL_GUN_QUANTITY && master < GENERAL_GUN_QUANTITY)
  1830. {
  1831. if(ShmPsuGrouping->GroupCollection[master].Role != _GROLE_MASTER &&
  1832. ShmPsuGrouping->GroupCollection[master].Role != _GROLE_REQUEST_TO_CHARGING)
  1833. {
  1834. return;
  1835. }
  1836. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_IDLE &&
  1837. ShmPsuGrouping->GroupCollection[group].Role != _GROLE_WAIT_SLAVE &&
  1838. ShmPsuGrouping->GroupCollection[group].Role != _GROLE_PRECHARGE_READY)
  1839. {
  1840. return;
  1841. }
  1842. SetPsuGroupToSlave(group, master + 1);
  1843. ShmPsuGrouping->GroupCollection[master].Partner.Member[ShmPsuGrouping->GroupCollection[master].Partner.Quantity] = group;
  1844. ShmPsuGrouping->GroupCollection[master].Partner.ParallelFlag[ShmPsuGrouping->GroupCollection[master].Partner.Quantity] = flag;
  1845. ShmPsuGrouping->GroupCollection[master].Partner.Quantity++;
  1846. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1847. {
  1848. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity++;
  1849. //ShmPsuGrouping->GroupCollection[master].GunPsuQuantity += ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity;
  1850. UpdateGunTotalPsuQuantity(master);
  1851. #if PSU_DEBUG_MSG
  1852. LOG_INFO("Gun %d Add Group [%02X], Quantity: %d (%d), Flag: %04X",
  1853. master + 1, group, ShmPsuGrouping->GroupCollection[master].Partner.Quantity,
  1854. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity, flag);
  1855. #endif
  1856. }
  1857. //printf("\r\n Add Group %02X To Gun %d (Quantity %d), Set Parallel Relay %d On", group, target + 1, ShmPsuGrouping->GroupCollection[target].Partner.Quantity, ParallelConfig);
  1858. }
  1859. }
  1860. // master: master group index
  1861. void AddAvailableMember(unsigned char master)
  1862. {
  1863. PsuGroupPartner partner;
  1864. char strMember[64];
  1865. FindPsuGroupPartner(master, MAX_GROUP_QUANTITY, &partner, _EXTEND_TYPE_QUANTITY, MAX_GROUP_QUANTITY);
  1866. sprintf(strMember, "Gun %d Add Available Member:", master + 1);
  1867. for(int i = 0; i < partner.Quantity; i++)
  1868. {
  1869. AddMember(partner.Member[i], master, partner.ParallelFlag[i]);
  1870. char strSlave[8];
  1871. sprintf(strSlave, " [%02X]", partner.Member[i]);
  1872. strcat(strMember, strSlave);
  1873. }
  1874. if(partner.Quantity > 0)
  1875. {
  1876. LOG_INFO("%s", strMember);
  1877. }
  1878. }
  1879. // master: master group index
  1880. // slave: slave group index
  1881. void RemoveMember(unsigned char master, unsigned char slave)
  1882. {
  1883. bool find = false;
  1884. int location = 0;
  1885. unsigned char other = 0;
  1886. unsigned short parallelFlag = 0;
  1887. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  1888. {
  1889. if(slave == ShmPsuGrouping->GroupCollection[master].Partner.Member[i])
  1890. {
  1891. ShmPsuGrouping->GroupCollection[master].Partner.Member[i] = 0;
  1892. location = i;
  1893. find = true;
  1894. break;
  1895. }
  1896. }
  1897. if(find)
  1898. {
  1899. for(int i = location + 1; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  1900. {
  1901. other = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  1902. parallelFlag = ShmPsuGrouping->GroupCollection[master].Partner.ParallelFlag[i];
  1903. ShmPsuGrouping->GroupCollection[master].Partner.Member[i] = 0;
  1904. ShmPsuGrouping->GroupCollection[master].Partner.Member[i - 1] = other;
  1905. ShmPsuGrouping->GroupCollection[master].Partner.ParallelFlag[i - 1] = parallelFlag;
  1906. }
  1907. ShmPsuGrouping->GroupCollection[master].Partner.Quantity--;
  1908. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  1909. {
  1910. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity--;
  1911. //ShmPsuGrouping->GroupCollection[master].GunPsuQuantity -= ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity;
  1912. UpdateGunTotalPsuQuantity(master);
  1913. }
  1914. #if PSU_DEBUG_MSG
  1915. LOG_INFO("Gun %d Remove Group [%02X], Quantity: %d (%d)",
  1916. master + 1, slave, ShmPsuGrouping->GroupCollection[master].Partner.Quantity,
  1917. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity);
  1918. #endif
  1919. }
  1920. }
  1921. // master: master group index
  1922. void RemoveNonGroupMember(unsigned char master)
  1923. {
  1924. unsigned char slave = 0;
  1925. char strMember[64];
  1926. PsuGroupPartner partner;
  1927. bool find = false;
  1928. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  1929. {
  1930. sprintf(strMember, "Gun %d Remove Member:", master + 1);
  1931. memcpy(&partner, &ShmPsuGrouping->GroupCollection[master].Partner, sizeof(PsuGroupPartner));
  1932. for(int i = 0; i < partner.Quantity; i++)
  1933. {
  1934. slave = partner.Member[i];
  1935. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE)
  1936. {
  1937. RemoveMember(master, slave);
  1938. if(ShmPsuGrouping->GroupCollection[slave].ReservedTarget == 0)
  1939. {
  1940. SetPsuGroupToIdle(slave);
  1941. }
  1942. else
  1943. {
  1944. SetPsuGroupRole(slave, _GROLE_WAIT_SLAVE);
  1945. }
  1946. char strSlave[8];
  1947. sprintf(strSlave, " [%02X]", slave);
  1948. strcat(strMember, strSlave);
  1949. find = true;
  1950. }
  1951. }
  1952. if(find)
  1953. {
  1954. LOG_INFO("%s", strMember);
  1955. }
  1956. }
  1957. }
  1958. void SetPsuGroupPowerOnOff(unsigned char group, unsigned char power_on_off)
  1959. {
  1960. if(group < CONNECTOR_QUANTITY)
  1961. {
  1962. enum PSU_POWER_CMD power_cmd = power_on_off == PSU_POWER_ON ? PSU_POWER_ON : PSU_POWER_OFF;
  1963. enum PSU_FLASH_CMD led_cmd = power_on_off == PSU_POWER_ON ? PSU_FLASH_ON : PSU_FLASH_NORMAL;
  1964. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1965. {
  1966. #if ONE_MODULE_OUTPUT
  1967. // for safety test (inrush current)
  1968. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.InPrechargeMode)
  1969. {
  1970. SinglePsuPower(ShmPsuPosition->GroupLocationInfo[group].PsuSN[0], power_cmd);
  1971. }
  1972. else
  1973. {
  1974. SwitchPower(group, power_cmd);
  1975. }
  1976. #else
  1977. SwitchPower(group, power_cmd);
  1978. #endif
  1979. Await();
  1980. FlashLed(group, led_cmd);
  1981. Await();
  1982. }
  1983. isStartOutputSwitch[group] = power_on_off == PSU_POWER_ON ? true : false;
  1984. }
  1985. }
  1986. // master: master group index
  1987. unsigned short GetPresentTargetCurrent(unsigned char master, unsigned char role_condition)
  1988. {
  1989. unsigned char slave = 0;
  1990. unsigned short current = 0;
  1991. current = ShmPsuGrouping->GroupOutput[master].GTargetCurrent;
  1992. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  1993. {
  1994. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  1995. if(role_condition == _GROLE_MASTER || ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE)
  1996. {
  1997. current += ShmPsuGrouping->GroupOutput[slave].GTargetCurrent;
  1998. }
  1999. }
  2000. return current;
  2001. }
  2002. // group: group index
  2003. int GetPsuModuleQuantity(unsigned char group)
  2004. {
  2005. int quantity = 0;
  2006. unsigned char slave = 0;
  2007. quantity = ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity;
  2008. for(int i = 0; i < ShmPsuGrouping->GroupCollection[group].Partner.Quantity; i++)
  2009. {
  2010. slave = ShmPsuGrouping->GroupCollection[group].Partner.Member[i];
  2011. quantity += ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity;
  2012. }
  2013. return quantity;
  2014. }
  2015. // group: group index
  2016. bool IsAvailableGroup(unsigned char group)
  2017. {
  2018. return ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0 ? true : false;
  2019. }
  2020. // master: master group index
  2021. int GetMasterAvailableGroup(unsigned char master)
  2022. {
  2023. int quantity = 0;
  2024. unsigned char slave = 0;
  2025. quantity = IsAvailableGroup(master) ? quantity + 1 : quantity;
  2026. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2027. {
  2028. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2029. quantity = IsAvailableGroup(slave) ? quantity + 1 : quantity;
  2030. }
  2031. return quantity;
  2032. }
  2033. // group: group index
  2034. void UpdateGunLoading(unsigned char group)
  2035. {
  2036. if(ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup != 0 &&
  2037. group == ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup - 1)
  2038. {
  2039. unsigned short TargetCurrent = (int)(chargingInfo[group]->EvBatterytargetCurrent * 10);
  2040. ShmPsuGrouping->GroupCollection[group].GunLoading = chargingInfo[group]->AvailableChargingCurrent != 0 ?
  2041. (TargetCurrent * 10000 / (int)chargingInfo[group]->AvailableChargingCurrent) : 0;
  2042. if(chargingInfo[group]->PresentChargingVoltage >= (PSU_MIN_VOL / 10))
  2043. {
  2044. ShmPsuGrouping->GroupCollection[group].DiffPower_Available = (((float)(chargingInfo[group]->AvailableChargingCurrent - ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent) / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000;
  2045. ShmPsuGrouping->GroupCollection[group].DiffPower_Capability = (((float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[group].CapabilityCurrent - ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent) / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000;
  2046. ShmPsuGrouping->GroupCollection[group].DiffPower_PhysicalLimit = (((float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[group].RemoteMaxPhysicalCurrent - ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent) / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000;
  2047. }
  2048. else
  2049. {
  2050. ShmPsuGrouping->GroupCollection[group].DiffPower_Available = 0;
  2051. ShmPsuGrouping->GroupCollection[group].DiffPower_Capability = 0;
  2052. ShmPsuGrouping->GroupCollection[group].DiffPower_PhysicalLimit = 0;
  2053. }
  2054. }
  2055. else
  2056. {
  2057. ShmPsuGrouping->GroupCollection[group].GunLoading = 0;
  2058. }
  2059. }
  2060. // group: group index
  2061. void UpdatePsuGroupLoading(unsigned char group)
  2062. {
  2063. ShmPsuGrouping->GroupOutput[group].OutputLoading = ShmPsuData->PsuGroup[group].GroupAvailableCurrent != 0 ?
  2064. (ShmPsuGrouping->GroupOutput[group].GTargetCurrent * 10000 / ShmPsuData->PsuGroup[group].GroupAvailableCurrent) : 0;
  2065. }
  2066. // master: master group index
  2067. void UpdateMasterPsuGroupLoading(unsigned char master)
  2068. {
  2069. unsigned char slave = 0;
  2070. UpdatePsuGroupLoading(master);
  2071. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2072. {
  2073. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2074. UpdatePsuGroupLoading(slave);
  2075. }
  2076. }
  2077. void PsuGroupIncreaseCurrent(unsigned char group, unsigned short needIncrease, unsigned short *realIncreaseCurrent)
  2078. {
  2079. unsigned short increase = needIncrease;
  2080. if((ShmPsuGrouping->GroupOutput[group].GTargetCurrent + increase) <= ShmPsuData->PsuGroup[group].GroupAvailableCurrent)
  2081. {
  2082. ShmPsuGrouping->GroupOutput[group].GTargetCurrent += increase;
  2083. }
  2084. else
  2085. {
  2086. increase = ShmPsuData->PsuGroup[group].GroupAvailableCurrent - ShmPsuGrouping->GroupOutput[group].GTargetCurrent;
  2087. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  2088. }
  2089. *realIncreaseCurrent += increase;
  2090. }
  2091. void PsuGroupDecreaseCurrent(unsigned char group, unsigned short needDecrease, unsigned short *realDecreaseCurrent)
  2092. {
  2093. unsigned short decrease = needDecrease;
  2094. if(ShmPsuGrouping->GroupOutput[group].GTargetCurrent >= (decrease + ZERO_CURRENT))
  2095. {
  2096. ShmPsuGrouping->GroupOutput[group].GTargetCurrent -= decrease;
  2097. }
  2098. else
  2099. {
  2100. decrease = ShmPsuGrouping->GroupOutput[group].GTargetCurrent - ZERO_CURRENT;
  2101. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = ZERO_CURRENT;
  2102. }
  2103. *realDecreaseCurrent += decrease;
  2104. }
  2105. void IncreaseCurrentByGunLoading(unsigned char group, unsigned short loading, unsigned short *realIncrease, unsigned short MaxIncrease)
  2106. {
  2107. unsigned short target = 0, need = 0;
  2108. if(*realIncrease >= MaxIncrease)
  2109. {
  2110. return;
  2111. }
  2112. target = (loading * ShmPsuData->PsuGroup[group].GroupAvailableCurrent) / 10000;
  2113. need = target >= ShmPsuGrouping->GroupOutput[group].GTargetCurrent ?
  2114. (target - ShmPsuGrouping->GroupOutput[group].GTargetCurrent) : 0;
  2115. need = need > (MaxIncrease - *realIncrease) ? (MaxIncrease - *realIncrease) : need;
  2116. //LOG_INFO("Group [%02X] Target Current %d.%d A, Increase Current: %d.%d A",
  2117. // group, (target / 10), (target % 10), (need / 10), (need % 10));
  2118. if(need > 0)
  2119. {
  2120. PsuGroupIncreaseCurrent(group, need, realIncrease);
  2121. }
  2122. }
  2123. void DecreaseCurrentByGunLoading(unsigned char group, unsigned short loading, unsigned short *realDecrease, unsigned short MaxDecrease)
  2124. {
  2125. unsigned short target = 0, need = 0;
  2126. if(*realDecrease >= MaxDecrease)
  2127. {
  2128. return;
  2129. }
  2130. target = (loading * ShmPsuData->PsuGroup[group].GroupAvailableCurrent) / 10000;
  2131. need = ShmPsuGrouping->GroupOutput[group].GTargetCurrent >= target ?
  2132. (ShmPsuGrouping->GroupOutput[group].GTargetCurrent - target) : 0;
  2133. need = need > (MaxDecrease - *realDecrease) ? (MaxDecrease - *realDecrease) : need;
  2134. //LOG_INFO("Group [%02X] Target Current %d.%d A, Decrease Current: %d.%d A",
  2135. // group, (target / 10), (target % 10), (need / 10), (need % 10));
  2136. if(need > 0)
  2137. {
  2138. PsuGroupDecreaseCurrent(group, need, realDecrease);
  2139. }
  2140. }
  2141. // master: master group index
  2142. void AverageIncreaseCurrent(unsigned char master, unsigned short NeedIncreaseCurrent)
  2143. {
  2144. unsigned char availableGroup = 0, slave = 0;
  2145. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realIncrease = 0;
  2146. #if PSU_DEBUG_MSG
  2147. PSU_LOG("Gun %d Increase By GunLoading %d.%02d Percent",
  2148. master + 1, (ShmPsuGrouping->GroupCollection[master].GunLoading / 100), (ShmPsuGrouping->GroupCollection[master].GunLoading % 100));
  2149. #endif
  2150. IncreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &realIncrease, NeedIncreaseCurrent);
  2151. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2152. {
  2153. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2154. IncreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &realIncrease, NeedIncreaseCurrent);
  2155. }
  2156. availableGroup = GetMasterAvailableGroup(master);
  2157. if(NeedIncreaseCurrent > realIncrease && availableGroup > 0)
  2158. {
  2159. // increase current by group quantity
  2160. avgGroupCurrent = (NeedIncreaseCurrent - realIncrease) / availableGroup;
  2161. #if PSU_DEBUG_MSG
  2162. PSU_LOG("Gun %d Increase By Group, AvgGroupCurrent %d.%d A",
  2163. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  2164. #endif
  2165. PsuGroupIncreaseCurrent(master, avgGroupCurrent, &realIncrease);
  2166. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2167. {
  2168. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2169. PsuGroupIncreaseCurrent(slave, avgGroupCurrent, &realIncrease);
  2170. }
  2171. }
  2172. if(NeedIncreaseCurrent > realIncrease)
  2173. {
  2174. // increase excess current to master group
  2175. excessCurrent = NeedIncreaseCurrent - realIncrease;
  2176. #if PSU_DEBUG_MSG
  2177. PSU_LOG("Gun %d Increase, ExcessCurrent %d.%d A", master + 1, (excessCurrent / 10), (excessCurrent % 10));
  2178. #endif
  2179. PsuGroupIncreaseCurrent(master, excessCurrent, &realIncrease);
  2180. }
  2181. }
  2182. // master: master group index
  2183. void AverageDecreaseCurrent(unsigned char master, unsigned short NeedDecreaseCurrent)
  2184. {
  2185. unsigned char availableGroup = 0, slave = 0;
  2186. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realDecrease = 0;
  2187. #if PSU_DEBUG_MSG
  2188. PSU_LOG("Gun %d Decrease By GunLoading %d.%02d Percent",
  2189. master + 1, (ShmPsuGrouping->GroupCollection[master].GunLoading / 100), (ShmPsuGrouping->GroupCollection[master].GunLoading % 100));
  2190. #endif
  2191. DecreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &realDecrease, NeedDecreaseCurrent);
  2192. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2193. {
  2194. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2195. DecreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &realDecrease, NeedDecreaseCurrent);
  2196. }
  2197. availableGroup = GetMasterAvailableGroup(master);
  2198. if(NeedDecreaseCurrent > realDecrease)
  2199. {
  2200. // decrease current by group quantity
  2201. avgGroupCurrent = (NeedDecreaseCurrent - realDecrease) / availableGroup;
  2202. #if PSU_DEBUG_MSG
  2203. PSU_LOG("Gun %d Decrease By Group, AvgGroupCurrent %d.%d A",
  2204. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  2205. #endif
  2206. PsuGroupDecreaseCurrent(master, avgGroupCurrent, &realDecrease);
  2207. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2208. {
  2209. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2210. PsuGroupDecreaseCurrent(slave, avgGroupCurrent, &realDecrease);
  2211. }
  2212. }
  2213. if(NeedDecreaseCurrent > realDecrease)
  2214. {
  2215. // decrease excess current
  2216. excessCurrent = NeedDecreaseCurrent - realDecrease;
  2217. #if PSU_DEBUG_MSG
  2218. PSU_LOG("Gun %d Decrease, ExcessCurrent %d.%d A", master + 1, (excessCurrent / 10), (excessCurrent % 10));
  2219. #endif
  2220. PsuGroupDecreaseCurrent(master, excessCurrent, &realDecrease);
  2221. }
  2222. }
  2223. void MasterIncreaseCurrent(unsigned char master, unsigned short NeedIncreaseCurrent)
  2224. {
  2225. unsigned short excessCurrent = 0, realIncrease = 0;
  2226. PsuGroupIncreaseCurrent(master, NeedIncreaseCurrent, &realIncrease);
  2227. if(NeedIncreaseCurrent > realIncrease)
  2228. {
  2229. excessCurrent = NeedIncreaseCurrent - realIncrease;
  2230. AverageIncreaseCurrent(master, excessCurrent);
  2231. }
  2232. }
  2233. void MasterDecreaseCurrent(unsigned char master, unsigned short NeedDecreaseCurrent)
  2234. {
  2235. unsigned char slave = 0;
  2236. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realDecrease = 0;
  2237. if(ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity > 0)
  2238. {
  2239. // decrease current by group quantity
  2240. avgGroupCurrent = NeedDecreaseCurrent / ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity;
  2241. #if PSU_DEBUG_MSG
  2242. PSU_LOG("Gun %d Decrease By Group, AvgGroupCurrent %d.%d A",
  2243. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  2244. #endif
  2245. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2246. {
  2247. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2248. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  2249. {
  2250. PsuGroupDecreaseCurrent(slave, avgGroupCurrent, &realDecrease);
  2251. }
  2252. }
  2253. }
  2254. if(NeedDecreaseCurrent > realDecrease)
  2255. {
  2256. excessCurrent = NeedDecreaseCurrent - realDecrease;
  2257. PsuGroupDecreaseCurrent(master, excessCurrent, &realDecrease);
  2258. }
  2259. }
  2260. void UpdateMaxCurrent(unsigned char master, unsigned short current)
  2261. {
  2262. int time = 0;
  2263. // update max stage current
  2264. if(current > StageMaxCurrent[master])
  2265. {
  2266. StageMaxCurrent[master] = current;
  2267. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  2268. GetClockTime(&_StageCurrent_time[master]);
  2269. }
  2270. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent)
  2271. {
  2272. time = GetTimeoutValue(_StageCurrent_time[master]) / uSEC_VAL;
  2273. if(time >= EV_MAX_STAGE_CURRENT_DELAY)
  2274. {
  2275. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = true;
  2276. LOG_INFO("Gun %d Reach Max Stage Current", master + 1);
  2277. }
  2278. }
  2279. // update max target current and reset max current time
  2280. if(current > MaxCurrentDemand[master])
  2281. {
  2282. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  2283. {
  2284. LOG_INFO("Gun %d Max Current Demand Timer Reset", master + 1);
  2285. }
  2286. MaxCurrentDemand[master] = current;
  2287. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand = false;
  2288. GetClockTime(&_MaxCurrent_time[master]);
  2289. }
  2290. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  2291. {
  2292. time = GetTimeoutValue(_MaxCurrent_time[master]) / uSEC_VAL;
  2293. if(time >= EV_MAX_CURRENT_DELAY)
  2294. {
  2295. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand = true;
  2296. LOG_INFO("Gun %d Reach Max Current Demand", master + 1);
  2297. }
  2298. }
  2299. }
  2300. // master: master group index
  2301. bool IsMasterOutputCurrentStable(unsigned char master)
  2302. {
  2303. int time = 0;
  2304. bool stable = false, reachTarget = false;
  2305. unsigned short presentOutput = (int)chargingInfo[master]->PresentChargingCurrent * 10;
  2306. unsigned short TargetCurrent = 0;
  2307. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2308. reachTarget = presentOutput > (TargetCurrent - REACH_CURRENT_TOLERANCE) && presentOutput < (TargetCurrent + REACH_CURRENT_TOLERANCE);
  2309. if((presentOutput >= StableOutputCurrent[master] && presentOutput - StableOutputCurrent[master] > REACH_CURRENT_TOLERANCE) ||
  2310. (presentOutput < StableOutputCurrent[master] && StableOutputCurrent[master] - presentOutput > REACH_CURRENT_TOLERANCE))
  2311. {
  2312. GetClockTime(&_ReachCurrent_time[master]);
  2313. StableOutputCurrent[master] = presentOutput;
  2314. }
  2315. time = GetTimeoutValue(_ReachCurrent_time[master]) / uSEC_VAL;
  2316. if(reachTarget || time >= CURRENT_STABLE_TIME ||
  2317. (ShmChargerInfo->Control.FCharging[master].FCtrl.bits.EnableForceCharging && time >= CURRENT_REACH_TARGET_TIME))
  2318. {
  2319. stable = true;
  2320. }
  2321. return stable;
  2322. }
  2323. // master: master group index
  2324. void MasterBalanceCurrent(unsigned char master)
  2325. {
  2326. int time = 0;
  2327. unsigned char slave = 0;
  2328. unsigned short realBalance = 0, grabCurrent = 0;
  2329. time = GetTimeoutValue(_BalanceCurrent_time[master]) / mSEC_VAL;
  2330. if(time >= BALANCE_CURRENT_INTERVAL)
  2331. {
  2332. //LOG_INFO("Gun %d Start Balance Current", master + 1);
  2333. DecreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  2334. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2335. {
  2336. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2337. DecreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  2338. }
  2339. //LOG_INFO("Gun %d Grab Balance Current %d.%d A", master + 1, (grabCurrent / 10), (grabCurrent % 10));
  2340. IncreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &realBalance, grabCurrent);
  2341. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2342. {
  2343. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2344. IncreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &realBalance, grabCurrent);
  2345. }
  2346. //LOG_INFO("Gun %d Increase Balance Current %d.%d A", master + 1, (realBalance / 10), (realBalance % 10));
  2347. UpdateMasterPsuGroupLoading(master);
  2348. GetClockTime(&_BalanceCurrent_time[master]);
  2349. }
  2350. }
  2351. unsigned short GetLoadingDiff(unsigned char master)
  2352. {
  2353. unsigned char slave = 0;
  2354. unsigned short maxLoading = 0, minLoading = 0, diffLoading = 0;
  2355. maxLoading = ShmPsuGrouping->GroupOutput[master].OutputLoading;
  2356. minLoading = ShmPsuGrouping->GroupOutput[master].OutputLoading;
  2357. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2358. {
  2359. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2360. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity == 0 || ShmPsuData->PsuGroup[slave].GroupAvailableCurrent == 0)
  2361. {
  2362. continue;
  2363. }
  2364. maxLoading = ShmPsuGrouping->GroupOutput[slave].OutputLoading > maxLoading ?
  2365. ShmPsuGrouping->GroupOutput[slave].OutputLoading : maxLoading;
  2366. minLoading = ShmPsuGrouping->GroupOutput[slave].OutputLoading < minLoading ?
  2367. ShmPsuGrouping->GroupOutput[slave].OutputLoading : minLoading;
  2368. }
  2369. diffLoading = maxLoading - minLoading;
  2370. return diffLoading;
  2371. }
  2372. void CheckCurrentBalance(unsigned char master)
  2373. {
  2374. unsigned short diffLoading = 0;
  2375. diffLoading = GetLoadingDiff(master);
  2376. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance)
  2377. {
  2378. if(diffLoading >= START_BALANCE_CRITERIA)
  2379. {
  2380. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = true;
  2381. PSU_LOG("Gun %d Output Current Is Unbalance, diffLoading = %d.%02d", master + 1, (diffLoading / 100), (diffLoading % 100));
  2382. GetClockTime(&_BalanceCurrent_time[master]);
  2383. }
  2384. }
  2385. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance)
  2386. {
  2387. MasterBalanceCurrent(master);
  2388. diffLoading = GetLoadingDiff(master);
  2389. if(diffLoading <= STOP_BALANCE_CRITERIA)
  2390. {
  2391. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  2392. PSU_LOG("Gun %d Output Current Is Balance", master + 1);
  2393. }
  2394. }
  2395. }
  2396. // master: master group index
  2397. // slave: slave group index
  2398. bool IsGroupPartner(unsigned char master, unsigned char slave)
  2399. {
  2400. bool find = false;
  2401. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2402. {
  2403. if(slave == ShmPsuGrouping->GroupCollection[master].Partner.Member[i])
  2404. {
  2405. find = true;
  2406. break;
  2407. }
  2408. }
  2409. return find;
  2410. }
  2411. void CleanSmoothDerating(unsigned char master)
  2412. {
  2413. ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget = 0;
  2414. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating = false;
  2415. }
  2416. void SetSmoothDerating(unsigned char master, unsigned char slave)
  2417. {
  2418. ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget = slave + 1;
  2419. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating = true;
  2420. GetClockTime(&_SmoothDerating_time[master]);
  2421. }
  2422. // master: master group index
  2423. void CheckSmoothBalance(unsigned char master)
  2424. {
  2425. int time = 0;
  2426. unsigned char slave = 0;
  2427. unsigned short realBalance = 0, grabCurrent = 0;
  2428. unsigned char _ReleaseSlave = 0;
  2429. if(ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget == 0 ||
  2430. !IsGroupPartner(master, ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget - 1))
  2431. {
  2432. CleanSmoothDerating(master);
  2433. LOG_INFO("Gun %d Smooth Derating Target Is Not Available!", master + 1);
  2434. return;
  2435. }
  2436. unsigned short _CapCurrent = 0; // unit: 0.1A
  2437. unsigned short _ReleaseCurrent = 0; // unit: 0.1A
  2438. unsigned short _ReleaseLoading = 0; // unit: 0.01%
  2439. unsigned short TargetCurrent = 0; // unit: 0.1A
  2440. PsuGroupPartner ReleaseMember;
  2441. _CapCurrent = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  2442. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2443. _ReleaseSlave = ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget - 1;
  2444. _ReleaseCurrent = (int)chargingInfo[master]->AvailableChargingCurrent > (int)ShmPsuData->PsuGroup[_ReleaseSlave].GroupAvailableCurrent ?
  2445. (int)chargingInfo[master]->AvailableChargingCurrent - (int)ShmPsuData->PsuGroup[_ReleaseSlave].GroupAvailableCurrent : 0;
  2446. _ReleaseLoading = _ReleaseCurrent != 0 ? (TargetCurrent * 10000 / _ReleaseCurrent) : 0;
  2447. if(ShmPsuGrouping->GroupOutput[_ReleaseSlave].GTargetCurrent <= MAX_ADJ_BALANCE_CURRENT)
  2448. {
  2449. float originalPower = 0, capabilityPower = 0, releasePower = 0;
  2450. memset(&ReleaseMember, 0x00, sizeof(PsuGroupPartner));
  2451. ReleaseMember.Member[0] = _ReleaseSlave;
  2452. ReleaseMember.Quantity = 1;
  2453. PrepareToPowerOff(0, &ReleaseMember);
  2454. originalPower = ((chargingInfo[master]->AvailableChargingCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2455. capabilityPower = ((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2456. releasePower = (((float)(_ReleaseCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2457. LOG_INFO("Gun %d Smooth Release [%02X]", master + 1, ReleaseMember.Member[0]);
  2458. LOG_INFO("Gun %d Available %d A (%d kW) >> %d A (%d kW)",
  2459. master + 1,
  2460. ((int)chargingInfo[master]->AvailableChargingCurrent / 10), (int)originalPower,
  2461. (_ReleaseCurrent / 10), (int)releasePower);
  2462. LOG_INFO("Gun %d Capability %d A (%d kW) >> %d A (%d kW)",
  2463. master + 1,
  2464. ((int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent / 10), (int)capabilityPower,
  2465. _ReleaseCurrent >= _CapCurrent ? (_CapCurrent / 10) : (_ReleaseCurrent / 10), (int)releasePower);
  2466. CleanSmoothDerating(master);
  2467. return;
  2468. }
  2469. time = GetTimeoutValue(_SmoothDerating_time[master]) / mSEC_VAL;
  2470. if(time >= SMOOTH_DERATING_INTERVAL)
  2471. {
  2472. DecreaseCurrentByGunLoading(master, _ReleaseLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  2473. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2474. {
  2475. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2476. if(slave != _ReleaseSlave)
  2477. {
  2478. DecreaseCurrentByGunLoading(slave, _ReleaseLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  2479. }
  2480. else
  2481. {
  2482. DecreaseCurrentByGunLoading(slave, 0, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  2483. }
  2484. }
  2485. //LOG_INFO("Gun %d Grab Balance Current %d.%d A", master + 1, (grabCurrent / 10), (grabCurrent % 10));
  2486. IncreaseCurrentByGunLoading(master, _ReleaseLoading, &realBalance, grabCurrent);
  2487. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2488. {
  2489. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2490. if(slave != _ReleaseSlave)
  2491. {
  2492. IncreaseCurrentByGunLoading(slave, _ReleaseLoading, &realBalance, grabCurrent);
  2493. }
  2494. }
  2495. //LOG_INFO("Gun %d Increase Balance Current %d.%d A", master + 1, (realBalance / 10), (realBalance % 10));
  2496. UpdateMasterPsuGroupLoading(master);
  2497. GetClockTime(&_SmoothDerating_time[master]);
  2498. }
  2499. }
  2500. bool IsOtherCharging(unsigned char group)
  2501. {
  2502. bool find = false;
  2503. int other = 0, selfTarget = 0, location = 0;
  2504. if(ShmPsuGrouping->GroupCollection[group].TargetGroup == 0)
  2505. {
  2506. return find;
  2507. }
  2508. selfTarget = ShmPsuGrouping->GroupCollection[group].TargetGroup - 1;
  2509. // search from left
  2510. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  2511. for(int i = location; i >= 0; i--)
  2512. {
  2513. other = ShmPsuGrouping->Layout[i];
  2514. if(ShmPsuGrouping->GroupCollection[other].Role == _GROLE_MASTER)
  2515. {
  2516. find = other != selfTarget ? true : false;
  2517. break;
  2518. }
  2519. }
  2520. if(!find)
  2521. {
  2522. // search from right
  2523. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  2524. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  2525. {
  2526. other = ShmPsuGrouping->Layout[i];
  2527. if(ShmPsuGrouping->GroupCollection[other].Role == _GROLE_MASTER)
  2528. {
  2529. find = other != selfTarget ? true : false;
  2530. break;
  2531. }
  2532. }
  2533. }
  2534. return find;
  2535. }
  2536. // master: master group index
  2537. void MasterReleasePsuGroup(unsigned char master)
  2538. {
  2539. bool findOtherInCharging = false;
  2540. unsigned char slave = 0, member = 0, releaseTarget = 0;
  2541. unsigned char otherCharging[MAX_GROUP_QUANTITY] = {0};
  2542. unsigned char releaseList[MAX_GROUP_QUANTITY] = {0};
  2543. float releasePower[MAX_GROUP_QUANTITY] = {0};
  2544. unsigned short releaseCurrent[MAX_GROUP_QUANTITY] = {0};
  2545. PsuGroupPartner ReleaseMember;
  2546. unsigned short target = 0, oriAvailableCurrent = 0, oriCapabilityCurrent = 0, maxReleaseCurrent = 0;
  2547. float targetPower = 0, oriAvailablePower = 0, capabilityPower = 0, maxReleasePower = 0;
  2548. if(ShmChargerInfo->PsuGrouping.GroupCollection[master].Role != _GROLE_MASTER ||
  2549. ShmPsuGrouping->GroupCollection[master].Partner.Quantity == 0)
  2550. {
  2551. return;
  2552. }
  2553. target = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2554. targetPower = (((float)(target) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2555. oriAvailableCurrent = (int)chargingInfo[master]->AvailableChargingCurrent;
  2556. oriAvailablePower = (((float)oriAvailableCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2557. oriCapabilityCurrent = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  2558. capabilityPower = (((float)oriCapabilityCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2559. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2560. {
  2561. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2562. unsigned short current = 0;
  2563. PsuGroupPowerOffCheck(slave, &ReleaseMember);
  2564. otherCharging[i] = IsOtherCharging(slave);
  2565. findOtherInCharging = otherCharging[i] ? otherCharging[i] : findOtherInCharging;
  2566. releaseList[i] = slave;
  2567. for(int j = 0; j < ReleaseMember.Quantity; j++)
  2568. {
  2569. member = ReleaseMember.Member[j];
  2570. current += ShmPsuData->PsuGroup[member].GroupAvailableCurrent;
  2571. }
  2572. releaseCurrent[i] = current;
  2573. float power = (((float)(oriAvailableCurrent - releaseCurrent[i]) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2574. if(power >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  2575. (oriAvailableCurrent - releaseCurrent[i]) >= oriCapabilityCurrent)
  2576. {
  2577. releasePower[i] = power;
  2578. }
  2579. else
  2580. {
  2581. releasePower[i] = 0;
  2582. }
  2583. //LOG_INFO("Gun %d Release [%02X], Total Release %d Group, Gun Loading %d.%02d >> %d.%02d", master + 1, slave, ReleaseMember.Quantity,
  2584. // (originalLoading / 100), (originalLoading % 100),
  2585. // (releaseLoading[i] / 100), (releaseLoading[i] % 100));
  2586. }
  2587. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2588. {
  2589. if((otherCharging[i] || !findOtherInCharging) &&
  2590. releasePower[i] > maxReleasePower && releasePower[i] != 0)
  2591. {
  2592. maxReleasePower = releasePower[i];
  2593. maxReleaseCurrent = releaseCurrent[i];
  2594. releaseTarget = releaseList[i];
  2595. }
  2596. }
  2597. if(maxReleasePower > 0)
  2598. {
  2599. unsigned short _current = 0;
  2600. _current = oriAvailableCurrent - maxReleaseCurrent;
  2601. ShmPsuGrouping->GroupCollection[releaseTarget].GroupCtrl.bits.SlavePowerOffRequest = true;
  2602. LOG_INFO("Gun %d Set [%02X] Release(%s)",
  2603. master + 1, releaseTarget, findOtherInCharging ? "Other In Charging" : "Normal");
  2604. LOG_INFO("Gun %d Available %d A (%d kW) >> %d A (%d kW)",
  2605. master + 1,
  2606. (oriAvailableCurrent / 10), (int)oriAvailablePower, (_current / 10), (int)maxReleasePower);
  2607. LOG_INFO("Gun %d Capability %d A (%d kW) >> %d A (%d kW)",
  2608. master + 1,
  2609. (oriCapabilityCurrent / 10), (int)capabilityPower,
  2610. _current >= oriCapabilityCurrent ? (oriCapabilityCurrent / 10) : (_current / 10), (int)maxReleasePower);
  2611. }
  2612. }
  2613. // master: master group index
  2614. void MasterDynamicRelease(unsigned char master)
  2615. {
  2616. unsigned char slave = 0;
  2617. PsuGroupPartner ReleaseMember;
  2618. unsigned short target = 0, releaseCurrent = 0, originalAvailable = 0, originalCapability = 0;
  2619. float targetPower = 0, originalPower = 0, capabilityPower = 0, releasePower = 0;
  2620. if(ShmChargerInfo->PsuGrouping.GroupCollection[master].Role != _GROLE_MASTER ||
  2621. ShmPsuGrouping->GroupCollection[master].Partner.Quantity == 0)
  2622. {
  2623. return;
  2624. }
  2625. target = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2626. targetPower = (((float)(target) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2627. originalAvailable = (int)chargingInfo[master]->AvailableChargingCurrent;
  2628. originalCapability = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  2629. releaseCurrent = 0;
  2630. memset(&ReleaseMember, 0x00, sizeof(PsuGroupPartner));
  2631. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2632. {
  2633. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2634. releasePower = (((float)(originalAvailable - releaseCurrent - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2635. if(releasePower >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  2636. (originalAvailable - releaseCurrent - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) >= originalCapability)
  2637. {
  2638. releaseCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  2639. ReleaseMember.Member[ReleaseMember.Quantity] = slave;
  2640. ReleaseMember.Quantity++;
  2641. }
  2642. }
  2643. if(ReleaseMember.Quantity > 0)
  2644. {
  2645. char strMember[64];
  2646. memset(strMember, 0x00, sizeof(strMember));
  2647. for(int i = 0; i < ReleaseMember.Quantity; i++)
  2648. {
  2649. char strTemp[16];
  2650. sprintf(strTemp, " [%02X]", ReleaseMember.Member[i]);
  2651. strcat(strMember, strTemp);
  2652. }
  2653. PrepareToPowerOff(0, &ReleaseMember);
  2654. originalPower = (((float)originalAvailable / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2655. capabilityPower = (((float)originalCapability / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2656. releasePower = (((float)(originalAvailable - releaseCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2657. LOG_INFO("Gun %d Direct Release%s", master + 1, strMember);
  2658. LOG_INFO("Gun %d Available %d A (%d kW) >> %d A (%d kW)",
  2659. master + 1,
  2660. (originalAvailable / 10), (int)originalPower,
  2661. ((originalAvailable - releaseCurrent) / 10), (int)releasePower);
  2662. LOG_INFO("Gun %d Capability %d A (%d kW) >> %d A (%d kW)",
  2663. master + 1,
  2664. (originalCapability / 10), (int)capabilityPower,
  2665. ((originalAvailable - releaseCurrent) / 10), (int)releasePower);
  2666. }
  2667. }
  2668. void FindDeratingPriority(unsigned char master, PsuGroupPartner *tPartner)
  2669. {
  2670. unsigned char slave = 0;
  2671. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  2672. if((ShmChargerInfo->Control.MaxConnector - 1 - ShmPsuGrouping->GroupCollection[master].Location) > ShmPsuGrouping->GroupCollection[master].Location - 0)
  2673. {
  2674. for(int i = ShmChargerInfo->Control.MaxConnector - 1; i >= 0; i--)
  2675. {
  2676. slave = ShmPsuGrouping->Layout[i];
  2677. if(slave != master)
  2678. {
  2679. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master)
  2680. {
  2681. tPartner->Member[tPartner->Quantity++] = slave;
  2682. }
  2683. }
  2684. else
  2685. {
  2686. break;
  2687. }
  2688. }
  2689. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  2690. {
  2691. slave = ShmPsuGrouping->Layout[i];
  2692. if(slave != master)
  2693. {
  2694. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master)
  2695. {
  2696. tPartner->Member[tPartner->Quantity++] = slave;
  2697. }
  2698. }
  2699. else
  2700. {
  2701. break;
  2702. }
  2703. }
  2704. }
  2705. else
  2706. {
  2707. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  2708. {
  2709. slave = ShmPsuGrouping->Layout[i];
  2710. if(slave != master)
  2711. {
  2712. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master)
  2713. {
  2714. tPartner->Member[tPartner->Quantity++] = slave;
  2715. }
  2716. }
  2717. else
  2718. {
  2719. break;
  2720. }
  2721. }
  2722. for(int i = ShmChargerInfo->Control.MaxConnector - 1; i >= 0; i--)
  2723. {
  2724. slave = ShmPsuGrouping->Layout[i];
  2725. if(slave != master)
  2726. {
  2727. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master)
  2728. {
  2729. tPartner->Member[tPartner->Quantity++] = slave;
  2730. }
  2731. }
  2732. else
  2733. {
  2734. break;
  2735. }
  2736. }
  2737. }
  2738. }
  2739. // master: master group index
  2740. void CheckSmoothDerating(unsigned char master)
  2741. {
  2742. unsigned char slave = 0;
  2743. unsigned short target = 0, originalAvailable = 0, originalCapability = 0;
  2744. float targetPower = 0, releasePower = 0;
  2745. PsuGroupPartner releasePriority;
  2746. if(ShmChargerInfo->PsuGrouping.GroupCollection[master].Role != _GROLE_MASTER ||
  2747. ShmPsuGrouping->GroupCollection[master].Partner.Quantity == 0 ||
  2748. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating)
  2749. {
  2750. return;
  2751. }
  2752. target = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2753. targetPower = (((float)(target) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2754. originalAvailable = (int)chargingInfo[master]->AvailableChargingCurrent;
  2755. originalCapability = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  2756. FindDeratingPriority(master, &releasePriority);
  2757. for(int i = 0; i < releasePriority.Quantity; i++)
  2758. {
  2759. slave = releasePriority.Member[i];
  2760. releasePower = (((float)(originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2761. if(releasePower >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  2762. (originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) >= originalCapability)
  2763. {
  2764. SetSmoothDerating(master, slave);
  2765. LOG_INFO("Gun %d Set Smooth Derating Member: [%02X]", master + 1, slave);
  2766. break;
  2767. }
  2768. }
  2769. /*
  2770. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2771. {
  2772. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2773. releasePower = (((float)(originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  2774. if(releasePower >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  2775. (originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) >= originalCapability)
  2776. {
  2777. SetSmoothDerating(master, slave);
  2778. LOG_INFO("Gun %d Set Smooth Derating Member: [%02X]", master + 1, slave);
  2779. break;
  2780. }
  2781. }
  2782. */
  2783. }
  2784. void CheckReleaseOrExtend(unsigned char master)
  2785. {
  2786. if((ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation || Is_EnableReleaseAndExtendWhenForceCharging(master)) &&
  2787. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue == 0 &&
  2788. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue == 0)
  2789. {
  2790. if(ShmPsuGrouping->GroupCollection[master].DiffPower_Available >= MIN_RELEASE_POWER)
  2791. {
  2792. if(ShmPsuGrouping->GroupCollection[master].Partner.Quantity > 0 &&
  2793. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent &&
  2794. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  2795. {
  2796. #if ENABLE_SMOOTH_DERATING == 0
  2797. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2798. {
  2799. MasterReleasePsuGroup(master);
  2800. }
  2801. else
  2802. {
  2803. MasterDynamicRelease(master);
  2804. }
  2805. #else
  2806. CheckSmoothDerating(master);
  2807. #endif
  2808. }
  2809. }
  2810. else if(ShmPsuGrouping->GroupCollection[master].DiffPower_Capability <= MAX_DIFF_POWER_TO_EXTEND &&
  2811. ShmPsuGrouping->GroupCollection[master].DiffPower_PhysicalLimit >= MIN_DIFF_PHYSICAL_POWER)
  2812. {
  2813. int available = GetPsuGroupAvailable(master);
  2814. if(available > 0)
  2815. {
  2816. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendAvailable)
  2817. {
  2818. LOG_INFO("Gun %d Extend Capability Available", master + 1);
  2819. GetClockTime(&_ExtendCapability_time[master]);
  2820. }
  2821. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendAvailable = true;
  2822. }
  2823. }
  2824. }
  2825. }
  2826. bool IsPsuGroupOutputConfigDifferent(unsigned char group)
  2827. {
  2828. bool different = false;
  2829. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage != PreGroupOutput[group].GTargetVoltage ||
  2830. ShmPsuGrouping->GroupOutput[group].GTargetCurrent != PreGroupOutput[group].GTargetCurrent)
  2831. {
  2832. different = true;
  2833. }
  2834. PreGroupOutput[group].GTargetVoltage = ShmPsuGrouping->GroupOutput[group].GTargetVoltage;
  2835. PreGroupOutput[group].GTargetCurrent = ShmPsuGrouping->GroupOutput[group].GTargetCurrent;
  2836. return different;
  2837. }
  2838. // Gun X Demand Change -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  2839. // Master [XX] -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  2840. // Member [XX] -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  2841. void ShowPsuGroupOutputConfig(unsigned char master)
  2842. {
  2843. unsigned char slave = 0;
  2844. PSU_LOG("Gun %d Demand Change -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  2845. master + 1, (ShmPsuGrouping->GroupOutput[master].GTargetVoltage / 10),
  2846. ((int)chargingInfo[master]->EvBatterytargetCurrent), (((int)(chargingInfo[master]->EvBatterytargetCurrent * 10)) % 10),
  2847. ((int)chargingInfo[master]->AvailableChargingCurrent / 10), ((int)chargingInfo[master]->AvailableChargingCurrent % 10),
  2848. (ShmPsuGrouping->GroupCollection[master].GunLoading / 100), (ShmPsuGrouping->GroupCollection[master].GunLoading % 100));
  2849. PSU_LOG(" Master [%02X] -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  2850. master, (ShmPsuGrouping->GroupOutput[master].GTargetVoltage / 10),
  2851. (ShmPsuGrouping->GroupOutput[master].GTargetCurrent / 10), (ShmPsuGrouping->GroupOutput[master].GTargetCurrent % 10),
  2852. (ShmPsuData->PsuGroup[master].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[master].GroupAvailableCurrent % 10),
  2853. (ShmPsuGrouping->GroupOutput[master].OutputLoading / 100), (ShmPsuGrouping->GroupOutput[master].OutputLoading % 100));
  2854. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2855. {
  2856. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2857. PSU_LOG(" Member [%02X] -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  2858. slave, (ShmPsuGrouping->GroupOutput[slave].GTargetVoltage / 10),
  2859. (ShmPsuGrouping->GroupOutput[slave].GTargetCurrent / 10), (ShmPsuGrouping->GroupOutput[slave].GTargetCurrent % 10),
  2860. (ShmPsuData->PsuGroup[slave].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[slave].GroupAvailableCurrent % 10),
  2861. (ShmPsuGrouping->GroupOutput[slave].OutputLoading / 100), (ShmPsuGrouping->GroupOutput[slave].OutputLoading % 100));
  2862. }
  2863. }
  2864. void PsuGroupOutputConfigCheck(unsigned char master)
  2865. {
  2866. unsigned char slave = 0;
  2867. bool show = false;
  2868. show = IsPsuGroupOutputConfigDifferent(master);
  2869. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2870. {
  2871. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2872. if(IsPsuGroupOutputConfigDifferent(slave))
  2873. {
  2874. show = true;
  2875. }
  2876. }
  2877. if(show)
  2878. {
  2879. ShowPsuGroupOutputConfig(master);
  2880. }
  2881. }
  2882. void StepRecognition(unsigned char master, unsigned short voltage)
  2883. {
  2884. if(_GfdStep[master] == PREPARE_STEP_NONE && voltage > 0)
  2885. {
  2886. // gfd step
  2887. _GfdStep[master] = PREPARE_STEP_CABLE_CHECK;
  2888. LOG_INFO("Gun %d GFD Start Step", master + 1);
  2889. }
  2890. else if(_GfdStep[master] == PREPARE_STEP_CABLE_CHECK && voltage == 0)
  2891. {
  2892. // gfd done step
  2893. _GfdStep[master] = PREPARE_STEP_GFD_DONE;
  2894. LOG_INFO("Gun %d GFD Stop Step", master + 1);
  2895. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.CableCheckDone = true;
  2896. }
  2897. else if(_GfdStep[master] == PREPARE_STEP_GFD_DONE && voltage >= SAFETY_PRECHARGE_OFFSET)
  2898. {
  2899. // precharge step
  2900. _GfdStep[master] = PREPARE_STEP_PRECHARGE;
  2901. LOG_INFO("Gun %d PreCharge Step", master + 1);
  2902. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode = true;
  2903. }
  2904. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode)
  2905. {
  2906. // charging step
  2907. if(_GfdStep[master] != PREPARE_STEP_CHARGING)
  2908. {
  2909. LOG_INFO("Gun %d Charging Step", master + 1);
  2910. }
  2911. _GfdStep[master] = PREPARE_STEP_CHARGING;
  2912. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode)
  2913. {
  2914. if(_VoltageResumeCnt[master] < MAX_CHARGING_DELAY_COUNT)
  2915. {
  2916. int time = GetTimeoutValue(_ChargingDelay_time[master]) / mSEC_VAL;
  2917. if(time >= CHARGING_DELAY_INTERVAL)
  2918. {
  2919. GetClockTime(&_ChargingDelay_time[master]);
  2920. _VoltageResumeCnt[master]++;
  2921. }
  2922. }
  2923. else
  2924. {
  2925. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode = false;
  2926. }
  2927. }
  2928. }
  2929. }
  2930. // master: master group index
  2931. // group: self group index
  2932. void UpdatePsuGroupOutputConfig(unsigned char master)
  2933. {
  2934. unsigned char slave = 0;
  2935. unsigned short TargetVoltage = 0, TargetCurrent = 0, PresentTargetCurrent = 0;
  2936. unsigned short current = 0;
  2937. TargetVoltage = (int)(chargingInfo[master]->EvBatterytargetVoltage * 10);
  2938. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  2939. if(Is_OutputK1K2_Ready(master) || ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.CableCheckDone)
  2940. {
  2941. // update target voltage
  2942. StepRecognition(master, TargetVoltage);
  2943. #if PRECHARGE_OFFSET
  2944. // for safety test (inrush current)
  2945. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode)
  2946. {
  2947. GCTargetVoltage[master] = _GfdStep[master] == PREPARE_STEP_PRECHARGE ?
  2948. TargetVoltage - SAFETY_PRECHARGE_OFFSET : TargetVoltage;
  2949. }
  2950. else
  2951. {
  2952. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode)
  2953. {
  2954. GCTargetVoltage[master] = TargetVoltage > (GCTargetVoltage[master] + VOLTAGE_RESUME_STEP) ?
  2955. (GCTargetVoltage[master] + VOLTAGE_RESUME_STEP) : TargetVoltage;
  2956. }
  2957. else
  2958. {
  2959. GCTargetVoltage[master] = TargetVoltage;
  2960. }
  2961. }
  2962. #else
  2963. GCTargetVoltage[master] = TargetVoltage;
  2964. #endif
  2965. ShmPsuGrouping->TotalGroupOutput[master].GTargetVoltage = GCTargetVoltage[master];
  2966. // set master group output voltage
  2967. ShmPsuGrouping->GroupOutput[master].GTargetVoltage = GCTargetVoltage[master];
  2968. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2969. {
  2970. // set slave group output voltage
  2971. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2972. #if ONE_MODULE_OUTPUT
  2973. // for safety test (inrush current)
  2974. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode)
  2975. {
  2976. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = 0;
  2977. }
  2978. else
  2979. {
  2980. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = GCTargetVoltage[master];
  2981. }
  2982. #else
  2983. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = GCTargetVoltage[master];
  2984. #endif
  2985. }
  2986. // update target current
  2987. if(((ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity + 1) * ZERO_CURRENT) > TargetCurrent ||
  2988. !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode)
  2989. {
  2990. current = ZERO_CURRENT;
  2991. #if PSU_DEBUG_MSG
  2992. if(ShmPsuGrouping->GroupOutput[master].GTargetCurrent != current)
  2993. {
  2994. LOG_INFO("Gun %d Need Minimum Target Current = %d", master + 1, current);
  2995. }
  2996. #endif
  2997. ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent = current;
  2998. ShmPsuGrouping->GroupOutput[master].GTargetCurrent = current;
  2999. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3000. {
  3001. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3002. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE || ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  3003. {
  3004. #if ONE_MODULE_OUTPUT
  3005. // for safety test (inrush current)
  3006. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = 0;
  3007. #else
  3008. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent =
  3009. ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0 ? ZERO_CURRENT : 0;
  3010. #endif
  3011. ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent += ShmPsuGrouping->GroupOutput[slave].GTargetCurrent;
  3012. }
  3013. }
  3014. unsigned short diffCurrent = 0;
  3015. diffCurrent = ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent < TargetCurrent ? (TargetCurrent - ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent) : 0;
  3016. ShmPsuGrouping->GroupOutput[master].GTargetCurrent += diffCurrent;
  3017. }
  3018. else
  3019. {
  3020. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  3021. {
  3022. // master in normal output mode
  3023. PresentTargetCurrent = GetPresentTargetCurrent(master, _GROLE_MASTER);
  3024. current = TargetCurrent <= (int)chargingInfo[master]->AvailableChargingCurrent ?
  3025. TargetCurrent : (int)chargingInfo[master]->AvailableChargingCurrent;
  3026. #if PSU_DEBUG_MSG
  3027. if(GCTargetCurrent[master] != current)
  3028. {
  3029. LOG_INFO("Gun %d Need Normal Target Current = %d.%d A", master + 1, (current / 10), (current % 10));
  3030. }
  3031. #endif
  3032. ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent = current;
  3033. GCTargetCurrent[master] = current;
  3034. UpdateMaxCurrent(master, GCTargetCurrent[master]);
  3035. if(GCTargetCurrent[master] > PresentTargetCurrent)
  3036. {
  3037. // increase current
  3038. OutputConfigStep[master] = _CURRENT_MODE_INCREASE;
  3039. PSU_LOG("Gun %d Increase Current: %d.%d A, %d.%d A -> %d.%d A", master + 1,
  3040. ((GCTargetCurrent[master] - PresentTargetCurrent) / 10), ((GCTargetCurrent[master] - PresentTargetCurrent) % 10),
  3041. (PresentTargetCurrent / 10), (PresentTargetCurrent % 10), (GCTargetCurrent[master] / 10), (GCTargetCurrent[master] % 10));
  3042. #if MASTER_OUTPUT_FIRST
  3043. // for safety test
  3044. if(GCTargetCurrent[master] <= CURRENT_BALANCE_CRITERIA)
  3045. {
  3046. MasterIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  3047. }
  3048. else
  3049. {
  3050. AverageIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  3051. }
  3052. #else
  3053. AverageIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  3054. #endif
  3055. }
  3056. else if(GCTargetCurrent[master] < PresentTargetCurrent)
  3057. {
  3058. // decrease current
  3059. OutputConfigStep[master] = _CURRENT_MODE_DECREASE;
  3060. PSU_LOG("Gun %d Decrease Current: %d.%d A, %d.%d A -> %d.%d A", master + 1,
  3061. ((PresentTargetCurrent - GCTargetCurrent[master]) / 10), ((PresentTargetCurrent - GCTargetCurrent[master]) % 10),
  3062. (PresentTargetCurrent / 10), (PresentTargetCurrent % 10), (GCTargetCurrent[master] / 10), (GCTargetCurrent[master] % 10));
  3063. #if MASTER_OUTPUT_FIRST
  3064. // for safety test
  3065. if(GCTargetCurrent[master] <= ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity * CURRENT_BALANCE_CRITERIA)
  3066. {
  3067. MasterDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  3068. }
  3069. else
  3070. {
  3071. AverageDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  3072. }
  3073. #else
  3074. AverageDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  3075. #endif
  3076. }
  3077. else
  3078. {
  3079. // balance current or do not change
  3080. OutputConfigStep[master] = _CURRENT_MODE_BALANCE;
  3081. if(OutputConfigStep[master] != _preOutputConfigStep[master])
  3082. {
  3083. GetClockTime(&_ReachCurrent_time[master]);
  3084. PSU_LOG("Gun %d In Balance Mode", master + 1);
  3085. StableOutputCurrent[master] = (int)chargingInfo[master]->PresentChargingCurrent * 10;
  3086. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable = false;
  3087. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  3088. }
  3089. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable)
  3090. {
  3091. if(IsMasterOutputCurrentStable(master))
  3092. {
  3093. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable = true;
  3094. LOG_INFO("Gun %d Output Current = %5.1f A Stable", master + 1, chargingInfo[master]->PresentChargingCurrent);
  3095. }
  3096. }
  3097. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable)
  3098. {
  3099. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating)
  3100. {
  3101. CheckSmoothBalance(master);
  3102. }
  3103. else
  3104. {
  3105. #if MASTER_OUTPUT_FIRST
  3106. // for safety test
  3107. if(GCTargetCurrent[master] > ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity * CURRENT_BALANCE_CRITERIA)
  3108. {
  3109. CheckCurrentBalance(master);
  3110. }
  3111. else
  3112. {
  3113. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  3114. }
  3115. #else
  3116. CheckCurrentBalance(master);
  3117. #endif
  3118. }
  3119. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance &&
  3120. !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating)
  3121. {
  3122. CheckReleaseOrExtend(master);
  3123. }
  3124. }
  3125. }
  3126. }
  3127. else
  3128. {
  3129. // master in derating mode
  3130. OutputConfigStep[master] = _CURRENT_MODE_DERATING;
  3131. if(OutputConfigStep[master] != _preOutputConfigStep[master])
  3132. {
  3133. LOG_INFO("Gun %d In Derating Mode", master + 1);
  3134. }
  3135. }
  3136. _preOutputConfigStep[master] = OutputConfigStep[master];
  3137. }
  3138. }
  3139. else
  3140. {
  3141. ShmPsuGrouping->GroupOutput[master].GTargetVoltage = 0;
  3142. ShmPsuGrouping->GroupOutput[master].GTargetCurrent = 0;
  3143. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3144. {
  3145. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3146. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = 0;
  3147. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = 0;
  3148. }
  3149. }
  3150. }
  3151. // group: self group index
  3152. void SetPsuGroupOutput(unsigned char group)
  3153. {
  3154. int time = 0;
  3155. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  3156. {
  3157. #if ONE_MODULE_OUTPUT
  3158. // for safety test (inrush current)
  3159. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.InPrechargeMode)
  3160. {
  3161. SingleOutputVol(ShmPsuPosition->GroupLocationInfo[group].PsuSN[0], ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3162. }
  3163. else
  3164. {
  3165. PresentOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3166. }
  3167. #else
  3168. PresentOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3169. #endif
  3170. Await();
  3171. }
  3172. //UpdatePsuGroupLoading(group);
  3173. UpdateGunLoading(group);
  3174. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage >= PSU_MIN_VOL)
  3175. {
  3176. if(!isStartOutputSwitch[group])
  3177. {
  3178. SetPsuGroupPowerOnOff(group, PSU_POWER_ON);
  3179. GetClockTime(&_PoweOnOff_time[group]);
  3180. }
  3181. time = GetTimeoutValue(_PoweOnOff_time[group]) / uSEC_VAL;
  3182. if(time > POWER_ONOFF_RESEND_INTERVAL)
  3183. {
  3184. SetPsuGroupPowerOnOff(group, PSU_POWER_ON);
  3185. GetClockTime(&_PoweOnOff_time[group]);
  3186. }
  3187. }
  3188. else
  3189. {
  3190. if(isStartOutputSwitch[group])
  3191. {
  3192. SetPsuGroupPowerOnOff(group, PSU_POWER_OFF);
  3193. GetClockTime(&_PoweOnOff_time[group]);
  3194. }
  3195. time = GetTimeoutValue(_PoweOnOff_time[group]) / uSEC_VAL;
  3196. if(time > POWER_ONOFF_RESEND_INTERVAL)
  3197. {
  3198. SetPsuGroupPowerOnOff(group, PSU_POWER_OFF);
  3199. GetClockTime(&_PoweOnOff_time[group]);
  3200. }
  3201. }
  3202. }
  3203. // master: master group index
  3204. bool CanMasterStartDerating(unsigned char master)
  3205. {
  3206. bool start = false;
  3207. unsigned short TargetCurrent = 0;
  3208. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  3209. if(TargetCurrent <= ShmPsuGrouping->GroupCollection[master].ReAssignAvailableCurrent)
  3210. {
  3211. start = true;
  3212. }
  3213. return start;
  3214. }
  3215. // master: master group index
  3216. // role_condition: when role_condition = _GROLE_MASTER, stop all member
  3217. void SetMemberStartPowerOff(unsigned char master, unsigned char role_condition)
  3218. {
  3219. unsigned char slave = 0;
  3220. char strMember[64];
  3221. bool find = false;
  3222. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  3223. {
  3224. sprintf(strMember, "Gun %d Set Member:", master + 1);
  3225. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3226. {
  3227. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3228. if(role_condition == _GROLE_MASTER || ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  3229. {
  3230. SetPsuGroupRole(slave, _GROLE_SLAVE_POWER_OFF);
  3231. char strSlave[8];
  3232. sprintf(strSlave, " [%02X]", slave);
  3233. strcat(strMember, strSlave);
  3234. find = true;
  3235. }
  3236. }
  3237. if(find)
  3238. {
  3239. strcat(strMember, " Power Off");
  3240. LOG_INFO("%s", strMember);
  3241. }
  3242. }
  3243. }
  3244. // master: master group index
  3245. bool IsMemberPowerOffOK(unsigned char master)
  3246. {
  3247. bool done = true;
  3248. unsigned char slave = 0;
  3249. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3250. {
  3251. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3252. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE && ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SWITCH_OFF_OK)
  3253. {
  3254. done = false;
  3255. }
  3256. }
  3257. return done;
  3258. }
  3259. // master: master group index
  3260. void SetMemberPowerOffDone(unsigned char master)
  3261. {
  3262. unsigned char slave = 0;
  3263. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  3264. {
  3265. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3266. {
  3267. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3268. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SWITCH_OFF_OK)
  3269. {
  3270. SetPsuGroupRole(slave, _GROLE_WAIT_IDLE);
  3271. }
  3272. else if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  3273. {
  3274. // can not power off normally
  3275. SetPsuGroupRole(slave, _GROLE_TERMINATE);
  3276. }
  3277. }
  3278. }
  3279. }
  3280. unsigned char _ParallelRelay[MAX_GROUP_QUANTITY];
  3281. void SetPCabinetParallelRelay(unsigned short parallelConfig)
  3282. {
  3283. bool change = false;
  3284. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  3285. {
  3286. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  3287. {
  3288. change = false;
  3289. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DeviceStatus_Idle ||
  3290. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DeviceStatus_Alarm ||
  3291. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DeviceStatus_Charging)
  3292. {
  3293. for(int j = 0; j < GENERAL_GUN_QUANTITY; j++)
  3294. {
  3295. _ParallelRelay[j] = (parallelConfig & (1 << j)) > 0 ? YES : NO;
  3296. if(_ParallelRelay[i] != ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRelaySetting[i])
  3297. {
  3298. change = true;
  3299. }
  3300. }
  3301. if(change)
  3302. {
  3303. char strParallel[128];
  3304. sprintf(strParallel, "Set PCabinet %d Parallel Relay:", i + 1);
  3305. for(int j = 0; j < GENERAL_GUN_QUANTITY; j++)
  3306. {
  3307. char strState[8];
  3308. sprintf(strState, " %3s", _ParallelRelay[j] ? "ON" : "OFF");
  3309. strcat(strParallel, strState);
  3310. }
  3311. LOG_INFO("%s", strParallel);
  3312. }
  3313. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRelaySetting[i] = _ParallelRelay[i];
  3314. }
  3315. }
  3316. }
  3317. }
  3318. // master: master group index
  3319. // Yes_No: Yes: relay on, No: relay off
  3320. // role_condition: when role_condition = _GROLE_MASTER, parallel relay of all member do the action(on or off)
  3321. void SetParallelRelayOnOff(unsigned char master, unsigned char Yes_No, unsigned char role_condition)
  3322. {
  3323. //int ParallelConfig = 0;
  3324. unsigned char slave = 0;
  3325. PsuGroupPartner *partner;
  3326. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER ||
  3327. (ShmPsuGrouping->GroupCollection[master].Role == _GROLE_REQUEST_TO_CHARGING && Yes_No))
  3328. {
  3329. ShmPsuGrouping->GroupCollection[master].ParallelCheck = 0;
  3330. partner = role_condition == _GROLE_PRECHARGE_READY ? &ShmPsuGrouping->GroupCollection[master].PossibleMember : &ShmPsuGrouping->GroupCollection[master].Partner;
  3331. for(int i = 0; i < partner->Quantity; i++)
  3332. {
  3333. slave = partner->Member[i];
  3334. if(role_condition == _GROLE_MASTER || ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE)
  3335. {
  3336. //ParallelConfig = ShmPsuGrouping->GroupCollection[master].ParallelConfig[slave];
  3337. //if(ParallelConfig != 0)
  3338. //{
  3339. // ShmPsuGrouping->GroupCollection[master].ParallelCheck |= (1 << (ParallelConfig - 1));
  3340. //}
  3341. ShmPsuGrouping->GroupCollection[master].ParallelCheck |= partner->ParallelFlag[i];
  3342. }
  3343. }
  3344. if(Yes_No)
  3345. {
  3346. ShmPsuGrouping->ParallelRelayConfig.CtrlValue |= ShmPsuGrouping->GroupCollection[master].ParallelCheck;
  3347. }
  3348. else
  3349. {
  3350. ShmPsuGrouping->ParallelRelayConfig.CtrlValue &= ~ShmPsuGrouping->GroupCollection[master].ParallelCheck;
  3351. }
  3352. SetPCabinetParallelRelay(ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  3353. }
  3354. }
  3355. // master: master group index
  3356. bool IsParallelRelayConfirmed(unsigned char master)
  3357. {
  3358. bool confirmed = true;
  3359. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3360. {
  3361. if((1 << i) & ShmPsuGrouping->GroupCollection[master].ParallelCheck)
  3362. {
  3363. if((ShmPsuGrouping->ParallelRelayConfig.CtrlValue & (1 << i)) != (ShmPsuGrouping->ParallelRelayConfirmed.CtrlValue & (1 << i)))
  3364. {
  3365. confirmed = false;
  3366. break;
  3367. }
  3368. }
  3369. }
  3370. return confirmed;
  3371. }
  3372. // master: master group index
  3373. bool IsPossibleMemberReady(unsigned char master)
  3374. {
  3375. bool ready = true;
  3376. unsigned char slave = 0;
  3377. if(ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity > 0)
  3378. {
  3379. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  3380. {
  3381. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  3382. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_WAIT_SLAVE)
  3383. {
  3384. ready = false;
  3385. }
  3386. }
  3387. }
  3388. return ready;
  3389. }
  3390. // master: master group index
  3391. bool IsExtendPrechargeReady(unsigned char master)
  3392. {
  3393. bool ready = true;
  3394. unsigned char slave = 0;
  3395. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  3396. {
  3397. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  3398. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity == 0 || ShmPsuData->PsuGroup[slave].GroupAvailableCurrent == 0)
  3399. {
  3400. continue;
  3401. }
  3402. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_PREPARE_ATTACH_ON ||
  3403. ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage > (ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage + PRECHARGE_RANGE_VOLTAGE) ||
  3404. ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage < (ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage - PRECHARGE_OFFSET_VOLTAGE - PRECHARGE_RANGE_VOLTAGE))
  3405. {
  3406. ready = false;
  3407. }
  3408. }
  3409. return ready;
  3410. }
  3411. // master: master group index
  3412. void SetExtendPrechargeCompleted(unsigned char master)
  3413. {
  3414. unsigned char slave = 0;
  3415. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  3416. {
  3417. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  3418. SetPsuGroupRole(slave, _GROLE_PRECHARGE_READY);
  3419. }
  3420. }
  3421. // master: master group index
  3422. void SetExtendPrechargeStop(unsigned char master)
  3423. {
  3424. unsigned char slave = 0;
  3425. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  3426. {
  3427. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  3428. SetPsuGroupRole(slave, _GROLE_EXTEND_STOP);
  3429. }
  3430. memset(&ShmPsuGrouping->GroupCollection[master].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  3431. }
  3432. // master: master group index
  3433. void AddExtendMember(unsigned char master)
  3434. {
  3435. unsigned char slave = 0;
  3436. unsigned short parallelFlag = 0;
  3437. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  3438. {
  3439. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  3440. parallelFlag = ShmPsuGrouping->GroupCollection[master].PossibleMember.ParallelFlag[i];
  3441. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PRECHARGE_READY)
  3442. {
  3443. AddMember(slave, master, parallelFlag);
  3444. }
  3445. }
  3446. }
  3447. // group: self group index
  3448. void CheckChargingRequest(unsigned char group)
  3449. {
  3450. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_IDLE)
  3451. {
  3452. return;
  3453. }
  3454. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest)
  3455. {
  3456. SetPsuGroupRole(group, _GROLE_REQUEST_TO_CHARGING);
  3457. }
  3458. }
  3459. // group: self group index
  3460. void ChargingRequestProcess(unsigned char group)
  3461. {
  3462. int time = 0;
  3463. PsuGroupPartner partner;
  3464. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_REQUEST_TO_CHARGING)
  3465. {
  3466. return;
  3467. }
  3468. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequestConfirmed)
  3469. {
  3470. GetClockTime(&_ChargingRequest_time[group]);
  3471. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest = false;
  3472. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequestConfirmed = true;
  3473. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupShareCheck = true;
  3474. }
  3475. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupShareCheck && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareConfirmed)
  3476. {
  3477. // check is there psu group to grab
  3478. if(PsuGroupGrabCheck(group, &partner))
  3479. {
  3480. GetClockTime(&_ChargingRequest_time[group]);
  3481. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GrabGroupWait = true;
  3482. PrepareToPowerOff(group + 1, &partner);
  3483. memcpy(&ShmPsuGrouping->GroupCollection[group].PossibleMember, &partner, sizeof(PsuGroupPartner));
  3484. }
  3485. else
  3486. {
  3487. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone = true;
  3488. LOG_INFO("Gun %d Grab Nothing", group + 1);
  3489. }
  3490. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareConfirmed = true;
  3491. }
  3492. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GrabGroupWait && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone)
  3493. {
  3494. // wait until grab psu ready or timeout
  3495. bool ready = IsPossibleMemberReady(group);
  3496. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  3497. if(ready || time >= WAIT_GRAB_TIME)
  3498. {
  3499. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone = true;
  3500. LOG_INFO("Gun %d Grab %s", group + 1, ready ? "OK" : "Timeout");
  3501. }
  3502. }
  3503. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.FindGroupPartner)
  3504. {
  3505. GetClockTime(&_ChargingRequest_time[group]);
  3506. AddAvailableMember(group);
  3507. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.FindGroupPartner = true;
  3508. }
  3509. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.FindGroupPartner && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayOn)
  3510. {
  3511. // after a little delay, set parallel relay on
  3512. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  3513. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  3514. {
  3515. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  3516. GetClockTime(&_ChargingRequest_time[group]);
  3517. SetParallelRelayOnOff(group, YES, _GROLE_MASTER);
  3518. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayOn = true;
  3519. LOG_INFO("Gun %d Charging Request Set All Member Parallel Relay On 0x%02X -> 0x%02X", group + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  3520. }
  3521. }
  3522. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayOn && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed)
  3523. {
  3524. // wait until parallel relay on confirmed
  3525. bool confirmed = IsParallelRelayConfirmed(group);
  3526. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  3527. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  3528. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  3529. {
  3530. GetClockTime(&_ChargingRequest_time[group]);
  3531. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed = true;
  3532. LOG_INFO("Gun %d Charging Request Parallel Relay Confirmed %s", group + 1, confirmed ? "OK" : "Timeout");
  3533. }
  3534. }
  3535. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupingDone)
  3536. {
  3537. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupingDone = true;
  3538. }
  3539. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupingDone)
  3540. {
  3541. SetPsuGroupToMaster(group);
  3542. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue = 0;
  3543. LOG_INFO("Gun %d Grouping Completed", group + 1);
  3544. }
  3545. }
  3546. // master: master group index
  3547. void PsuGroupDeratingProcess(unsigned char master)
  3548. {
  3549. int time = 0;
  3550. // slave group set NeedDerating flag to start re-assign
  3551. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedDerating)
  3552. {
  3553. // reduce output current capability start
  3554. GetClockTime(&_PsuGroupDerating_time[master]);
  3555. unsigned short ReAssignCurrent = GetPresentTargetCurrent(master, _GROLE_SLAVE);
  3556. ShmPsuGrouping->GroupCollection[master].ReAssignAvailableCurrent = ReAssignCurrent;
  3557. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedDerating = false;
  3558. LOG_INFO("Gun %d DeratingConfirmed%s, ReAssignAvailableCurrent = %d.%d A",
  3559. master + 1,
  3560. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed ? " Again" : "",
  3561. (ReAssignCurrent / 10), (ReAssignCurrent % 10));
  3562. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  3563. {
  3564. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  3565. }
  3566. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed = true;
  3567. }
  3568. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingStart)
  3569. {
  3570. // wait until derating from ev or timeout
  3571. bool start = CanMasterStartDerating(master);
  3572. bool bypass = ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode ? false : true;
  3573. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  3574. if(start || time >= WAIT_EV_DERATING_TIMEOUT || bypass)
  3575. {
  3576. GetClockTime(&_PsuGroupDerating_time[master]);
  3577. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingStart = true;
  3578. LOG_INFO("Gun %d %s Start Derating%s", master + 1, start ? "Normal" : "Force", bypass ? " (Bypass)" : "");
  3579. SetMemberStartPowerOff(master, _GROLE_PREPARE_SWITCH_OFF);
  3580. if(!bypass)
  3581. {
  3582. // update stage max current and reset max current time
  3583. StageMaxCurrent[master] = start ?
  3584. (int)(chargingInfo[master]->EvBatterytargetCurrent * 10) :
  3585. ShmPsuGrouping->GroupCollection[master].ReAssignAvailableCurrent;
  3586. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  3587. GetClockTime(&_StageCurrent_time[master]);
  3588. }
  3589. }
  3590. }
  3591. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingStart && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone)
  3592. {
  3593. // wait until psu member power off ok or timeout
  3594. bool power_off_ok = IsMemberPowerOffOK(master);
  3595. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  3596. if(power_off_ok || time >= WAIT_SLAVE_POWER_OFF_TIMEOUT)
  3597. {
  3598. GetClockTime(&_PsuGroupDerating_time[master]);
  3599. SetMemberPowerOffDone(master);
  3600. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone = true;
  3601. LOG_INFO("Gun %d Set Derating Member Power Off %s", master + 1, power_off_ok ? "OK" : "Timeout");
  3602. }
  3603. }
  3604. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayOff)
  3605. {
  3606. // after a little delay, set parallel relay off
  3607. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  3608. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  3609. {
  3610. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  3611. GetClockTime(&_PsuGroupDerating_time[master]);
  3612. SetParallelRelayOnOff(master, NO, _GROLE_SWITCH_OFF_OK);
  3613. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayOff = true;
  3614. LOG_INFO("Gun %d Set Parallel Relay Off 0x%02X -> 0x%02X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  3615. }
  3616. }
  3617. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayOff && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed)
  3618. {
  3619. // wait until parallel relay off confirmed
  3620. bool confirmed = IsParallelRelayConfirmed(master);
  3621. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  3622. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  3623. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  3624. {
  3625. GetClockTime(&_PsuGroupDerating_time[master]);
  3626. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed = true;
  3627. LOG_INFO("Gun %d Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  3628. }
  3629. }
  3630. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingCompleted)
  3631. {
  3632. // remove all non group member
  3633. RemoveNonGroupMember(master);
  3634. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingCompleted = true;
  3635. }
  3636. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingCompleted)
  3637. {
  3638. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  3639. UpdateGunAvailableCapability(master);
  3640. LOG_INFO("Gun %d Derating Completed", master + 1);
  3641. }
  3642. }
  3643. void MasterStopChargingProcess(unsigned char master)
  3644. {
  3645. int time = 0;
  3646. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingRequest && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingConfirmed)
  3647. {
  3648. // set all member to power off
  3649. GetClockTime(&_StopCharging_time[master]);
  3650. SetMemberStartPowerOff(master, _GROLE_MASTER);
  3651. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingConfirmed = true;
  3652. }
  3653. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllPowerOffDone)
  3654. {
  3655. // wait until psu member power off ok or timeout
  3656. bool power_off_ok = IsMemberPowerOffOK(master);
  3657. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  3658. if(power_off_ok || time >= WAIT_SLAVE_POWER_OFF_TIMEOUT)
  3659. {
  3660. GetClockTime(&_StopCharging_time[master]);
  3661. SetMemberPowerOffDone(master);
  3662. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllPowerOffDone = true;
  3663. LOG_INFO("Gun %d Set All Member Power Off %s", master + 1, power_off_ok ? "OK" : "Timeout");
  3664. }
  3665. }
  3666. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllPowerOffDone && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayOff)
  3667. {
  3668. // after a little delay, set all member parallel relay off
  3669. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  3670. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  3671. {
  3672. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  3673. GetClockTime(&_StopCharging_time[master]);
  3674. SetParallelRelayOnOff(master, NO, _GROLE_MASTER);
  3675. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayOff = true;
  3676. LOG_INFO("Gun %d Set All Member Parallel Relay Off 0x%02X -> 0x%02X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  3677. }
  3678. }
  3679. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayOff && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed)
  3680. {
  3681. // wait until parallel relay off confirmed
  3682. bool confirmed = IsParallelRelayConfirmed(master);
  3683. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  3684. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  3685. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  3686. {
  3687. GetClockTime(&_StopCharging_time[master]);
  3688. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed = true;
  3689. LOG_INFO("Gun %d All Member Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  3690. }
  3691. }
  3692. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted)
  3693. {
  3694. // remove all non group member
  3695. RemoveNonGroupMember(master);
  3696. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted = true;
  3697. LOG_INFO("Gun %d All Member Stop Completed", master + 1);
  3698. }
  3699. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingCompleted)
  3700. {
  3701. // check self group power down or not
  3702. if(ShmPsuData->PsuGroup[master].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT)
  3703. {
  3704. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingCompleted = true;
  3705. }
  3706. }
  3707. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingCompleted)
  3708. {
  3709. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  3710. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  3711. SetPsuGroupToIdle(master);
  3712. LOG_INFO("Gun %d Stop Charging Completed", master + 1);
  3713. }
  3714. }
  3715. void SlaveStopChargingProcess(unsigned char slave)
  3716. {
  3717. int time = 0;
  3718. unsigned char master = 0;
  3719. PsuGroupPartner PowerOffMember, GrabMember;
  3720. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE)
  3721. {
  3722. return;
  3723. }
  3724. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  3725. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.CheckSlaveReady)
  3726. {
  3727. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.CheckSlaveReady = true;
  3728. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_REQUEST_TO_CHARGING)
  3729. {
  3730. LOG_INFO("Gun %d Need Wait Gun %d Grouping Completed", slave + 1, master + 1);
  3731. GetClockTime(&_CheckSlaveReady_time[slave]);
  3732. }
  3733. else if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER && ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  3734. {
  3735. LOG_INFO("Gun %d Need Wait Gun %d Derating Completed", slave + 1, master + 1);
  3736. GetClockTime(&_CheckSlaveReady_time[slave]);
  3737. }
  3738. else
  3739. {
  3740. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest = true;
  3741. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady = true;
  3742. LOG_INFO("Gun %d Need Power Off And Request To Charging", slave + 1);
  3743. }
  3744. }
  3745. // wait until master derating completed
  3746. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.CheckSlaveReady && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady)
  3747. {
  3748. time = GetTimeoutValue(_CheckSlaveReady_time[slave]) / uSEC_VAL;
  3749. if(time >= WAIT_SLAVE_READY_TIME ||
  3750. (ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed))
  3751. {
  3752. if(!(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed))
  3753. {
  3754. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  3755. LOG_INFO("Gun %d Wait Gun %d Timeout", slave + 1);
  3756. }
  3757. else
  3758. {
  3759. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady = true;
  3760. LOG_INFO("Gun %d Is Ready", slave + 1);
  3761. GetClockTime(&_CheckSlaveReady_time[slave]);
  3762. }
  3763. }
  3764. }
  3765. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest)
  3766. {
  3767. time = GetTimeoutValue(_CheckSlaveReady_time[slave]) / uSEC_VAL;
  3768. if(time >= WAIT_SLAVE_DELAY)
  3769. {
  3770. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest = true;
  3771. }
  3772. }
  3773. // self group, other slave or master set SlavePowerOffRequest flag to power off
  3774. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffConfirmed)
  3775. {
  3776. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  3777. {
  3778. PsuGroupPowerOffCheck(slave, &PowerOffMember);
  3779. }
  3780. else
  3781. {
  3782. PowerOffMember.Quantity = 1;
  3783. PowerOffMember.Member[0] = slave;
  3784. }
  3785. if(PowerOffMember.Quantity == 1 && ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest)
  3786. {
  3787. // grab here
  3788. if(PsuGroupGrabCheck(slave, &GrabMember))
  3789. {
  3790. for(int i = 0; i < GrabMember.Quantity; i++)
  3791. {
  3792. PowerOffMember.Member[PowerOffMember.Quantity++] = GrabMember.Member[i];
  3793. }
  3794. }
  3795. }
  3796. if(PowerOffMember.Quantity > 0)
  3797. {
  3798. unsigned char target = ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest ? (slave + 1) : 0;
  3799. PrepareToPowerOff(target, &PowerOffMember);
  3800. if(PowerOffMember.Quantity > 1 && ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest)
  3801. {
  3802. ShmPsuGrouping->GroupCollection[slave].PossibleMember.Quantity = PowerOffMember.Quantity - 1;
  3803. memcpy(ShmPsuGrouping->GroupCollection[slave].PossibleMember.Member, &PowerOffMember.Member[1], PowerOffMember.Quantity - 1);
  3804. }
  3805. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffConfirmed = true;
  3806. }
  3807. else
  3808. {
  3809. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  3810. }
  3811. }
  3812. }
  3813. void MasterExtendCapabilityProcess(unsigned char master)
  3814. {
  3815. int time = 0;
  3816. // stop extend capability process when derating or stop charging
  3817. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue != 0 ||
  3818. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingRequest)
  3819. {
  3820. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed)
  3821. {
  3822. SetExtendPrechargeStop(master);
  3823. }
  3824. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  3825. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  3826. return;
  3827. }
  3828. // start extend capability
  3829. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerRequest && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed)
  3830. {
  3831. GetClockTime(&_ExtendCapability_time[master]);
  3832. memset(&ShmPsuGrouping->GroupCollection[master].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  3833. FindPsuGroupPartner(master, (int)ShmPsuGrouping->GroupCollection[master].DiffPower_PhysicalLimit, &ShmPsuGrouping->GroupCollection[master].PossibleMember, _EXTEND_TYPE_POWER, ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit);
  3834. if(ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity > 0)
  3835. {
  3836. PrepareToExtendCapability(master + 1, &ShmPsuGrouping->GroupCollection[master].PossibleMember);
  3837. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed = true;
  3838. }
  3839. else
  3840. {
  3841. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  3842. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  3843. }
  3844. }
  3845. // set extend member precharge voltage
  3846. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone)
  3847. {
  3848. unsigned short voltage = ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage >= PRECHARGE_OFFSET_VOLTAGE ?
  3849. ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage - PRECHARGE_OFFSET_VOLTAGE : 0;
  3850. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  3851. {
  3852. unsigned char slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  3853. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0 && ShmPsuData->PsuGroup[slave].GroupAvailableCurrent > 0)
  3854. {
  3855. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = voltage;
  3856. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = ZERO_CURRENT;
  3857. }
  3858. }
  3859. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrecharge)
  3860. {
  3861. GetClockTime(&_ExtendCapability_time[master]);
  3862. LOG_INFO("Gun %d Set ExtendPrecharge Voltage %d.%d V", master + 1, (voltage / 10), (voltage % 10));
  3863. }
  3864. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrecharge = true;
  3865. }
  3866. // wait for extend member precharge ready
  3867. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrecharge && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone)
  3868. {
  3869. bool ready = IsExtendPrechargeReady(master);
  3870. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  3871. if(ready || time >= WAIT_PRECHARGE_TIME)
  3872. {
  3873. LOG_INFO("Gun %d ExtendPrecharge %s", master + 1, ready ? "Ready" : "Timeout");
  3874. if(ready)
  3875. {
  3876. GetClockTime(&_ExtendCapability_time[master]);
  3877. SetExtendPrechargeCompleted(master);
  3878. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone = true;
  3879. }
  3880. else
  3881. {
  3882. SetExtendPrechargeStop(master);
  3883. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  3884. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  3885. }
  3886. }
  3887. }
  3888. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayOn)
  3889. {
  3890. // after a little delay, set extend parallel relay on
  3891. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  3892. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  3893. {
  3894. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  3895. GetClockTime(&_ExtendCapability_time[master]);
  3896. SetParallelRelayOnOff(master, YES, _GROLE_PRECHARGE_READY);
  3897. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayOn = true;
  3898. LOG_INFO("Gun %d Set Extend Parallel Relay On 0x%02X -> 0x%02X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  3899. }
  3900. }
  3901. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayOn && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed)
  3902. {
  3903. // wait until extend parallel relay on confirmed
  3904. bool confirmed = IsParallelRelayConfirmed(master);
  3905. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  3906. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  3907. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  3908. {
  3909. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed = true;
  3910. LOG_INFO("Gun %d Extend Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  3911. }
  3912. }
  3913. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendCompleted)
  3914. {
  3915. AddExtendMember(master);
  3916. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendCompleted = true;
  3917. }
  3918. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendCompleted)
  3919. {
  3920. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  3921. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  3922. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable = false;
  3923. GetClockTime(&_ReachCurrent_time[master]);
  3924. UpdateGunAvailableCapability(master);
  3925. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  3926. GetClockTime(&_StageCurrent_time[master]);
  3927. LOG_INFO("Gun %d Extend Capability Completed", master + 1);
  3928. }
  3929. }
  3930. void ShowGunVoltageCurrent(unsigned char master)
  3931. {
  3932. unsigned short voltage = 0, current = 0, fireVoltage = 0;
  3933. unsigned short diffVoltage = 0, diffCurrent = 0;
  3934. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  3935. {
  3936. voltage = (int)(chargingInfo[master]->PresentChargingVoltage * 10);
  3937. current = (int)(chargingInfo[master]->PresentChargingCurrent * 10);
  3938. fireVoltage = chargingInfo[master]->FireChargingVoltage;
  3939. diffVoltage = voltage >= evseOutVol[master] ? voltage - evseOutVol[master] : evseOutVol[master] - voltage;
  3940. diffCurrent = current >= evseOutCur[master] ? current - evseOutCur[master] : evseOutCur[master] - current;
  3941. if(diffVoltage >= 10 || diffCurrent >= 10)
  3942. {
  3943. evseOutputDelay[master] = SHOW_OUTPUT_DELAY;
  3944. evseOutVol[master] = voltage;
  3945. evseOutCur[master] = current;
  3946. }
  3947. if(evseOutputDelay[master] != 0)
  3948. {
  3949. LOG_INFO("Gun %d NeedV: %4d V, NeedC: %3d A, OutputV: %4d.%d V, OutputC: %3d.%d A, FireV: %4d V",
  3950. master + 1, (int)chargingInfo[master]->EvBatterytargetVoltage, (int)chargingInfo[master]->EvBatterytargetCurrent,
  3951. (voltage / 10), (voltage % 10), (current / 10), (current % 10), fireVoltage / 10);
  3952. evseOutputDelay[master]--;
  3953. }
  3954. }
  3955. }
  3956. void PsuGroupControlProcess(void)
  3957. {
  3958. int time = 0;
  3959. unsigned char role = 0;
  3960. //unsigned short TargetVoltage = 0, TargetCurrent = 0;
  3961. for(byte group = 0; group < GENERAL_GUN_QUANTITY; group++)
  3962. {
  3963. role = ShmPsuGrouping->GroupCollection[group].Role;
  3964. switch(role)
  3965. {
  3966. case _GROLE_IDLE:
  3967. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  3968. {
  3969. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  3970. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue = 0;
  3971. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  3972. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  3973. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  3974. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  3975. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  3976. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = 0;
  3977. PSU_LOG("===== PSU Group[%02X] ===== Idle", group);
  3978. GetClockTime(&_PsuGroupRole_time[group]);
  3979. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  3980. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  3981. PreGroupOutput[group].GTargetVoltage = 0;
  3982. PreGroupOutput[group].GTargetCurrent = 0;
  3983. ShmPsuGrouping->TotalGroupOutput[group].GTargetVoltage = 0;
  3984. ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent = 0;
  3985. SetPsuGroupOutput(group);
  3986. }
  3987. if(isStartOutputSwitch[group])
  3988. {
  3989. SetPsuGroupPowerOnOff(group, PSU_POWER_OFF);
  3990. }
  3991. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue != 0 &&
  3992. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  3993. {
  3994. CheckChargingRequest(group);
  3995. }
  3996. break;
  3997. case _GROLE_MASTER:
  3998. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  3999. {
  4000. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4001. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  4002. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  4003. //ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  4004. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = 0;
  4005. PSU_LOG("===== PSU Group[%02X] ===== Master", group);
  4006. GetClockTime(&_PsuGroupRole_time[group]);
  4007. OutputConfigStep[group] = _CURRENT_MODE_NONE;
  4008. _preOutputConfigStep[group] = _CURRENT_MODE_NONE;
  4009. MaxCurrentDemand[group] = 0;
  4010. StageMaxCurrent[group] = 0;
  4011. _GfdStep[group] = PREPARE_STEP_NONE;
  4012. _VoltageResumeCnt[group] = 0;
  4013. }
  4014. if(!ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.AlreadyInChargingMode)
  4015. {
  4016. if(chargingInfo[group]->SystemStatus == S_CHARGING && Is_OutputK1K2_Ready(group))
  4017. {
  4018. LOG_INFO("Gun %d Enter Charging Mode", group + 1);
  4019. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.AlreadyInChargingMode = true;
  4020. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ReachMaxCurrentDemand = false;
  4021. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ReachMaxStageCurrent = false;
  4022. GetClockTime(&_MaxCurrent_time[group]);
  4023. GetClockTime(&_StageCurrent_time[group]);
  4024. GetClockTime(&_ChargingDelay_time[group]);
  4025. }
  4026. }
  4027. if((chargingInfo[group]->SystemStatus <= S_IDLE) ||
  4028. (chargingInfo[group]->SystemStatus >= S_TERMINATING && chargingInfo[group]->SystemStatus <= S_FAULT))
  4029. {
  4030. if(!ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  4031. {
  4032. LOG_INFO("Gun %d SystemStatus(%d) Need Stop Charging", group + 1, chargingInfo[group]->SystemStatus);
  4033. }
  4034. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest = true;
  4035. }
  4036. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue == 0)
  4037. {
  4038. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ExtendAvailable)
  4039. {
  4040. time = GetTimeoutValue(_ExtendCapability_time[group]) / uSEC_VAL;
  4041. if(time >= EXTEND_CAPABILITY_DELAY)
  4042. {
  4043. #if 0
  4044. int available = GetPsuGroupAvailable(group);
  4045. if(available > 0)
  4046. {
  4047. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = MAX_GROUP_QUANTITY;
  4048. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.MorePowerRequest = true;
  4049. LOG_INFO("Gun %d Start Extend Capability", group + 1);
  4050. }
  4051. #endif
  4052. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = 0;
  4053. int reason = IsExtendConflict(group);
  4054. char *str_conflict_reason[] = {STR_EXTEND_CONFLICT_NONE, STR_EXTEND_CONFLICT_AVAILABLE, STR_EXTEND_CONFLICT_AVERAGE, STR_EXTEND_CONFLICT_PRIORITY, STR_EXTEND_CONFLICT_UNKNOWN};
  4055. if(reason == _CONFLICT_NONE)
  4056. {
  4057. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.MorePowerRequest = true;
  4058. LOG_INFO("Gun %d Start Extend Capability", group + 1);
  4059. }
  4060. else
  4061. {
  4062. LOG_INFO("Gun %d Extend Capability Conflict: %s", group + 1, str_conflict_reason[reason]);
  4063. }
  4064. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ExtendAvailable = false;
  4065. }
  4066. }
  4067. }
  4068. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue != 0)
  4069. {
  4070. CleanSmoothDerating(group);
  4071. MasterExtendCapabilityProcess(group);
  4072. }
  4073. // need derating
  4074. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue != 0 &&
  4075. !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  4076. {
  4077. CleanSmoothDerating(group);
  4078. PsuGroupDeratingProcess(group);
  4079. }
  4080. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  4081. {
  4082. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  4083. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  4084. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  4085. ShmPsuGrouping->TotalGroupOutput[group].GTargetVoltage = 0;
  4086. ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent = 0;
  4087. CleanSmoothDerating(group);
  4088. MasterStopChargingProcess(group);
  4089. }
  4090. else
  4091. {
  4092. UpdateGunLoading(group);
  4093. UpdatePsuGroupOutputConfig(group);
  4094. UpdateMasterPsuGroupLoading(group);
  4095. PsuGroupOutputConfigCheck(group);
  4096. }
  4097. ShowGunVoltageCurrent(group);
  4098. SetPsuGroupOutput(group);
  4099. break;
  4100. case _GROLE_SLAVE:
  4101. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4102. {
  4103. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4104. PSU_LOG("===== PSU Group[%02X] ===== Slave", group);
  4105. GetClockTime(&_PsuGroupRole_time[group]);
  4106. }
  4107. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue != 0)
  4108. {
  4109. SlaveStopChargingProcess(group);
  4110. }
  4111. else
  4112. {
  4113. SetPsuGroupOutput(group);
  4114. }
  4115. break;
  4116. case _GROLE_PREPARE_SWITCH_OFF:
  4117. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4118. {
  4119. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4120. PSU_LOG("===== PSU Group[%02X] ===== Prepare Off", group);
  4121. GetClockTime(&_PsuGroupRole_time[group]);
  4122. }
  4123. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  4124. if(time >= MAX_PREPARE_SWITCH_OFF_TIME)
  4125. {
  4126. // timeout shall not happen
  4127. SetPsuGroupRole(group, _GROLE_SLAVE_POWER_OFF);
  4128. }
  4129. else
  4130. {
  4131. SetPsuGroupOutput(group);
  4132. }
  4133. break;
  4134. case _GROLE_SLAVE_POWER_OFF:
  4135. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4136. {
  4137. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4138. PSU_LOG("===== PSU Group[%02X] ===== Slave Power Off", group);
  4139. GetClockTime(&_PsuGroupRole_time[group]);
  4140. }
  4141. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  4142. if(time >= MIN_POWER_OFF_TIME &&
  4143. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT || _isTriggerShutdown))
  4144. {
  4145. SetPsuGroupRole(group, _GROLE_SWITCH_OFF_OK);
  4146. }
  4147. else
  4148. {
  4149. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  4150. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  4151. SetPsuGroupOutput(group);
  4152. }
  4153. break;
  4154. case _GROLE_SWITCH_OFF_OK:
  4155. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4156. {
  4157. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4158. PSU_LOG("===== PSU Group[%02X] ===== Switch Off OK, %d.%d V, %d.%d A", group,
  4159. (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10),
  4160. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent % 10));
  4161. GetClockTime(&_PsuGroupRole_time[group]);
  4162. }
  4163. break;
  4164. case _GROLE_WAIT_IDLE:
  4165. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4166. {
  4167. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4168. PSU_LOG("===== PSU Group[%02X] ===== Wait Idle, %d.%d V, %d.%d A", group,
  4169. (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10),
  4170. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent % 10));
  4171. GetClockTime(&_PsuGroupRole_time[group]);
  4172. }
  4173. break;
  4174. case _GROLE_WAIT_SLAVE:
  4175. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4176. {
  4177. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4178. PSU_LOG("===== PSU Group[%02X] ===== Wait Slave", group);
  4179. GetClockTime(&_PsuGroupRole_time[group]);
  4180. }
  4181. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  4182. if(ShmPsuGrouping->GroupCollection[group].ReservedTarget != 0)
  4183. {
  4184. if(time >= WAIT_SLAVE_TO_CHARGING && (ShmPsuGrouping->GroupCollection[group].ReservedTarget - 1) == group)
  4185. {
  4186. SetPsuGroupRole(group, _GROLE_REQUEST_TO_CHARGING);
  4187. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest = true;
  4188. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  4189. break;
  4190. }
  4191. }
  4192. if(time >= WAIT_SLAVE_TIMEOUT)
  4193. {
  4194. PSU_LOG("Group[%02X] Wait Slave Timeout", group);
  4195. SetPsuGroupToIdle(group);
  4196. }
  4197. break;
  4198. case _GROLE_PREPARE_ATTACH_ON:
  4199. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4200. {
  4201. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4202. PSU_LOG("===== PSU Group[%02X] ===== Prepare Attach On", group);
  4203. GetClockTime(&_PsuGroupRole_time[group]);
  4204. }
  4205. SetPsuGroupOutput(group);
  4206. break;
  4207. case _GROLE_PRECHARGE_READY:
  4208. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4209. {
  4210. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4211. PSU_LOG("===== PSU Group[%02X] ===== Precharge %d.%d V Ready",
  4212. group, (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10));
  4213. GetClockTime(&_PsuGroupRole_time[group]);
  4214. }
  4215. break;
  4216. case _GROLE_EXTEND_STOP:
  4217. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4218. {
  4219. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4220. PSU_LOG("===== PSU Group[%02X] ===== Extend Stop", group);
  4221. GetClockTime(&_PsuGroupRole_time[group]);
  4222. }
  4223. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  4224. if(time >= MIN_POWER_OFF_TIME ||
  4225. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT &&
  4226. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage <= MAX_PSU_POWER_OFF_VOLTAGE))
  4227. {
  4228. SetPsuGroupToIdle(group);
  4229. }
  4230. else
  4231. {
  4232. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  4233. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  4234. SetPsuGroupOutput(group);
  4235. }
  4236. break;
  4237. case _GROLE_REQUEST_TO_CHARGING:
  4238. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4239. {
  4240. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4241. PSU_LOG("===== PSU Group[%02X] ===== Request To Charging", group);
  4242. GetClockTime(&_PsuGroupRole_time[group]);
  4243. }
  4244. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue != 0)
  4245. {
  4246. ChargingRequestProcess(group);
  4247. }
  4248. break;
  4249. case _GROLE_TERMINATE:
  4250. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4251. {
  4252. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4253. PSU_LOG("===== PSU Group[%02X] ===== Terminate", group);
  4254. GetClockTime(&_PsuGroupRole_time[group]);
  4255. }
  4256. break;
  4257. case _GROLE_WAIT_TERMINATED:
  4258. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4259. {
  4260. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4261. PSU_LOG("===== PSU Group[%02X] ===== Wait Terminated", group);
  4262. GetClockTime(&_PsuGroupRole_time[group]);
  4263. }
  4264. break;
  4265. case _GROLE_NONE:
  4266. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4267. {
  4268. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4269. PSU_LOG("===== PSU Group[%02X] ===== None", group);
  4270. GetClockTime(&_PsuGroupRole_time[group]);
  4271. }
  4272. break;
  4273. default:
  4274. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  4275. {
  4276. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  4277. PSU_LOG("===== PSU Group[%02X] ===== Unknown Role = %d", group, role);
  4278. GetClockTime(&_PsuGroupRole_time[group]);
  4279. }
  4280. SetPsuGroupRole(group, _GROLE_NONE);
  4281. break;
  4282. }
  4283. }
  4284. }
  4285. // only for test & debug purpose
  4286. void PsuGroupOutputSimulation(void)
  4287. {
  4288. byte master = 0;
  4289. unsigned short pVoltage = 0, pCurrent = 0;
  4290. unsigned short gVoltage = 0, gCurrent = 0;
  4291. // psu output voltage & current simulation
  4292. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  4293. {
  4294. pVoltage = ShmPsuGrouping->GroupOutput[i].GTargetVoltage;
  4295. pCurrent = ShmPsuGrouping->GroupOutput[i].GTargetCurrent;
  4296. ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage = pVoltage;
  4297. ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent = pCurrent;
  4298. ShmPsuData->PsuGroup[i].GroupPresentOutputPower = (pVoltage * pCurrent) / 100 / 100;
  4299. }
  4300. // psu AvailableCurrent & GroupAvailablePower simulation
  4301. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  4302. {
  4303. if(ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage == 0)
  4304. {
  4305. ShmPsuData->PsuGroup[i].GroupAvailableCurrent = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity * 1000;
  4306. }
  4307. else
  4308. {
  4309. if(ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage <= 3000)
  4310. {
  4311. ShmPsuData->PsuGroup[i].GroupAvailableCurrent = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity * 1000;
  4312. }
  4313. else
  4314. {
  4315. ShmPsuData->PsuGroup[i].GroupAvailableCurrent = (30000 * 100 * ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity) / ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage;
  4316. }
  4317. }
  4318. ShmPsuData->PsuGroup[i].GroupAvailablePower = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity * 300;
  4319. }
  4320. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  4321. {
  4322. master = ShmChargerInfo->PsuGrouping.GroupCollection[i].TargetGroup;
  4323. if(master == 0)
  4324. {
  4325. // calculate PresentChargingVoltage and PresentChargingCurrent of self group
  4326. chargingInfo[i]->PresentChargingVoltage = (float)ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage / 10;
  4327. chargingInfo[i]->PresentChargingCurrent = (float)ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent / 10;
  4328. }
  4329. else
  4330. {
  4331. if(i == (master - 1))
  4332. {
  4333. gVoltage = ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage;
  4334. gCurrent = ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent;
  4335. for(byte j = 0; j < ShmPsuGrouping->GroupCollection[i].Partner.Quantity; j++)
  4336. {
  4337. byte slave = ShmPsuGrouping->GroupCollection[i].Partner.Member[j];
  4338. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  4339. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF ||
  4340. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  4341. {
  4342. gCurrent += ShmPsuData->PsuGroup[slave].GroupPresentOutputCurrent;
  4343. }
  4344. }
  4345. chargingInfo[i]->PresentChargingVoltage = (float)gVoltage / 10;
  4346. chargingInfo[i]->PresentChargingCurrent = (float)gCurrent / 10;
  4347. }
  4348. }
  4349. }
  4350. }
  4351. // only for test & debug purpose
  4352. void PsuGroupAvailableSimulation(void)
  4353. {
  4354. unsigned char master = 0;
  4355. int _groupCurrent = 0, _groupPower = 0;
  4356. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  4357. {
  4358. master = ShmChargerInfo->PsuGrouping.GroupCollection[i].TargetGroup;
  4359. // calculate output group capability
  4360. if(master == 0)
  4361. {
  4362. chargingInfo[i]->AvailableChargingCurrent = ShmPsuData->PsuGroup[i].GroupAvailableCurrent;
  4363. chargingInfo[i]->AvailableChargingPower = ShmPsuData->PsuGroup[i].GroupAvailablePower;
  4364. }
  4365. else
  4366. {
  4367. _groupCurrent = ShmPsuData->PsuGroup[master - 1].GroupAvailableCurrent;
  4368. _groupPower = ShmPsuData->PsuGroup[master - 1].GroupAvailablePower;
  4369. for(byte j = 0; j < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; j++)
  4370. {
  4371. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[j];
  4372. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  4373. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  4374. {
  4375. _groupCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  4376. _groupPower += ShmPsuData->PsuGroup[slave].GroupAvailablePower;
  4377. }
  4378. }
  4379. if(ShmPsuGrouping->GroupCollection[master - 1].GroupCtrl.bits.DeratingConfirmed)
  4380. {
  4381. chargingInfo[master - 1]->AvailableChargingCurrent = ShmPsuGrouping->GroupCollection[master - 1].ReAssignAvailableCurrent;
  4382. }
  4383. else
  4384. {
  4385. chargingInfo[master - 1]->AvailableChargingCurrent = _groupCurrent;
  4386. }
  4387. chargingInfo[master - 1]->AvailableChargingPower = _groupPower;
  4388. chargingInfo[master - 1]->MaximumChargingVoltage = 9500;
  4389. if((master - 1) != i)
  4390. {
  4391. chargingInfo[i]->AvailableChargingCurrent = 0;;
  4392. chargingInfo[i]->AvailableChargingPower = 0;
  4393. chargingInfo[i]->RealRatingPower = 0;
  4394. chargingInfo[i]->MaximumChargingVoltage = 0;
  4395. }
  4396. }
  4397. }
  4398. }
  4399. void CapabilitySimulation(void)
  4400. {
  4401. unsigned short voltage = 0, current = 0, power = 0;
  4402. unsigned short limitPower = 0, limitCurrent = 0;
  4403. unsigned char inUsingCnt = 0;
  4404. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4405. {
  4406. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  4407. {
  4408. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  4409. }
  4410. }
  4411. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  4412. {
  4413. voltage = chargingInfo[i]->MaximumChargingVoltage;
  4414. current = chargingInfo[i]->AvailableChargingCurrent;
  4415. power = chargingInfo[i]->AvailableChargingPower;
  4416. // ********** GetPhysicalLimitVoltageAndCurrent **********
  4417. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage != 0 &&
  4418. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage <= voltage)
  4419. {
  4420. voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage;
  4421. }
  4422. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent != 0 &&
  4423. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent <= current)
  4424. {
  4425. current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent;
  4426. }
  4427. // ********** GetModelNameLimitPower **********
  4428. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  4429. ShmChargerInfo->Control.ChargerRatingPower != 0)
  4430. {
  4431. limitPower = ShmChargerInfo->Control.ChargerRatingPower;
  4432. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4433. {
  4434. limitPower += ShmChargerInfo->ParallelCabinet.TotalParallelPower;
  4435. }
  4436. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity) / inUsingCnt;
  4437. }
  4438. if(limitPower != 0 && limitPower <= power)
  4439. {
  4440. power = limitPower;
  4441. }
  4442. // ********** GetConfigLimitPowerAndCurrent **********
  4443. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  4444. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  4445. {
  4446. limitPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  4447. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity) / inUsingCnt;
  4448. }
  4449. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].MaxTotalChargingPower = limitPower;
  4450. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  4451. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  4452. {
  4453. limitCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  4454. limitCurrent = (limitCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity) / inUsingCnt;
  4455. }
  4456. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].MaxTotalChargingCurrent = limitCurrent;
  4457. if(limitPower != 0 && limitPower <= power)
  4458. {
  4459. power = limitPower;
  4460. }
  4461. if(limitCurrent != 0 && limitCurrent <= current)
  4462. {
  4463. current = limitCurrent;
  4464. }
  4465. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  4466. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  4467. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  4468. }
  4469. }
  4470. void PsuGroupingInitial(void)
  4471. {
  4472. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  4473. {
  4474. ShmPsuGrouping->GroupCollection[i].Role = 0;
  4475. ShmPsuGrouping->GroupCollection[i].PreRole = 0xFF;
  4476. memset(&ShmPsuGrouping->GroupCollection[i].Partner, 0x00, sizeof(PsuGroupPartner));
  4477. memset(&ShmPsuGrouping->GroupCollection[i].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  4478. ShmPsuGrouping->GroupCollection[i].TargetGroup = 0;
  4479. ShmPsuGrouping->GroupCollection[i].ReservedTarget = 0;
  4480. memset(&ShmPsuGrouping->GroupCollection[i].GroupCtrl, 0x00, sizeof(PsuGroupControl));
  4481. ShmPsuGrouping->GroupCollection[i].ReAssignAvailableCurrent = 0;
  4482. ShmPsuGrouping->GroupCollection[i].ParallelCheck = 0;
  4483. ShmPsuGrouping->GroupCollection[i].GunLoading = 0;
  4484. memset(&ShmPsuGrouping->GroupOutput[i], 0x00, sizeof(GroupOutputConfigInfo));
  4485. }
  4486. }
  4487. void PsuCommLostCheck(void)
  4488. {
  4489. if(ShmPsuData->Work_Step != INITIAL_START &&
  4490. ShmPsuData->Work_Step != _NO_WORKING &&
  4491. ShmPsuData->Work_Step != _INIT_PSU_STATUS)
  4492. {
  4493. if(!isCommStart)
  4494. {
  4495. isCommStart = true;
  4496. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt = 0;
  4497. lostCnt = 0;
  4498. GetClockTime(&_PsuCommCheck_time);
  4499. }
  4500. if((GetTimeoutValue(_PsuCommCheck_time) / uSEC_VAL) >= PSU_COMM_CHECK_TIME)
  4501. {
  4502. ShmChargerInfo->Control.CommInfo.PsuComm.CommCnt = ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt;
  4503. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt = 0;
  4504. if(ShmChargerInfo->Control.CommInfo.PsuComm.CommCnt > COMM_LOST_CRITICAL)
  4505. {
  4506. if(ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost)
  4507. {
  4508. LOG_INFO("Psu Communication Recovery");
  4509. }
  4510. ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost = NO;
  4511. lostCnt = 0;
  4512. }
  4513. else
  4514. {
  4515. lostCnt++;
  4516. if(lostCnt >= LOST_CNT_CRITICAL)
  4517. {
  4518. if(!ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost)
  4519. {
  4520. LOG_INFO("Psu Communication Lost");
  4521. }
  4522. ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost = YES;
  4523. }
  4524. }
  4525. GetClockTime(&_PsuCommCheck_time);
  4526. }
  4527. }
  4528. else
  4529. {
  4530. isCommStart = false;
  4531. ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost = NO;
  4532. lostCnt = 0;
  4533. /*
  4534. if(ShmChargerInfo->Control.PsuCtrl.bits.Paused)
  4535. {
  4536. system("ifconfig can1 down");
  4537. sleep(1);
  4538. system("ifconfig can1 up");
  4539. sleep(1);
  4540. LOG_INFO("Psu Can Port Down & Up");
  4541. }
  4542. ShmChargerInfo->Control.PsuCtrl.bits.Paused = NO;
  4543. */
  4544. }
  4545. }
  4546. void InitialGunLimitSequence(void)
  4547. {
  4548. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  4549. {
  4550. _GunIndexOfPrioritySequence[i] = i;
  4551. }
  4552. }
  4553. bool IsShutdownTrigger(void)
  4554. {
  4555. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == YES ||
  4556. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency == YES)
  4557. {
  4558. if(!_isTriggerShutdown)
  4559. {
  4560. PSU_LOG("Psu Shutdown Flag Is Trigger!");
  4561. }
  4562. _isTriggerShutdown = true;
  4563. return true;
  4564. }
  4565. if(_isTriggerShutdown)
  4566. {
  4567. PSU_LOG("Psu Shutdown Flag Recover");
  4568. }
  4569. _isTriggerShutdown = false;
  4570. return false;
  4571. }
  4572. bool IsShutdownCompleted(void)
  4573. {
  4574. bool done = false;
  4575. if(IsShutdownTrigger())
  4576. {
  4577. done = true;
  4578. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4579. {
  4580. if(ShmPsuGrouping->GroupCollection[i].Role != _GROLE_IDLE)
  4581. {
  4582. done = false;
  4583. break;
  4584. }
  4585. }
  4586. if(done)
  4587. {
  4588. if(!_isShutdownCompleted)
  4589. {
  4590. PSU_LOG("Psu Shutdown Completed!");
  4591. }
  4592. _isShutdownCompleted = true;
  4593. }
  4594. return done;
  4595. }
  4596. if(_isShutdownCompleted)
  4597. {
  4598. PSU_LOG("Psu Shutdown Clear!");
  4599. }
  4600. _isShutdownCompleted = false;
  4601. return done;
  4602. }
  4603. int main(void)
  4604. {
  4605. byte _TotalModuleCount = 0;
  4606. byte _PrePsuWorkStep = 0;
  4607. byte isInitialComp = NO;
  4608. if(InitShareMemory() == FAIL)
  4609. {
  4610. #ifdef SystemLogMessage
  4611. LOG_ERROR("InitShareMemory NG");
  4612. #endif
  4613. if(ShmStatusCodeData != NULL)
  4614. {
  4615. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  4616. }
  4617. sleep(5);
  4618. return 0;
  4619. }
  4620. LOG_INFO("Psu Task PID = %d", getpid());
  4621. signal(SIGCHLD, SIG_IGN);
  4622. // register callback function
  4623. RefreshStatus(&GetStatusCallback);
  4624. RefreshModuleCount(&GetModuleCountCallback);
  4625. RefreshAvailableCap(&GetAvailableCapCallback);
  4626. RefreshFwVersion(&GetFwCallback);
  4627. RefreshInputVol(&GetInputVoltageCallback);
  4628. RefreshGetOutput(&GetPresentOutputCallback);
  4629. RefreshMisInfo(&GetMisCallback);
  4630. RefreshIavailable(&GetIavailableCallback);
  4631. RefreshGetOutputF(&GetPresentOutputFCallback);
  4632. // GetPresentOutputCallback & GetStatusCallback
  4633. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  4634. // GetStatusCallback
  4635. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  4636. // GetInputVoltageCallback
  4637. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  4638. sleep(2);
  4639. InitialGunLimitSequence();
  4640. _gunCount = GENERAL_GUN_QUANTITY;
  4641. _maxGroupCount = MAX_GROUP_QUANTITY;
  4642. _PrePsuWorkStep = _INIT_PSU_STATUS;
  4643. ShmPsuData->Work_Step = INITIAL_START;
  4644. isInitialComp = NO;
  4645. isCommStart = false;
  4646. _isTriggerShutdown = false;
  4647. _isShutdownCompleted = false;
  4648. LOG_INFO("PSU Work Status = %d", ShmPsuData->Work_Step);
  4649. // initial object
  4650. Initialization();
  4651. InitialPsuData();
  4652. //libInitialize = InitialCommunication();
  4653. Run_PsuRxCommProcess();
  4654. //main loop
  4655. while (1)
  4656. {
  4657. PsuCommLostCheck();
  4658. IsShutdownCompleted();
  4659. // 斷電狀態
  4660. if (ShmChargerInfo->Control.RelayCtrl.bits.AcContactor == NO ||
  4661. ShmChargerInfo->Control.PsuCtrl.bits.NeedSelfTest == YES ||
  4662. ShmChargerInfo->Control.PsuCtrl.bits.Paused == YES ||
  4663. _isShutdownCompleted)
  4664. {
  4665. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  4666. if (!isInitialComp)
  4667. {
  4668. _PrePsuWorkStep = _INIT_PSU_STATUS;
  4669. ShmPsuData->Work_Step = INITIAL_START;
  4670. psuCmdSeq = _PSU_CMD_STATUS;
  4671. _TotalModuleCount = 0;
  4672. PsuGroupingInitial();
  4673. sleep(2);
  4674. InitialPsuData();
  4675. isInitialComp = YES;
  4676. }
  4677. if(ShmChargerInfo->Control.PsuCtrl.bits.NeedSelfTest)
  4678. {
  4679. ShmChargerInfo->Control.PsuCtrl.bits.NeedSelfTest = NO;
  4680. ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK = NO;
  4681. memset(ShmChargerInfo->Control.PsuInitQuantity, 0x00, sizeof(ShmChargerInfo->Control.PsuInitQuantity));
  4682. LOG_INFO("Psu Need Execute Self Test!");
  4683. }
  4684. sleep(1);
  4685. continue;
  4686. }
  4687. else
  4688. {
  4689. if(isInitialComp)
  4690. {
  4691. system("ifconfig can1 down");
  4692. sleep(1);
  4693. system("ifconfig can1 up");
  4694. LOG_INFO("Psu Can Port Down & Up");
  4695. }
  4696. isInitialComp = NO;
  4697. }
  4698. // only for test & debug purpose
  4699. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  4700. {
  4701. ShmPsuData->Work_Step = _WORK_CHARGING;
  4702. ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity = 3;
  4703. ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity = 3;
  4704. ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity = 3;
  4705. ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity = 3;
  4706. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].RemoteMaxPhysicalVoltage = 9500;
  4707. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].RemoteMaxPhysicalCurrent = 5000;
  4708. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].RemoteMaxPhysicalVoltage = 9500;
  4709. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].RemoteMaxPhysicalCurrent = 2000;
  4710. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[2].RemoteMaxPhysicalVoltage = 9500;
  4711. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[2].RemoteMaxPhysicalCurrent = 5000;
  4712. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[3].RemoteMaxPhysicalVoltage = 9500;
  4713. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[3].RemoteMaxPhysicalCurrent = 2500;
  4714. PsuGroupOutputSimulation();
  4715. PsuGroupAvailableSimulation();
  4716. CapabilitySimulation();
  4717. }
  4718. // 自檢失敗
  4719. if (ShmPsuData->Work_Step == _NO_WORKING)
  4720. {
  4721. LOG_INFO("== PSU == self test fail.");
  4722. sleep(5);
  4723. }
  4724. if((GetTimeoutValue(_PsuReceiveRecoveryCheck_time) / uSEC_VAL) >= PSU_TASK_CHECK_TIME)
  4725. {
  4726. //PsuReceiveRecoveryCheck();
  4727. if(!Is_PsuRxComm_Alive())
  4728. {
  4729. Run_PsuRxCommProcess();
  4730. }
  4731. GetClockTime(&_PsuReceiveRecoveryCheck_time);
  4732. }
  4733. switch(ShmPsuData->Work_Step)
  4734. {
  4735. case INITIAL_START:
  4736. {
  4737. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  4738. {
  4739. _PrePsuWorkStep = ShmPsuData->Work_Step;
  4740. LOG_INFO("== PSU == INITIAL_START");
  4741. }
  4742. sleep(5);
  4743. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  4744. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  4745. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  4746. {
  4747. isStartOutputSwitch[index] = false;
  4748. }
  4749. ShmPsuData->Work_Step = GET_PSU_COUNT;
  4750. }
  4751. break;
  4752. case GET_PSU_COUNT:
  4753. {
  4754. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  4755. {
  4756. _PrePsuWorkStep = ShmPsuData->Work_Step;
  4757. LOG_INFO("== PSU == GET_PSU_COUNT");
  4758. GetClockTime(&_PsuWorkStep_time);
  4759. GetClockTime(&_cmdSubPriority_time);
  4760. }
  4761. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  4762. int interval = GetTimeoutValue(_cmdSubPriority_time) / uSEC_VAL;
  4763. if(interval > GET_PSU_COUNT_INTERVAL)
  4764. {
  4765. _TotalModuleCount = 0;
  4766. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  4767. {
  4768. // 總和各群模組數量
  4769. _TotalModuleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  4770. }
  4771. LOG_INFO("== PSU == indexCount = %d, moduleCount = %d, sysCount = %d",
  4772. ShmPsuData->GroupCount, _TotalModuleCount, ShmPsuData->SystemInitialPsuQuantity);
  4773. // 判斷系統數量與各群數量一致
  4774. if(_TotalModuleCount == ShmPsuData->SystemInitialPsuQuantity && _TotalModuleCount > 0 &&
  4775. time > GET_PSU_COUNT_TIME)
  4776. {
  4777. LOG_INFO("Psu Count = %d", _TotalModuleCount);
  4778. ShmPsuData->Work_Step = Get_PSU_LOCATION;
  4779. }
  4780. else
  4781. {
  4782. // 發送取得目前全部模組數量
  4783. GetModuleCount(SYSTEM_CMD);
  4784. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  4785. {
  4786. // 取各群模組數量
  4787. GetModuleCount(index);
  4788. }
  4789. }
  4790. GetClockTime(&_cmdSubPriority_time);
  4791. }
  4792. }
  4793. break;
  4794. case Get_PSU_LOCATION:
  4795. {
  4796. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  4797. {
  4798. _PrePsuWorkStep = ShmPsuData->Work_Step;
  4799. LOG_INFO("== PSU == Get_PSU_LOCATION");
  4800. // clean psu location info
  4801. memset(ShmPsuPosition, 0x00, sizeof(PsuPositionInfoData));
  4802. GetClockTime(&_cmdSubPriority_time);
  4803. }
  4804. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  4805. if(interval > GET_PSU_LOCATION_INTERVAL)
  4806. {
  4807. for(byte index = 0; index < ShmPsuData->GroupCount; index++)
  4808. {
  4809. if(ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity > 0)
  4810. {
  4811. // 取得狀態 : 支援模塊不須按照順序功能
  4812. GetStatus(index);
  4813. }
  4814. }
  4815. }
  4816. if(ShmPsuPosition->PsuLocationInit)
  4817. {
  4818. for(int i = 0; i < _maxGroupCount; i++)
  4819. {
  4820. UpdateGunTotalPsuQuantity(i);
  4821. #if 1
  4822. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  4823. {
  4824. ShowGroupMember(i);
  4825. }
  4826. #endif
  4827. }
  4828. //ShmPsuData->Work_Step = Get_PSU_VERSION;
  4829. ShmPsuData->Work_Step = PSU_COUNT_CONFIRM;
  4830. }
  4831. if(ShmPsuPosition->ReInitPsuLocation)
  4832. {
  4833. LOG_INFO("Retry Psu Location Initialization");
  4834. ShmPsuData->Work_Step = GET_PSU_COUNT;
  4835. }
  4836. }
  4837. break;
  4838. case PSU_COUNT_CONFIRM:
  4839. {
  4840. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  4841. {
  4842. _PrePsuWorkStep = ShmPsuData->Work_Step;
  4843. LOG_INFO("== PSU == PSU_COUNT_CONFIRM");
  4844. GetClockTime(&_PsuWorkStep_time);
  4845. GetClockTime(&_cmdSubPriority_time);
  4846. }
  4847. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  4848. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  4849. if(interval > PSU_COUNT_CONFIRM_INTERVAL)
  4850. {
  4851. _TotalModuleCount = 0;
  4852. for (byte index = 0; index < _maxGroupCount; index++)
  4853. {
  4854. // 總和各群模組數量
  4855. _TotalModuleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  4856. }
  4857. if(time > PSU_COUNT_CONFIRM_TIME)
  4858. {
  4859. if(_TotalModuleCount == ShmPsuData->SystemInitialPsuQuantity && _TotalModuleCount > 0)
  4860. {
  4861. ShmPsuData->Work_Step = Get_PSU_VERSION;
  4862. }
  4863. else
  4864. {
  4865. LOG_INFO("Total PSU = %d, System PSU Quantity = %d", _TotalModuleCount, ShmPsuData->SystemInitialPsuQuantity);
  4866. LOG_INFO("PSU Quantity Confirm Fail");
  4867. ShmPsuData->Work_Step = GET_PSU_COUNT;
  4868. }
  4869. break;
  4870. }
  4871. // 發送取得目前全部模組數量
  4872. GetModuleCount(SYSTEM_CMD);
  4873. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  4874. {
  4875. // 取各群模組數量
  4876. GetModuleCount(index);
  4877. }
  4878. GetClockTime(&_cmdSubPriority_time);
  4879. }
  4880. }
  4881. break;
  4882. case Get_PSU_VERSION:
  4883. {
  4884. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  4885. {
  4886. _PrePsuWorkStep = ShmPsuData->Work_Step;
  4887. LOG_INFO("== PSU == Get_PSU_VERSION");
  4888. GetClockTime(&_cmdSubPriority_time);
  4889. // clean version info
  4890. memset(ShmPsuData->PsuVersion, 0x00, sizeof(ShmPsuData->PsuVersion));
  4891. }
  4892. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  4893. if (interval > GET_PSU_VERSION_INTERVAL)
  4894. {
  4895. bool isGetVersion = true;
  4896. for(byte psu = 0; psu < ShmPsuData->SystemInitialPsuQuantity; psu++)
  4897. {
  4898. if (strcmp((char *)ShmPsuData->PsuVersion[psu].FwPrimaryVersion, "") == EQUAL)
  4899. {
  4900. isGetVersion = false;
  4901. break;
  4902. }
  4903. }
  4904. if(isGetVersion)
  4905. {
  4906. #if 1
  4907. for(int i = 0; i < _maxGroupCount; i++)
  4908. {
  4909. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  4910. {
  4911. ShowPsuVersion(i);
  4912. }
  4913. }
  4914. #endif
  4915. ShmPsuData->Work_Step = GET_SYS_CAP;
  4916. }
  4917. else
  4918. {
  4919. for(byte group = 0; group < _maxGroupCount; group++)
  4920. {
  4921. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  4922. {
  4923. // 取版號
  4924. GetModuleVer(group);
  4925. }
  4926. }
  4927. }
  4928. GetClockTime(&_cmdSubPriority_time);
  4929. }
  4930. }
  4931. break;
  4932. case GET_SYS_CAP:
  4933. {
  4934. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  4935. {
  4936. _PrePsuWorkStep = ShmPsuData->Work_Step;
  4937. LOG_INFO("== PSU == GET_SYS_CAP");
  4938. GetClockTime(&_PsuWorkStep_time);
  4939. GetClockTime(&_cmdSubPriority_time);
  4940. }
  4941. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  4942. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  4943. if (interval > GET_PSU_CAP_INTERVAL)
  4944. {
  4945. if(time > GET_PSU_CAP_TIME && ShmPsuData->SystemAvailablePower > 0 &&
  4946. ShmPsuData->SystemAvailableCurrent > 0)
  4947. {
  4948. #if 1
  4949. for(int i = 0; i < _maxGroupCount; i++)
  4950. {
  4951. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  4952. {
  4953. ShowGroupAvailableCurrentPower(i);
  4954. }
  4955. }
  4956. #endif
  4957. // 判斷系統輸出額定功率與電流
  4958. LOG_INFO("SystemAvailableCurrent = %d, SystemAvailablePower = %d",
  4959. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  4960. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  4961. }
  4962. else
  4963. {
  4964. for(byte index = 0; index < ShmPsuData->GroupCount; index++)
  4965. {
  4966. if(ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity > 0)
  4967. {
  4968. // 取系統總輸出能力
  4969. GetModuleCap(index);
  4970. }
  4971. }
  4972. }
  4973. GetClockTime(&_cmdSubPriority_time);
  4974. }
  4975. }
  4976. break;
  4977. case BOOTING_COMPLETE:
  4978. {
  4979. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  4980. {
  4981. _PrePsuWorkStep = ShmPsuData->Work_Step;
  4982. LOG_INFO("== PSU == BOOTING_COMPLETE");
  4983. }
  4984. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  4985. {
  4986. ShmPsuData->Work_Step = _WORK_CHARGING;
  4987. }
  4988. else
  4989. {
  4990. LOG_INFO("== PSU == Self Test OK!");
  4991. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  4992. {
  4993. ShmChargerInfo->Control.PsuInitQuantity[i] = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity;
  4994. }
  4995. }
  4996. ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK = true;
  4997. sleep(1);
  4998. }
  4999. break;
  5000. case _WORK_CHARGING:
  5001. {
  5002. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  5003. {
  5004. _PrePsuWorkStep = ShmPsuData->Work_Step;
  5005. LOG_INFO("== PSU == _WORK_CHARGING");
  5006. GetClockTime(&_PsuWorkStep_time);
  5007. GetClockTime(&_cmdSubPriority_time);
  5008. PsuGroupingInitial();
  5009. }
  5010. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  5011. // 低 Priority 的指令
  5012. if (time >= 1000)
  5013. {
  5014. GetModuleCount(SYSTEM_CMD);
  5015. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  5016. {
  5017. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  5018. {
  5019. GetStatus(i);
  5020. GetModuleCount(i);
  5021. GetModuleInput(i);
  5022. }
  5023. }
  5024. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  5025. {
  5026. UpdateGunTotalPsuQuantity(i);
  5027. }
  5028. GetClockTime(&_cmdSubPriority_time);
  5029. }
  5030. PsuGroupRoutineQuery();
  5031. PsuGroupControlProcess();
  5032. break;
  5033. }
  5034. case _TEST_MODE:
  5035. {
  5036. // 在測試模式中,保持與模塊的通訊
  5037. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  5038. if (time > 1500)
  5039. {
  5040. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  5041. {
  5042. // 取系統總輸出能力
  5043. GetModuleCap(index); Await();
  5044. // 取各群輸出電壓電流 (float)
  5045. GetModuleOutputF(index); Await();
  5046. }
  5047. GetClockTime(&_cmdSubPriority_time);
  5048. }
  5049. byte _switch = 0x00;
  5050. if ((chargingInfo[0]->EvBatterytargetVoltage * 10) > 0 && (chargingInfo[0]->EvBatterytargetCurrent * 10) > 0)
  5051. _switch = 0x01;
  5052. for (byte _groupCount_1 = 0; _groupCount_1 < conn_1_count; _groupCount_1++)
  5053. {
  5054. SetDirModulePresentOutput(connector_1[_groupCount_1],
  5055. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  5056. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  5057. _switch, _switch); Await();
  5058. }
  5059. for (byte _groupCount_2 = 0; _groupCount_2 < conn_2_count; _groupCount_2++)
  5060. {
  5061. SetDirModulePresentOutput(connector_2[_groupCount_2],
  5062. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  5063. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  5064. _switch, _switch); Await();
  5065. }
  5066. }
  5067. break;
  5068. case _INIT_PSU_STATUS:
  5069. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  5070. {
  5071. _PrePsuWorkStep = ShmPsuData->Work_Step;
  5072. LOG_INFO("== PSU == _INIT_PSU_STATUS");
  5073. GetClockTime(&_cmdSubPriority_time);
  5074. }
  5075. break;
  5076. default:
  5077. break;
  5078. }
  5079. usleep(20000);
  5080. }
  5081. return FAIL;
  5082. }