Module_PsuComm.c 220 KB

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