Module_PsuComm.c 280 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394
  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 PSU_TASK_CHECK_TIME 1
  18. #define PSU_COMM_CHECK_TIME 1
  19. #define GET_PSU_COUNT_INTERVAL 1 // unit: second
  20. #define GET_PSU_COUNT_TIME 13 // unit: second
  21. #define GET_PSU_LOCATION_INTERVAL 200 // unit: millisecond
  22. #define PSU_COUNT_CONFIRM_INTERVAL 200 // unit: millisecond
  23. #define PSU_COUNT_CONFIRM_TIME 1 // unit: second
  24. #define GET_PSU_VERSION_INTERVAL 200 // unit: millisecond
  25. #define GET_PSU_CAP_INTERVAL 200 // unit: millisecond
  26. #define GET_PSU_CAP_TIME 1 // unit: second
  27. #define STABLE_CURRENT_TOLERANCE 10 // unit: 0.1A, 1A
  28. #define MAX_STABLE_COUNT 24
  29. #define WAIT_EV_DERATING_TIMEOUT 5 // unit: second
  30. #define WAIT_SLAVE_POWER_OFF_TIMEOUT 5 // unit: second
  31. #define WAIT_PARALLEL_RELAY_DELAY 1 // unit: second
  32. #define WAIT_RELAY_CONFIRMED_TIME 3 // unit: second
  33. #define MAX_PREPARE_SWITCH_OFF_TIME 10 // unit: second
  34. #define MIN_POWER_OFF_TIME 1 // unit: second
  35. #define WAIT_SLAVE_TO_CHARGING 1 // unit: second
  36. #define WAIT_SLAVE_TIMEOUT 5 // unit: second
  37. #define WAIT_DUMMY_FAULT_TIME 3 // 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 200 // unit: 0.1V, 10V
  43. #define WAIT_PRECHARGE_TIME 10 // unit: second
  44. #define EXTEND_CAPABILITY_DELAY 10 // unit: second
  45. #define EXTEND_AFTER_DERATING_DELAY 3 // unit: second
  46. #define EXTEND_GRAB_INTERVAL 30 // unit: second
  47. #define EXTEND_LOADING 8000 // unit: 0.01%, 80%
  48. #define RELEASE_LOADING 5000 // unit: 0.01%, 50%
  49. #define RELEASE_LOADING_OFFSET 1000 // unit: 0.01%, 10%
  50. #define BALANCE_CURRENT_INTERVAL 100 // unit: 1ms, 100ms
  51. #define SMOOTH_DERATING_INTERVAL 100 // unit: 1ms, 100ms
  52. #define CURRENT_REACH_TARGET_TIME 5 // unit: 1s, 5s
  53. #define CURRENT_STABLE_TIME 5 // unit: 1s, 5s
  54. #define REACH_CURRENT_TOLERANCE 10 // unit: 0.1A, 1A
  55. #define MAX_ADJ_BALANCE_CURRENT 30 // unit: 0.1A, 3A
  56. #define START_BALANCE_CRITERIA 100 // unit: 0.01%, 1%
  57. #define STOP_BALANCE_CRITERIA 10 // unit: 0.01%, 0.1%
  58. #define EV_MAX_CURRENT_DELAY 60 // unit: second
  59. #define EV_MAX_STAGE_CURRENT_DELAY 30 // unit: second
  60. #define WAIT_SLAVE_READY_TIME 15 // unit: second
  61. #define WAIT_SLAVE_DELAY 1 // unit: second
  62. #define WAIT_DUMMY_MASTER_OFF 15 // unit: second
  63. #define SHOW_OUTPUT_DELAY 1
  64. #define CURRENT_BALANCE_CRITERIA 400 // unit: 0.1A, 40A
  65. #define SAFETY_PRECHARGE_OFFSET 200 // unit: 0.1V, 20V
  66. #define CHARGING_DELAY_INTERVAL 200 // unit: 1ms, 400ms
  67. #define MAX_CHARGING_DELAY_COUNT 15
  68. #define VOLTAGE_RESUME_STEP 10 // unit: 0.1V, 1V
  69. #define POWER_ONOFF_RESEND_INTERVAL 1 // unit: 1s, 1s
  70. #define COMM_LOST_CRITICAL 0
  71. #define LOST_CNT_CRITICAL 10
  72. #define PSU_CLEAN_ERR_INTERVAL 3 // unit: 1s, 3s
  73. #if SAFETY_TEST_ENABLE
  74. #define PRECHARGE_OFFSET 1 // 0: normal output, 1: precharge voltage offset enable
  75. #define ONE_MODULE_OUTPUT 1 // 0: normal output, 1: only one module output enable
  76. #define MASTER_OUTPUT_FIRST 0 // 0: normal output, 1: master output first enable
  77. #else
  78. #define PRECHARGE_OFFSET 0 // 0: normal output, 1: precharge voltage offset enable
  79. #define ONE_MODULE_OUTPUT 0 // 0: normal output, 1: only one module output enable
  80. #define MASTER_OUTPUT_FIRST 0 // 0: normal output, 1: master output first enable
  81. #endif
  82. #define MIN_RELEASE_POWER 30 // unit: 1kw
  83. #define MAX_DIFF_POWER_TO_DERATING 5 // unit: 1kw
  84. #define MAX_DIFF_POWER_TO_EXTEND 3 // unit: 1kw
  85. #define MIN_DIFF_PHYSICAL_POWER 3 // unit: 1kw
  86. #define MIN_DIFF_CONFIG_POWER 3 // unit: 1kw
  87. #define PSU_DEBUG_MSG 0
  88. #define ENABLE_SMOOTH_DERATING 1
  89. #define PSU_INDICATION_TIME 10 // unit: 1s
  90. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  91. struct StatusCodeData *ShmStatusCodeData;
  92. struct PsuData *ShmPsuData;
  93. ChargerInfoData *ShmChargerInfo;
  94. PsuPositionInfoData *ShmPsuPosition;
  95. PsuGroupingInfoData *ShmPsuGrouping;
  96. bool libInitialize = false;
  97. byte _gunCount = 0;
  98. byte _maxGroupCount = 0;
  99. byte getAvailableCapOffset = 5;
  100. byte deratingKeepCount = 0;
  101. byte psuCmdSeq = _PSU_CMD_CAP;
  102. bool psuReceiveRecovery = false;
  103. bool isCommStart = false;
  104. int lostCnt = 0;
  105. bool _isTriggerShutdown = false;
  106. bool _isShutdownCompleted = false;
  107. pid_t _RxPsuPid = 0;
  108. unsigned short evseOutVol[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  109. unsigned short evseOutCur[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  110. unsigned short evseOutputDelay[CONNECTOR_QUANTITY] = {0, 0, 0, 0};
  111. struct timespec _PsuReceiveRecoveryCheck_time;
  112. struct timespec _PsuWorkStep_time;
  113. struct timespec _cmdSubPriority_time;
  114. struct timespec _PsuGroupRole_time[CONNECTOR_QUANTITY];
  115. struct timespec _ChargingRequest_time[CONNECTOR_QUANTITY];
  116. struct timespec _DummyMasterRequest_time[CONNECTOR_QUANTITY];
  117. struct timespec _PsuGroupDerating_time[CONNECTOR_QUANTITY];
  118. struct timespec _StopCharging_time[CONNECTOR_QUANTITY];
  119. struct timespec _ExtendCapability_time[CONNECTOR_QUANTITY];
  120. struct timespec _ReachCurrent_time[CONNECTOR_QUANTITY];
  121. struct timespec _BalanceCurrent_time[CONNECTOR_QUANTITY];
  122. struct timespec _MaxCurrent_time[CONNECTOR_QUANTITY];
  123. struct timespec _StageCurrent_time[CONNECTOR_QUANTITY];
  124. struct timespec _CheckSlaveReady_time[CONNECTOR_QUANTITY];
  125. struct timespec _ChargingDelay_time[CONNECTOR_QUANTITY];
  126. struct timespec _PoweOnOff_time[CONNECTOR_QUANTITY];
  127. struct timespec _PsuCommCheck_time;
  128. struct timespec _SmoothDerating_time[CONNECTOR_QUANTITY];
  129. struct timespec _CleanPsuAlarm_time[CONNECTOR_QUANTITY];
  130. struct timespec _ExtendGrab_time[CONNECTOR_QUANTITY];
  131. unsigned short GCTargetVoltage[CONNECTOR_QUANTITY];
  132. unsigned short GCTargetCurrent[CONNECTOR_QUANTITY];
  133. unsigned short StableOutputCurrent[CONNECTOR_QUANTITY];
  134. unsigned short MaxCurrentDemand[CONNECTOR_QUANTITY];
  135. unsigned short StageMaxCurrent[CONNECTOR_QUANTITY];
  136. GroupOutputConfigInfo PreGroupOutput[MAX_GROUP_QUANTITY];
  137. unsigned char OutputConfigStep[MAX_GROUP_QUANTITY];
  138. unsigned char _preOutputConfigStep[MAX_GROUP_QUANTITY];
  139. unsigned char _GfdStep[MAX_GROUP_QUANTITY];
  140. unsigned char _VoltageResumeCnt[MAX_GROUP_QUANTITY];
  141. unsigned char _GunIndexOfPrioritySequence[MAX_GROUP_QUANTITY];
  142. PsuStatusInfo _LocalPsuStatus[MAX_PSU_MODULE_QUANTITY];
  143. bool _Psu_Indication_Enable[MAX_PSU_MODULE_QUANTITY];
  144. int _Psu_Indication_Count[MAX_PSU_MODULE_QUANTITY];
  145. int GetPsuModuleQuantity(unsigned char group);
  146. //=================================
  147. // Common routine
  148. //=================================
  149. bool Is_OutputK1K2_Ready(int gunIndex)
  150. {
  151. bool ready = false;
  152. if(gunIndex < CONNECTOR_QUANTITY)
  153. {
  154. if(chargingInfo[gunIndex]->RelayK1K2Status || ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  155. {
  156. ready = true;
  157. }
  158. }
  159. return ready;
  160. }
  161. // default is return enable
  162. bool Is_EnableReleaseAndExtendWhenForceCharging(int gunIndex)
  163. {
  164. if(ShmChargerInfo->Control.FCharging[gunIndex].FCtrl.bits.EnableForceCharging)
  165. {
  166. return ShmChargerInfo->Control.FCharging[gunIndex].FCtrl.bits.EnableReleaseAndExtend;
  167. }
  168. return true;
  169. }
  170. // return psu group number
  171. // return -1 when out of TotalPsuQuantity
  172. int FindTargetGroup(byte address)
  173. {
  174. return address < ShmPsuPosition->TotalPsuQuantity ? ShmPsuPosition->PsuAddressInfo[address].GroupNo : -1;
  175. }
  176. // return psu index in the group
  177. // return -1 when out of TotalPsuQuantity
  178. int FindGroupIndex(byte address)
  179. {
  180. return address < ShmPsuPosition->TotalPsuQuantity ? ShmPsuPosition->PsuAddressInfo[address].GIndex : -1;
  181. }
  182. int FindPsuIndex(byte group, byte gIndex)
  183. {
  184. int psuIndex = 0;
  185. for(int i = 0; i < _maxGroupCount; i++)
  186. {
  187. if(i < group)
  188. {
  189. psuIndex += ShmChargerInfo->Control.PsuInitQuantity[i];
  190. }
  191. else
  192. {
  193. break;
  194. }
  195. }
  196. psuIndex += gIndex;
  197. return psuIndex;
  198. }
  199. void PsuGroupOnOff(byte group, byte on_off)
  200. {
  201. if(on_off == _PSU_ON)
  202. {
  203. SwitchPower_On(group);
  204. }
  205. else
  206. {
  207. SwitchPower_Off(group);
  208. }
  209. }
  210. void PsuSingleOnOff(byte address, byte on_off)
  211. {
  212. if(on_off == _PSU_ON)
  213. {
  214. SinglePsuPower_On(address);
  215. }
  216. else
  217. {
  218. SinglePsuPower_Off(address);
  219. }
  220. }
  221. void LedGroupOnOff(byte group, byte on_off)
  222. {
  223. if(on_off == _PSU_ON)
  224. {
  225. FlashLed_Blinking(group);
  226. }
  227. else
  228. {
  229. FlashLed_Static(group);
  230. }
  231. }
  232. void LedSingleOnOff(byte address, byte on_off)
  233. {
  234. if(on_off == _PSU_ON)
  235. {
  236. SingleFlashLed_Blinking(address);
  237. }
  238. else
  239. {
  240. SingleFlashLed_Static(address);
  241. }
  242. }
  243. //=================================
  244. // Save data to share memory Function
  245. //=================================
  246. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  247. {
  248. if(GENERAL_GUN_QUANTITY > 0 && target < GENERAL_GUN_QUANTITY)
  249. {
  250. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].GeneralChargingData;
  251. return true;
  252. }
  253. return false;
  254. }
  255. //=================================
  256. // Alarm code mapping to share memory Function
  257. //=================================
  258. // 檢查 Byte 中某個 Bit 的值
  259. // _byte : 欲改變的 byte
  260. // _bit : 該 byte 的第幾個 bit
  261. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  262. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  263. {
  264. return ( _byte & mask_table[_bit] ) != 0x00;
  265. }
  266. void AbnormalStopAnalysis(byte gun_index, int errCode)
  267. {
  268. for (char i = 0; i < 4; i++)
  269. {
  270. unsigned char byteIndex = (errCode >> (8 * i)) & 0xff;
  271. for (char bitIndex = 0; bitIndex < 8; bitIndex++)
  272. {
  273. if(DetectBitValue(byteIndex , bitIndex) == 1)
  274. {
  275. switch(i)
  276. {
  277. case 0:
  278. {
  279. // err 1
  280. if (bitIndex == 2)
  281. {
  282. // 012307
  283. // fuse burn-out
  284. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFuseBurnOut = YES;
  285. }
  286. else if (bitIndex == 3)
  287. {
  288. // 012308
  289. // Communication fault between PFC and DCDC
  290. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcAndDcdcCommFault = YES;
  291. }
  292. else if (bitIndex == 6)
  293. {
  294. // 012309
  295. // Unbalance positive and negative BUS voltage
  296. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageUnbalance = YES;
  297. }
  298. else if (bitIndex == 7)
  299. {
  300. // 012310
  301. // BUS over voltage
  302. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusOverVoltage = YES;
  303. }
  304. }
  305. break;
  306. case 1:
  307. {
  308. // err 2
  309. if (bitIndex == 0)
  310. {
  311. // 012311
  312. // BUS voltage abnormal
  313. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageAbnormal = YES;
  314. }
  315. else if (bitIndex == 1)
  316. {
  317. // 012270
  318. // Over voltage of any phase
  319. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = YES;
  320. }
  321. else if (bitIndex == 2)
  322. {
  323. // 012263
  324. // ID number repetition
  325. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = YES;
  326. }
  327. else if (bitIndex == 3)
  328. {
  329. // 012312
  330. // BUS under voltage
  331. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusUnderVoltage = YES;
  332. }
  333. else if (bitIndex == 4)
  334. {
  335. // 012313
  336. // Phase loss
  337. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = YES;
  338. }
  339. else if (bitIndex == 6)
  340. {
  341. // 012271
  342. // Under voltage of any phase
  343. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  344. }
  345. }
  346. break;
  347. case 2:
  348. {
  349. // err3
  350. if (bitIndex == 0)
  351. {
  352. // 012240
  353. // CAN communication fault
  354. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCommunicationFail = YES;
  355. }
  356. else if (bitIndex == 1)
  357. {
  358. // 012275
  359. // DCDC uneven current sharing
  360. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuSevereUnevenCurrent = YES;
  361. }
  362. else if (bitIndex == 3)
  363. {
  364. // 012278
  365. // PFC power off
  366. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFfcSideShutDown = YES;
  367. }
  368. else if (bitIndex == 5)
  369. {
  370. // 012314
  371. // Full speed of fan
  372. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFullSpeed = YES;
  373. }
  374. else if (bitIndex == 6)
  375. {
  376. // 012266
  377. // DCDC power off
  378. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = YES;
  379. }
  380. else if (bitIndex == 7)
  381. {
  382. // 012273
  383. // Module unders power limiting status
  384. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = YES;
  385. }
  386. }
  387. break;
  388. case 3:
  389. {
  390. // err 4
  391. if (bitIndex == 0)
  392. {
  393. // 012315
  394. // Temperature power limiting
  395. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuTemperaturePowerLimit = YES;
  396. }
  397. else if (bitIndex == 1)
  398. {
  399. // 012316
  400. // AC power limiting
  401. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuAcPowerLimit = YES;
  402. }
  403. else if (bitIndex == 2)
  404. {
  405. // 012317
  406. // DCDC eeprom faults
  407. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcEepromFault = YES;
  408. }
  409. else if (bitIndex == 3)
  410. {
  411. // 012269
  412. // Fan faults
  413. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = YES;
  414. }
  415. else if (bitIndex == 4)
  416. {
  417. // 012264
  418. // DCDC short circuit
  419. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = YES;
  420. }
  421. else if (bitIndex == 5)
  422. {
  423. // 012318
  424. // PFC eeprom faults
  425. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcEepromFault = YES;
  426. }
  427. else if (bitIndex == 6)
  428. {
  429. // 012226
  430. // DCDC over temperature
  431. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = YES;
  432. }
  433. else if (bitIndex == 7)
  434. {
  435. // 012319
  436. // DCDC output over voltage
  437. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = YES;
  438. }
  439. }
  440. break;
  441. }
  442. }
  443. // else
  444. // {
  445. // switch (byteIndex) {
  446. // case 0: {
  447. // if (bitIndex == 0)
  448. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  449. // else if (bitIndex == 5)
  450. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  451. // }
  452. // break;
  453. // case 1: {
  454. // if (bitIndex == 1)
  455. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  456. // else if (bitIndex == 2)
  457. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  458. // else if (bitIndex == 3)
  459. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  460. // else if (bitIndex == 4)
  461. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  462. // else if (bitIndex == 5)
  463. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  464. // }
  465. // break;
  466. // case 2: {
  467. // if (bitIndex == 1)
  468. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  469. // if (bitIndex == 2)
  470. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseOnputImbalance = NO;
  471. // else if (bitIndex == 3)
  472. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  473. // else if (bitIndex == 4)
  474. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuThreePhaseInputInadequate = NO;
  475. // else if (bitIndex == 5)
  476. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  477. // else if (bitIndex == 6)
  478. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  479. // }
  480. // break;
  481. // }
  482. // }
  483. }
  484. }
  485. }
  486. // return single psu alarm status: PsuStatusInfo
  487. unsigned int InfyPwrStateAnalysis(int psu_index, unsigned char state_0, unsigned char state_1, unsigned char state_2)
  488. {
  489. bool _changed = false;
  490. PsuStatusInfo _alarmStatus;
  491. _alarmStatus.StatusVal = 0;
  492. if(ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[0] != state_0 ||
  493. (ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[1] & 0xFE) != (state_1 & 0xFE) ||
  494. (ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[2] & 0x7F) != (state_2 & 0x7F))
  495. {
  496. _changed = true;
  497. }
  498. InfyPsuStateFlagInfo _infyState;
  499. _infyState.StateVal[0] = state_0;
  500. _infyState.StateVal[1] = state_1;
  501. _infyState.StateVal[2] = state_2;
  502. if(_infyState.bits.OutputShort ||
  503. _infyState.bits.PsuFault ||
  504. _infyState.bits.FanFault ||
  505. _infyState.bits.PsuProtect ||
  506. _infyState.bits.FanFault ||
  507. _infyState.bits.OverTemperature ||
  508. _infyState.bits.OutputOverVoltage ||
  509. _infyState.bits.CanCommInterrupt ||
  510. _infyState.bits.PsuIdRepetition ||
  511. _infyState.bits.InputPhaseLost ||
  512. _infyState.bits.InputUnderVoltage ||
  513. _infyState.bits.InputOverVoltage)
  514. {
  515. _alarmStatus.bits.Fault = true;
  516. }
  517. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrStateFlag.bits.Changed = _changed;
  518. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[0] = state_0;
  519. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[1] = state_1;
  520. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[2] = state_2;
  521. return _alarmStatus.StatusVal;
  522. }
  523. //=================================
  524. // Callback Function
  525. //=================================
  526. // 0x04: PSU_RCmd_ModuleStatus
  527. void GetGroupCallback(byte group, byte psu_index, unsigned short full_address)
  528. {
  529. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  530. if(ShmPsuData->Work_Step < Get_PSU_LOCATION)
  531. {
  532. return;
  533. }
  534. if(group >= _maxGroupCount || psu_index >= MAX_PSU_MODULE_QUANTITY)
  535. {
  536. return;
  537. }
  538. ShmPsuPosition->PsuAddressInfo[psu_index].Full_Address = full_address;
  539. ShmPsuPosition->PsuAddressInfo[psu_index].GroupNo = group;
  540. if(!ShmPsuPosition->PsuAddressInfo[psu_index].CheckIn)
  541. {
  542. ShmPsuPosition->PsuAddressInfo[psu_index].CheckIn = true;
  543. ShmPsuPosition->TotalPsuQuantity++;
  544. //LOG_INFO("SN = %d At Group %02X", SN, group);
  545. }
  546. if(ShmPsuPosition->TotalPsuQuantity == ShmPsuData->SystemInitialPsuQuantity)
  547. {
  548. byte groupNo = 0, quantity = 0;
  549. if(!ShmPsuPosition->PsuLocationInit)
  550. {
  551. memset(ShmPsuPosition->GroupLocationInfo, 0x00, sizeof(ShmPsuPosition->GroupLocationInfo));
  552. for(int i = 0; i < MAX_PSU_MODULE_QUANTITY; i++)
  553. {
  554. if(ShmPsuPosition->PsuAddressInfo[i].CheckIn)
  555. {
  556. groupNo = ShmPsuPosition->PsuAddressInfo[i].GroupNo;
  557. quantity = ShmPsuPosition->GroupLocationInfo[groupNo].GroupPsuQuantity;
  558. ShmPsuPosition->PsuAddressInfo[i].GIndex = quantity;
  559. ShmPsuPosition->GroupLocationInfo[groupNo].PsuIndex[quantity] = i;
  560. ShmPsuPosition->GroupLocationInfo[groupNo].GroupPsuQuantity++;
  561. //LOG_INFO("Psu %d Assign To Group %02X At %d", index, group, ShmPsuPosition->PsuAddressInfo[index].GIndex);
  562. }
  563. }
  564. bool match = true;
  565. for(int i = 0; i < _maxGroupCount; i++)
  566. {
  567. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity != ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity)
  568. {
  569. match = false;
  570. }
  571. }
  572. if(match)
  573. {
  574. ShmPsuPosition->PsuLocationInit = true;
  575. }
  576. else
  577. {
  578. // try to re-initial psu location
  579. ShmPsuPosition->ReInitPsuLocation = true;
  580. }
  581. }
  582. }
  583. }
  584. // 0x04: PSU_RCmd_ModuleStatus_alarm_flag
  585. void GetAlarmFlagCallback(byte address, unsigned int alarm1, unsigned int alarm2)
  586. {
  587. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  588. if(ShmPsuData->Work_Step < Get_PSU_LOCATION)
  589. {
  590. return;
  591. }
  592. int group = FindTargetGroup(address);
  593. int gIndex = FindGroupIndex(address);
  594. if(group < 0 || gIndex < 0)
  595. {
  596. return;
  597. }
  598. if(ShmPsuPosition->PsuLocationInit)
  599. {
  600. }
  601. }
  602. void UpdatePsuGroupAlarmStatus(byte group)
  603. {
  604. int psu_index = 0;
  605. unsigned int status_or = 0, status_and = 0xFFFFFFFF;
  606. if(ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity > 0)
  607. {
  608. for(int i = 0; i < ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity; i++)
  609. {
  610. psu_index = ShmPsuPosition->GroupLocationInfo[group].PsuIndex[i];
  611. status_or |= ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal;
  612. status_and &= ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal;
  613. }
  614. ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[group].StatusVal = status_or;
  615. ShmChargerInfo->PsuGrouping.GroupPsuStatus_AND[group].StatusVal = status_and;
  616. }
  617. else
  618. {
  619. ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[group].StatusVal = 0;
  620. ShmChargerInfo->PsuGrouping.GroupPsuStatus_AND[group].StatusVal = 0;
  621. }
  622. }
  623. // 0x04: PSU_RCmd_ModuleStatus_alarm_status
  624. void GetAlarmStatusCallback(byte address, unsigned int status)
  625. {
  626. bool _update = false;
  627. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  628. if(ShmPsuData->Work_Step < Get_PSU_LOCATION)
  629. {
  630. return;
  631. }
  632. int group = FindTargetGroup(address);
  633. int gIndex = FindGroupIndex(address);
  634. if(group < 0 || gIndex < 0)
  635. {
  636. return;
  637. }
  638. if(ShmPsuPosition->PsuLocationInit)
  639. {
  640. if(ShmPsuPosition->SinglePsuStatus[address].StatusVal != status)
  641. {
  642. _update = true;
  643. }
  644. ShmPsuPosition->SinglePsuStatus[address].StatusVal = status;
  645. if(_update)
  646. {
  647. UpdatePsuGroupAlarmStatus(group);
  648. }
  649. }
  650. }
  651. void GetInfyPwrAlarmFlagCallback(byte address, byte state_2, byte state_1, byte state_0)
  652. {
  653. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  654. if(ShmPsuData->Work_Step < Get_PSU_LOCATION || address >= ShmPsuData->SystemInitialPsuQuantity ||
  655. !ShmPsuPosition->PsuLocationInit)
  656. {
  657. return;
  658. }
  659. int group = FindTargetGroup(address);
  660. int gIndex = FindGroupIndex(address);
  661. if(group < 0 || gIndex < 0)
  662. {
  663. return;
  664. }
  665. unsigned int _alarmStatus = 0;
  666. _alarmStatus = InfyPwrStateAnalysis(address, state_0, state_1, state_2);
  667. GetAlarmStatusCallback(address, _alarmStatus);
  668. }
  669. void GetPhPwrAlarmFlagCallback(byte address, unsigned int status, unsigned int alarm_1, unsigned int alarm_0)
  670. {
  671. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  672. if(ShmPsuData->Work_Step < Get_PSU_LOCATION)
  673. {
  674. return;
  675. }
  676. int group = FindTargetGroup(address);
  677. int gIndex = FindGroupIndex(address);
  678. if(group < 0 || gIndex < 0)
  679. {
  680. return;
  681. }
  682. if(ShmPsuPosition->PsuLocationInit)
  683. {
  684. GetAlarmStatusCallback(address, status);
  685. }
  686. }
  687. // 0x04: PSU_RCmd_ModuleStatus_ambient
  688. void GetAmbientCallback(byte address, byte temp)
  689. {
  690. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  691. if(ShmPsuData->Work_Step < Get_PSU_LOCATION)
  692. {
  693. return;
  694. }
  695. int group = FindTargetGroup(address);
  696. int gIndex = FindGroupIndex(address);
  697. if(group < 0 || gIndex < 0)
  698. {
  699. return;
  700. }
  701. if(ShmPsuPosition->PsuLocationInit)
  702. {
  703. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp1 = temp;
  704. }
  705. }
  706. // 0x02: PSU_RCmd_SysModuleCount
  707. void GetModuleCountCallback(byte group, byte count, bool summation)
  708. {
  709. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  710. if(group < _maxGroupCount)
  711. {
  712. ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity = count;
  713. }
  714. if(summation)
  715. {
  716. int total = 0;
  717. for(int i = 0; i < _maxGroupCount; i++)
  718. {
  719. total += ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity;
  720. }
  721. if(ShmPsuData->Work_Step == GET_PSU_COUNT)
  722. {
  723. ShmPsuData->SystemInitialPsuQuantity = total;
  724. }
  725. ShmPsuData->SystemPresentPsuQuantity = total;
  726. }
  727. }
  728. // 0x02: PSU_RCmd_SysModuleCount_SystemPresentPsuQuantity
  729. void GetSysModuleCountCallback(byte group, byte count)
  730. {
  731. if(ShmPsuData->Work_Step == GET_PSU_COUNT)
  732. {
  733. ShmPsuData->SystemInitialPsuQuantity = count;
  734. }
  735. ShmPsuData->SystemPresentPsuQuantity = count;
  736. }
  737. void UpdateGunAvailableCapability(unsigned char group)
  738. {
  739. int _availableCurrent = 0, _availablePower = 0, _realPower = 0;
  740. unsigned char master = 0;
  741. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  742. // calculate output group capability
  743. if(master == 0)
  744. {
  745. chargingInfo[group]->AvailableChargingCurrent = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  746. chargingInfo[group]->AvailableChargingPower = ShmPsuData->PsuGroup[group].GroupAvailablePower;
  747. chargingInfo[group]->RealRatingPower = ShmPsuData->PsuGroup[group].GroupRealOutputPower * 10;
  748. }
  749. else
  750. {
  751. if(ShmPsuGrouping->GroupCollection[master - 1].Role == _GROLE_MASTER)
  752. {
  753. _availableCurrent = ShmPsuData->PsuGroup[master - 1].GroupAvailableCurrent;
  754. _availablePower = ShmPsuData->PsuGroup[master - 1].GroupAvailablePower;
  755. _realPower = ShmPsuData->PsuGroup[master - 1].GroupRealOutputPower * 10;
  756. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  757. {
  758. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  759. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  760. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  761. {
  762. _availableCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  763. _availablePower += ShmPsuData->PsuGroup[slave].GroupAvailablePower;
  764. _realPower += ShmPsuData->PsuGroup[slave].GroupRealOutputPower * 10;
  765. }
  766. }
  767. if(ShmPsuGrouping->GroupCollection[master - 1].GroupCtrl.bits.DeratingConfirmed)
  768. {
  769. chargingInfo[master - 1]->AvailableChargingCurrent = ShmPsuGrouping->GroupCollection[master - 1].ReAssignAvailableCurrent;
  770. }
  771. else
  772. {
  773. chargingInfo[master - 1]->AvailableChargingCurrent = _availableCurrent;
  774. }
  775. chargingInfo[master - 1]->AvailableChargingPower = _availablePower;
  776. chargingInfo[master - 1]->RealRatingPower = _realPower;
  777. // if((master - 1) != group)
  778. // {
  779. // chargingInfo[group]->AvailableChargingCurrent = 0;;
  780. // chargingInfo[group]->AvailableChargingPower = 0;
  781. // chargingInfo[group]->RealRatingPower = 0;
  782. // }
  783. }
  784. }
  785. }
  786. void UpdateSystemAvailable(void)
  787. {
  788. int _sysCurrent = 0, _sysPower = 0;
  789. // summation of system current & power
  790. for(byte i = 0; i < _maxGroupCount; i++)
  791. {
  792. _sysCurrent += ShmPsuData->PsuGroup[i].GroupAvailableCurrent;
  793. _sysPower += ShmPsuData->PsuGroup[i].GroupAvailablePower;
  794. }
  795. ShmPsuData->SystemAvailableCurrent = _sysCurrent;
  796. ShmPsuData->SystemAvailablePower = _sysPower;
  797. }
  798. // maxVol, minVol unit: 0.1V
  799. // maxCur unit: 0.1A
  800. // totalPow unit: 0.1kW
  801. // 0x0A: PSU_RCmd_ModuleCapability
  802. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  803. {
  804. int _groupCurrent = 0, _groupPower = 0;
  805. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  806. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  807. !ShmPsuPosition->PsuLocationInit)
  808. {
  809. return;
  810. }
  811. byte master = 0;
  812. int group = FindTargetGroup(address);
  813. int gIndex = FindGroupIndex(address);
  814. if(group < 0 || gIndex < 0)
  815. {
  816. return;
  817. }
  818. // calculate psu module available power
  819. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailablePower = totalPow;
  820. //if(ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage >= PSU_MIN_VOL)
  821. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage >= PSU_MIN_VOL)
  822. {
  823. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailableCurrent =
  824. ((ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailablePower * 100) * 10) / (ShmPsuGrouping->GroupOutput[group].GTargetVoltage / 10);
  825. }
  826. else
  827. {
  828. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AvailableCurrent = maxCur > PSU_MAX_CUR ? PSU_MAX_CUR : maxCur;
  829. }
  830. // summation of psu group current & power
  831. for(byte i = 0; i < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; i++)
  832. {
  833. _groupCurrent += ShmPsuData->PsuGroup[group].PsuModule[i].AvailableCurrent;
  834. _groupPower += ShmPsuData->PsuGroup[group].PsuModule[i].AvailablePower;
  835. }
  836. if(ShmPsuData->PsuGroup[group].StableIAvailableCurrent != 0)
  837. {
  838. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = ShmPsuData->PsuGroup[group].StableIAvailableCurrent;
  839. }
  840. else
  841. {
  842. if(ShmPsuData->PsuGroup[group].GroupRealOutputPower != 0 &&
  843. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage >= PSU_MIN_VOL)
  844. {
  845. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = ((ShmPsuData->PsuGroup[group].GroupRealOutputPower * 1000) * 10) / (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10);
  846. }
  847. else
  848. {
  849. ShmPsuData->PsuGroup[group].GroupAvailableCurrent = _groupCurrent;
  850. }
  851. }
  852. ShmPsuData->PsuGroup[group].GroupAvailablePower = _groupPower;
  853. // update MaximumChargingVoltage
  854. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  855. if(master == 0)
  856. {
  857. chargingInfo[group]->MaximumChargingVoltage = maxVol;
  858. }
  859. else
  860. {
  861. if(ShmPsuGrouping->GroupCollection[master - 1].Role == _GROLE_MASTER)
  862. {
  863. chargingInfo[master - 1]->MaximumChargingVoltage = maxVol;
  864. // if((master - 1) != group)
  865. // {
  866. // chargingInfo[group]->MaximumChargingVoltage = 0;
  867. // }
  868. }
  869. }
  870. UpdateGunAvailableCapability(group);
  871. UpdateSystemAvailable();
  872. //LOG_INFO("address = %d, maxVol = %d, minVol = %d, maxCur = %d, totalPow = %d", address, maxVol, minVol, maxCur, totalPow);
  873. }
  874. // 0x07: PSU_RCmd_ModuleVersion
  875. void GetFwCallback(byte address, short dcSwVer, short pfcSwVer, short hwVer)
  876. {
  877. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  878. if (ShmPsuData->Work_Step != Get_PSU_VERSION || address >= ShmPsuData->SystemInitialPsuQuantity ||
  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. sprintf((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  890. sprintf((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, "PFC %d.%02d", (pfcSwVer & 0xFF00) >> 8, pfcSwVer & 0xFF);
  891. sprintf((char *)ShmPsuData->PsuGroup[group].PsuModule[gIndex].FwVersion, "DC %d.%02d", (dcSwVer & 0xFF00) >> 8, dcSwVer & 0xFF);
  892. //LOG_INFO("Psu %d %s %s", address, ShmPsuData->PsuVersion[address].FwPrimaryVersion, ShmPsuData->PsuVersion[address].FwSecondVersion);
  893. }
  894. // 0x07: PSU_RCmd_ModuleVersion_dc
  895. void GetDcFwCallback(byte address, char *dcSwVer)
  896. {
  897. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  898. if (ShmPsuData->Work_Step != Get_PSU_VERSION || address >= ShmPsuData->SystemInitialPsuQuantity ||
  899. !ShmPsuPosition->PsuLocationInit)
  900. {
  901. return;
  902. }
  903. int group = FindTargetGroup(address);
  904. int gIndex = FindGroupIndex(address);
  905. if(group < 0 || gIndex < 0)
  906. {
  907. return;
  908. }
  909. strcpy((char *)ShmPsuData->PsuVersion[address].FwPrimaryVersion, dcSwVer);
  910. strcpy((char *)ShmPsuData->PsuGroup[group].PsuModule[gIndex].FwVersion, dcSwVer);
  911. }
  912. // 0x07: PSU_RCmd_ModuleVersion_pfc
  913. void GetPfcFwCallback(byte address, char *pfcSwVer)
  914. {
  915. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  916. if (ShmPsuData->Work_Step != Get_PSU_VERSION || address >= ShmPsuData->SystemInitialPsuQuantity ||
  917. !ShmPsuPosition->PsuLocationInit)
  918. {
  919. return;
  920. }
  921. int group = FindTargetGroup(address);
  922. int gIndex = FindGroupIndex(address);
  923. if(group < 0 || gIndex < 0)
  924. {
  925. return;
  926. }
  927. strcpy((char *)ShmPsuData->PsuVersion[address].FwSecondVersion, pfcSwVer);
  928. }
  929. // no using -- GetInputVoltageCallback
  930. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  931. {
  932. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  933. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  934. !ShmPsuPosition->PsuLocationInit)
  935. {
  936. return;
  937. }
  938. int group = FindTargetGroup(address);
  939. int gIndex = FindGroupIndex(address);
  940. if(group < 0 || gIndex < 0)
  941. {
  942. return;
  943. }
  944. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL1 = vol1;
  945. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL2 = vol2;
  946. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL3 = vol3;
  947. }
  948. // no using -- GetInputVoltageCallback End
  949. // no using -- GetPresentOutputCallback
  950. void GetPresentOutputCallback(byte group, unsigned short outVol, unsigned short outCur)
  951. {
  952. }
  953. // no using -- GetPresentOutputCallback End
  954. // type 1: FAN_SPEED_CMD
  955. // type 2: TEMP_DC_CMD
  956. // type 3: TEMP_PFC_CMD
  957. // 0x0E: PSU_RCmd_ModuleMiscInfo
  958. void GetMisCallback(byte address, unsigned int value, byte type)
  959. {
  960. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  961. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  962. !ShmPsuPosition->PsuLocationInit)
  963. {
  964. return;
  965. }
  966. int group = FindTargetGroup(address);
  967. int gIndex = FindGroupIndex(address);
  968. if(group < 0 || gIndex < 0)
  969. {
  970. return;
  971. }
  972. if (type == 1)
  973. {
  974. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_1 = value;
  975. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_2 = value;
  976. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_3 = value;
  977. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_4 = value;
  978. }
  979. else if (type == 2)
  980. {
  981. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp1 = value;
  982. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp2 = value;
  983. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp3 = value;
  984. ShmPsuData->PsuGroup[group].PsuModule[gIndex].ExletTemp = value;
  985. }
  986. else if (type == 3)
  987. {
  988. //printf("PFC - group = %d, index = %d, value = %d \n", group, address, value);
  989. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InletTemp = value;
  990. }
  991. }
  992. // 0x0E: PSU_RCmd_ModuleMiscInfo_FanSpeed
  993. void GetFanSpeedCallback(byte address, unsigned int speed)
  994. {
  995. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  996. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  997. !ShmPsuPosition->PsuLocationInit)
  998. {
  999. return;
  1000. }
  1001. int group = FindTargetGroup(address);
  1002. int gIndex = FindGroupIndex(address);
  1003. if(group < 0 || gIndex < 0)
  1004. {
  1005. return;
  1006. }
  1007. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_1 = speed;
  1008. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_2 = speed;
  1009. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_3 = speed;
  1010. ShmPsuData->PsuGroup[group].PsuModule[gIndex].FanSpeed_4 = speed;
  1011. }
  1012. // 0x0E: PSU_RCmd_ModuleMiscInfo_InletTemp
  1013. void GetInletTempCallback(byte address, char temp)
  1014. {
  1015. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1016. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1017. !ShmPsuPosition->PsuLocationInit)
  1018. {
  1019. return;
  1020. }
  1021. int group = FindTargetGroup(address);
  1022. int gIndex = FindGroupIndex(address);
  1023. if(group < 0 || gIndex < 0)
  1024. {
  1025. return;
  1026. }
  1027. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InletTemp = temp;
  1028. }
  1029. // 0x0E: PSU_RCmd_ModuleMiscInfo_ExletTemp
  1030. void GetExletTempCallback(byte address, char temp)
  1031. {
  1032. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1033. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1034. !ShmPsuPosition->PsuLocationInit)
  1035. {
  1036. return;
  1037. }
  1038. int group = FindTargetGroup(address);
  1039. int gIndex = FindGroupIndex(address);
  1040. if(group < 0 || gIndex < 0)
  1041. {
  1042. return;
  1043. }
  1044. ShmPsuData->PsuGroup[group].PsuModule[gIndex].ExletTemp = temp;
  1045. }
  1046. // 0x0E: PSU_RCmd_ModuleMiscInfo_OutletTemp
  1047. void GetOutletTempCallback(byte address, char temp)
  1048. {
  1049. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1050. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1051. !ShmPsuPosition->PsuLocationInit)
  1052. {
  1053. return;
  1054. }
  1055. int group = FindTargetGroup(address);
  1056. int gIndex = FindGroupIndex(address);
  1057. if(group < 0 || gIndex < 0)
  1058. {
  1059. return;
  1060. }
  1061. ShmPsuData->PsuGroup[group].PsuModule[gIndex].OutletTemp = temp;
  1062. }
  1063. // temp1: dd in 1
  1064. // temp2: dd in 2
  1065. // temp3: dd out 1
  1066. // temp4: dd out 2
  1067. // 0x0E: PSU_RCmd_ModuleMiscInfo_OtherTemp
  1068. void GetOtherTempCallback(byte address, char temp1, char temp2, char temp3, char temp4)
  1069. {
  1070. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1071. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1072. !ShmPsuPosition->PsuLocationInit)
  1073. {
  1074. return;
  1075. }
  1076. int group = FindTargetGroup(address);
  1077. int gIndex = FindGroupIndex(address);
  1078. if(group < 0 || gIndex < 0)
  1079. {
  1080. return;
  1081. }
  1082. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InletTemp_1 = temp1;
  1083. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InletTemp_2 = temp2;
  1084. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp2 = temp3;
  1085. ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp3 = temp4;
  1086. }
  1087. // Iavail unit: 0.1A
  1088. // Vext unit: 0.1V
  1089. // 0x0C: PSU_RCmd_ModuleIAvailable
  1090. // 0x02CCF0XX
  1091. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  1092. {
  1093. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1094. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1095. !ShmPsuPosition->PsuLocationInit)
  1096. {
  1097. return;
  1098. }
  1099. byte master = 0;
  1100. unsigned short diffIAvailable = 0;
  1101. int totalCurrent = 0;
  1102. int group = FindTargetGroup(address);
  1103. int gIndex = FindGroupIndex(address);
  1104. if(group < 0 || gIndex < 0)
  1105. {
  1106. return;
  1107. }
  1108. ShmPsuData->PsuGroup[group].PsuModule[gIndex].IAvailableCurrent = Iavail;
  1109. // summation of psu group i available current
  1110. for (byte i = 0; i < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; i++)
  1111. {
  1112. totalCurrent += ShmPsuData->PsuGroup[group].PsuModule[i].IAvailableCurrent;
  1113. }
  1114. ShmPsuData->PsuGroup[group].TotalIAvailableCurrent = totalCurrent;
  1115. diffIAvailable = totalCurrent >= ShmPsuData->PsuGroup[group].TempIAvailableCurrent ?
  1116. totalCurrent - ShmPsuData->PsuGroup[group].TempIAvailableCurrent : ShmPsuData->PsuGroup[group].TempIAvailableCurrent - totalCurrent;
  1117. if(diffIAvailable > STABLE_CURRENT_TOLERANCE)
  1118. {
  1119. ShmPsuData->PsuGroup[group].StableCurrentCounter = 0;
  1120. ShmPsuData->PsuGroup[group].StableIAvailableCurrent = 0;
  1121. }
  1122. ShmPsuData->PsuGroup[group].TempIAvailableCurrent = ShmPsuData->PsuGroup[group].TotalIAvailableCurrent;
  1123. ShmPsuData->PsuGroup[group].StableCurrentCounter++;
  1124. if(ShmPsuData->PsuGroup[group].StableCurrentCounter >= MAX_STABLE_COUNT)
  1125. {
  1126. // get stable StableIAvailableCurrent
  1127. ShmPsuData->PsuGroup[group].StableIAvailableCurrent = ShmPsuData->PsuGroup[group].TotalIAvailableCurrent;
  1128. // get stable GroupRealOutputPower
  1129. ShmPsuData->PsuGroup[group].GroupRealOutputPower = (ShmPsuData->PsuGroup[group].StableIAvailableCurrent * ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage) / 100 / 1000;
  1130. ShmPsuData->PsuGroup[group].StableCurrentCounter = 0;
  1131. }
  1132. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  1133. if(master == 0)
  1134. {
  1135. // unit: 0.1A
  1136. chargingInfo[group]->DeratingChargingCurrent = ShmPsuData->PsuGroup[group].StableIAvailableCurrent;
  1137. chargingInfo[group]->DeratingChargingPower = (chargingInfo[group]->DeratingChargingCurrent * chargingInfo[group]->PresentChargingVoltage) / 10 / 100;
  1138. }
  1139. else
  1140. {
  1141. // calculate DeratingChargingCurrent of master group
  1142. totalCurrent = ShmPsuData->PsuGroup[master - 1].StableIAvailableCurrent;
  1143. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  1144. {
  1145. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  1146. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE)
  1147. {
  1148. totalCurrent += ShmPsuData->PsuGroup[slave].StableIAvailableCurrent;
  1149. }
  1150. }
  1151. chargingInfo[master - 1]->DeratingChargingCurrent = totalCurrent;
  1152. chargingInfo[master - 1]->DeratingChargingPower = (chargingInfo[master - 1]->DeratingChargingCurrent * chargingInfo[master - 1]->PresentChargingVoltage) / 10 / 100;
  1153. if((master - 1) != group)
  1154. {
  1155. chargingInfo[group]->DeratingChargingCurrent = 0;
  1156. chargingInfo[group]->DeratingChargingPower = 0;
  1157. }
  1158. }
  1159. }
  1160. // outVol unit: 1V
  1161. // outCur unit: 1A
  1162. // 0x01: PSU_RCmd_SysOutputVolCur_F
  1163. void GetPresentOutputFCallback(byte group, float outVol, float outCur)
  1164. {
  1165. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1166. if (ShmPsuData->Work_Step < GET_SYS_CAP || group >= _maxGroupCount ||
  1167. !ShmPsuPosition->PsuLocationInit)
  1168. {
  1169. return;
  1170. }
  1171. byte master = 0;
  1172. unsigned short pVoltage = 0, pCurrent = 0;
  1173. unsigned short gVoltage = 0, gCurrent = 0;
  1174. pVoltage = isinf(outVol) == 0 ? (unsigned short)(outVol * 10) : 0;
  1175. pCurrent = isinf(outCur) == 0 ? (unsigned short)(outCur * 10) : 0;
  1176. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage = pVoltage;
  1177. ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent = pCurrent;
  1178. ShmPsuData->PsuGroup[group].GroupPresentOutputPower = (pVoltage * pCurrent) / 100 / 100;
  1179. master = ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup;
  1180. if(master == 0)
  1181. {
  1182. // calculate PresentChargingVoltage and PresentChargingCurrent of self group
  1183. chargingInfo[group]->PresentChargingVoltage = (float)ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10;
  1184. chargingInfo[group]->PresentChargingCurrent = (float)ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10;
  1185. }
  1186. else
  1187. {
  1188. // calculate PresentChargingVoltage and PresentChargingCurrent of master group
  1189. gVoltage = ShmPsuData->PsuGroup[master - 1].GroupPresentOutputVoltage;
  1190. gCurrent = ShmPsuData->PsuGroup[master - 1].GroupPresentOutputCurrent;
  1191. for(byte i = 0; i < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; i++)
  1192. {
  1193. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[i];
  1194. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  1195. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF ||
  1196. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  1197. {
  1198. if(ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage > gVoltage)
  1199. {
  1200. gVoltage = ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage;
  1201. }
  1202. gCurrent += ShmPsuData->PsuGroup[slave].GroupPresentOutputCurrent;
  1203. }
  1204. }
  1205. chargingInfo[master - 1]->PresentChargingVoltage = (float)gVoltage / 10;
  1206. chargingInfo[master - 1]->PresentChargingCurrent = (float)gCurrent / 10;
  1207. if((master - 1) != group)
  1208. {
  1209. chargingInfo[group]->PresentChargingVoltage = 0;
  1210. chargingInfo[group]->PresentChargingCurrent = 0;
  1211. }
  1212. }
  1213. }
  1214. // outVol unit: 1V
  1215. // outCur unit: 1A
  1216. void GetModuleOutputCallback(byte address, float outVol, float outCur)
  1217. {
  1218. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1219. if (ShmPsuData->Work_Step < GET_SYS_CAP || !ShmPsuPosition->PsuLocationInit)
  1220. {
  1221. return;
  1222. }
  1223. unsigned short voltage = isinf(outVol) == 0 ? (unsigned short)(outVol * 10) : 0;
  1224. unsigned short current = isinf(outCur) == 0 ? (unsigned short)(outCur * 10) : 0;
  1225. int group = FindTargetGroup(address);
  1226. int gIndex = FindGroupIndex(address);
  1227. if(group < 0 || gIndex < 0)
  1228. {
  1229. return;
  1230. }
  1231. ShmPsuData->PsuGroup[group].PsuModule[gIndex].PresentOutputVoltage = voltage;
  1232. ShmPsuData->PsuGroup[group].PsuModule[gIndex].PresentOutputCurrent = current;
  1233. }
  1234. // phihong pwr only
  1235. void GetErrorRecordCallback(byte psu_index, byte count_down, byte *error_record)
  1236. {
  1237. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1238. if (ShmPsuData->Work_Step < GET_SYS_CAP || !ShmPsuPosition->PsuLocationInit)
  1239. {
  1240. return;
  1241. }
  1242. int err_cnt = 0;
  1243. bool _change = false;
  1244. for(int i = 0; i < 7; i++)
  1245. {
  1246. if(error_record[i] != 0)
  1247. {
  1248. err_cnt++;
  1249. if(ShmPsuPosition->SinglePhPsuError[psu_index].PsuError[i] != error_record[i] || _change)
  1250. {
  1251. _change = true;
  1252. }
  1253. }
  1254. else
  1255. {
  1256. break;
  1257. }
  1258. }
  1259. if(_change)
  1260. {
  1261. memset(ShmPsuPosition->SinglePhPsuError[psu_index].PsuError, 0x00, sizeof(ShmPsuPosition->SinglePhPsuError[psu_index].PsuError));
  1262. memcpy(ShmPsuPosition->SinglePhPsuError[psu_index].PsuError, error_record, err_cnt);
  1263. }
  1264. else
  1265. {
  1266. _change = ShmPsuPosition->SinglePhPsuError[psu_index].ErrorCount != err_cnt ? true : false;
  1267. }
  1268. ShmPsuPosition->SinglePhPsuError[psu_index].ErrorCount = err_cnt;
  1269. ShmPsuPosition->SinglePhPsuError[psu_index].PhPwrStateFlag.bits.Changed |= _change;
  1270. }
  1271. //==========================================
  1272. // 特規用指令
  1273. //==========================================
  1274. // 0x1901: Nexton_PSU_DcOutputValue
  1275. void GetOutputAndTempCallback(byte address, unsigned short outputVol_s,
  1276. unsigned short outputCur_s, unsigned short outputPower, unsigned char Temperature)
  1277. {
  1278. return;
  1279. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1280. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1281. !ShmPsuPosition->PsuLocationInit)
  1282. {
  1283. return;
  1284. }
  1285. int group = FindTargetGroup(address);
  1286. int gIndex = FindGroupIndex(address);
  1287. if(group < 0 || gIndex < 0)
  1288. {
  1289. return;
  1290. }
  1291. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp1 = Temperature;
  1292. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp2 = Temperature;
  1293. //ShmPsuData->PsuGroup[group].PsuModule[gIndex].CriticalTemp3 = Temperature;
  1294. }
  1295. // 0x1902: Nexton_PSU_StatusEvent
  1296. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  1297. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  1298. {
  1299. return;
  1300. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1301. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1302. !ShmPsuPosition->PsuLocationInit)
  1303. {
  1304. return;
  1305. }
  1306. int group = FindTargetGroup(address);
  1307. int gIndex = FindGroupIndex(address);
  1308. if(group < 0 || gIndex < 0)
  1309. {
  1310. return;
  1311. }
  1312. int alarm = (err4 << 24) | (err3 << 16) | (err2 << 8) | err1;
  1313. ShmPsuData->PsuGroup[group].PsuModule[gIndex].AlarmCode = alarm;
  1314. AbnormalStopAnalysis(group, alarm);
  1315. if(isErr)
  1316. {
  1317. // 012267
  1318. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  1319. ShmPsuData->PsuGroup[group].GroupErrorFlag.bits.PsuFailure = true;
  1320. }
  1321. }
  1322. // 0x1903: Nexton_PSU_AcInputValue
  1323. void GetModuleInputCallback(byte address, unsigned short inputR,
  1324. unsigned short inputS, unsigned short inputT)
  1325. {
  1326. return;
  1327. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt++;
  1328. if (ShmPsuData->Work_Step < GET_SYS_CAP || address >= ShmPsuData->SystemInitialPsuQuantity ||
  1329. !ShmPsuPosition->PsuLocationInit)
  1330. {
  1331. return;
  1332. }
  1333. int group = FindTargetGroup(address);
  1334. int gIndex = FindGroupIndex(address);
  1335. if(group < 0 || gIndex < 0)
  1336. {
  1337. return;
  1338. }
  1339. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL1 = inputR;
  1340. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL2 = inputS;
  1341. ShmPsuData->PsuGroup[group].PsuModule[gIndex].InputVoltageL3 = inputT;
  1342. }
  1343. //==========================================
  1344. // Init all share memory
  1345. //==========================================
  1346. int InitShareMemory()
  1347. {
  1348. int result = PASS;
  1349. int MeterSMId;
  1350. //creat ShmSysConfigAndInfo
  1351. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  1352. {
  1353. #ifdef SystemLogMessage
  1354. LOG_ERROR("shmget ShmSysConfigAndInfo NG %d");
  1355. #endif
  1356. result = FAIL;
  1357. }
  1358. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1359. {
  1360. #ifdef SystemLogMessage
  1361. LOG_ERROR("shmat ShmSysConfigAndInfo NG");
  1362. #endif
  1363. result = FAIL;
  1364. }
  1365. else
  1366. {}
  1367. //creat ShmStatusCodeData
  1368. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  1369. {
  1370. #ifdef SystemLogMessage
  1371. LOG_ERROR("shmget ShmStatusCodeData NG");
  1372. #endif
  1373. result = FAIL;
  1374. }
  1375. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1376. {
  1377. #ifdef SystemLogMessage
  1378. LOG_ERROR("shmat ShmStatusCodeData NG");
  1379. #endif
  1380. result = FAIL;
  1381. }
  1382. else
  1383. {}
  1384. //creat ShmPsuData
  1385. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  1386. {
  1387. #ifdef SystemLogMessage
  1388. LOG_ERROR("shmget ShmPsuData NG");
  1389. #endif
  1390. result = FAIL;
  1391. }
  1392. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1393. {
  1394. #ifdef SystemLogMessage
  1395. LOG_ERROR("shmat ShmPsuData NG");
  1396. #endif
  1397. result = FAIL;
  1398. }
  1399. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  1400. {
  1401. #ifdef SystemLogMessage
  1402. LOG_ERROR("shmat ChargerInfoData NG");
  1403. #endif
  1404. result = FAIL;
  1405. }
  1406. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1407. {
  1408. #ifdef SystemLogMessage
  1409. LOG_ERROR("shmat ChargerInfoData NG");
  1410. #endif
  1411. result = FAIL;
  1412. }
  1413. if(result == PASS)
  1414. {
  1415. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  1416. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  1417. }
  1418. return result;
  1419. }
  1420. //================================================
  1421. // Main process
  1422. //================================================
  1423. void InitialPsuData(void)
  1424. {
  1425. ShmPsuData->SystemInitialPsuQuantity = 0;
  1426. ShmPsuData->SystemPresentPsuQuantity = 0;
  1427. ShmPsuData->SystemAvailablePower = 0;
  1428. LOG_INFO("************ psu Group = %d", ShmPsuData->GroupCount);
  1429. for (byte _groupCount = 0; _groupCount < ShmPsuData->GroupCount; _groupCount++)
  1430. {
  1431. ShmPsuData->PsuGroup[_groupCount].GroupPresentPsuQuantity = 0;
  1432. ShmPsuData->PsuGroup[_groupCount].GroupAvailablePower = 0;
  1433. ShmPsuData->PsuGroup[_groupCount].GroupAvailableCurrent = 0;
  1434. ShmPsuData->PsuGroup[_groupCount].GroupErrorFlag.PsuGroupErrorValue = 0;
  1435. }
  1436. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFuseBurnOut = NO;
  1437. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcAndDcdcCommFault = NO;
  1438. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageUnbalance = NO;
  1439. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusOverVoltage = NO;
  1440. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusVoltageAbnormal = NO;
  1441. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = NO;
  1442. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = NO;
  1443. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuBusUnderVoltage = NO;
  1444. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = NO;
  1445. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = NO;
  1446. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCommunicationFail = NO;
  1447. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuSevereUnevenCurrent = NO;
  1448. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFfcSideShutDown = NO;
  1449. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFullSpeed = NO;
  1450. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcSideShutDown = NO;
  1451. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = NO;
  1452. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuTemperaturePowerLimit = NO;
  1453. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuAcPowerLimit = NO;
  1454. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcEepromFault = NO;
  1455. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = NO;
  1456. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = NO;
  1457. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPfcEepromFault = NO;
  1458. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = NO;
  1459. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = NO;
  1460. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFault = NO;
  1461. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = NO;
  1462. memset(_LocalPsuStatus, 0x00, sizeof(_LocalPsuStatus));
  1463. memset(_Psu_Indication_Enable, 0x00, sizeof(_Psu_Indication_Enable));
  1464. memset(_Psu_Indication_Count, 0x00, sizeof(_Psu_Indication_Count));
  1465. memset(&ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR, 0x00, sizeof(ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR));
  1466. memset(&ShmChargerInfo->PsuGrouping.GroupPsuStatus_AND, 0x00, sizeof(ShmChargerInfo->PsuGrouping.GroupPsuStatus_AND));
  1467. }
  1468. void Initialization()
  1469. {
  1470. bool isPass = false;
  1471. while(!isPass)
  1472. {
  1473. isPass = true;
  1474. for (byte _index = 0; _index < _maxGroupCount; _index++)
  1475. {
  1476. if (!FindChargingInfoData(_index, &chargingInfo[0]))
  1477. {
  1478. LOG_ERROR("Module_PsuComm : FindChargingInfoData false");
  1479. isPass = false;
  1480. break;
  1481. }
  1482. }
  1483. sleep(1);
  1484. }
  1485. ShmPsuData->GroupCount = _maxGroupCount;
  1486. }
  1487. bool Is_PsuRxComm_Alive(void)
  1488. {
  1489. if(_RxPsuPid > 0)
  1490. {
  1491. if(kill(_RxPsuPid, 0) == 0)
  1492. {
  1493. // Process exists.
  1494. return true;
  1495. }
  1496. }
  1497. return false;
  1498. }
  1499. void Run_PsuRxCommProcess(void)
  1500. {
  1501. _RxPsuPid = InitialCommunication();
  1502. LOG_INFO("Create PsuRx PID = %d", _RxPsuPid);
  1503. }
  1504. void PsuReceiveRecoveryCheck(void)
  1505. {
  1506. char *ptrSave, *ptrToken;
  1507. int fd, psuTaskCount = 0;
  1508. char pathBuffer[64], psuTaskPidString[64];
  1509. system("pidof Module_PsuComm > /tmp/Module_PsuTask");
  1510. snprintf(pathBuffer, sizeof(pathBuffer), "/tmp/Module_PsuTask");
  1511. fd = open(pathBuffer, O_RDWR);
  1512. if(fd < 0)
  1513. {
  1514. close(fd);
  1515. return;
  1516. }
  1517. if(read(fd, psuTaskPidString, 64) < 0)
  1518. {
  1519. close(fd);
  1520. return;
  1521. }
  1522. ptrToken = strtok_r(psuTaskPidString, " ", &ptrSave);
  1523. while(ptrToken != NULL)
  1524. {
  1525. int psuPid = atoi(ptrToken);
  1526. if(psuPid > 0)
  1527. {
  1528. psuTaskCount++;
  1529. }
  1530. ptrToken = strtok_r(NULL, " ", &ptrSave);
  1531. }
  1532. close(fd);
  1533. if(psuTaskCount == 1)
  1534. {
  1535. LOG_INFO("************ PSU Receive Task Need Recovery ************\n");
  1536. Run_PsuRxCommProcess();
  1537. psuReceiveRecovery = true;
  1538. }
  1539. }
  1540. void ShowGroupMember(unsigned char group)
  1541. {
  1542. if(group < MAX_GROUP_QUANTITY)
  1543. {
  1544. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1545. {
  1546. for(byte psu = 0; psu < ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity; psu++)
  1547. {
  1548. LOG_INFO("Group %d - Number = %d, SN = %02X", group + 1, psu,
  1549. ShmPsuPosition->GroupLocationInfo[group].PsuIndex[psu]);
  1550. }
  1551. }
  1552. }
  1553. }
  1554. void ShowPsuVersion(unsigned char group)
  1555. {
  1556. if(group < MAX_GROUP_QUANTITY)
  1557. {
  1558. if(ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity > 0)
  1559. {
  1560. LOG_INFO("Group %d PSU Version Info", group + 1);
  1561. for(int i = 0; i < ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity; i++)
  1562. {
  1563. int psuIndex = ShmPsuPosition->GroupLocationInfo[group].PsuIndex[i];
  1564. LOG_INFO("PSU Primary: %s, Second: %s",
  1565. ShmPsuData->PsuVersion[psuIndex].FwPrimaryVersion,
  1566. ShmPsuData->PsuVersion[psuIndex].FwSecondVersion);
  1567. }
  1568. }
  1569. }
  1570. }
  1571. void ShowGroupAvailableCurrentPower(unsigned char group)
  1572. {
  1573. if(group < MAX_GROUP_QUANTITY)
  1574. {
  1575. LOG_INFO("Group %d Available Current = %3d.%dA, Power = %3d.%dkW", group + 1,
  1576. (ShmPsuData->PsuGroup[group].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[group].GroupAvailableCurrent % 10),
  1577. (ShmPsuData->PsuGroup[group].GroupAvailablePower / 10), (ShmPsuData->PsuGroup[group].GroupAvailablePower % 10));
  1578. }
  1579. }
  1580. unsigned char _TempType = 0;
  1581. void PsuGroupRoutineQuery(void)
  1582. {
  1583. for(byte group = 0; group < GENERAL_GUN_QUANTITY; group++)
  1584. {
  1585. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  1586. {
  1587. if (psuCmdSeq == _PSU_CMD_CAP)
  1588. {
  1589. // 取系統總輸出能力
  1590. GetModuleCap(group);
  1591. }
  1592. else if (psuCmdSeq == _PSU_CMD_OUTPUT)
  1593. {
  1594. // 取各群輸出電壓電流 (float)
  1595. GetModuleOutputF(group);
  1596. }
  1597. else if (psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1598. {
  1599. // 取得模塊輸出額定電流能力
  1600. unsigned short voltage = 0;
  1601. if(ShmPsuGrouping->GroupCollection[group].TargetGroup == 0)
  1602. {
  1603. voltage = (unsigned short)(chargingInfo[group]->FireChargingVoltage);
  1604. }
  1605. else
  1606. {
  1607. voltage = (unsigned short)(chargingInfo[ShmPsuGrouping->GroupCollection[group].TargetGroup - 1]->FireChargingVoltage);
  1608. }
  1609. GetModuleIavailable(group, voltage);
  1610. }
  1611. else if (psuCmdSeq == _PSU_CMD_TEMP)
  1612. {
  1613. // 取得模塊溫度
  1614. if(_TempType == _PSU_TMP_DCDC)
  1615. {
  1616. GetDcTemperature(group);
  1617. }
  1618. else
  1619. {
  1620. GetPfcTemperature(group);
  1621. }
  1622. }
  1623. }
  1624. }
  1625. if(psuCmdSeq == _PSU_CMD_CAP)
  1626. {
  1627. psuCmdSeq = _PSU_CMD_OUTPUT;
  1628. }
  1629. else if(psuCmdSeq == _PSU_CMD_OUTPUT)
  1630. {
  1631. psuCmdSeq = _PSU_CMD_IVAILIABLE;
  1632. }
  1633. else if(psuCmdSeq == _PSU_CMD_IVAILIABLE)
  1634. {
  1635. psuCmdSeq = _PSU_CMD_TEMP;
  1636. _TempType = _TempType == _PSU_TMP_DCDC ? _PSU_TMP_PFC : _PSU_TMP_DCDC;
  1637. }
  1638. else
  1639. {
  1640. psuCmdSeq = _PSU_CMD_CAP;
  1641. }
  1642. }
  1643. void SetPsuGroupRole(byte group, byte role)
  1644. {
  1645. if(group < GENERAL_GUN_QUANTITY)
  1646. {
  1647. ShmPsuGrouping->GroupCollection[group].Role = role;
  1648. }
  1649. }
  1650. void SetPsuGroupToIdle(byte group)
  1651. {
  1652. if(group < GENERAL_GUN_QUANTITY)
  1653. {
  1654. SetPsuGroupRole(group, _GROLE_IDLE);
  1655. memset(&ShmPsuGrouping->GroupCollection[group].Partner, 0x00, sizeof(PsuGroupPartner));
  1656. memset(&ShmPsuGrouping->GroupCollection[group].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  1657. ShmPsuGrouping->GroupCollection[group].TargetGroup = 0;
  1658. ShmPsuGrouping->GroupCollection[group].ReservedTarget = 0;
  1659. //LOG_INFO("Reset Group[%02X] To Idle", group);
  1660. }
  1661. }
  1662. // group: self group index
  1663. void SetPsuGroupToMaster(byte group)
  1664. {
  1665. if(group < GENERAL_GUN_QUANTITY)
  1666. {
  1667. SetPsuGroupRole(group, _GROLE_MASTER);
  1668. ShmPsuGrouping->GroupCollection[group].TargetGroup = group + 1;
  1669. //LOG_INFO("Set Group[%02X] As Master To Gun %d", group, group + 1);
  1670. }
  1671. }
  1672. // group: self group index
  1673. // target: target group index + 1
  1674. void SetPsuGroupToSlave(byte group, byte target)
  1675. {
  1676. if(group < GENERAL_GUN_QUANTITY && target <= GENERAL_GUN_QUANTITY)
  1677. {
  1678. SetPsuGroupRole(group, _GROLE_SLAVE);
  1679. ShmPsuGrouping->GroupCollection[group].TargetGroup = target;
  1680. ShmPsuGrouping->GroupCollection[group].ReservedTarget = 0;
  1681. memset(&ShmPsuGrouping->GroupCollection[group].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  1682. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  1683. //LOG_INFO("Set Group[%02X] As Slave To Gun %d", group, target);
  1684. }
  1685. }
  1686. // target: target group index + 1
  1687. // tPartner: set member to power off
  1688. void PrepareToPowerOff(byte target, PsuGroupPartner *tPartner)
  1689. {
  1690. unsigned char master = 0, slave = 0;
  1691. char strMember[64];
  1692. bool find = false;
  1693. sprintf(strMember, "Set Member:");
  1694. for(int i = 0; i < tPartner->Quantity; i++)
  1695. {
  1696. slave = tPartner->Member[i];
  1697. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  1698. SetPsuGroupRole(slave, _GROLE_PREPARE_SWITCH_OFF);
  1699. ShmPsuGrouping->GroupCollection[slave].ReservedTarget = target;
  1700. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedDerating = true;
  1701. char strSlave[8];
  1702. sprintf(strSlave, " [%02X]", slave);
  1703. strcat(strMember, strSlave);
  1704. find = true;
  1705. }
  1706. if(find)
  1707. {
  1708. char strTemp[32];
  1709. if(target != 0)
  1710. {
  1711. sprintf(strTemp, " Prepare To Change To Gun %d", target);
  1712. }
  1713. else
  1714. {
  1715. sprintf(strTemp, " Prepare To Power Of");
  1716. }
  1717. strcat(strMember, strTemp);
  1718. LOG_INFO("%s", strMember);
  1719. }
  1720. }
  1721. // target: target group index + 1
  1722. // tPartner: set member to prepare to attach on
  1723. void PrepareToExtendCapability(byte target, PsuGroupPartner *tPartner)
  1724. {
  1725. char strMember[64];
  1726. sprintf(strMember, "Set Member:");
  1727. for(int i = 0; i < tPartner->Quantity; i++)
  1728. {
  1729. SetPsuGroupRole(tPartner->Member[i], _GROLE_PREPARE_ATTACH_ON);
  1730. ShmPsuGrouping->GroupCollection[tPartner->Member[i]].ReservedTarget = target;
  1731. char strSlave[8];
  1732. sprintf(strSlave, " [%02X]", tPartner->Member[i]);
  1733. strcat(strMember, strSlave);
  1734. }
  1735. if(tPartner->Quantity > 0)
  1736. {
  1737. char strTemp[32];
  1738. sprintf(strTemp, " Prepare To Attach To Gun %d", target);
  1739. strcat(strMember, strTemp);
  1740. LOG_INFO("%s", strMember);
  1741. }
  1742. }
  1743. void FindPsuGroupPartner(byte master, int condition, PsuGroupPartner *tPartner, int type, int quantityLimit)
  1744. {
  1745. int slave = 0, location = 0;
  1746. PsuGroupPartner partner;
  1747. int extendPower = 0;
  1748. bool done = false;
  1749. int parallelRelay = 0;
  1750. unsigned short parallelFlag = 0;
  1751. memset(&partner, 0x00, sizeof(PsuGroupPartner));
  1752. // search from left
  1753. location = ShmPsuGrouping->GroupCollection[master].Location - 1;
  1754. for(int i = location; i >= 0; i--)
  1755. {
  1756. switch(type)
  1757. {
  1758. case _EXTEND_TYPE_QUANTITY:
  1759. if(partner.Quantity >= condition)
  1760. {
  1761. done = true;
  1762. }
  1763. break;
  1764. case _EXTEND_TYPE_POWER:
  1765. if(extendPower >= condition)
  1766. {
  1767. done = true;
  1768. }
  1769. break;
  1770. default:
  1771. done = true;
  1772. break;
  1773. }
  1774. if(done || partner.Quantity >= quantityLimit)
  1775. {
  1776. break;
  1777. }
  1778. slave = ShmPsuGrouping->Layout[i];
  1779. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_IDLE ||
  1780. (ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_WAIT_SLAVE && (ShmPsuGrouping->GroupCollection[slave].ReservedTarget - 1) == master))
  1781. {
  1782. parallelRelay = ShmPsuGrouping->GroupCollection[master].ParallelConfig[slave];
  1783. parallelFlag |= parallelRelay != 0 ? (1 << (parallelRelay - 1)) : 0;
  1784. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  1785. {
  1786. partner.Member[partner.Quantity] = slave;
  1787. partner.ParallelFlag[partner.Quantity] = parallelFlag;
  1788. partner.Quantity++;
  1789. extendPower += (ShmPsuData->PsuGroup[slave].GroupAvailablePower / 10);
  1790. parallelFlag = 0;
  1791. }
  1792. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay)
  1793. {
  1794. parallelFlag = 0;
  1795. }
  1796. }
  1797. else
  1798. {
  1799. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1800. {
  1801. // Enable6ParallelRelay = 0, need to check slave is the same target or not
  1802. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE && master == (ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1))
  1803. {
  1804. continue;
  1805. }
  1806. break;
  1807. }
  1808. }
  1809. }
  1810. parallelFlag = 0;
  1811. // search from right
  1812. location = ShmPsuGrouping->GroupCollection[master].Location + 1;
  1813. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  1814. {
  1815. switch(type)
  1816. {
  1817. case _EXTEND_TYPE_QUANTITY:
  1818. if(partner.Quantity >= condition)
  1819. {
  1820. done = true;
  1821. }
  1822. break;
  1823. case _EXTEND_TYPE_POWER:
  1824. if(extendPower >= condition)
  1825. {
  1826. done = true;
  1827. }
  1828. break;
  1829. default:
  1830. done = true;
  1831. break;
  1832. }
  1833. if(done || partner.Quantity >= quantityLimit)
  1834. {
  1835. break;
  1836. }
  1837. slave = ShmPsuGrouping->Layout[i];
  1838. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_IDLE ||
  1839. (ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_WAIT_SLAVE && (ShmPsuGrouping->GroupCollection[slave].ReservedTarget - 1) == master))
  1840. {
  1841. parallelRelay = ShmPsuGrouping->GroupCollection[master].ParallelConfig[slave];
  1842. parallelFlag |= parallelRelay != 0 ? (1 << (parallelRelay - 1)) : 0;
  1843. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  1844. {
  1845. partner.Member[partner.Quantity] = slave;
  1846. partner.ParallelFlag[partner.Quantity] = parallelFlag;
  1847. partner.Quantity++;
  1848. extendPower += (ShmPsuData->PsuGroup[slave].GroupAvailablePower / 10);
  1849. parallelFlag = 0;
  1850. }
  1851. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay)
  1852. {
  1853. parallelFlag = 0;
  1854. }
  1855. }
  1856. else
  1857. {
  1858. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  1859. {
  1860. // Enable6ParallelRelay = 0, need to check slave is the same target or not
  1861. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE && master == (ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1))
  1862. {
  1863. continue;
  1864. }
  1865. break;
  1866. }
  1867. }
  1868. }
  1869. memcpy(tPartner, &partner, sizeof(PsuGroupPartner));
  1870. }
  1871. int GetPsuGroupAvailable(byte group)
  1872. {
  1873. PsuGroupPartner partner;
  1874. FindPsuGroupPartner(group, MAX_GROUP_QUANTITY, &partner, _EXTEND_TYPE_QUANTITY, MAX_GROUP_QUANTITY);
  1875. return partner.Quantity;
  1876. }
  1877. // return quantity which role is _GROLE_MASTER
  1878. int FindChargingMasterQuantity(void)
  1879. {
  1880. int quantity = 0;
  1881. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1882. {
  1883. if(ShmPsuGrouping->GroupCollection[i].Role == _GROLE_MASTER)
  1884. {
  1885. quantity++;
  1886. }
  1887. }
  1888. return quantity;
  1889. }
  1890. // return total psu group quantity
  1891. int FindTotalPsuGroupQuantity(void)
  1892. {
  1893. int quantity = 0;
  1894. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  1895. {
  1896. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  1897. {
  1898. quantity++;
  1899. }
  1900. }
  1901. return quantity;
  1902. }
  1903. void ShowGunPrioritySequence(unsigned char *sequence, int quantity)
  1904. {
  1905. char strSequence[256];
  1906. sprintf(strSequence, "Priority Sequence:");
  1907. for(int i = 0; i < quantity; i++)
  1908. {
  1909. char strGun[32];
  1910. sprintf(strGun, " [Gun %d: %d kW]",
  1911. sequence[i] + 1,
  1912. (int)ShmPsuGrouping->GroupCollection[sequence[i]].DiffPower_PhysicalLimit);
  1913. strcat(strSequence, strGun);
  1914. }
  1915. PSU_LOG("%s", strSequence);
  1916. }
  1917. void CheckExtendConflictPriority(unsigned char *sequence, int quantity)
  1918. {
  1919. bool done = true;
  1920. int temp = 0;
  1921. for(int i = 0; i < quantity - 1; i++)
  1922. {
  1923. done = true;
  1924. for(int j = 0; j < quantity - i - 1; j++)
  1925. {
  1926. if(ShmPsuGrouping->GroupCollection[sequence[j + 1]].DiffPower_PhysicalLimit > ShmPsuGrouping->GroupCollection[sequence[j]].DiffPower_PhysicalLimit)
  1927. {
  1928. temp = sequence[j];
  1929. sequence[j] = sequence[j + 1];
  1930. sequence[j + 1] = temp;
  1931. done = false;
  1932. }
  1933. }
  1934. //ShowGunPrioritySequence(sequence, quantity);
  1935. if(done)
  1936. {
  1937. break;
  1938. }
  1939. }
  1940. }
  1941. // return conflict reason
  1942. int IsExtendConflict(byte master)
  1943. {
  1944. int available = 0, conflictMasterCnt = 0, conflictGroupCnt = 0, average = 0, remainder = 0;
  1945. available = GetPsuGroupAvailable(master);
  1946. if(available == 0)
  1947. {
  1948. return _CONFLICT_AVAILABLE;
  1949. }
  1950. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1951. {
  1952. if(ShmPsuGrouping->GroupCollection[i].GroupCtrl.bits.ExtendAvailable ||
  1953. ShmPsuGrouping->GroupCollection[i].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue != 0)
  1954. {
  1955. conflictMasterCnt++;
  1956. conflictGroupCnt += ShmPsuGrouping->GroupCollection[i].Partner.Quantity + 1;
  1957. }
  1958. }
  1959. average = conflictMasterCnt != 0 ? ((conflictGroupCnt + available) / conflictMasterCnt) : 0;
  1960. remainder = conflictMasterCnt != 0 ? ((conflictGroupCnt + available) % conflictMasterCnt) : 0;
  1961. if(ShmPsuGrouping->GroupCollection[master].Partner.Quantity + 1 < average)
  1962. {
  1963. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = average - ShmPsuGrouping->GroupCollection[master].Partner.Quantity - 1;
  1964. if(ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit > 1)
  1965. {
  1966. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 1;
  1967. }
  1968. #if PSU_DEBUG_MSG
  1969. LOG_INFO("Gun %d Extend Average First, Quantity Limit: %d", master + 1, ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit);
  1970. #endif
  1971. return _CONFLICT_NONE;
  1972. }
  1973. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1974. {
  1975. if(ShmPsuGrouping->GroupCollection[i].GroupCtrl.bits.ExtendAvailable ||
  1976. ShmPsuGrouping->GroupCollection[i].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue != 0)
  1977. {
  1978. if(ShmPsuGrouping->GroupCollection[i].Partner.Quantity + 1 < average)
  1979. {
  1980. return _CONFLICT_AVERAGE;
  1981. }
  1982. }
  1983. }
  1984. if(remainder > 0)
  1985. {
  1986. int find = 0;
  1987. CheckExtendConflictPriority(_GunIndexOfPrioritySequence, MAX_GROUP_QUANTITY);
  1988. ShowGunPrioritySequence(_GunIndexOfPrioritySequence, MAX_GROUP_QUANTITY);
  1989. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  1990. {
  1991. if(find >= remainder)
  1992. {
  1993. break;
  1994. }
  1995. if(ShmPsuGrouping->GroupCollection[_GunIndexOfPrioritySequence[i]].GroupCtrl.bits.ExtendAvailable)
  1996. {
  1997. find++;
  1998. if(master == _GunIndexOfPrioritySequence[i])
  1999. {
  2000. ShmPsuGrouping->GroupCollection[_GunIndexOfPrioritySequence[i]].ExtendQuantityLimit = 1;
  2001. #if PSU_DEBUG_MSG
  2002. LOG_INFO("Gun %d Extend Via Priority First, Quantity Limit: %d", master + 1, ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit);
  2003. #endif
  2004. return _CONFLICT_NONE;
  2005. }
  2006. }
  2007. }
  2008. }
  2009. return _CONFLICT_PRIORITY;
  2010. }
  2011. void FindGrabPartner(byte group, byte GrabMaster, PsuGroupPartner *tPartner)
  2012. {
  2013. bool find = false;
  2014. int location = 0, master = 0, slave = 0;
  2015. // find other group in the same direction
  2016. if(ShmPsuGrouping->Location[group] < ShmPsuGrouping->Location[GrabMaster])
  2017. {
  2018. // find other group far to GrabMaster in the same direction
  2019. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  2020. for(int i = location; i >= 0; i--)
  2021. {
  2022. slave = ShmPsuGrouping->Layout[i];
  2023. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2024. if(master == GrabMaster)
  2025. {
  2026. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster != 0)
  2027. {
  2028. if((ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1) == slave)
  2029. {
  2030. continue;
  2031. }
  2032. }
  2033. tPartner->Member[tPartner->Quantity] = slave;
  2034. tPartner->Quantity++;
  2035. find = true;
  2036. #if PSU_DEBUG_MSG
  2037. LOG_INFO("Find Priority Partner [%02X] Far To GrabMaster At Right Side", slave);
  2038. #endif
  2039. break;
  2040. }
  2041. }
  2042. if(!find)
  2043. {
  2044. // find other group near to GrabMaster in the same direction
  2045. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  2046. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  2047. {
  2048. slave = ShmPsuGrouping->Layout[i];
  2049. if(slave == GrabMaster)
  2050. {
  2051. break;
  2052. }
  2053. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2054. if(master == GrabMaster)
  2055. {
  2056. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster != 0)
  2057. {
  2058. if((ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1) == slave)
  2059. {
  2060. continue;
  2061. }
  2062. }
  2063. tPartner->Member[tPartner->Quantity] = slave;
  2064. tPartner->Quantity++;
  2065. find = true;
  2066. #if PSU_DEBUG_MSG
  2067. LOG_INFO("Find Priority Partner [%02X] Near To GrabMaster At Right Side", slave);
  2068. #endif
  2069. break;
  2070. }
  2071. }
  2072. if(!find)
  2073. {
  2074. for(int i = ShmChargerInfo->Control.MaxConnector - 1; i >= 0; i--)
  2075. {
  2076. slave = ShmPsuGrouping->Layout[i];
  2077. if(slave == GrabMaster)
  2078. {
  2079. break;
  2080. }
  2081. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2082. if(master == GrabMaster)
  2083. {
  2084. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster != 0)
  2085. {
  2086. if((ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1) == slave)
  2087. {
  2088. continue;
  2089. }
  2090. }
  2091. tPartner->Member[tPartner->Quantity] = slave;
  2092. tPartner->Quantity++;
  2093. find = true;
  2094. #if PSU_DEBUG_MSG
  2095. LOG_INFO("Find Priority Partner [%02X] At Right Side At Ring Mode", slave);
  2096. #endif
  2097. break;
  2098. }
  2099. }
  2100. }
  2101. }
  2102. }
  2103. if(ShmPsuGrouping->Location[group] > ShmPsuGrouping->Location[GrabMaster])
  2104. {
  2105. // find other group far to GrabMaster in the same direction
  2106. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  2107. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  2108. {
  2109. slave = ShmPsuGrouping->Layout[i];
  2110. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2111. if(master == GrabMaster)
  2112. {
  2113. tPartner->Member[tPartner->Quantity] = slave;
  2114. tPartner->Quantity++;
  2115. find = true;
  2116. #if PSU_DEBUG_MSG
  2117. LOG_INFO("Find Priority Partner [%02X] Far To GrabMaster At Left Side", slave);
  2118. #endif
  2119. break;
  2120. }
  2121. }
  2122. if(!find)
  2123. {
  2124. // find other group near to GrabMaster in the same direction
  2125. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  2126. for(int i = location; i >= 0; i--)
  2127. {
  2128. slave = ShmPsuGrouping->Layout[i];
  2129. if(slave == GrabMaster)
  2130. {
  2131. break;
  2132. }
  2133. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2134. if(master == GrabMaster)
  2135. {
  2136. tPartner->Member[tPartner->Quantity] = slave;
  2137. tPartner->Quantity++;
  2138. find = true;
  2139. #if PSU_DEBUG_MSG
  2140. LOG_INFO("Find Priority Partner [%02X] Near To GrabMaster At Left Side", slave);
  2141. #endif
  2142. break;
  2143. }
  2144. }
  2145. if(!find)
  2146. {
  2147. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  2148. {
  2149. slave = ShmPsuGrouping->Layout[i];
  2150. if(slave == GrabMaster)
  2151. {
  2152. break;
  2153. }
  2154. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2155. if(master == GrabMaster)
  2156. {
  2157. tPartner->Member[tPartner->Quantity] = slave;
  2158. tPartner->Quantity++;
  2159. find = true;
  2160. #if PSU_DEBUG_MSG
  2161. LOG_INFO("Find Priority Partner [%02X] At Left Side At Ring Mode", slave);
  2162. #endif
  2163. break;
  2164. }
  2165. }
  2166. }
  2167. }
  2168. }
  2169. }
  2170. // return keep or stop
  2171. bool AlternateMasterGrabLogic(unsigned char master, int grabLocation, int *GrabTarget, int *MaxMember)
  2172. {
  2173. bool keep = true;
  2174. unsigned char slave = 0, target = 0;
  2175. slave = ShmPsuGrouping->Layout[grabLocation];
  2176. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE &&
  2177. ShmPsuGrouping->GroupCollection[slave].TargetGroup != master + 1)
  2178. {
  2179. target = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2180. if(ShmPsuGrouping->GroupCollection[target].AlternateMaster == 0)
  2181. {
  2182. if(ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity > *MaxMember)
  2183. {
  2184. *MaxMember = ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity;
  2185. *GrabTarget = target + 1;
  2186. }
  2187. }
  2188. else
  2189. {
  2190. if(ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity - 1 > *MaxMember)
  2191. {
  2192. *MaxMember = ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity - 1;
  2193. *GrabTarget = target + 1;
  2194. }
  2195. }
  2196. }
  2197. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2198. {
  2199. keep = false;
  2200. }
  2201. return keep;
  2202. }
  2203. // return keep or stop
  2204. bool AlternateMasterDeratingLogic(unsigned char master, int grabLocation, int *GrabTarget, int *MaxMember)
  2205. {
  2206. bool keep = true;
  2207. unsigned char slave = 0, target = 0;
  2208. slave = ShmPsuGrouping->Layout[grabLocation];
  2209. if((ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF ||
  2210. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF ||
  2211. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SWITCH_OFF_OK ||
  2212. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_WAIT_IDLE) &&
  2213. ShmPsuGrouping->GroupCollection[slave].TargetGroup != master + 1)
  2214. {
  2215. target = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2216. if(ShmPsuGrouping->GroupCollection[target].AlternateMaster == 0)
  2217. {
  2218. if(ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity > *MaxMember)
  2219. {
  2220. *MaxMember = ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity;
  2221. *GrabTarget = target + 1;
  2222. }
  2223. }
  2224. else
  2225. {
  2226. if(ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity - 1 > *MaxMember)
  2227. {
  2228. *MaxMember = ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity - 1;
  2229. *GrabTarget = target + 1;
  2230. }
  2231. }
  2232. }
  2233. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2234. {
  2235. keep = false;
  2236. }
  2237. return keep;
  2238. }
  2239. bool CheckAlternateMaster(byte group, PsuGroupPartner *tPartner, bool isSlave)
  2240. {
  2241. int location = 0;
  2242. int GrabTarget = 0, MaxMember = 0;
  2243. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  2244. // search from left
  2245. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  2246. for(int i = location; i >= 0; i--)
  2247. {
  2248. if(isSlave)
  2249. {
  2250. if(!AlternateMasterGrabLogic(group, i, &GrabTarget, &MaxMember))
  2251. {
  2252. break;
  2253. }
  2254. }
  2255. else
  2256. {
  2257. if(!AlternateMasterDeratingLogic(group, i, &GrabTarget, &MaxMember))
  2258. {
  2259. break;
  2260. }
  2261. }
  2262. }
  2263. // search from right
  2264. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  2265. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  2266. {
  2267. if(isSlave)
  2268. {
  2269. if(!AlternateMasterGrabLogic(group, i, &GrabTarget, &MaxMember))
  2270. {
  2271. break;
  2272. }
  2273. }
  2274. else
  2275. {
  2276. if(!AlternateMasterDeratingLogic(group, i, &GrabTarget, &MaxMember))
  2277. {
  2278. break;
  2279. }
  2280. }
  2281. }
  2282. if(GrabTarget > 0)
  2283. {
  2284. FindGrabPartner(group, GrabTarget - 1, tPartner);
  2285. }
  2286. return tPartner->Quantity > 0 ? true : false;
  2287. }
  2288. // return grab success or fail
  2289. // return tPartner with possible member
  2290. bool PsuGroupGrabCheck(byte group, PsuGroupPartner *tPartner)
  2291. {
  2292. int slave = 0, target = 0, location = 0, average = 0, total = 0;
  2293. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_SLAVE && ShmPsuGrouping->GroupCollection[group].Role != _GROLE_REQUEST_TO_CHARGING)
  2294. {
  2295. return false;
  2296. }
  2297. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  2298. total = GetPsuGroupAvailable(group) + 1;
  2299. average = FindTotalPsuGroupQuantity() / (1 + FindChargingMasterQuantity());
  2300. if(total < average)
  2301. {
  2302. // search from left
  2303. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  2304. for(int i = location; i >= 0; i--)
  2305. {
  2306. if(total + tPartner->Quantity < average)
  2307. {
  2308. slave = ShmPsuGrouping->Layout[i];
  2309. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE &&
  2310. (ShmPsuGrouping->GroupCollection[group].Role == _GROLE_REQUEST_TO_CHARGING ||
  2311. ShmPsuGrouping->GroupCollection[slave].TargetGroup == ShmPsuGrouping->GroupCollection[group].TargetGroup))
  2312. {
  2313. target = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2314. if((ShmPsuGrouping->GroupCollection[target].Partner.Quantity + 1) > average)
  2315. {
  2316. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2317. {
  2318. tPartner->Member[tPartner->Quantity] = slave;
  2319. tPartner->Quantity++;
  2320. }
  2321. else
  2322. {
  2323. FindGrabPartner(group, target, tPartner);
  2324. }
  2325. }
  2326. }
  2327. else
  2328. {
  2329. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2330. {
  2331. break;
  2332. }
  2333. }
  2334. }
  2335. }
  2336. // search from right
  2337. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  2338. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  2339. {
  2340. if(total + tPartner->Quantity < average)
  2341. {
  2342. slave = ShmPsuGrouping->Layout[i];
  2343. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE &&
  2344. (ShmPsuGrouping->GroupCollection[group].Role == _GROLE_REQUEST_TO_CHARGING ||
  2345. ShmPsuGrouping->GroupCollection[slave].TargetGroup == ShmPsuGrouping->GroupCollection[group].TargetGroup))
  2346. {
  2347. target = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2348. if((ShmPsuGrouping->GroupCollection[target].Partner.Quantity + 1) > average)
  2349. {
  2350. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2351. {
  2352. tPartner->Member[tPartner->Quantity] = slave;
  2353. tPartner->Quantity++;
  2354. }
  2355. else
  2356. {
  2357. FindGrabPartner(group, target, tPartner);
  2358. }
  2359. }
  2360. }
  2361. else
  2362. {
  2363. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2364. {
  2365. break;
  2366. }
  2367. }
  2368. }
  2369. }
  2370. }
  2371. return tPartner->Quantity > 0 ? true : false;
  2372. }
  2373. bool IsAnyGroupDerating(void)
  2374. {
  2375. bool isDerating = false;
  2376. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  2377. {
  2378. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  2379. {
  2380. if(ShmPsuGrouping->GroupCollection[i].Role != _GROLE_MASTER && ShmPsuGrouping->GroupCollection[i].Role != _GROLE_SLAVE)
  2381. {
  2382. isDerating = true;
  2383. }
  2384. }
  2385. }
  2386. return isDerating;
  2387. }
  2388. // return keep or stop
  2389. bool ExtendGrabLogic(unsigned char master, int grabLocation, int *GrabTarget, int *MaxGrabTotal, int average)
  2390. {
  2391. bool keep = true;
  2392. unsigned char slave = 0, target = 0;
  2393. slave = ShmPsuGrouping->Layout[grabLocation];
  2394. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE &&
  2395. ShmPsuGrouping->GroupCollection[slave].TargetGroup != master + 1)
  2396. {
  2397. target = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  2398. if(ShmPsuGrouping->GroupCollection[target].AlternateMaster == 0)
  2399. {
  2400. if((ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity + 1) > average &&
  2401. (ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity + 1) > *MaxGrabTotal)
  2402. {
  2403. *MaxGrabTotal = ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity + 1;
  2404. *GrabTarget = target + 1;
  2405. }
  2406. }
  2407. else
  2408. {
  2409. if((ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity) > average &&
  2410. (ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity) > *MaxGrabTotal)
  2411. {
  2412. *MaxGrabTotal = ShmPsuGrouping->GroupCollection[target].Partner.RealQuantity;
  2413. *GrabTarget = target + 1;
  2414. }
  2415. }
  2416. }
  2417. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2418. {
  2419. keep = false;
  2420. }
  2421. return keep;
  2422. }
  2423. // return grab success or fail
  2424. // return tPartner with possible member
  2425. bool ExtendCapabilityGrabCheck(byte group, PsuGroupPartner *tPartner)
  2426. {
  2427. int location = 0, average = 0, total = 0;
  2428. int GrabTarget = 0, MaxTotal = 0;
  2429. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_MASTER)
  2430. {
  2431. return false;
  2432. }
  2433. if(IsAnyGroupDerating())
  2434. {
  2435. return false;
  2436. }
  2437. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  2438. total = ShmPsuGrouping->GroupCollection[group].Partner.RealQuantity;
  2439. total += ShmPsuGrouping->GroupCollection[group].AlternateMaster > 0 ? 1 : 0;
  2440. average = FindTotalPsuGroupQuantity() / FindChargingMasterQuantity();
  2441. if(total < average)
  2442. {
  2443. // search from left
  2444. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  2445. for(int i = location; i >= 0; i--)
  2446. {
  2447. if(!ExtendGrabLogic(group, i, &GrabTarget, &MaxTotal, average))
  2448. {
  2449. break;
  2450. }
  2451. }
  2452. // search from right
  2453. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  2454. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  2455. {
  2456. if(!ExtendGrabLogic(group, i, &GrabTarget, &MaxTotal, average))
  2457. {
  2458. break;
  2459. }
  2460. }
  2461. }
  2462. if(GrabTarget > 0)
  2463. {
  2464. FindGrabPartner(group, GrabTarget - 1, tPartner);
  2465. }
  2466. return tPartner->Quantity > 0 ? true : false;
  2467. }
  2468. // return tPartner with possible member when group power off
  2469. void PsuGroupPowerOffCheck(byte group, PsuGroupPartner *tPartner)
  2470. {
  2471. int master = 0, quantity = 0, location = 0;
  2472. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  2473. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_SLAVE)
  2474. {
  2475. return;
  2476. }
  2477. master = ShmPsuGrouping->GroupCollection[group].TargetGroup - 1;
  2478. quantity = ShmPsuGrouping->GroupCollection[master].Partner.Quantity;
  2479. for(int i = 0; i < quantity; i++)
  2480. {
  2481. if(tPartner->Quantity == 0)
  2482. {
  2483. if(group == ShmPsuGrouping->GroupCollection[master].Partner.Member[i])
  2484. {
  2485. location = i;
  2486. tPartner->Member[tPartner->Quantity] = group;
  2487. tPartner->Quantity++;
  2488. }
  2489. }
  2490. else
  2491. {
  2492. // find other group in the same direction
  2493. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[location]] < ShmPsuGrouping->Location[master])
  2494. {
  2495. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[i]] < ShmPsuGrouping->Location[master])
  2496. {
  2497. tPartner->Member[tPartner->Quantity] = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2498. tPartner->Quantity++;
  2499. //printf("\r\n Find Other Group %02X In The Same Direction", ShmPsuGrouping->GroupCollection[master].Partner.Member[i]);
  2500. }
  2501. }
  2502. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[location]] > ShmPsuGrouping->Location[master])
  2503. {
  2504. if(ShmPsuGrouping->Location[ShmPsuGrouping->GroupCollection[master].Partner.Member[i]] > ShmPsuGrouping->Location[master])
  2505. {
  2506. tPartner->Member[tPartner->Quantity] = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2507. tPartner->Quantity++;
  2508. //printf("\r\n Find Other Group %02X In The Same Direction", ShmPsuGrouping->GroupCollection[master].Partner.Member[i]);
  2509. }
  2510. }
  2511. }
  2512. }
  2513. }
  2514. void UpdateGunTotalPsuQuantity(int master)
  2515. {
  2516. ShmPsuGrouping->GroupCollection[master].GunPsuQuantity = GetPsuModuleQuantity(master);
  2517. }
  2518. // group: group index, target: target index
  2519. // master: master group index
  2520. void AddMember(byte group, byte master, unsigned short flag)
  2521. {
  2522. if(group < GENERAL_GUN_QUANTITY && master < GENERAL_GUN_QUANTITY)
  2523. {
  2524. if(ShmPsuGrouping->GroupCollection[master].Role != _GROLE_MASTER &&
  2525. ShmPsuGrouping->GroupCollection[master].Role != _GROLE_REQUEST_TO_CHARGING &&
  2526. ShmPsuGrouping->GroupCollection[master].Role != _GROLE_DUMMY_MASTER)
  2527. {
  2528. return;
  2529. }
  2530. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_IDLE &&
  2531. ShmPsuGrouping->GroupCollection[group].Role != _GROLE_WAIT_SLAVE &&
  2532. ShmPsuGrouping->GroupCollection[group].Role != _GROLE_PRECHARGE_READY)
  2533. {
  2534. return;
  2535. }
  2536. SetPsuGroupToSlave(group, master + 1);
  2537. ShmPsuGrouping->GroupCollection[master].Partner.Member[ShmPsuGrouping->GroupCollection[master].Partner.Quantity] = group;
  2538. ShmPsuGrouping->GroupCollection[master].Partner.ParallelFlag[ShmPsuGrouping->GroupCollection[master].Partner.Quantity] = flag;
  2539. ShmPsuGrouping->GroupCollection[master].Partner.Quantity++;
  2540. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  2541. {
  2542. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity++;
  2543. //ShmPsuGrouping->GroupCollection[master].GunPsuQuantity += ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity;
  2544. UpdateGunTotalPsuQuantity(master);
  2545. #if PSU_DEBUG_MSG
  2546. LOG_INFO("Gun %d Add Group [%02X], Quantity: %d (%d), Flag: %04X",
  2547. master + 1, group, ShmPsuGrouping->GroupCollection[master].Partner.Quantity,
  2548. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity, flag);
  2549. #endif
  2550. }
  2551. //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);
  2552. }
  2553. }
  2554. // master: master group index
  2555. void AddSpecificMember(unsigned char master, PsuGroupPartner *partner)
  2556. {
  2557. char strMember[64];
  2558. sprintf(strMember, "Gun %d Add Member:", master + 1);
  2559. for(int i = 0; i < partner->Quantity; i++)
  2560. {
  2561. AddMember(partner->Member[i], master, partner->ParallelFlag[i]);
  2562. char strSlave[8];
  2563. sprintf(strSlave, " [%02X]", partner->Member[i]);
  2564. strcat(strMember, strSlave);
  2565. }
  2566. if(partner->Quantity > 0)
  2567. {
  2568. LOG_INFO("%s", strMember);
  2569. }
  2570. }
  2571. // master: master group index
  2572. void AddAvailableMember(unsigned char master)
  2573. {
  2574. PsuGroupPartner partner;
  2575. FindPsuGroupPartner(master, MAX_GROUP_QUANTITY, &partner, _EXTEND_TYPE_QUANTITY, MAX_GROUP_QUANTITY);
  2576. AddSpecificMember(master, &partner);
  2577. }
  2578. // master: master group index
  2579. void AddDummyMaster(unsigned char master, PsuGroupPartner *partner)
  2580. {
  2581. ShmPsuGrouping->GroupCollection[master].AlternateMaster = partner->Member[0] + 1;
  2582. AddSpecificMember(master, partner);
  2583. }
  2584. // master: master group index
  2585. // slave: slave group index
  2586. void RemoveMember(unsigned char master, unsigned char slave)
  2587. {
  2588. bool find = false;
  2589. int location = 0;
  2590. unsigned char other = 0;
  2591. unsigned short parallelFlag = 0;
  2592. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2593. {
  2594. if(slave == ShmPsuGrouping->GroupCollection[master].Partner.Member[i])
  2595. {
  2596. ShmPsuGrouping->GroupCollection[master].Partner.Member[i] = 0;
  2597. location = i;
  2598. find = true;
  2599. break;
  2600. }
  2601. }
  2602. if(find)
  2603. {
  2604. for(int i = location + 1; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2605. {
  2606. other = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2607. parallelFlag = ShmPsuGrouping->GroupCollection[master].Partner.ParallelFlag[i];
  2608. ShmPsuGrouping->GroupCollection[master].Partner.Member[i] = 0;
  2609. ShmPsuGrouping->GroupCollection[master].Partner.Member[i - 1] = other;
  2610. ShmPsuGrouping->GroupCollection[master].Partner.ParallelFlag[i - 1] = parallelFlag;
  2611. }
  2612. ShmPsuGrouping->GroupCollection[master].Partner.Quantity--;
  2613. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  2614. {
  2615. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity--;
  2616. //ShmPsuGrouping->GroupCollection[master].GunPsuQuantity -= ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity;
  2617. UpdateGunTotalPsuQuantity(master);
  2618. }
  2619. #if PSU_DEBUG_MSG
  2620. LOG_INFO("Gun %d Remove Group [%02X], Quantity: %d (%d)",
  2621. master + 1, slave, ShmPsuGrouping->GroupCollection[master].Partner.Quantity,
  2622. ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity);
  2623. #endif
  2624. }
  2625. }
  2626. // master: master group index
  2627. void RemoveNonGroupMember(unsigned char master)
  2628. {
  2629. unsigned char slave = 0;
  2630. char strMember[64];
  2631. PsuGroupPartner partner;
  2632. bool find = false;
  2633. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  2634. {
  2635. sprintf(strMember, "Gun %d Remove Member:", master + 1);
  2636. memcpy(&partner, &ShmPsuGrouping->GroupCollection[master].Partner, sizeof(PsuGroupPartner));
  2637. for(int i = 0; i < partner.Quantity; i++)
  2638. {
  2639. slave = partner.Member[i];
  2640. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE)
  2641. {
  2642. RemoveMember(master, slave);
  2643. if(ShmPsuGrouping->GroupCollection[slave].ReservedTarget == 0)
  2644. {
  2645. SetPsuGroupToIdle(slave);
  2646. }
  2647. else
  2648. {
  2649. SetPsuGroupRole(slave, _GROLE_WAIT_SLAVE);
  2650. }
  2651. char strSlave[8];
  2652. sprintf(strSlave, " [%02X]", slave);
  2653. strcat(strMember, strSlave);
  2654. find = true;
  2655. }
  2656. }
  2657. if(find)
  2658. {
  2659. LOG_INFO("%s", strMember);
  2660. }
  2661. }
  2662. }
  2663. void SetPsuGroupPowerOnOff(unsigned char group, unsigned char power_on_off)
  2664. {
  2665. if(group < CONNECTOR_QUANTITY)
  2666. {
  2667. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  2668. {
  2669. #if ONE_MODULE_OUTPUT
  2670. // for safety test (inrush current)
  2671. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.InPrechargeMode)
  2672. {
  2673. PsuSingleOnOff(ShmPsuPosition->GroupLocationInfo[group].PsuSN[0], power_on_off);
  2674. }
  2675. else
  2676. {
  2677. PsuGroupOnOff(group, power_on_off);
  2678. }
  2679. #else
  2680. PsuGroupOnOff(group, power_on_off);
  2681. #endif
  2682. LedGroupOnOff(group, power_on_off);
  2683. }
  2684. isStartOutputSwitch[group] = power_on_off == _PSU_ON ? true : false;
  2685. }
  2686. }
  2687. // master: master group index
  2688. unsigned short GetPresentTargetCurrent(unsigned char master, unsigned char role_condition)
  2689. {
  2690. unsigned char slave = 0;
  2691. unsigned short current = 0;
  2692. current = ShmPsuGrouping->GroupOutput[master].GTargetCurrent;
  2693. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2694. {
  2695. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2696. if(role_condition == _GROLE_MASTER || ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE)
  2697. {
  2698. current += ShmPsuGrouping->GroupOutput[slave].GTargetCurrent;
  2699. }
  2700. }
  2701. return current;
  2702. }
  2703. // group: group index
  2704. int GetPsuModuleQuantity(unsigned char group)
  2705. {
  2706. int quantity = 0;
  2707. unsigned char slave = 0;
  2708. quantity = ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity;
  2709. for(int i = 0; i < ShmPsuGrouping->GroupCollection[group].Partner.Quantity; i++)
  2710. {
  2711. slave = ShmPsuGrouping->GroupCollection[group].Partner.Member[i];
  2712. quantity += ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity;
  2713. }
  2714. return quantity;
  2715. }
  2716. // group: group index
  2717. bool IsAvailableGroup(unsigned char group)
  2718. {
  2719. return ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0 ? true : false;
  2720. }
  2721. // master: master group index
  2722. int GetMasterAvailableGroup(unsigned char master)
  2723. {
  2724. int quantity = 0;
  2725. unsigned char slave = 0;
  2726. quantity = IsAvailableGroup(master) ? quantity + 1 : quantity;
  2727. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2728. {
  2729. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2730. quantity = IsAvailableGroup(slave) ? quantity + 1 : quantity;
  2731. }
  2732. return quantity;
  2733. }
  2734. // group: group index
  2735. void UpdateGunLoading(unsigned char group)
  2736. {
  2737. if(ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup != 0 &&
  2738. group == ShmChargerInfo->PsuGrouping.GroupCollection[group].TargetGroup - 1)
  2739. {
  2740. unsigned short TargetCurrent = (int)(chargingInfo[group]->EvBatterytargetCurrent * 10);
  2741. ShmPsuGrouping->GroupCollection[group].GunLoading = chargingInfo[group]->AvailableChargingCurrent != 0 ?
  2742. (TargetCurrent * 10000 / (int)chargingInfo[group]->AvailableChargingCurrent) : 0;
  2743. if(chargingInfo[group]->PresentChargingVoltage >= (PSU_MIN_VOL / 10))
  2744. {
  2745. // unit: 1kW
  2746. float _diffPowerByCurrent = 0, _diffPowerByPower = 0, _presentTargerPower = 0;
  2747. _presentTargerPower = ((((float)ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000);
  2748. // DiffPower_Available
  2749. ShmPsuGrouping->GroupCollection[group].DiffPower_Available = (((float)(chargingInfo[group]->AvailableChargingCurrent - ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent) / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000;
  2750. // DiffPower_Capability
  2751. _diffPowerByCurrent = (((float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[group].CapabilityCurrent - ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent) / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000;
  2752. _diffPowerByPower = (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[group].CapabilityPower / 10) - _presentTargerPower;
  2753. ShmPsuGrouping->GroupCollection[group].DiffPower_Capability = _diffPowerByPower < _diffPowerByCurrent ? _diffPowerByPower : _diffPowerByCurrent;
  2754. // DiffPower_PhysicalLimit
  2755. ShmPsuGrouping->GroupCollection[group].DiffPower_PhysicalLimit = (((float)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[group].RemoteMaxPhysicalCurrent - ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent) / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000;
  2756. // DiffPower_ConfigLimit
  2757. _diffPowerByCurrent = (((float)(ShmChargerInfo->OutputLimit.GunLimitCurrent[group] - ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent) / 10) * chargingInfo[group]->PresentChargingVoltage) / 1000;
  2758. _diffPowerByPower = (ShmChargerInfo->OutputLimit.GunLimitPower[group] / 10) - _presentTargerPower;
  2759. ShmPsuGrouping->GroupCollection[group].DiffPower_ConfigLimit = _diffPowerByPower < _diffPowerByCurrent ? _diffPowerByPower : _diffPowerByCurrent;
  2760. }
  2761. else
  2762. {
  2763. ShmPsuGrouping->GroupCollection[group].DiffPower_Available = 0;
  2764. ShmPsuGrouping->GroupCollection[group].DiffPower_Capability = 0;
  2765. ShmPsuGrouping->GroupCollection[group].DiffPower_PhysicalLimit = 0;
  2766. ShmPsuGrouping->GroupCollection[group].DiffPower_ConfigLimit = 0;
  2767. }
  2768. }
  2769. else
  2770. {
  2771. ShmPsuGrouping->GroupCollection[group].GunLoading = 0;
  2772. }
  2773. }
  2774. // group: group index
  2775. void UpdatePsuGroupLoading(unsigned char group)
  2776. {
  2777. ShmPsuGrouping->GroupOutput[group].OutputLoading = ShmPsuData->PsuGroup[group].GroupAvailableCurrent != 0 ?
  2778. (ShmPsuGrouping->GroupOutput[group].GTargetCurrent * 10000 / ShmPsuData->PsuGroup[group].GroupAvailableCurrent) : 0;
  2779. }
  2780. // master: master group index
  2781. void UpdateMasterPsuGroupLoading(unsigned char master)
  2782. {
  2783. unsigned char slave = 0;
  2784. UpdatePsuGroupLoading(master);
  2785. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2786. {
  2787. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2788. UpdatePsuGroupLoading(slave);
  2789. }
  2790. }
  2791. void PsuGroupIncreaseCurrent(unsigned char group, unsigned short needIncrease, unsigned short *realIncreaseCurrent)
  2792. {
  2793. unsigned short increase = needIncrease;
  2794. if((ShmPsuGrouping->GroupOutput[group].GTargetCurrent + increase) <= ShmPsuData->PsuGroup[group].GroupAvailableCurrent)
  2795. {
  2796. ShmPsuGrouping->GroupOutput[group].GTargetCurrent += increase;
  2797. }
  2798. else
  2799. {
  2800. increase = ShmPsuData->PsuGroup[group].GroupAvailableCurrent - ShmPsuGrouping->GroupOutput[group].GTargetCurrent;
  2801. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = ShmPsuData->PsuGroup[group].GroupAvailableCurrent;
  2802. }
  2803. *realIncreaseCurrent += increase;
  2804. }
  2805. void PsuGroupDecreaseCurrent(unsigned char group, unsigned short needDecrease, unsigned short *realDecreaseCurrent)
  2806. {
  2807. unsigned short decrease = needDecrease;
  2808. if(ShmPsuGrouping->GroupOutput[group].GTargetCurrent >= (decrease + ZERO_CURRENT))
  2809. {
  2810. ShmPsuGrouping->GroupOutput[group].GTargetCurrent -= decrease;
  2811. }
  2812. else
  2813. {
  2814. decrease = ShmPsuGrouping->GroupOutput[group].GTargetCurrent - ZERO_CURRENT;
  2815. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = ZERO_CURRENT;
  2816. }
  2817. *realDecreaseCurrent += decrease;
  2818. }
  2819. void IncreaseCurrentByGunLoading(unsigned char group, unsigned short loading, unsigned short *realIncrease, unsigned short MaxIncrease)
  2820. {
  2821. unsigned short target = 0, need = 0;
  2822. if(*realIncrease >= MaxIncrease)
  2823. {
  2824. return;
  2825. }
  2826. target = (loading * ShmPsuData->PsuGroup[group].GroupAvailableCurrent) / 10000;
  2827. need = target >= ShmPsuGrouping->GroupOutput[group].GTargetCurrent ?
  2828. (target - ShmPsuGrouping->GroupOutput[group].GTargetCurrent) : 0;
  2829. need = need > (MaxIncrease - *realIncrease) ? (MaxIncrease - *realIncrease) : need;
  2830. //LOG_INFO("Group [%02X] Target Current %d.%d A, Increase Current: %d.%d A",
  2831. // group, (target / 10), (target % 10), (need / 10), (need % 10));
  2832. if(need > 0)
  2833. {
  2834. PsuGroupIncreaseCurrent(group, need, realIncrease);
  2835. }
  2836. }
  2837. void DecreaseCurrentByGunLoading(unsigned char group, unsigned short loading, unsigned short *realDecrease, unsigned short MaxDecrease)
  2838. {
  2839. unsigned short target = 0, need = 0;
  2840. if(*realDecrease >= MaxDecrease)
  2841. {
  2842. return;
  2843. }
  2844. target = (loading * ShmPsuData->PsuGroup[group].GroupAvailableCurrent) / 10000;
  2845. need = ShmPsuGrouping->GroupOutput[group].GTargetCurrent >= target ?
  2846. (ShmPsuGrouping->GroupOutput[group].GTargetCurrent - target) : 0;
  2847. need = need > (MaxDecrease - *realDecrease) ? (MaxDecrease - *realDecrease) : need;
  2848. //LOG_INFO("Group [%02X] Target Current %d.%d A, Decrease Current: %d.%d A",
  2849. // group, (target / 10), (target % 10), (need / 10), (need % 10));
  2850. if(need > 0)
  2851. {
  2852. PsuGroupDecreaseCurrent(group, need, realDecrease);
  2853. }
  2854. }
  2855. // master: master group index
  2856. void AverageIncreaseCurrent(unsigned char master, unsigned short NeedIncreaseCurrent)
  2857. {
  2858. unsigned char availableGroup = 0, slave = 0;
  2859. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realIncrease = 0;
  2860. unsigned char OutputMaster = 0;
  2861. #if PSU_DEBUG_MSG
  2862. PSU_LOG("Gun %d Increase By GunLoading %d.%02d Percent",
  2863. master + 1, (ShmPsuGrouping->GroupCollection[master].GunLoading / 100), (ShmPsuGrouping->GroupCollection[master].GunLoading % 100));
  2864. #endif
  2865. OutputMaster = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ? master : (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1);
  2866. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0)
  2867. {
  2868. IncreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &realIncrease, NeedIncreaseCurrent);
  2869. }
  2870. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2871. {
  2872. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2873. IncreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &realIncrease, NeedIncreaseCurrent);
  2874. }
  2875. availableGroup = GetMasterAvailableGroup(master);
  2876. if(NeedIncreaseCurrent > realIncrease && availableGroup > 0)
  2877. {
  2878. // increase current by group quantity
  2879. avgGroupCurrent = (NeedIncreaseCurrent - realIncrease) / availableGroup;
  2880. #if PSU_DEBUG_MSG
  2881. PSU_LOG("Gun %d Increase By Group, AvgGroupCurrent %d.%d A",
  2882. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  2883. #endif
  2884. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0)
  2885. {
  2886. PsuGroupIncreaseCurrent(master, avgGroupCurrent, &realIncrease);
  2887. }
  2888. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2889. {
  2890. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2891. PsuGroupIncreaseCurrent(slave, avgGroupCurrent, &realIncrease);
  2892. }
  2893. }
  2894. if(NeedIncreaseCurrent > realIncrease)
  2895. {
  2896. // increase excess current to master group
  2897. excessCurrent = NeedIncreaseCurrent - realIncrease;
  2898. #if PSU_DEBUG_MSG
  2899. PSU_LOG("Gun %d Increase, ExcessCurrent %d.%d A", master + 1, (excessCurrent / 10), (excessCurrent % 10));
  2900. #endif
  2901. PsuGroupIncreaseCurrent(OutputMaster, excessCurrent, &realIncrease);
  2902. }
  2903. }
  2904. // master: master group index
  2905. void AverageDecreaseCurrent(unsigned char master, unsigned short NeedDecreaseCurrent)
  2906. {
  2907. unsigned char availableGroup = 0, slave = 0;
  2908. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realDecrease = 0;
  2909. unsigned char OutputMaster = 0;
  2910. #if PSU_DEBUG_MSG
  2911. PSU_LOG("Gun %d Decrease By GunLoading %d.%02d Percent",
  2912. master + 1, (ShmPsuGrouping->GroupCollection[master].GunLoading / 100), (ShmPsuGrouping->GroupCollection[master].GunLoading % 100));
  2913. #endif
  2914. OutputMaster = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ? master : (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1);
  2915. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0)
  2916. {
  2917. DecreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &realDecrease, NeedDecreaseCurrent);
  2918. }
  2919. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2920. {
  2921. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2922. DecreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &realDecrease, NeedDecreaseCurrent);
  2923. }
  2924. availableGroup = GetMasterAvailableGroup(master);
  2925. if(NeedDecreaseCurrent > realDecrease)
  2926. {
  2927. // decrease current by group quantity
  2928. avgGroupCurrent = (NeedDecreaseCurrent - realDecrease) / availableGroup;
  2929. #if PSU_DEBUG_MSG
  2930. PSU_LOG("Gun %d Decrease By Group, AvgGroupCurrent %d.%d A",
  2931. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  2932. #endif
  2933. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0)
  2934. {
  2935. PsuGroupDecreaseCurrent(master, avgGroupCurrent, &realDecrease);
  2936. }
  2937. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2938. {
  2939. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2940. PsuGroupDecreaseCurrent(slave, avgGroupCurrent, &realDecrease);
  2941. }
  2942. }
  2943. if(NeedDecreaseCurrent > realDecrease)
  2944. {
  2945. // decrease excess current
  2946. excessCurrent = NeedDecreaseCurrent - realDecrease;
  2947. #if PSU_DEBUG_MSG
  2948. PSU_LOG("Gun %d Decrease, ExcessCurrent %d.%d A", master + 1, (excessCurrent / 10), (excessCurrent % 10));
  2949. #endif
  2950. PsuGroupDecreaseCurrent(OutputMaster, excessCurrent, &realDecrease);
  2951. }
  2952. }
  2953. void MasterIncreaseCurrent(unsigned char master, unsigned short NeedIncreaseCurrent)
  2954. {
  2955. unsigned short excessCurrent = 0, realIncrease = 0;
  2956. PsuGroupIncreaseCurrent(master, NeedIncreaseCurrent, &realIncrease);
  2957. if(NeedIncreaseCurrent > realIncrease)
  2958. {
  2959. excessCurrent = NeedIncreaseCurrent - realIncrease;
  2960. AverageIncreaseCurrent(master, excessCurrent);
  2961. }
  2962. }
  2963. void MasterDecreaseCurrent(unsigned char master, unsigned short NeedDecreaseCurrent)
  2964. {
  2965. unsigned char slave = 0;
  2966. unsigned short avgGroupCurrent = 0, excessCurrent = 0, realDecrease = 0;
  2967. if(ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity > 0)
  2968. {
  2969. // decrease current by group quantity
  2970. avgGroupCurrent = NeedDecreaseCurrent / ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity;
  2971. #if PSU_DEBUG_MSG
  2972. PSU_LOG("Gun %d Decrease By Group, AvgGroupCurrent %d.%d A",
  2973. master + 1, (avgGroupCurrent / 10), (avgGroupCurrent % 10));
  2974. #endif
  2975. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  2976. {
  2977. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  2978. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0)
  2979. {
  2980. PsuGroupDecreaseCurrent(slave, avgGroupCurrent, &realDecrease);
  2981. }
  2982. }
  2983. }
  2984. if(NeedDecreaseCurrent > realDecrease)
  2985. {
  2986. excessCurrent = NeedDecreaseCurrent - realDecrease;
  2987. PsuGroupDecreaseCurrent(master, excessCurrent, &realDecrease);
  2988. }
  2989. }
  2990. void UpdateMaxCurrent(unsigned char master, unsigned short current)
  2991. {
  2992. int time = 0;
  2993. // update max stage current
  2994. if(current > StageMaxCurrent[master])
  2995. {
  2996. StageMaxCurrent[master] = current;
  2997. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  2998. GetClockTime(&_StageCurrent_time[master]);
  2999. }
  3000. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent)
  3001. {
  3002. time = GetTimeoutValue(_StageCurrent_time[master]) / uSEC_VAL;
  3003. if(time >= EV_MAX_STAGE_CURRENT_DELAY)
  3004. {
  3005. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = true;
  3006. LOG_INFO("Gun %d Reach Max Stage Current", master + 1);
  3007. }
  3008. }
  3009. // update max target current and reset max current time
  3010. if(current > MaxCurrentDemand[master])
  3011. {
  3012. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  3013. {
  3014. LOG_INFO("Gun %d Max Current Demand Timer Reset", master + 1);
  3015. }
  3016. MaxCurrentDemand[master] = current;
  3017. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand = false;
  3018. GetClockTime(&_MaxCurrent_time[master]);
  3019. }
  3020. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  3021. {
  3022. time = GetTimeoutValue(_MaxCurrent_time[master]) / uSEC_VAL;
  3023. if(time >= EV_MAX_CURRENT_DELAY)
  3024. {
  3025. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand = true;
  3026. LOG_INFO("Gun %d Reach Max Current Demand", master + 1);
  3027. }
  3028. }
  3029. }
  3030. // master: master group index
  3031. bool IsMasterOutputCurrentStable(unsigned char master)
  3032. {
  3033. int time = 0;
  3034. bool stable = false, reachTarget = false;
  3035. unsigned short presentOutput = (int)chargingInfo[master]->PresentChargingCurrent * 10;
  3036. unsigned short TargetCurrent = 0;
  3037. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  3038. reachTarget = presentOutput > (TargetCurrent - REACH_CURRENT_TOLERANCE) && presentOutput < (TargetCurrent + REACH_CURRENT_TOLERANCE);
  3039. if((presentOutput >= StableOutputCurrent[master] && presentOutput - StableOutputCurrent[master] > REACH_CURRENT_TOLERANCE) ||
  3040. (presentOutput < StableOutputCurrent[master] && StableOutputCurrent[master] - presentOutput > REACH_CURRENT_TOLERANCE))
  3041. {
  3042. GetClockTime(&_ReachCurrent_time[master]);
  3043. StableOutputCurrent[master] = presentOutput;
  3044. }
  3045. time = GetTimeoutValue(_ReachCurrent_time[master]) / uSEC_VAL;
  3046. if(reachTarget || time >= CURRENT_STABLE_TIME ||
  3047. (ShmChargerInfo->Control.FCharging[master].FCtrl.bits.EnableForceCharging && time >= CURRENT_REACH_TARGET_TIME))
  3048. {
  3049. stable = true;
  3050. }
  3051. return stable;
  3052. }
  3053. // master: master group index
  3054. void MasterBalanceCurrent(unsigned char master)
  3055. {
  3056. int time = 0;
  3057. unsigned char slave = 0;
  3058. unsigned short realBalance = 0, grabCurrent = 0;
  3059. time = GetTimeoutValue(_BalanceCurrent_time[master]) / mSEC_VAL;
  3060. if(time >= BALANCE_CURRENT_INTERVAL)
  3061. {
  3062. //LOG_INFO("Gun %d Start Balance Current", master + 1);
  3063. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0)
  3064. {
  3065. DecreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  3066. }
  3067. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3068. {
  3069. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3070. DecreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  3071. }
  3072. //LOG_INFO("Gun %d Grab Balance Current %d.%d A", master + 1, (grabCurrent / 10), (grabCurrent % 10));
  3073. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0)
  3074. {
  3075. IncreaseCurrentByGunLoading(master, ShmPsuGrouping->GroupCollection[master].GunLoading, &realBalance, grabCurrent);
  3076. }
  3077. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3078. {
  3079. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3080. IncreaseCurrentByGunLoading(slave, ShmPsuGrouping->GroupCollection[master].GunLoading, &realBalance, grabCurrent);
  3081. }
  3082. //LOG_INFO("Gun %d Increase Balance Current %d.%d A", master + 1, (realBalance / 10), (realBalance % 10));
  3083. UpdateMasterPsuGroupLoading(master);
  3084. GetClockTime(&_BalanceCurrent_time[master]);
  3085. }
  3086. }
  3087. unsigned short GetLoadingDiff(unsigned char master)
  3088. {
  3089. unsigned char slave = 0;
  3090. unsigned short maxLoading = 0, minLoading = 0, diffLoading = 0;
  3091. unsigned char OutputMaster = 0;
  3092. OutputMaster = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ? master : (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1);
  3093. maxLoading = ShmPsuGrouping->GroupOutput[OutputMaster].OutputLoading;
  3094. minLoading = ShmPsuGrouping->GroupOutput[OutputMaster].OutputLoading;
  3095. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3096. {
  3097. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3098. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity == 0 || ShmPsuData->PsuGroup[slave].GroupAvailableCurrent == 0)
  3099. {
  3100. continue;
  3101. }
  3102. maxLoading = ShmPsuGrouping->GroupOutput[slave].OutputLoading > maxLoading ?
  3103. ShmPsuGrouping->GroupOutput[slave].OutputLoading : maxLoading;
  3104. minLoading = ShmPsuGrouping->GroupOutput[slave].OutputLoading < minLoading ?
  3105. ShmPsuGrouping->GroupOutput[slave].OutputLoading : minLoading;
  3106. }
  3107. diffLoading = maxLoading - minLoading;
  3108. return diffLoading;
  3109. }
  3110. void CheckCurrentBalance(unsigned char master)
  3111. {
  3112. unsigned short diffLoading = 0;
  3113. diffLoading = GetLoadingDiff(master);
  3114. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance)
  3115. {
  3116. if(diffLoading >= START_BALANCE_CRITERIA)
  3117. {
  3118. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = true;
  3119. PSU_LOG("Gun %d Output Current Is Unbalance, diffLoading = %d.%02d", master + 1, (diffLoading / 100), (diffLoading % 100));
  3120. GetClockTime(&_BalanceCurrent_time[master]);
  3121. }
  3122. }
  3123. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance)
  3124. {
  3125. MasterBalanceCurrent(master);
  3126. diffLoading = GetLoadingDiff(master);
  3127. if(diffLoading <= STOP_BALANCE_CRITERIA)
  3128. {
  3129. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  3130. PSU_LOG("Gun %d Output Current Is Balance", master + 1);
  3131. }
  3132. }
  3133. }
  3134. // master: master group index
  3135. // slave: slave group index
  3136. bool IsGroupPartner(unsigned char master, unsigned char slave)
  3137. {
  3138. bool find = false;
  3139. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3140. {
  3141. if(slave == ShmPsuGrouping->GroupCollection[master].Partner.Member[i])
  3142. {
  3143. find = true;
  3144. break;
  3145. }
  3146. }
  3147. return find;
  3148. }
  3149. void CleanSmoothDerating(unsigned char master)
  3150. {
  3151. ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget = 0;
  3152. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating = false;
  3153. }
  3154. void SetSmoothDerating(unsigned char master, unsigned char slave)
  3155. {
  3156. ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget = slave + 1;
  3157. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating = true;
  3158. GetClockTime(&_SmoothDerating_time[master]);
  3159. }
  3160. // master: master group index
  3161. void CheckSmoothBalance(unsigned char master)
  3162. {
  3163. int time = 0;
  3164. unsigned char slave = 0;
  3165. unsigned short realBalance = 0, grabCurrent = 0;
  3166. unsigned char _ReleaseSlave = 0;
  3167. if(ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget == 0 ||
  3168. !IsGroupPartner(master, ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget - 1))
  3169. {
  3170. CleanSmoothDerating(master);
  3171. LOG_INFO("Gun %d Smooth Derating Target Is Not Available!", master + 1);
  3172. return;
  3173. }
  3174. unsigned short _CapCurrent = 0; // unit: 0.1A
  3175. unsigned short _ReleaseCurrent = 0; // unit: 0.1A
  3176. unsigned short _ReleaseLoading = 0; // unit: 0.01%
  3177. unsigned short TargetCurrent = 0; // unit: 0.1A
  3178. PsuGroupPartner ReleaseMember;
  3179. _CapCurrent = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  3180. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  3181. _ReleaseSlave = ShmPsuGrouping->GroupCollection[master].SmoothDeratingTarget - 1;
  3182. _ReleaseCurrent = (int)chargingInfo[master]->AvailableChargingCurrent > (int)ShmPsuData->PsuGroup[_ReleaseSlave].GroupAvailableCurrent ?
  3183. (int)chargingInfo[master]->AvailableChargingCurrent - (int)ShmPsuData->PsuGroup[_ReleaseSlave].GroupAvailableCurrent : 0;
  3184. _ReleaseLoading = _ReleaseCurrent != 0 ? (TargetCurrent * 10000 / _ReleaseCurrent) : 0;
  3185. if(ShmPsuGrouping->GroupOutput[_ReleaseSlave].GTargetCurrent <= MAX_ADJ_BALANCE_CURRENT)
  3186. {
  3187. float originalPower = 0, capabilityPower = 0, releasePower = 0;
  3188. memset(&ReleaseMember, 0x00, sizeof(PsuGroupPartner));
  3189. ReleaseMember.Member[0] = _ReleaseSlave;
  3190. ReleaseMember.Quantity = 1;
  3191. PrepareToPowerOff(0, &ReleaseMember);
  3192. originalPower = ((chargingInfo[master]->AvailableChargingCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3193. capabilityPower = ((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3194. releasePower = (((float)(_ReleaseCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3195. LOG_INFO("Gun %d Smooth Release [%02X]", master + 1, ReleaseMember.Member[0]);
  3196. LOG_INFO("Gun %d Available %d A (%d kW) >> %d A (%d kW)",
  3197. master + 1,
  3198. ((int)chargingInfo[master]->AvailableChargingCurrent / 10), (int)originalPower,
  3199. (_ReleaseCurrent / 10), (int)releasePower);
  3200. LOG_INFO("Gun %d Capability %d A (%d kW) >> %d A (%d kW)",
  3201. master + 1,
  3202. ((int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent / 10), (int)capabilityPower,
  3203. _ReleaseCurrent >= _CapCurrent ? (_CapCurrent / 10) : (_ReleaseCurrent / 10), (int)releasePower);
  3204. CleanSmoothDerating(master);
  3205. return;
  3206. }
  3207. time = GetTimeoutValue(_SmoothDerating_time[master]) / mSEC_VAL;
  3208. if(time >= SMOOTH_DERATING_INTERVAL)
  3209. {
  3210. DecreaseCurrentByGunLoading(master, _ReleaseLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  3211. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3212. {
  3213. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3214. if(slave != _ReleaseSlave)
  3215. {
  3216. DecreaseCurrentByGunLoading(slave, _ReleaseLoading, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  3217. }
  3218. else
  3219. {
  3220. DecreaseCurrentByGunLoading(slave, 0, &grabCurrent, MAX_ADJ_BALANCE_CURRENT);
  3221. }
  3222. }
  3223. //LOG_INFO("Gun %d Grab Balance Current %d.%d A", master + 1, (grabCurrent / 10), (grabCurrent % 10));
  3224. IncreaseCurrentByGunLoading(master, _ReleaseLoading, &realBalance, grabCurrent);
  3225. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3226. {
  3227. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3228. if(slave != _ReleaseSlave)
  3229. {
  3230. IncreaseCurrentByGunLoading(slave, _ReleaseLoading, &realBalance, grabCurrent);
  3231. }
  3232. }
  3233. //LOG_INFO("Gun %d Increase Balance Current %d.%d A", master + 1, (realBalance / 10), (realBalance % 10));
  3234. UpdateMasterPsuGroupLoading(master);
  3235. GetClockTime(&_SmoothDerating_time[master]);
  3236. }
  3237. }
  3238. bool IsOtherCharging(unsigned char group)
  3239. {
  3240. bool find = false;
  3241. int other = 0, selfTarget = 0, location = 0;
  3242. if(ShmPsuGrouping->GroupCollection[group].TargetGroup == 0)
  3243. {
  3244. return find;
  3245. }
  3246. selfTarget = ShmPsuGrouping->GroupCollection[group].TargetGroup - 1;
  3247. // search from left
  3248. location = ShmPsuGrouping->GroupCollection[group].Location - 1;
  3249. for(int i = location; i >= 0; i--)
  3250. {
  3251. other = ShmPsuGrouping->Layout[i];
  3252. if(ShmPsuGrouping->GroupCollection[other].Role == _GROLE_MASTER)
  3253. {
  3254. find = other != selfTarget ? true : false;
  3255. break;
  3256. }
  3257. }
  3258. if(!find)
  3259. {
  3260. // search from right
  3261. location = ShmPsuGrouping->GroupCollection[group].Location + 1;
  3262. for(int i = location; i < ShmChargerInfo->Control.MaxConnector; i++)
  3263. {
  3264. other = ShmPsuGrouping->Layout[i];
  3265. if(ShmPsuGrouping->GroupCollection[other].Role == _GROLE_MASTER)
  3266. {
  3267. find = other != selfTarget ? true : false;
  3268. break;
  3269. }
  3270. }
  3271. }
  3272. return find;
  3273. }
  3274. // master: master group index
  3275. void MasterReleasePsuGroup(unsigned char master)
  3276. {
  3277. bool findOtherInCharging = false;
  3278. unsigned char slave = 0, member = 0, releaseTarget = 0;
  3279. unsigned char otherCharging[MAX_GROUP_QUANTITY] = {0};
  3280. unsigned char releaseList[MAX_GROUP_QUANTITY] = {0};
  3281. float releasePower[MAX_GROUP_QUANTITY] = {0};
  3282. unsigned short releaseCurrent[MAX_GROUP_QUANTITY] = {0};
  3283. PsuGroupPartner ReleaseMember;
  3284. unsigned short target = 0, oriAvailableCurrent = 0, oriCapabilityCurrent = 0, maxReleaseCurrent = 0;
  3285. float targetPower = 0, oriAvailablePower = 0, capabilityPower = 0, maxReleasePower = 0;
  3286. if(ShmChargerInfo->PsuGrouping.GroupCollection[master].Role != _GROLE_MASTER ||
  3287. ShmPsuGrouping->GroupCollection[master].Partner.Quantity == 0)
  3288. {
  3289. return;
  3290. }
  3291. target = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  3292. targetPower = (((float)(target) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3293. oriAvailableCurrent = (int)chargingInfo[master]->AvailableChargingCurrent;
  3294. oriAvailablePower = (((float)oriAvailableCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3295. oriCapabilityCurrent = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  3296. capabilityPower = (((float)oriCapabilityCurrent / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3297. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3298. {
  3299. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3300. unsigned short current = 0;
  3301. PsuGroupPowerOffCheck(slave, &ReleaseMember);
  3302. otherCharging[i] = IsOtherCharging(slave);
  3303. findOtherInCharging = otherCharging[i] ? otherCharging[i] : findOtherInCharging;
  3304. releaseList[i] = slave;
  3305. for(int j = 0; j < ReleaseMember.Quantity; j++)
  3306. {
  3307. member = ReleaseMember.Member[j];
  3308. current += ShmPsuData->PsuGroup[member].GroupAvailableCurrent;
  3309. }
  3310. releaseCurrent[i] = current;
  3311. float power = (((float)(oriAvailableCurrent - releaseCurrent[i]) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3312. if(power >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  3313. (oriAvailableCurrent - releaseCurrent[i]) >= oriCapabilityCurrent)
  3314. {
  3315. releasePower[i] = power;
  3316. }
  3317. else
  3318. {
  3319. releasePower[i] = 0;
  3320. }
  3321. //LOG_INFO("Gun %d Release [%02X], Total Release %d Group, Gun Loading %d.%02d >> %d.%02d", master + 1, slave, ReleaseMember.Quantity,
  3322. // (originalLoading / 100), (originalLoading % 100),
  3323. // (releaseLoading[i] / 100), (releaseLoading[i] % 100));
  3324. }
  3325. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3326. {
  3327. if((otherCharging[i] || !findOtherInCharging) &&
  3328. releasePower[i] > maxReleasePower && releasePower[i] != 0)
  3329. {
  3330. maxReleasePower = releasePower[i];
  3331. maxReleaseCurrent = releaseCurrent[i];
  3332. releaseTarget = releaseList[i];
  3333. }
  3334. }
  3335. if(maxReleasePower > 0)
  3336. {
  3337. unsigned short _current = 0;
  3338. _current = oriAvailableCurrent - maxReleaseCurrent;
  3339. ShmPsuGrouping->GroupCollection[releaseTarget].GroupCtrl.bits.SlavePowerOffRequest = true;
  3340. LOG_INFO("Gun %d Set [%02X] Release(%s)",
  3341. master + 1, releaseTarget, findOtherInCharging ? "Other In Charging" : "Normal");
  3342. LOG_INFO("Gun %d Available %d A (%d kW) >> %d A (%d kW)",
  3343. master + 1,
  3344. (oriAvailableCurrent / 10), (int)oriAvailablePower, (_current / 10), (int)maxReleasePower);
  3345. LOG_INFO("Gun %d Capability %d A (%d kW) >> %d A (%d kW)",
  3346. master + 1,
  3347. (oriCapabilityCurrent / 10), (int)capabilityPower,
  3348. _current >= oriCapabilityCurrent ? (oriCapabilityCurrent / 10) : (_current / 10), (int)maxReleasePower);
  3349. }
  3350. }
  3351. // master: master group index
  3352. void MasterDynamicRelease(unsigned char master)
  3353. {
  3354. unsigned char slave = 0;
  3355. PsuGroupPartner ReleaseMember;
  3356. unsigned short target = 0, releaseCurrent = 0, originalAvailable = 0, originalCapability = 0;
  3357. float targetPower = 0, originalPower = 0, capabilityPower = 0, releasePower = 0;
  3358. if(ShmChargerInfo->PsuGrouping.GroupCollection[master].Role != _GROLE_MASTER ||
  3359. ShmPsuGrouping->GroupCollection[master].Partner.Quantity == 0)
  3360. {
  3361. return;
  3362. }
  3363. target = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  3364. targetPower = (((float)(target) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3365. originalAvailable = (int)chargingInfo[master]->AvailableChargingCurrent;
  3366. originalCapability = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  3367. releaseCurrent = 0;
  3368. memset(&ReleaseMember, 0x00, sizeof(PsuGroupPartner));
  3369. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3370. {
  3371. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3372. releasePower = (((float)(originalAvailable - releaseCurrent - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3373. if(releasePower >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  3374. (originalAvailable - releaseCurrent - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) >= originalCapability)
  3375. {
  3376. releaseCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  3377. ReleaseMember.Member[ReleaseMember.Quantity] = slave;
  3378. ReleaseMember.Quantity++;
  3379. }
  3380. }
  3381. if(ReleaseMember.Quantity > 0)
  3382. {
  3383. char strMember[64];
  3384. memset(strMember, 0x00, sizeof(strMember));
  3385. for(int i = 0; i < ReleaseMember.Quantity; i++)
  3386. {
  3387. char strTemp[16];
  3388. sprintf(strTemp, " [%02X]", ReleaseMember.Member[i]);
  3389. strcat(strMember, strTemp);
  3390. }
  3391. PrepareToPowerOff(0, &ReleaseMember);
  3392. originalPower = (((float)originalAvailable / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3393. capabilityPower = (((float)originalCapability / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3394. releasePower = (((float)(originalAvailable - releaseCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3395. LOG_INFO("Gun %d Direct Release%s", master + 1, strMember);
  3396. LOG_INFO("Gun %d Available %d A (%d kW) >> %d A (%d kW)",
  3397. master + 1,
  3398. (originalAvailable / 10), (int)originalPower,
  3399. ((originalAvailable - releaseCurrent) / 10), (int)releasePower);
  3400. LOG_INFO("Gun %d Capability %d A (%d kW) >> %d A (%d kW)",
  3401. master + 1,
  3402. (originalCapability / 10), (int)capabilityPower,
  3403. ((originalAvailable - releaseCurrent) / 10), (int)releasePower);
  3404. }
  3405. }
  3406. void FindDeratingPriority(unsigned char master, PsuGroupPartner *tPartner)
  3407. {
  3408. unsigned char slave = 0;
  3409. memset(tPartner, 0x00, sizeof(PsuGroupPartner));
  3410. if((ShmChargerInfo->Control.MaxConnector - 1 - ShmPsuGrouping->GroupCollection[master].Location) > ShmPsuGrouping->GroupCollection[master].Location - 0)
  3411. {
  3412. for(int i = ShmChargerInfo->Control.MaxConnector - 1; i >= 0; i--)
  3413. {
  3414. slave = ShmPsuGrouping->Layout[i];
  3415. if(slave != master)
  3416. {
  3417. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master &&
  3418. (ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 || slave != (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1)))
  3419. {
  3420. tPartner->Member[tPartner->Quantity++] = slave;
  3421. }
  3422. }
  3423. else
  3424. {
  3425. break;
  3426. }
  3427. }
  3428. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  3429. {
  3430. slave = ShmPsuGrouping->Layout[i];
  3431. if(slave != master)
  3432. {
  3433. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master &&
  3434. (ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 || slave != (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1)))
  3435. {
  3436. tPartner->Member[tPartner->Quantity++] = slave;
  3437. }
  3438. }
  3439. else
  3440. {
  3441. break;
  3442. }
  3443. }
  3444. }
  3445. else
  3446. {
  3447. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  3448. {
  3449. slave = ShmPsuGrouping->Layout[i];
  3450. if(slave != master)
  3451. {
  3452. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master &&
  3453. (ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 || slave != (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1)))
  3454. {
  3455. tPartner->Member[tPartner->Quantity++] = slave;
  3456. }
  3457. }
  3458. else
  3459. {
  3460. break;
  3461. }
  3462. }
  3463. for(int i = ShmChargerInfo->Control.MaxConnector - 1; i >= 0; i--)
  3464. {
  3465. slave = ShmPsuGrouping->Layout[i];
  3466. if(slave != master)
  3467. {
  3468. if((ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1) == master &&
  3469. (ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 || slave != (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1)))
  3470. {
  3471. tPartner->Member[tPartner->Quantity++] = slave;
  3472. }
  3473. }
  3474. else
  3475. {
  3476. break;
  3477. }
  3478. }
  3479. }
  3480. }
  3481. // master: master group index
  3482. void CheckSmoothDerating(unsigned char master)
  3483. {
  3484. unsigned char slave = 0;
  3485. unsigned short target = 0, originalAvailable = 0, originalCapability = 0;
  3486. float targetPower = 0, releasePower = 0;
  3487. PsuGroupPartner releasePriority;
  3488. if(ShmChargerInfo->PsuGrouping.GroupCollection[master].Role != _GROLE_MASTER ||
  3489. ShmPsuGrouping->GroupCollection[master].Partner.Quantity == 0 ||
  3490. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating)
  3491. {
  3492. return;
  3493. }
  3494. target = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  3495. targetPower = (((float)(target) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3496. originalAvailable = (int)chargingInfo[master]->AvailableChargingCurrent;
  3497. originalCapability = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[master].CapabilityCurrent;
  3498. FindDeratingPriority(master, &releasePriority);
  3499. for(int i = 0; i < releasePriority.Quantity; i++)
  3500. {
  3501. slave = releasePriority.Member[i];
  3502. releasePower = (((float)(originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3503. if(releasePower >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  3504. (originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) >= originalCapability)
  3505. {
  3506. SetSmoothDerating(master, slave);
  3507. LOG_INFO("Gun %d Set Smooth Derating Member: [%02X]", master + 1, slave);
  3508. break;
  3509. }
  3510. }
  3511. /*
  3512. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3513. {
  3514. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3515. releasePower = (((float)(originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) / 10) * chargingInfo[master]->PresentChargingVoltage) / 1000;
  3516. if(releasePower >= (targetPower + MAX_DIFF_POWER_TO_DERATING) ||
  3517. (originalAvailable - ShmPsuData->PsuGroup[slave].GroupAvailableCurrent) >= originalCapability)
  3518. {
  3519. SetSmoothDerating(master, slave);
  3520. LOG_INFO("Gun %d Set Smooth Derating Member: [%02X]", master + 1, slave);
  3521. break;
  3522. }
  3523. }
  3524. */
  3525. }
  3526. void CheckReleaseOrExtend(unsigned char master)
  3527. {
  3528. int time = 0;
  3529. PsuGroupPartner partner;
  3530. if((ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation || Is_EnableReleaseAndExtendWhenForceCharging(master)) &&
  3531. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue == 0 &&
  3532. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue == 0)
  3533. {
  3534. if(ShmPsuGrouping->GroupCollection[master].DiffPower_Available >= MIN_RELEASE_POWER)
  3535. {
  3536. if((ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 && ShmPsuGrouping->GroupCollection[master].Partner.Quantity > 0) ||
  3537. (ShmPsuGrouping->GroupCollection[master].AlternateMaster != 0 && ShmPsuGrouping->GroupCollection[master].Partner.Quantity > 1))
  3538. {
  3539. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent &&
  3540. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxCurrentDemand)
  3541. {
  3542. #if ENABLE_SMOOTH_DERATING == 0
  3543. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  3544. {
  3545. MasterReleasePsuGroup(master);
  3546. }
  3547. else
  3548. {
  3549. MasterDynamicRelease(master);
  3550. }
  3551. #else
  3552. CheckSmoothDerating(master);
  3553. #endif
  3554. }
  3555. }
  3556. }
  3557. else if(ShmPsuGrouping->GroupCollection[master].DiffPower_Available < MAX_DIFF_POWER_TO_EXTEND &&
  3558. ShmPsuGrouping->GroupCollection[master].DiffPower_Capability < MAX_DIFF_POWER_TO_EXTEND &&
  3559. ShmPsuGrouping->GroupCollection[master].DiffPower_PhysicalLimit >= MIN_DIFF_PHYSICAL_POWER)
  3560. {
  3561. time = GetSecTimeoutValue(_ExtendCapability_time[master]);
  3562. if(time >= EXTEND_AFTER_DERATING_DELAY)
  3563. {
  3564. int available = GetPsuGroupAvailable(master);
  3565. if(available > 0)
  3566. {
  3567. if(ShmPsuGrouping->GroupCollection[master].DiffPower_ConfigLimit >= MIN_DIFF_CONFIG_POWER)
  3568. {
  3569. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendAvailable)
  3570. {
  3571. LOG_INFO("Gun %d Extend Capability Available, DifPcap: %.1f kW, DiffConfig: %.1f kW, DifPphy: %.1f kW",
  3572. master + 1,
  3573. ShmPsuGrouping->GroupCollection[master].DiffPower_Capability,
  3574. ShmPsuGrouping->GroupCollection[master].DiffPower_ConfigLimit,
  3575. ShmPsuGrouping->GroupCollection[master].DiffPower_PhysicalLimit);
  3576. GetClockTime(&_ExtendCapability_time[master]);
  3577. }
  3578. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendAvailable = true;
  3579. }
  3580. }
  3581. else
  3582. {
  3583. if(!(ShmPsuGrouping->GroupCollection[master].DiffPower_ConfigLimit < MIN_DIFF_CONFIG_POWER && ShmChargerInfo->OutputLimit.IndividualLimit[master] > 0))
  3584. {
  3585. // check & grab
  3586. time = GetSecTimeoutValue(_ExtendGrab_time[master]);
  3587. if(time >= EXTEND_GRAB_INTERVAL)
  3588. {
  3589. if(ExtendCapabilityGrabCheck(master, &partner))
  3590. {
  3591. PrepareToPowerOff(0, &partner);
  3592. }
  3593. GetClockTime(&_ExtendGrab_time[master]);
  3594. }
  3595. }
  3596. }
  3597. }
  3598. }
  3599. }
  3600. }
  3601. bool IsPsuGroupOutputConfigDifferent(unsigned char group)
  3602. {
  3603. bool different = false;
  3604. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage != PreGroupOutput[group].GTargetVoltage ||
  3605. ShmPsuGrouping->GroupOutput[group].GTargetCurrent != PreGroupOutput[group].GTargetCurrent)
  3606. {
  3607. different = true;
  3608. }
  3609. PreGroupOutput[group].GTargetVoltage = ShmPsuGrouping->GroupOutput[group].GTargetVoltage;
  3610. PreGroupOutput[group].GTargetCurrent = ShmPsuGrouping->GroupOutput[group].GTargetCurrent;
  3611. return different;
  3612. }
  3613. // Gun X Demand Change -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  3614. // Master [XX] -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  3615. // Member [XX] -> Voltage: XXXX.X V, Current: XXXX.X A, Available: XXXX.X A, Loading: XXX.XX
  3616. void ShowPsuGroupOutputConfig(unsigned char master)
  3617. {
  3618. unsigned char slave = 0;
  3619. PSU_LOG("Gun %d Demand Change -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  3620. master + 1, (ShmPsuGrouping->GroupOutput[master].GTargetVoltage / 10),
  3621. ((int)chargingInfo[master]->EvBatterytargetCurrent), (((int)(chargingInfo[master]->EvBatterytargetCurrent * 10)) % 10),
  3622. ((int)chargingInfo[master]->AvailableChargingCurrent / 10), ((int)chargingInfo[master]->AvailableChargingCurrent % 10),
  3623. (ShmPsuGrouping->GroupCollection[master].GunLoading / 100), (ShmPsuGrouping->GroupCollection[master].GunLoading % 100));
  3624. PSU_LOG(" Master [%02X] -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  3625. master, (ShmPsuGrouping->GroupOutput[master].GTargetVoltage / 10),
  3626. (ShmPsuGrouping->GroupOutput[master].GTargetCurrent / 10), (ShmPsuGrouping->GroupOutput[master].GTargetCurrent % 10),
  3627. (ShmPsuData->PsuGroup[master].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[master].GroupAvailableCurrent % 10),
  3628. (ShmPsuGrouping->GroupOutput[master].OutputLoading / 100), (ShmPsuGrouping->GroupOutput[master].OutputLoading % 100));
  3629. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3630. {
  3631. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3632. PSU_LOG(" Member [%02X] -> Voltage: %4d V, Current: %4d.%d A, Available: %4d.%d A, Loading: %3d.%02d",
  3633. slave, (ShmPsuGrouping->GroupOutput[slave].GTargetVoltage / 10),
  3634. (ShmPsuGrouping->GroupOutput[slave].GTargetCurrent / 10), (ShmPsuGrouping->GroupOutput[slave].GTargetCurrent % 10),
  3635. (ShmPsuData->PsuGroup[slave].GroupAvailableCurrent / 10), (ShmPsuData->PsuGroup[slave].GroupAvailableCurrent % 10),
  3636. (ShmPsuGrouping->GroupOutput[slave].OutputLoading / 100), (ShmPsuGrouping->GroupOutput[slave].OutputLoading % 100));
  3637. }
  3638. }
  3639. void PsuGroupOutputConfigCheck(unsigned char master)
  3640. {
  3641. unsigned char slave = 0;
  3642. bool show = false;
  3643. show = IsPsuGroupOutputConfigDifferent(master);
  3644. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3645. {
  3646. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3647. if(IsPsuGroupOutputConfigDifferent(slave))
  3648. {
  3649. show = true;
  3650. }
  3651. }
  3652. if(show)
  3653. {
  3654. ShowPsuGroupOutputConfig(master);
  3655. }
  3656. }
  3657. void StepRecognition(unsigned char master, unsigned short voltage)
  3658. {
  3659. if(_GfdStep[master] == PREPARE_STEP_NONE && voltage > 0)
  3660. {
  3661. // gfd step
  3662. _GfdStep[master] = PREPARE_STEP_CABLE_CHECK;
  3663. LOG_INFO("Gun %d GFD Start Step", master + 1);
  3664. }
  3665. else if(_GfdStep[master] == PREPARE_STEP_CABLE_CHECK && voltage == 0)
  3666. {
  3667. // gfd done step
  3668. _GfdStep[master] = PREPARE_STEP_GFD_DONE;
  3669. LOG_INFO("Gun %d GFD Stop Step", master + 1);
  3670. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.CableCheckDone = true;
  3671. }
  3672. else if(_GfdStep[master] == PREPARE_STEP_GFD_DONE && voltage >= SAFETY_PRECHARGE_OFFSET)
  3673. {
  3674. // precharge step
  3675. _GfdStep[master] = PREPARE_STEP_PRECHARGE;
  3676. LOG_INFO("Gun %d PreCharge Step", master + 1);
  3677. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode = true;
  3678. }
  3679. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode)
  3680. {
  3681. // charging step
  3682. if(_GfdStep[master] != PREPARE_STEP_CHARGING)
  3683. {
  3684. LOG_INFO("Gun %d Charging Step", master + 1);
  3685. }
  3686. _GfdStep[master] = PREPARE_STEP_CHARGING;
  3687. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode)
  3688. {
  3689. if(_VoltageResumeCnt[master] < MAX_CHARGING_DELAY_COUNT)
  3690. {
  3691. int time = GetTimeoutValue(_ChargingDelay_time[master]) / mSEC_VAL;
  3692. if(time >= CHARGING_DELAY_INTERVAL)
  3693. {
  3694. GetClockTime(&_ChargingDelay_time[master]);
  3695. _VoltageResumeCnt[master]++;
  3696. }
  3697. }
  3698. else
  3699. {
  3700. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode = false;
  3701. }
  3702. }
  3703. }
  3704. }
  3705. // master: master group index
  3706. // voltage: target voltage, unit: 0.1V
  3707. void UpdateOutputGroupVoltage(unsigned char master, unsigned short voltage)
  3708. {
  3709. unsigned char slave = 0;
  3710. ShmPsuGrouping->TotalGroupOutput[master].GTargetVoltage = voltage;
  3711. // set master group output voltage
  3712. ShmPsuGrouping->GroupOutput[master].GTargetVoltage = voltage;
  3713. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3714. {
  3715. // set slave group output voltage
  3716. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3717. #if ONE_MODULE_OUTPUT
  3718. // for safety test (inrush current)
  3719. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode)
  3720. {
  3721. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = 0;
  3722. }
  3723. else
  3724. {
  3725. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = voltage;
  3726. }
  3727. #else
  3728. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  3729. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  3730. {
  3731. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = voltage;
  3732. }
  3733. #endif
  3734. }
  3735. }
  3736. // master: master group index
  3737. // group: self group index
  3738. void UpdatePsuGroupOutputConfig(unsigned char master)
  3739. {
  3740. unsigned char slave = 0;
  3741. unsigned short TargetVoltage = 0, TargetCurrent = 0, PresentTargetCurrent = 0;
  3742. unsigned short current = 0;
  3743. unsigned char OutputMaster = 0;
  3744. OutputMaster = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ? master : (ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1);
  3745. TargetVoltage = (int)(chargingInfo[master]->EvBatterytargetVoltage * 10);
  3746. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  3747. if(Is_OutputK1K2_Ready(master) || ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.CableCheckDone)
  3748. {
  3749. // update target voltage
  3750. StepRecognition(master, TargetVoltage);
  3751. #if PRECHARGE_OFFSET
  3752. // for safety test (inrush current)
  3753. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode)
  3754. {
  3755. GCTargetVoltage[master] = _GfdStep[master] == PREPARE_STEP_PRECHARGE ?
  3756. TargetVoltage - SAFETY_PRECHARGE_OFFSET : TargetVoltage;
  3757. }
  3758. else
  3759. {
  3760. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.InPrechargeMode)
  3761. {
  3762. GCTargetVoltage[master] = TargetVoltage > (GCTargetVoltage[master] + VOLTAGE_RESUME_STEP) ?
  3763. (GCTargetVoltage[master] + VOLTAGE_RESUME_STEP) : TargetVoltage;
  3764. }
  3765. else
  3766. {
  3767. GCTargetVoltage[master] = TargetVoltage;
  3768. }
  3769. }
  3770. #else
  3771. GCTargetVoltage[master] = TargetVoltage;
  3772. #endif
  3773. UpdateOutputGroupVoltage(master, GCTargetVoltage[master]);
  3774. // update target current
  3775. if(((ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity + 1) * ZERO_CURRENT) > TargetCurrent ||
  3776. !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode)
  3777. {
  3778. current = ZERO_CURRENT;
  3779. #if PSU_DEBUG_MSG
  3780. if(ShmPsuGrouping->GroupOutput[master].GTargetCurrent != current)
  3781. {
  3782. LOG_INFO("Gun %d Need Minimum Target Current = %d", master + 1, current);
  3783. }
  3784. #endif
  3785. ShmPsuGrouping->GroupOutput[master].GTargetCurrent = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ? current : 0;
  3786. ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent = ShmPsuGrouping->GroupOutput[master].GTargetCurrent;
  3787. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3788. {
  3789. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3790. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE || ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  3791. {
  3792. #if ONE_MODULE_OUTPUT
  3793. // for safety test (inrush current)
  3794. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = 0;
  3795. #else
  3796. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent =
  3797. ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0 ? ZERO_CURRENT : 0;
  3798. #endif
  3799. ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent += ShmPsuGrouping->GroupOutput[slave].GTargetCurrent;
  3800. }
  3801. }
  3802. if(ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent < 20)
  3803. {
  3804. unsigned short diffCurrent = 0;
  3805. diffCurrent = 20 - ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent;
  3806. ShmPsuGrouping->GroupOutput[OutputMaster].GTargetCurrent += diffCurrent;
  3807. }
  3808. }
  3809. else
  3810. {
  3811. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  3812. {
  3813. // master in normal output mode
  3814. PresentTargetCurrent = GetPresentTargetCurrent(master, _GROLE_MASTER);
  3815. current = TargetCurrent <= (int)chargingInfo[master]->AvailableChargingCurrent ?
  3816. TargetCurrent : (int)chargingInfo[master]->AvailableChargingCurrent;
  3817. #if PSU_DEBUG_MSG
  3818. if(GCTargetCurrent[master] != current)
  3819. {
  3820. LOG_INFO("Gun %d Need Normal Target Current = %d.%d A", master + 1, (current / 10), (current % 10));
  3821. }
  3822. #endif
  3823. ShmPsuGrouping->TotalGroupOutput[master].GTargetCurrent = current;
  3824. GCTargetCurrent[master] = current;
  3825. UpdateMaxCurrent(master, GCTargetCurrent[master]);
  3826. if(GCTargetCurrent[master] > PresentTargetCurrent)
  3827. {
  3828. // increase current
  3829. OutputConfigStep[master] = _CURRENT_MODE_INCREASE;
  3830. PSU_LOG("Gun %d Increase Current: %d.%d A, %d.%d A -> %d.%d A", master + 1,
  3831. ((GCTargetCurrent[master] - PresentTargetCurrent) / 10), ((GCTargetCurrent[master] - PresentTargetCurrent) % 10),
  3832. (PresentTargetCurrent / 10), (PresentTargetCurrent % 10), (GCTargetCurrent[master] / 10), (GCTargetCurrent[master] % 10));
  3833. #if MASTER_OUTPUT_FIRST
  3834. // for safety test
  3835. if(GCTargetCurrent[master] <= CURRENT_BALANCE_CRITERIA)
  3836. {
  3837. MasterIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  3838. }
  3839. else
  3840. {
  3841. AverageIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  3842. }
  3843. #else
  3844. AverageIncreaseCurrent(master, GCTargetCurrent[master] - PresentTargetCurrent);
  3845. #endif
  3846. }
  3847. else if(GCTargetCurrent[master] < PresentTargetCurrent)
  3848. {
  3849. // decrease current
  3850. OutputConfigStep[master] = _CURRENT_MODE_DECREASE;
  3851. PSU_LOG("Gun %d Decrease Current: %d.%d A, %d.%d A -> %d.%d A", master + 1,
  3852. ((PresentTargetCurrent - GCTargetCurrent[master]) / 10), ((PresentTargetCurrent - GCTargetCurrent[master]) % 10),
  3853. (PresentTargetCurrent / 10), (PresentTargetCurrent % 10), (GCTargetCurrent[master] / 10), (GCTargetCurrent[master] % 10));
  3854. #if MASTER_OUTPUT_FIRST
  3855. // for safety test
  3856. if(GCTargetCurrent[master] <= ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity * CURRENT_BALANCE_CRITERIA)
  3857. {
  3858. MasterDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  3859. }
  3860. else
  3861. {
  3862. AverageDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  3863. }
  3864. #else
  3865. AverageDecreaseCurrent(master, PresentTargetCurrent - GCTargetCurrent[master]);
  3866. #endif
  3867. }
  3868. else
  3869. {
  3870. // balance current or do not change
  3871. OutputConfigStep[master] = _CURRENT_MODE_BALANCE;
  3872. if(OutputConfigStep[master] != _preOutputConfigStep[master])
  3873. {
  3874. GetClockTime(&_ReachCurrent_time[master]);
  3875. PSU_LOG("Gun %d In Balance Mode", master + 1);
  3876. StableOutputCurrent[master] = (int)chargingInfo[master]->PresentChargingCurrent * 10;
  3877. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable = false;
  3878. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  3879. }
  3880. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable)
  3881. {
  3882. if(IsMasterOutputCurrentStable(master))
  3883. {
  3884. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable = true;
  3885. LOG_INFO("Gun %d Output Current = %5.1f A Stable", master + 1, chargingInfo[master]->PresentChargingCurrent);
  3886. }
  3887. }
  3888. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable ||
  3889. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating)
  3890. {
  3891. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating)
  3892. {
  3893. CheckSmoothBalance(master);
  3894. }
  3895. else
  3896. {
  3897. #if MASTER_OUTPUT_FIRST
  3898. // for safety test
  3899. if(GCTargetCurrent[master] > ShmPsuGrouping->GroupCollection[master].Partner.RealQuantity * CURRENT_BALANCE_CRITERIA)
  3900. {
  3901. CheckCurrentBalance(master);
  3902. }
  3903. else
  3904. {
  3905. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance = false;
  3906. }
  3907. #else
  3908. CheckCurrentBalance(master);
  3909. #endif
  3910. }
  3911. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedCurrentBalance &&
  3912. !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.SmoothDerating)
  3913. {
  3914. CheckReleaseOrExtend(master);
  3915. }
  3916. }
  3917. }
  3918. }
  3919. else
  3920. {
  3921. // master in derating mode
  3922. OutputConfigStep[master] = _CURRENT_MODE_DERATING;
  3923. if(OutputConfigStep[master] != _preOutputConfigStep[master])
  3924. {
  3925. LOG_INFO("Gun %d In Derating Mode", master + 1);
  3926. }
  3927. }
  3928. _preOutputConfigStep[master] = OutputConfigStep[master];
  3929. }
  3930. }
  3931. else
  3932. {
  3933. ShmPsuGrouping->GroupOutput[master].GTargetVoltage = 0;
  3934. ShmPsuGrouping->GroupOutput[master].GTargetCurrent = 0;
  3935. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  3936. {
  3937. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  3938. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = 0;
  3939. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = 0;
  3940. }
  3941. }
  3942. }
  3943. // group: self group index
  3944. void SetPsuGroupOutput(unsigned char group)
  3945. {
  3946. int time = 0;
  3947. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  3948. {
  3949. if(ShmPsuGrouping->GroupOutput[group].GTargetVoltage >= PSU_MIN_VOL)
  3950. {
  3951. #if ONE_MODULE_OUTPUT
  3952. // for safety test (inrush current)
  3953. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.InPrechargeMode)
  3954. {
  3955. SingleOutputVol(ShmPsuPosition->GroupLocationInfo[group].PsuSN[0], ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3956. }
  3957. else
  3958. {
  3959. PresentOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3960. }
  3961. #else
  3962. PresentOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3963. #endif
  3964. if(!isStartOutputSwitch[group])
  3965. {
  3966. SetPsuGroupPowerOnOff(group, _PSU_ON);
  3967. GetClockTime(&_PoweOnOff_time[group]);
  3968. }
  3969. }
  3970. else
  3971. {
  3972. if(isStartOutputSwitch[group])
  3973. {
  3974. SetPsuGroupPowerOnOff(group, _PSU_OFF);
  3975. GetClockTime(&_PoweOnOff_time[group]);
  3976. }
  3977. #if ONE_MODULE_OUTPUT
  3978. // for safety test (inrush current)
  3979. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.InPrechargeMode)
  3980. {
  3981. SingleOutputVol(ShmPsuPosition->GroupLocationInfo[group].PsuSN[0], ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3982. }
  3983. else
  3984. {
  3985. PresentOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3986. }
  3987. #else
  3988. PresentOutputVol(group, ShmPsuGrouping->GroupOutput[group].GTargetVoltage, ShmPsuGrouping->GroupOutput[group].GTargetCurrent);
  3989. #endif
  3990. }
  3991. }
  3992. time = GetTimeoutValue(_PoweOnOff_time[group]) / uSEC_VAL;
  3993. if(time > POWER_ONOFF_RESEND_INTERVAL)
  3994. {
  3995. SetPsuGroupPowerOnOff(group, isStartOutputSwitch[group] ? _PSU_ON : _PSU_OFF);
  3996. GetClockTime(&_PoweOnOff_time[group]);
  3997. }
  3998. //UpdatePsuGroupLoading(group);
  3999. UpdateGunLoading(group);
  4000. }
  4001. // master: master group index
  4002. bool CanMasterStartDerating(unsigned char master)
  4003. {
  4004. bool start = false;
  4005. unsigned short TargetCurrent = 0;
  4006. TargetCurrent = (int)(chargingInfo[master]->EvBatterytargetCurrent * 10);
  4007. if(TargetCurrent <= ShmPsuGrouping->GroupCollection[master].ReAssignAvailableCurrent)
  4008. {
  4009. start = true;
  4010. }
  4011. return start;
  4012. }
  4013. // master: master group index
  4014. // role_condition: when role_condition = _GROLE_MASTER, stop all member
  4015. void SetMemberStartPowerOff(unsigned char master, unsigned char role_condition)
  4016. {
  4017. unsigned char slave = 0;
  4018. char strMember[64];
  4019. bool find = false;
  4020. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  4021. {
  4022. sprintf(strMember, "Gun %d Set Member:", master + 1);
  4023. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  4024. {
  4025. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  4026. if(role_condition == _GROLE_MASTER || ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  4027. {
  4028. SetPsuGroupRole(slave, _GROLE_SLAVE_POWER_OFF);
  4029. char strSlave[8];
  4030. sprintf(strSlave, " [%02X]", slave);
  4031. strcat(strMember, strSlave);
  4032. find = true;
  4033. }
  4034. }
  4035. if(find)
  4036. {
  4037. strcat(strMember, " Power Off");
  4038. LOG_INFO("%s", strMember);
  4039. }
  4040. }
  4041. }
  4042. // master: master group index
  4043. bool IsMemberPowerOffOK(unsigned char master)
  4044. {
  4045. bool done = true;
  4046. unsigned char slave = 0;
  4047. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  4048. {
  4049. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  4050. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE && ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SWITCH_OFF_OK)
  4051. {
  4052. done = false;
  4053. }
  4054. }
  4055. return done;
  4056. }
  4057. // master: master group index
  4058. void SetMemberPowerOffDone(unsigned char master)
  4059. {
  4060. unsigned char slave = 0;
  4061. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  4062. {
  4063. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].Partner.Quantity; i++)
  4064. {
  4065. slave = ShmPsuGrouping->GroupCollection[master].Partner.Member[i];
  4066. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SWITCH_OFF_OK)
  4067. {
  4068. SetPsuGroupRole(slave, _GROLE_WAIT_IDLE);
  4069. }
  4070. else if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  4071. {
  4072. // can not power off normally
  4073. SetPsuGroupRole(slave, _GROLE_TERMINATE);
  4074. }
  4075. }
  4076. }
  4077. }
  4078. unsigned char _ParallelRelay[MAX_GROUP_QUANTITY];
  4079. void SetPCabinetParallelRelay(unsigned short parallelConfig)
  4080. {
  4081. bool change = false;
  4082. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4083. {
  4084. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4085. {
  4086. change = false;
  4087. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DeviceStatus_Idle ||
  4088. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DeviceStatus_Alarm ||
  4089. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DeviceStatus_Charging)
  4090. {
  4091. for(int j = 0; j < GENERAL_GUN_QUANTITY; j++)
  4092. {
  4093. _ParallelRelay[j] = (parallelConfig & (1 << j)) > 0 ? YES : NO;
  4094. if(_ParallelRelay[i] != ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRelaySetting[i])
  4095. {
  4096. change = true;
  4097. }
  4098. }
  4099. if(change)
  4100. {
  4101. char strParallel[128];
  4102. sprintf(strParallel, "Set PCabinet %d Parallel Relay:", i + 1);
  4103. for(int j = 0; j < GENERAL_GUN_QUANTITY; j++)
  4104. {
  4105. char strState[8];
  4106. sprintf(strState, " %3s", _ParallelRelay[j] ? "ON" : "OFF");
  4107. strcat(strParallel, strState);
  4108. }
  4109. LOG_INFO("%s", strParallel);
  4110. }
  4111. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRelaySetting[i] = _ParallelRelay[i];
  4112. }
  4113. }
  4114. }
  4115. }
  4116. // master: master group index
  4117. // Yes_No: Yes: relay on, No: relay off
  4118. // role_condition: when role_condition = _GROLE_MASTER, parallel relay of all member do the action(on or off)
  4119. void SetParallelRelayOnOff(unsigned char master, unsigned char Yes_No, unsigned char role_condition)
  4120. {
  4121. //int ParallelConfig = 0;
  4122. unsigned char slave = 0;
  4123. PsuGroupPartner *partner;
  4124. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER ||
  4125. ShmPsuGrouping->GroupCollection[master].Role == _GROLE_DUMMY_MASTER ||
  4126. (ShmPsuGrouping->GroupCollection[master].Role == _GROLE_REQUEST_TO_CHARGING && Yes_No))
  4127. {
  4128. ShmPsuGrouping->GroupCollection[master].ParallelCheck = 0;
  4129. partner = role_condition == _GROLE_PRECHARGE_READY ? &ShmPsuGrouping->GroupCollection[master].PossibleMember : &ShmPsuGrouping->GroupCollection[master].Partner;
  4130. for(int i = 0; i < partner->Quantity; i++)
  4131. {
  4132. slave = partner->Member[i];
  4133. if(role_condition == _GROLE_MASTER || ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE)
  4134. {
  4135. //ParallelConfig = ShmPsuGrouping->GroupCollection[master].ParallelConfig[slave];
  4136. //if(ParallelConfig != 0)
  4137. //{
  4138. // ShmPsuGrouping->GroupCollection[master].ParallelCheck |= (1 << (ParallelConfig - 1));
  4139. //}
  4140. ShmPsuGrouping->GroupCollection[master].ParallelCheck |= partner->ParallelFlag[i];
  4141. }
  4142. }
  4143. if(Yes_No)
  4144. {
  4145. ShmPsuGrouping->ParallelRelayConfig.CtrlValue |= ShmPsuGrouping->GroupCollection[master].ParallelCheck;
  4146. }
  4147. else
  4148. {
  4149. ShmPsuGrouping->ParallelRelayConfig.CtrlValue &= ~ShmPsuGrouping->GroupCollection[master].ParallelCheck;
  4150. }
  4151. SetPCabinetParallelRelay(ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  4152. }
  4153. }
  4154. // master: master group index
  4155. bool IsParallelRelayConfirmed(unsigned char master)
  4156. {
  4157. bool confirmed = true;
  4158. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4159. {
  4160. if((1 << i) & ShmPsuGrouping->GroupCollection[master].ParallelCheck)
  4161. {
  4162. if((ShmPsuGrouping->ParallelRelayConfig.CtrlValue & (1 << i)) != (ShmPsuGrouping->ParallelRelayConfirmed.CtrlValue & (1 << i)))
  4163. {
  4164. confirmed = false;
  4165. break;
  4166. }
  4167. }
  4168. }
  4169. return confirmed;
  4170. }
  4171. // master: master group index
  4172. bool IsPossibleMemberReady(unsigned char master)
  4173. {
  4174. bool ready = true;
  4175. unsigned char slave = 0;
  4176. if(ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity > 0)
  4177. {
  4178. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  4179. {
  4180. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  4181. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_WAIT_SLAVE)
  4182. {
  4183. ready = false;
  4184. }
  4185. }
  4186. }
  4187. return ready;
  4188. }
  4189. // master: master group index
  4190. bool IsExtendPrechargeReady(unsigned char master)
  4191. {
  4192. bool ready = true;
  4193. unsigned char slave = 0;
  4194. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  4195. {
  4196. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  4197. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity == 0 || ShmPsuData->PsuGroup[slave].GroupAvailableCurrent == 0)
  4198. {
  4199. continue;
  4200. }
  4201. unsigned short voltage = 0;
  4202. voltage = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ?
  4203. ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage : ShmPsuData->PsuGroup[ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1].GroupPresentOutputVoltage;
  4204. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_PREPARE_ATTACH_ON ||
  4205. ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage > (voltage + PRECHARGE_RANGE_VOLTAGE) ||
  4206. ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage < (voltage - PRECHARGE_OFFSET_VOLTAGE - PRECHARGE_RANGE_VOLTAGE))
  4207. {
  4208. ready = false;
  4209. }
  4210. }
  4211. return ready;
  4212. }
  4213. // master: master group index
  4214. bool CheckExtendPrechargeReady(unsigned char master)
  4215. {
  4216. bool ready = false;
  4217. unsigned char slave = 0;
  4218. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  4219. {
  4220. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  4221. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity == 0 || ShmPsuData->PsuGroup[slave].GroupAvailableCurrent == 0)
  4222. {
  4223. continue;
  4224. }
  4225. unsigned short voltage = 0;
  4226. voltage = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ?
  4227. ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage : ShmPsuData->PsuGroup[ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1].GroupPresentOutputVoltage;
  4228. char message[512];
  4229. sprintf(message, "Gun %d PsuGroup[%d] PresentVoltqage: %4d V ", master + 1, slave, (ShmPsuData->PsuGroup[slave].GroupPresentOutputVoltage / 10));
  4230. for(int j = 0; j < ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity; j++)
  4231. {
  4232. char strVoltage[32];
  4233. sprintf(strVoltage, " Psu[%d][%d] %4d V", slave, j, (ShmPsuData->PsuGroup[slave].PsuModule[j].PresentOutputVoltage / 10));
  4234. strcat(message, strVoltage);
  4235. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_ATTACH_ON &&
  4236. ShmPsuData->PsuGroup[slave].PsuModule[j].PresentOutputVoltage > (voltage + PRECHARGE_RANGE_VOLTAGE) &&
  4237. ShmPsuData->PsuGroup[slave].PsuModule[j].PresentOutputVoltage < (voltage - PRECHARGE_OFFSET_VOLTAGE - PRECHARGE_RANGE_VOLTAGE))
  4238. {
  4239. ready = true;
  4240. }
  4241. }
  4242. PSU_LOG("%s", message);
  4243. }
  4244. return ready;
  4245. }
  4246. // master: master group index
  4247. void SetExtendPrechargeCompleted(unsigned char master)
  4248. {
  4249. unsigned char slave = 0;
  4250. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  4251. {
  4252. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  4253. SetPsuGroupRole(slave, _GROLE_PRECHARGE_READY);
  4254. }
  4255. }
  4256. // master: master group index
  4257. void SetExtendPrechargeStop(unsigned char master)
  4258. {
  4259. unsigned char slave = 0;
  4260. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  4261. {
  4262. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  4263. SetPsuGroupRole(slave, _GROLE_EXTEND_STOP);
  4264. }
  4265. memset(&ShmPsuGrouping->GroupCollection[master].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  4266. }
  4267. // master: master group index
  4268. void AddExtendMember(unsigned char master)
  4269. {
  4270. unsigned char slave = 0;
  4271. unsigned short parallelFlag = 0;
  4272. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  4273. {
  4274. slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  4275. parallelFlag = ShmPsuGrouping->GroupCollection[master].PossibleMember.ParallelFlag[i];
  4276. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PRECHARGE_READY)
  4277. {
  4278. AddMember(slave, master, parallelFlag);
  4279. }
  4280. }
  4281. }
  4282. // group: self group index
  4283. void CheckChargingRequest(unsigned char group)
  4284. {
  4285. PsuGroupPartner partner;
  4286. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_IDLE)
  4287. {
  4288. return;
  4289. }
  4290. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest)
  4291. {
  4292. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  4293. {
  4294. SetPsuGroupRole(group, _GROLE_REQUEST_TO_CHARGING);
  4295. }
  4296. else
  4297. {
  4298. if(GetPsuGroupAvailable(group) > 0 || CheckAlternateMaster(group, &partner, YES))
  4299. {
  4300. SetPsuGroupRole(group, _GROLE_DUMMY_MASTER);
  4301. }
  4302. else
  4303. {
  4304. SetPsuGroupRole(group, _GROLE_NO_RESOURCE);
  4305. }
  4306. }
  4307. }
  4308. }
  4309. // group: self group index
  4310. void ChargingRequestProcess(unsigned char group)
  4311. {
  4312. int time = 0;
  4313. PsuGroupPartner partner;
  4314. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_REQUEST_TO_CHARGING)
  4315. {
  4316. return;
  4317. }
  4318. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequestConfirmed)
  4319. {
  4320. GetClockTime(&_ChargingRequest_time[group]);
  4321. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest = false;
  4322. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequestConfirmed = true;
  4323. #if GRAB_AT_START_CHARGING
  4324. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupShareCheck = true;
  4325. #else
  4326. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone = true;
  4327. #endif
  4328. }
  4329. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupShareCheck && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareConfirmed)
  4330. {
  4331. // check is there psu group to grab
  4332. if(PsuGroupGrabCheck(group, &partner))
  4333. {
  4334. GetClockTime(&_ChargingRequest_time[group]);
  4335. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GrabGroupWait = true;
  4336. PrepareToPowerOff(group + 1, &partner);
  4337. memcpy(&ShmPsuGrouping->GroupCollection[group].PossibleMember, &partner, sizeof(PsuGroupPartner));
  4338. }
  4339. else
  4340. {
  4341. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone = true;
  4342. LOG_INFO("Gun %d Grab Nothing", group + 1);
  4343. }
  4344. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareConfirmed = true;
  4345. }
  4346. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GrabGroupWait && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone)
  4347. {
  4348. // wait until grab psu ready or timeout
  4349. bool ready = IsPossibleMemberReady(group);
  4350. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  4351. if(ready || time >= WAIT_GRAB_TIME)
  4352. {
  4353. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone = true;
  4354. LOG_INFO("Gun %d Grab %s", group + 1, ready ? "OK" : "Timeout");
  4355. }
  4356. }
  4357. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ShareCheckDone && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.FindGroupPartner)
  4358. {
  4359. GetClockTime(&_ChargingRequest_time[group]);
  4360. AddAvailableMember(group);
  4361. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.FindGroupPartner = true;
  4362. }
  4363. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.FindGroupPartner && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayOn)
  4364. {
  4365. // after a little delay, set parallel relay on
  4366. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  4367. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  4368. {
  4369. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  4370. GetClockTime(&_ChargingRequest_time[group]);
  4371. SetParallelRelayOnOff(group, YES, _GROLE_MASTER);
  4372. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayOn = true;
  4373. LOG_INFO("Gun %d Charging Request Set All Member Parallel Relay On 0x%02X -> 0x%02X", group + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  4374. }
  4375. }
  4376. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayOn && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed)
  4377. {
  4378. // wait until parallel relay on confirmed
  4379. bool confirmed = IsParallelRelayConfirmed(group);
  4380. time = GetTimeoutValue(_ChargingRequest_time[group]) / uSEC_VAL;
  4381. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  4382. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  4383. {
  4384. GetClockTime(&_ChargingRequest_time[group]);
  4385. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed = true;
  4386. LOG_INFO("Gun %d Charging Request Parallel Relay Confirmed %s", group + 1, confirmed ? "OK" : "Timeout");
  4387. }
  4388. }
  4389. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ParallelRelayConfirmed && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupingDone)
  4390. {
  4391. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupingDone = true;
  4392. }
  4393. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.GroupingDone)
  4394. {
  4395. SetPsuGroupToMaster(group);
  4396. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue = 0;
  4397. LOG_INFO("Gun %d Grouping Completed", group + 1);
  4398. }
  4399. }
  4400. // group: self group index
  4401. void DummuMasterProcess(unsigned char group)
  4402. {
  4403. int time = 0;
  4404. PsuGroupPartner partner;
  4405. if(ShmPsuGrouping->GroupCollection[group].Role != _GROLE_DUMMY_MASTER)
  4406. {
  4407. return;
  4408. }
  4409. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterRequest && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterReqConfirmed)
  4410. {
  4411. LOG_INFO("Gun %d Dummy Master Request Start", group + 1);
  4412. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterReqConfirmed = true;
  4413. }
  4414. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterReqConfirmed && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterPermission)
  4415. {
  4416. memset(&partner, 0x00, sizeof(PsuGroupPartner));
  4417. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterPermission = true;
  4418. FindPsuGroupPartner(group, MAX_GROUP_QUANTITY, &partner, _EXTEND_TYPE_QUANTITY, 1);
  4419. if(partner.Quantity > 0)
  4420. {
  4421. GetClockTime(&_DummyMasterRequest_time[group]);
  4422. AddDummyMaster(group, &partner);
  4423. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterGrabCheck = true;
  4424. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFindPartner = true;
  4425. }
  4426. else
  4427. {
  4428. LOG_INFO("Gun %d need to make a grab for dummy master", group + 1);
  4429. }
  4430. }
  4431. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterPermission && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterGrabCheck)
  4432. {
  4433. memset(&partner, 0x00, sizeof(PsuGroupPartner));
  4434. // check is there psu group to grab
  4435. if(CheckAlternateMaster(group, &partner, YES))
  4436. {
  4437. GetClockTime(&_DummyMasterRequest_time[group]);
  4438. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterWait = true;
  4439. PrepareToPowerOff(group + 1, &partner);
  4440. memcpy(&ShmPsuGrouping->GroupCollection[group].PossibleMember, &partner, sizeof(PsuGroupPartner));
  4441. }
  4442. else
  4443. {
  4444. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterCompleted = true;
  4445. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFault = true;
  4446. LOG_INFO("Gun %d Dummy Master Grab Nothing", group + 1);
  4447. }
  4448. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterGrabCheck = true;
  4449. }
  4450. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterWait && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFindPartner)
  4451. {
  4452. // wait until grab psu ready or timeout
  4453. bool ready = IsPossibleMemberReady(group);
  4454. time = GetTimeoutValue(_DummyMasterRequest_time[group]) / uSEC_VAL;
  4455. if(ready || time >= WAIT_GRAB_TIME)
  4456. {
  4457. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterReady = true;
  4458. LOG_INFO("Gun %d Grab %s", group + 1, ready ? "OK" : "Timeout");
  4459. }
  4460. }
  4461. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterReady && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFindPartner)
  4462. {
  4463. GetClockTime(&_DummyMasterRequest_time[group]);
  4464. memset(&partner, 0x00, sizeof(PsuGroupPartner));
  4465. FindPsuGroupPartner(group, MAX_GROUP_QUANTITY, &partner, _EXTEND_TYPE_QUANTITY, 1);
  4466. if(partner.Quantity > 0)
  4467. {
  4468. AddDummyMaster(group, &partner);
  4469. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFindPartner = true;
  4470. }
  4471. else
  4472. {
  4473. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterCompleted = true;
  4474. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFault = true;
  4475. LOG_INFO("Gun %d Dummy Master Grab Fault", group + 1);
  4476. }
  4477. }
  4478. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFindPartner && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterRelayOn)
  4479. {
  4480. // after a little delay, set parallel relay on
  4481. time = GetTimeoutValue(_DummyMasterRequest_time[group]) / uSEC_VAL;
  4482. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  4483. {
  4484. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  4485. GetClockTime(&_DummyMasterRequest_time[group]);
  4486. SetParallelRelayOnOff(group, YES, _GROLE_MASTER);
  4487. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterRelayOn = true;
  4488. LOG_INFO("Gun %d Set Dummy Member Parallel Relay On 0x%02X -> 0x%02X", group + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  4489. }
  4490. }
  4491. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterRelayOn && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterRelayConfirmed)
  4492. {
  4493. // wait until parallel relay on confirmed
  4494. bool confirmed = IsParallelRelayConfirmed(group);
  4495. time = GetTimeoutValue(_DummyMasterRequest_time[group]) / uSEC_VAL;
  4496. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  4497. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  4498. {
  4499. GetClockTime(&_DummyMasterRequest_time[group]);
  4500. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterRelayConfirmed = true;
  4501. LOG_INFO("Gun %d Dummy Member Parallel Relay Confirmed %s", group + 1, confirmed ? "OK" : "Timeout");
  4502. }
  4503. }
  4504. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterRelayConfirmed && !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterCompleted)
  4505. {
  4506. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterCompleted = true;
  4507. }
  4508. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterCompleted)
  4509. {
  4510. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.DummyMasterFault)
  4511. {
  4512. SetPsuGroupRole(group, _GROLE_DUMMY_FAULT);
  4513. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DummyMasterCtrlValue = 0;
  4514. LOG_INFO("Gun %d Dummy Master Grouping Fault", group + 1);
  4515. }
  4516. else
  4517. {
  4518. SetPsuGroupToMaster(group);
  4519. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue = 0;
  4520. LOG_INFO("Gun %d Dummy Master Grouping Completed", group + 1);
  4521. }
  4522. }
  4523. }
  4524. // master: master group index
  4525. void PsuGroupDeratingProcess(unsigned char master)
  4526. {
  4527. int time = 0;
  4528. // slave group set NeedDerating flag to start re-assign
  4529. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedDerating)
  4530. {
  4531. // reduce output current capability start
  4532. GetClockTime(&_PsuGroupDerating_time[master]);
  4533. unsigned short ReAssignCurrent = GetPresentTargetCurrent(master, _GROLE_SLAVE);
  4534. ShmPsuGrouping->GroupCollection[master].ReAssignAvailableCurrent = ReAssignCurrent;
  4535. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.NeedDerating = false;
  4536. LOG_INFO("Gun %d DeratingConfirmed%s, ReAssignAvailableCurrent = %d.%d A",
  4537. master + 1,
  4538. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed ? " Again" : "",
  4539. (ReAssignCurrent / 10), (ReAssignCurrent % 10));
  4540. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  4541. {
  4542. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  4543. }
  4544. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed = true;
  4545. }
  4546. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingStart)
  4547. {
  4548. // wait until derating from ev or timeout
  4549. bool start = CanMasterStartDerating(master);
  4550. bool bypass = ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlreadyInChargingMode ? false : true;
  4551. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  4552. if(start || time >= WAIT_EV_DERATING_TIMEOUT || bypass)
  4553. {
  4554. GetClockTime(&_PsuGroupDerating_time[master]);
  4555. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingStart = true;
  4556. LOG_INFO("Gun %d %s Start Derating%s", master + 1, start ? "Normal" : "Force", bypass ? " (Bypass)" : "");
  4557. SetMemberStartPowerOff(master, _GROLE_PREPARE_SWITCH_OFF);
  4558. if(!bypass)
  4559. {
  4560. // update stage max current and reset max current time
  4561. StageMaxCurrent[master] = start ?
  4562. (int)(chargingInfo[master]->EvBatterytargetCurrent * 10) :
  4563. ShmPsuGrouping->GroupCollection[master].ReAssignAvailableCurrent;
  4564. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  4565. GetClockTime(&_StageCurrent_time[master]);
  4566. }
  4567. }
  4568. }
  4569. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingStart && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone)
  4570. {
  4571. // wait until psu member power off ok or timeout
  4572. bool power_off_ok = IsMemberPowerOffOK(master);
  4573. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  4574. if(power_off_ok || time >= WAIT_SLAVE_POWER_OFF_TIMEOUT)
  4575. {
  4576. GetClockTime(&_PsuGroupDerating_time[master]);
  4577. SetMemberPowerOffDone(master);
  4578. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone = true;
  4579. LOG_INFO("Gun %d Set Derating Member Power Off %s", master + 1, power_off_ok ? "OK" : "Timeout");
  4580. }
  4581. }
  4582. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingPowerOffDone && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayOff)
  4583. {
  4584. // after a little delay, set parallel relay off
  4585. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  4586. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  4587. {
  4588. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  4589. GetClockTime(&_PsuGroupDerating_time[master]);
  4590. SetParallelRelayOnOff(master, NO, _GROLE_SWITCH_OFF_OK);
  4591. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayOff = true;
  4592. LOG_INFO("Gun %d Set Parallel Relay Off 0x%02X -> 0x%02X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  4593. }
  4594. }
  4595. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayOff && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed)
  4596. {
  4597. // wait until parallel relay off confirmed
  4598. bool confirmed = IsParallelRelayConfirmed(master);
  4599. time = GetTimeoutValue(_PsuGroupDerating_time[master]) / uSEC_VAL;
  4600. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  4601. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  4602. {
  4603. GetClockTime(&_PsuGroupDerating_time[master]);
  4604. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed = true;
  4605. LOG_INFO("Gun %d Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  4606. }
  4607. }
  4608. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingRelayConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingCompleted)
  4609. {
  4610. // remove all non group member
  4611. RemoveNonGroupMember(master);
  4612. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingCompleted = true;
  4613. }
  4614. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingCompleted)
  4615. {
  4616. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  4617. UpdateGunAvailableCapability(master);
  4618. GetClockTime(&_ExtendCapability_time[master]);
  4619. LOG_INFO("Gun %d Derating Completed", master + 1);
  4620. }
  4621. }
  4622. void MasterStopChargingProcess(unsigned char master)
  4623. {
  4624. int time = 0;
  4625. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingRequest && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingConfirmed)
  4626. {
  4627. // set all member to power off
  4628. GetClockTime(&_StopCharging_time[master]);
  4629. SetMemberStartPowerOff(master, _GROLE_MASTER);
  4630. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingConfirmed = true;
  4631. }
  4632. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllPowerOffDone)
  4633. {
  4634. // wait until psu member power off ok or timeout
  4635. bool power_off_ok = IsMemberPowerOffOK(master);
  4636. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  4637. if(power_off_ok || time >= WAIT_SLAVE_POWER_OFF_TIMEOUT)
  4638. {
  4639. GetClockTime(&_StopCharging_time[master]);
  4640. SetMemberPowerOffDone(master);
  4641. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllPowerOffDone = true;
  4642. LOG_INFO("Gun %d Set All Member Power Off %s", master + 1, power_off_ok ? "OK" : "Timeout");
  4643. }
  4644. }
  4645. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllPowerOffDone && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayOff)
  4646. {
  4647. // after a little delay, set all member parallel relay off
  4648. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  4649. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  4650. {
  4651. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  4652. GetClockTime(&_StopCharging_time[master]);
  4653. SetParallelRelayOnOff(master, NO, _GROLE_MASTER);
  4654. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayOff = true;
  4655. LOG_INFO("Gun %d Set All Member Parallel Relay Off 0x%02X -> 0x%02X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  4656. }
  4657. }
  4658. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayOff && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed)
  4659. {
  4660. // wait until parallel relay off confirmed
  4661. bool confirmed = IsParallelRelayConfirmed(master);
  4662. time = GetTimeoutValue(_StopCharging_time[master]) / uSEC_VAL;
  4663. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  4664. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  4665. {
  4666. GetClockTime(&_StopCharging_time[master]);
  4667. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed = true;
  4668. LOG_INFO("Gun %d All Member Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  4669. }
  4670. }
  4671. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllParallelRelayConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted)
  4672. {
  4673. // remove all non group member
  4674. RemoveNonGroupMember(master);
  4675. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted = true;
  4676. LOG_INFO("Gun %d All Member Stop Completed", master + 1);
  4677. }
  4678. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AllMemberStopCompleted && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingCompleted)
  4679. {
  4680. // check self group power down or not
  4681. if(ShmPsuData->PsuGroup[master].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT)
  4682. {
  4683. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingCompleted = true;
  4684. }
  4685. }
  4686. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingCompleted)
  4687. {
  4688. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  4689. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  4690. SetPsuGroupToIdle(master);
  4691. LOG_INFO("Gun %d Stop Charging Completed", master + 1);
  4692. }
  4693. }
  4694. void SlaveStopChargingProcess(unsigned char slave)
  4695. {
  4696. int time = 0;
  4697. unsigned char master = 0;
  4698. PsuGroupPartner PowerOffMember;
  4699. #if GRAB_AT_START_CHARGING
  4700. PsuGroupPartner GrabMember;
  4701. #endif
  4702. if(ShmPsuGrouping->GroupCollection[slave].Role != _GROLE_SLAVE)
  4703. {
  4704. return;
  4705. }
  4706. master = ShmPsuGrouping->GroupCollection[slave].TargetGroup - 1;
  4707. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.CheckSlaveReady)
  4708. {
  4709. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.CheckSlaveReady = true;
  4710. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_REQUEST_TO_CHARGING)
  4711. {
  4712. LOG_INFO("Gun %d Need Wait Gun %d Grouping Completed", slave + 1, master + 1);
  4713. GetClockTime(&_CheckSlaveReady_time[slave]);
  4714. }
  4715. else if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER && ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed)
  4716. {
  4717. LOG_INFO("Gun %d Need Wait Gun %d Derating Completed", slave + 1, master + 1);
  4718. GetClockTime(&_CheckSlaveReady_time[slave]);
  4719. }
  4720. else
  4721. {
  4722. bool isAlternate = false;
  4723. if(ShmPsuGrouping->GroupCollection[master].AlternateMaster != 0)
  4724. {
  4725. if((ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1) == slave)
  4726. {
  4727. isAlternate = true;
  4728. }
  4729. }
  4730. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady = true;
  4731. if(isAlternate)
  4732. {
  4733. ShmPsuGrouping->GroupCollection[slave].ReservedTarget = slave + 1;
  4734. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitDummyMaster = true;
  4735. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.AlternateMasterStopRequest = true;
  4736. LOG_INFO("Gun %d Request DummyMaster To Power Off", slave + 1);
  4737. GetClockTime(&_CheckSlaveReady_time[slave]);
  4738. }
  4739. else
  4740. {
  4741. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest = true;
  4742. LOG_INFO("Gun %d Need Power Off And Request To Charging", slave + 1);
  4743. }
  4744. }
  4745. }
  4746. // wait until master derating completed
  4747. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.CheckSlaveReady && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady)
  4748. {
  4749. time = GetTimeoutValue(_CheckSlaveReady_time[slave]) / uSEC_VAL;
  4750. if(time >= WAIT_SLAVE_READY_TIME ||
  4751. (ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed))
  4752. {
  4753. if(!(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.DeratingConfirmed))
  4754. {
  4755. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  4756. LOG_INFO("Gun %d Wait Gun %d Timeout", slave + 1);
  4757. }
  4758. else
  4759. {
  4760. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady = true;
  4761. LOG_INFO("Gun %d Is Ready", slave + 1);
  4762. GetClockTime(&_CheckSlaveReady_time[slave]);
  4763. }
  4764. }
  4765. }
  4766. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitSlaveReady && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest)
  4767. {
  4768. time = GetTimeoutValue(_CheckSlaveReady_time[slave]) / uSEC_VAL;
  4769. if(time >= WAIT_SLAVE_DELAY)
  4770. {
  4771. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest = true;
  4772. }
  4773. }
  4774. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.WaitDummyMaster && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest)
  4775. {
  4776. time = GetTimeoutValue(_CheckSlaveReady_time[slave]) / uSEC_VAL;
  4777. if(time >= WAIT_DUMMY_MASTER_OFF)
  4778. {
  4779. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  4780. LOG_INFO("Gun %d Wait DummyMaster %d Power Off Timeout", slave + 1, master + 1);
  4781. }
  4782. }
  4783. // self group, other slave or master set SlavePowerOffRequest flag to power off
  4784. if(ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffRequest && !ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffConfirmed)
  4785. {
  4786. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  4787. {
  4788. PsuGroupPowerOffCheck(slave, &PowerOffMember);
  4789. }
  4790. else
  4791. {
  4792. PowerOffMember.Quantity = 1;
  4793. PowerOffMember.Member[0] = slave;
  4794. }
  4795. #if GRAB_AT_START_CHARGING
  4796. if(PowerOffMember.Quantity == 1 && ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest)
  4797. {
  4798. // grab here
  4799. if(PsuGroupGrabCheck(slave, &GrabMember))
  4800. {
  4801. for(int i = 0; i < GrabMember.Quantity; i++)
  4802. {
  4803. PowerOffMember.Member[PowerOffMember.Quantity++] = GrabMember.Member[i];
  4804. }
  4805. }
  4806. }
  4807. #endif
  4808. if(PowerOffMember.Quantity > 0)
  4809. {
  4810. unsigned char target = ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest ? (slave + 1) : 0;
  4811. PrepareToPowerOff(target, &PowerOffMember);
  4812. if(PowerOffMember.Quantity > 1 && ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlaveChargingRequest)
  4813. {
  4814. ShmPsuGrouping->GroupCollection[slave].PossibleMember.Quantity = PowerOffMember.Quantity - 1;
  4815. memcpy(ShmPsuGrouping->GroupCollection[slave].PossibleMember.Member, &PowerOffMember.Member[1], PowerOffMember.Quantity - 1);
  4816. }
  4817. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.bits.SlavePowerOffConfirmed = true;
  4818. }
  4819. else
  4820. {
  4821. ShmPsuGrouping->GroupCollection[slave].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  4822. }
  4823. }
  4824. }
  4825. void MasterExtendCapabilityProcess(unsigned char master)
  4826. {
  4827. int time = 0;
  4828. // stop extend capability process when derating or stop charging
  4829. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.DeratingCtrlValue != 0 ||
  4830. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.StopChargingRequest)
  4831. {
  4832. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed)
  4833. {
  4834. SetExtendPrechargeStop(master);
  4835. }
  4836. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  4837. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  4838. return;
  4839. }
  4840. // start extend capability
  4841. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerRequest && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed)
  4842. {
  4843. GetClockTime(&_ExtendCapability_time[master]);
  4844. memset(&ShmPsuGrouping->GroupCollection[master].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  4845. FindPsuGroupPartner(master, (int)ShmPsuGrouping->GroupCollection[master].DiffPower_PhysicalLimit, &ShmPsuGrouping->GroupCollection[master].PossibleMember, _EXTEND_TYPE_POWER, ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit);
  4846. if(ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity > 0)
  4847. {
  4848. PrepareToExtendCapability(master + 1, &ShmPsuGrouping->GroupCollection[master].PossibleMember);
  4849. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed = true;
  4850. }
  4851. else
  4852. {
  4853. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  4854. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  4855. }
  4856. }
  4857. // set extend member precharge voltage
  4858. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.MorePowerConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone)
  4859. {
  4860. //unsigned short voltage = ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage >= PRECHARGE_OFFSET_VOLTAGE ?
  4861. // ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage - PRECHARGE_OFFSET_VOLTAGE : 0;
  4862. unsigned short voltage = 0;
  4863. voltage = ShmPsuGrouping->GroupCollection[master].AlternateMaster == 0 ?
  4864. ShmPsuData->PsuGroup[master].GroupPresentOutputVoltage : ShmPsuData->PsuGroup[ShmPsuGrouping->GroupCollection[master].AlternateMaster - 1].GroupPresentOutputVoltage;
  4865. voltage = voltage >= PRECHARGE_OFFSET_VOLTAGE ? voltage - PRECHARGE_OFFSET_VOLTAGE : 0;
  4866. for(int i = 0; i < ShmPsuGrouping->GroupCollection[master].PossibleMember.Quantity; i++)
  4867. {
  4868. unsigned char slave = ShmPsuGrouping->GroupCollection[master].PossibleMember.Member[i];
  4869. if(ShmPsuData->PsuGroup[slave].GroupPresentPsuQuantity > 0 && ShmPsuData->PsuGroup[slave].GroupAvailableCurrent > 0)
  4870. {
  4871. ShmPsuGrouping->GroupOutput[slave].GTargetVoltage = voltage;
  4872. ShmPsuGrouping->GroupOutput[slave].GTargetCurrent = ZERO_CURRENT;
  4873. }
  4874. }
  4875. if(!ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrecharge)
  4876. {
  4877. GetClockTime(&_ExtendCapability_time[master]);
  4878. LOG_INFO("Gun %d Set ExtendPrecharge Voltage %d.%d V", master + 1, (voltage / 10), (voltage % 10));
  4879. }
  4880. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrecharge = true;
  4881. }
  4882. // wait for extend member precharge ready
  4883. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrecharge && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone)
  4884. {
  4885. bool ready = IsExtendPrechargeReady(master);
  4886. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  4887. if(ready || time >= WAIT_PRECHARGE_TIME)
  4888. {
  4889. LOG_INFO("Gun %d ExtendPrecharge %s", master + 1, ready ? "Ready" : "Timeout");
  4890. if(!ready)
  4891. {
  4892. ready = CheckExtendPrechargeReady(master);
  4893. LOG_INFO("Gun %d check every psu's voltage and ExtendPrecharge is %s", master + 1, ready ? "Ready" : "Timeout");
  4894. }
  4895. if(ready)
  4896. {
  4897. GetClockTime(&_ExtendCapability_time[master]);
  4898. SetExtendPrechargeCompleted(master);
  4899. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone = true;
  4900. }
  4901. else
  4902. {
  4903. SetExtendPrechargeStop(master);
  4904. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  4905. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  4906. }
  4907. }
  4908. }
  4909. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendPrechargeDone && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayOn)
  4910. {
  4911. // after a little delay, set extend parallel relay on
  4912. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  4913. if(time >= WAIT_PARALLEL_RELAY_DELAY)
  4914. {
  4915. unsigned short original = ShmPsuGrouping->ParallelRelayConfig.CtrlValue;
  4916. GetClockTime(&_ExtendCapability_time[master]);
  4917. SetParallelRelayOnOff(master, YES, _GROLE_PRECHARGE_READY);
  4918. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayOn = true;
  4919. LOG_INFO("Gun %d Set Extend Parallel Relay On 0x%02X -> 0x%02X", master + 1, original, ShmPsuGrouping->ParallelRelayConfig.CtrlValue);
  4920. }
  4921. }
  4922. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayOn && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed)
  4923. {
  4924. // wait until extend parallel relay on confirmed
  4925. bool confirmed = IsParallelRelayConfirmed(master);
  4926. time = GetTimeoutValue(_ExtendCapability_time[master]) / uSEC_VAL;
  4927. //if(confirmed || time >= WAIT_RELAY_CONFIRMED_TIME)
  4928. if(time >= WAIT_RELAY_CONFIRMED_TIME)
  4929. {
  4930. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed = true;
  4931. LOG_INFO("Gun %d Extend Parallel Relay Confirmed %s", master + 1, confirmed ? "OK" : "Timeout");
  4932. }
  4933. }
  4934. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendRelayConfirmed && !ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendCompleted)
  4935. {
  4936. AddExtendMember(master);
  4937. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendCompleted = true;
  4938. }
  4939. if(ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ExtendCompleted)
  4940. {
  4941. ShmPsuGrouping->GroupCollection[master].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  4942. ShmPsuGrouping->GroupCollection[master].ExtendQuantityLimit = 0;
  4943. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.OutputCurrentStable = false;
  4944. GetClockTime(&_ReachCurrent_time[master]);
  4945. UpdateGunAvailableCapability(master);
  4946. ShmPsuGrouping->GroupCollection[master].GroupCtrl.bits.ReachMaxStageCurrent = false;
  4947. GetClockTime(&_StageCurrent_time[master]);
  4948. LOG_INFO("Gun %d Extend Capability Completed", master + 1);
  4949. }
  4950. }
  4951. void ShowGunVoltageCurrent(unsigned char master)
  4952. {
  4953. unsigned short voltage = 0, current = 0, fireVoltage = 0;
  4954. unsigned short diffVoltage = 0, diffCurrent = 0;
  4955. if(ShmPsuGrouping->GroupCollection[master].Role == _GROLE_MASTER)
  4956. {
  4957. voltage = (int)(chargingInfo[master]->PresentChargingVoltage * 10);
  4958. current = (int)(chargingInfo[master]->PresentChargingCurrent * 10);
  4959. fireVoltage = chargingInfo[master]->FireChargingVoltage;
  4960. diffVoltage = voltage >= evseOutVol[master] ? voltage - evseOutVol[master] : evseOutVol[master] - voltage;
  4961. diffCurrent = current >= evseOutCur[master] ? current - evseOutCur[master] : evseOutCur[master] - current;
  4962. if(diffVoltage >= 10 || diffCurrent >= 10)
  4963. {
  4964. evseOutputDelay[master] = SHOW_OUTPUT_DELAY;
  4965. evseOutVol[master] = voltage;
  4966. evseOutCur[master] = current;
  4967. }
  4968. if(evseOutputDelay[master] != 0)
  4969. {
  4970. LOG_INFO("Gun %d NeedV: %4d V, NeedC: %3d A, OutputV: %4d.%d V, OutputC: %3d.%d A, FireV: %4d V",
  4971. master + 1, (int)chargingInfo[master]->EvBatterytargetVoltage, (int)chargingInfo[master]->EvBatterytargetCurrent,
  4972. (voltage / 10), (voltage % 10), (current / 10), (current % 10), fireVoltage / 10);
  4973. evseOutputDelay[master]--;
  4974. }
  4975. }
  4976. }
  4977. // group: self group index
  4978. void TryCleanPsuAlarm(unsigned char group)
  4979. {
  4980. for(int i = 0; i < ShmPsuPosition->GroupLocationInfo[group].GroupPsuQuantity; i++)
  4981. {
  4982. int psu_index = ShmPsuPosition->GroupLocationInfo[group].PsuIndex[i];
  4983. if(ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal == PSU_STATUS_FLAG_LOCK)
  4984. {
  4985. UnlockPsuAlarm(ShmPsuPosition->PsuAddressInfo[psu_index].Full_Address);
  4986. PSU_LOG("Unlock PSU Group[%d] Index[%d] Alarm Status", group, ShmPsuPosition->PsuAddressInfo[psu_index].GIndex);
  4987. }
  4988. }
  4989. }
  4990. void CheckPsuAlarmStatus(void)
  4991. {
  4992. if(_isTriggerShutdown)
  4993. {
  4994. return;
  4995. }
  4996. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  4997. {
  4998. for(int j = 0; j < ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity; j++)
  4999. {
  5000. int psu_index = ShmPsuPosition->GroupLocationInfo[i].PsuIndex[j];
  5001. int group = ShmPsuPosition->PsuAddressInfo[psu_index].GroupNo;
  5002. int gIndex = ShmPsuPosition->PsuAddressInfo[psu_index].GIndex;
  5003. if(ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal != _LocalPsuStatus[psu_index].StatusVal)
  5004. {
  5005. if(((ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal & 0x02) != (_LocalPsuStatus[psu_index].StatusVal & 0x02)))
  5006. {
  5007. if((ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal & 0x02) != 0)
  5008. {
  5009. LOG_INFO("Group[%d][%d] Psu[%2d] Fault Occur", group, gIndex, psu_index);
  5010. }
  5011. else
  5012. {
  5013. LOG_INFO("Group[%d][%d] Psu[%2d] Fault Recovery", group, gIndex, psu_index);
  5014. }
  5015. }
  5016. if(((ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal & 0x04) != (_LocalPsuStatus[psu_index].StatusVal & 0x04)))
  5017. {
  5018. if((ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal & 0x04) != 0)
  5019. {
  5020. LOG_INFO("Group[%d][%d] Psu[%2d] Alarm Occur", group, gIndex, psu_index);
  5021. }
  5022. else
  5023. {
  5024. LOG_INFO("Group[%d][%d] Psu[%2d] Alarm Recovery", group, gIndex, psu_index);
  5025. }
  5026. }
  5027. if(((ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal & 0x08) != (_LocalPsuStatus[psu_index].StatusVal & 0x08)))
  5028. {
  5029. if((ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal & 0x08) != 0)
  5030. {
  5031. LOG_INFO("Group[%d][%d] Psu[%2d] Lock Occur", group, gIndex, psu_index);
  5032. }
  5033. else
  5034. {
  5035. LOG_INFO("Group[%d][%d] Psu[%2d] Lock Recovery", group, gIndex, psu_index);
  5036. }
  5037. }
  5038. }
  5039. _LocalPsuStatus[psu_index].StatusVal = ShmPsuPosition->SinglePsuStatus[psu_index].StatusVal;
  5040. }
  5041. }
  5042. }
  5043. void ShowInfyPsuErrState(int psu_index)
  5044. {
  5045. LOG_INFO("Group[%d][%d] Psu[%2d] State2: [%02X], State1: [%02X], State0: [%02X]",
  5046. ShmPsuPosition->PsuAddressInfo[psu_index].GroupNo,
  5047. ShmPsuPosition->PsuAddressInfo[psu_index].GIndex,
  5048. psu_index,
  5049. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[2],
  5050. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[1],
  5051. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrState.StateVal[0]);
  5052. }
  5053. void InfyPwrStateSummary(void)
  5054. {
  5055. InfyPsuStateFlagInfo _InfyPwrState;
  5056. memset(&_InfyPwrState, 0x00, sizeof(_InfyPwrState));
  5057. for(int i = 0; i < MAX_PSU_MODULE_QUANTITY; i++)
  5058. {
  5059. _InfyPwrState.StateVal[0] |= ShmPsuPosition->SingleInfyPwrState[i].InfyPwrState.StateVal[0];
  5060. _InfyPwrState.StateVal[1] |= ShmPsuPosition->SingleInfyPwrState[i].InfyPwrState.StateVal[1];
  5061. _InfyPwrState.StateVal[2] |= ShmPsuPosition->SingleInfyPwrState[i].InfyPwrState.StateVal[2];
  5062. }
  5063. // 012264
  5064. // DCDC short circuit
  5065. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuOutputShortCircuit = _InfyPwrState.bits.OutputShort ? YES : NO;
  5066. // 012334
  5067. // PSU Failure Alarm
  5068. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFault = _InfyPwrState.bits.PsuFault ? YES : NO;
  5069. // 012268
  5070. // PSU Protection Alarm
  5071. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = _InfyPwrState.bits.PsuProtect ? YES : NO;
  5072. // 012269
  5073. // Fan faults
  5074. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFanFailureAlarm = _InfyPwrState.bits.FanFault ? YES : NO;
  5075. // 012226
  5076. // DCDC over temperature
  5077. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuCriticalPointOTP = _InfyPwrState.bits.OverTemperature ? YES : NO;
  5078. // 012319
  5079. // DCDC output over voltage
  5080. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDcdcOverVoltage = _InfyPwrState.bits.OutputOverVoltage ? YES : NO;
  5081. // 012273
  5082. // Module unders power limiting status
  5083. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuPowerLimitedState = _InfyPwrState.bits.PowerLimit ? YES : NO;
  5084. // 012263
  5085. // ID number repetition
  5086. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDuplicateID = _InfyPwrState.bits.PsuIdRepetition ? YES : NO;
  5087. // 012313
  5088. // Phase loss
  5089. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputPhaseLoss = _InfyPwrState.bits.InputPhaseLost ? YES : NO;
  5090. // 012271
  5091. // Under voltage of any phase
  5092. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputUVP = _InfyPwrState.bits.InputUnderVoltage ? YES : NO;
  5093. // 012270
  5094. // Over voltage of any phase
  5095. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuInputOVP = _InfyPwrState.bits.InputOverVoltage ? YES : NO;
  5096. }
  5097. void ShowPhPsuErrRecord(int psu_index)
  5098. {
  5099. if(ShmPsuPosition->SinglePhPsuError[psu_index].ErrorCount == 0)
  5100. {
  5101. LOG_INFO("Group[%d][%d] Psu[%2d] Error Record Clear",
  5102. ShmPsuPosition->PsuAddressInfo[psu_index].GroupNo,
  5103. ShmPsuPosition->PsuAddressInfo[psu_index].GIndex,
  5104. psu_index);
  5105. }
  5106. else
  5107. {
  5108. char strErrRecord[128];
  5109. char strError[16];
  5110. sprintf(strErrRecord, "Group[%d][%d] Psu[%2d] Err:",
  5111. ShmPsuPosition->PsuAddressInfo[psu_index].GroupNo,
  5112. ShmPsuPosition->PsuAddressInfo[psu_index].GIndex,
  5113. psu_index);
  5114. for(int i = 0; i < ShmPsuPosition->SinglePhPsuError[psu_index].ErrorCount; i++)
  5115. {
  5116. sprintf(strError, " E-%2d", ShmPsuPosition->SinglePhPsuError[psu_index].PsuError[i]);
  5117. strcat(strErrRecord, strError);
  5118. }
  5119. LOG_INFO("%s", strErrRecord);
  5120. }
  5121. }
  5122. void CheckPsuErrRecord(void)
  5123. {
  5124. if(_isTriggerShutdown)
  5125. {
  5126. return;
  5127. }
  5128. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  5129. {
  5130. for(int j = 0; j < ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity; j++)
  5131. {
  5132. int psu_index = ShmPsuPosition->GroupLocationInfo[i].PsuIndex[j];
  5133. if(ShmChargerInfo->Control.LibCtrl.bits.InfyPwrLib)
  5134. {
  5135. if(ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrStateFlag.bits.Changed)
  5136. {
  5137. ShowInfyPsuErrState(psu_index);
  5138. InfyPwrStateSummary();
  5139. ShmPsuPosition->SingleInfyPwrState[psu_index].InfyPwrStateFlag.bits.Changed = false;
  5140. }
  5141. }
  5142. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  5143. {
  5144. if(ShmPsuPosition->SinglePhPsuError[psu_index].PhPwrStateFlag.bits.Changed)
  5145. {
  5146. ShowPhPsuErrRecord(psu_index);
  5147. ShmPsuPosition->SinglePhPsuError[psu_index].PhPwrStateFlag.bits.Changed = false;
  5148. }
  5149. }
  5150. }
  5151. }
  5152. }
  5153. bool IsPsuGroupAnyAlarm(unsigned char group)
  5154. {
  5155. if((ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[group].StatusVal & PSU_STATUS_FLAG_ALARM) > 0)
  5156. {
  5157. return true;
  5158. }
  5159. return false;
  5160. }
  5161. bool IsPsuGroupAnyFault(unsigned char group)
  5162. {
  5163. if((ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[group].StatusVal & PSU_STATUS_FLAG_FAULT) > 0)
  5164. {
  5165. return true;
  5166. }
  5167. return false;
  5168. }
  5169. bool IsPsuGroupAllAlarm(unsigned char group)
  5170. {
  5171. if((ShmChargerInfo->PsuGrouping.GroupPsuStatus_AND[group].StatusVal & PSU_STATUS_FLAG_ALARM) > 0)
  5172. {
  5173. return true;
  5174. }
  5175. return false;
  5176. }
  5177. bool IsPsuGroupAllFault(unsigned char group)
  5178. {
  5179. if((ShmChargerInfo->PsuGrouping.GroupPsuStatus_AND[group].StatusVal & PSU_STATUS_FLAG_FAULT) > 0)
  5180. {
  5181. return true;
  5182. }
  5183. return false;
  5184. }
  5185. bool IsPsuGroupAnyLock(unsigned char group)
  5186. {
  5187. if((ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[group].StatusVal & PSU_STATUS_FLAG_LOCK))
  5188. {
  5189. return true;
  5190. }
  5191. return false;
  5192. }
  5193. bool IsAlternateMasterStop(unsigned char group)
  5194. {
  5195. if(ShmPsuGrouping->GroupCollection[group].AlternateMaster != 0)
  5196. {
  5197. return ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.AlternateMasterStopRequest;
  5198. }
  5199. return false;
  5200. }
  5201. void PsuGroupControlProcess(void)
  5202. {
  5203. int time = 0;
  5204. unsigned char role = 0;
  5205. PsuGroupPartner partner;
  5206. //unsigned short TargetVoltage = 0, TargetCurrent = 0;
  5207. for(byte group = 0; group < GENERAL_GUN_QUANTITY; group++)
  5208. {
  5209. role = ShmPsuGrouping->GroupCollection[group].Role;
  5210. switch(role)
  5211. {
  5212. case _GROLE_IDLE:
  5213. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5214. {
  5215. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5216. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue = 0;
  5217. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  5218. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  5219. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  5220. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue = 0;
  5221. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  5222. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DummyMasterCtrlValue = 0;
  5223. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = 0;
  5224. ShmPsuGrouping->GroupCollection[group].AlternateMaster = 0;
  5225. PSU_LOG("===== PSU Group[%02X] ===== Idle", group);
  5226. GetClockTime(&_PsuGroupRole_time[group]);
  5227. GetClockTime(&_CleanPsuAlarm_time[group]);
  5228. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  5229. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  5230. PreGroupOutput[group].GTargetVoltage = 0;
  5231. PreGroupOutput[group].GTargetCurrent = 0;
  5232. ShmPsuGrouping->TotalGroupOutput[group].GTargetVoltage = 0;
  5233. ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent = 0;
  5234. SetPsuGroupOutput(group);
  5235. UpdatePsuGroupLoading(group);
  5236. }
  5237. if(isStartOutputSwitch[group])
  5238. {
  5239. SetPsuGroupPowerOnOff(group, _PSU_OFF);
  5240. }
  5241. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  5242. {
  5243. if(IsPsuGroupAnyAlarm(group))
  5244. {
  5245. time = GetTimeoutValue(_CleanPsuAlarm_time[group]) / uSEC_VAL;
  5246. if(time >= PSU_CLEAN_ERR_INTERVAL)
  5247. {
  5248. PSU_LOG("PSU Group[%d] Alarm or Fault Occur, Need Do Something", group);
  5249. GetClockTime(&_CleanPsuAlarm_time[group]);
  5250. }
  5251. // if module exist any alarm or fault, break here
  5252. break;
  5253. }
  5254. else if(IsPsuGroupAnyLock(group))
  5255. {
  5256. time = GetTimeoutValue(_CleanPsuAlarm_time[group]) / uSEC_VAL;
  5257. if(time >= PSU_CLEAN_ERR_INTERVAL)
  5258. {
  5259. TryCleanPsuAlarm(group);
  5260. GetClockTime(&_CleanPsuAlarm_time[group]);
  5261. }
  5262. // if module exist any lock, break here
  5263. break;
  5264. }
  5265. }
  5266. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue != 0)
  5267. {
  5268. CheckChargingRequest(group);
  5269. }
  5270. else
  5271. {
  5272. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity == 0)
  5273. {
  5274. if(GetPsuGroupAvailable(group) == 0)
  5275. {
  5276. if(!CheckAlternateMaster(group, &partner, YES) && !CheckAlternateMaster(group, &partner, NO))
  5277. {
  5278. SetPsuGroupRole(group, _GROLE_NO_RESOURCE);
  5279. }
  5280. }
  5281. }
  5282. }
  5283. break;
  5284. case _GROLE_MASTER:
  5285. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5286. {
  5287. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5288. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.MasterCtrlValue = 0;
  5289. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.StopChargingCtrlValue = 0;
  5290. //ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  5291. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = 0;
  5292. PSU_LOG("===== PSU Group[%02X] ===== Master", group);
  5293. GetClockTime(&_PsuGroupRole_time[group]);
  5294. OutputConfigStep[group] = _CURRENT_MODE_NONE;
  5295. _preOutputConfigStep[group] = _CURRENT_MODE_NONE;
  5296. MaxCurrentDemand[group] = 0;
  5297. StageMaxCurrent[group] = 0;
  5298. _GfdStep[group] = PREPARE_STEP_NONE;
  5299. _VoltageResumeCnt[group] = 0;
  5300. }
  5301. if(!ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.AlreadyInChargingMode)
  5302. {
  5303. if(chargingInfo[group]->SystemStatus == S_CHARGING && Is_OutputK1K2_Ready(group))
  5304. {
  5305. LOG_INFO("Gun %d Enter Charging Mode", group + 1);
  5306. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.AlreadyInChargingMode = true;
  5307. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ReachMaxCurrentDemand = false;
  5308. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ReachMaxStageCurrent = false;
  5309. GetClockTime(&_MaxCurrent_time[group]);
  5310. GetClockTime(&_StageCurrent_time[group]);
  5311. GetClockTime(&_ChargingDelay_time[group]);
  5312. }
  5313. }
  5314. if((chargingInfo[group]->SystemStatus <= S_IDLE) ||
  5315. (chargingInfo[group]->SystemStatus >= S_TERMINATING && chargingInfo[group]->SystemStatus <= S_FAULT))
  5316. {
  5317. if(!ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  5318. {
  5319. LOG_INFO("Gun %d SystemStatus(%d) Need Stop Charging", group + 1, chargingInfo[group]->SystemStatus);
  5320. }
  5321. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest = true;
  5322. }
  5323. else if(IsAlternateMasterStop(group))
  5324. {
  5325. if(!ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  5326. {
  5327. LOG_INFO("Gun %d Alternate Master Stop", group + 1, chargingInfo[group]->SystemStatus);
  5328. }
  5329. ShmChargerInfo->GunError[group].ErrFlag.bits.PsuGroupNoResource = true;
  5330. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest = true;
  5331. }
  5332. if((IsPsuGroupAllAlarm(group) || IsPsuGroupAllFault(group)) &&
  5333. !_isTriggerShutdown &&
  5334. !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  5335. {
  5336. if(!ShmChargerInfo->GunError[group].ErrFlag.bits.PsuGroupNoResource)
  5337. {
  5338. LOG_INFO("Gun %d PsuGroup All Alarm/Fault >> No Resource, Need Stop Charging", group + 1);
  5339. }
  5340. ShmChargerInfo->GunError[group].ErrFlag.bits.PsuGroupNoResource = true;
  5341. }
  5342. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue == 0)
  5343. {
  5344. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ExtendAvailable)
  5345. {
  5346. time = GetTimeoutValue(_ExtendCapability_time[group]) / uSEC_VAL;
  5347. if(time >= EXTEND_CAPABILITY_DELAY)
  5348. {
  5349. #if 0
  5350. int available = GetPsuGroupAvailable(group);
  5351. if(available > 0)
  5352. {
  5353. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = MAX_GROUP_QUANTITY;
  5354. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.MorePowerRequest = true;
  5355. LOG_INFO("Gun %d Start Extend Capability", group + 1);
  5356. }
  5357. #endif
  5358. ShmPsuGrouping->GroupCollection[group].ExtendQuantityLimit = 0;
  5359. int reason = IsExtendConflict(group);
  5360. char *str_conflict_reason[] = {STR_EXTEND_CONFLICT_NONE, STR_EXTEND_CONFLICT_AVAILABLE, STR_EXTEND_CONFLICT_AVERAGE, STR_EXTEND_CONFLICT_PRIORITY, STR_EXTEND_CONFLICT_UNKNOWN};
  5361. if(reason == _CONFLICT_NONE)
  5362. {
  5363. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.MorePowerRequest = true;
  5364. LOG_INFO("Gun %d Start Extend Capability", group + 1);
  5365. }
  5366. else
  5367. {
  5368. LOG_INFO("Gun %d Extend Capability Conflict: %s", group + 1, str_conflict_reason[reason]);
  5369. }
  5370. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ExtendAvailable = false;
  5371. }
  5372. }
  5373. }
  5374. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.ExtendCapabilityCtrlValue != 0)
  5375. {
  5376. CleanSmoothDerating(group);
  5377. MasterExtendCapabilityProcess(group);
  5378. }
  5379. // need derating
  5380. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue != 0 &&
  5381. !ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  5382. {
  5383. CleanSmoothDerating(group);
  5384. PsuGroupDeratingProcess(group);
  5385. }
  5386. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.StopChargingRequest)
  5387. {
  5388. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.DeratingCtrlValue = 0;
  5389. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  5390. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  5391. ShmPsuGrouping->TotalGroupOutput[group].GTargetVoltage = 0;
  5392. ShmPsuGrouping->TotalGroupOutput[group].GTargetCurrent = 0;
  5393. CleanSmoothDerating(group);
  5394. MasterStopChargingProcess(group);
  5395. }
  5396. else
  5397. {
  5398. UpdateGunLoading(group);
  5399. UpdatePsuGroupOutputConfig(group);
  5400. UpdateMasterPsuGroupLoading(group);
  5401. PsuGroupOutputConfigCheck(group);
  5402. }
  5403. ShowGunVoltageCurrent(group);
  5404. SetPsuGroupOutput(group);
  5405. break;
  5406. case _GROLE_SLAVE:
  5407. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5408. {
  5409. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5410. PSU_LOG("===== PSU Group[%02X] ===== Slave", group);
  5411. GetClockTime(&_PsuGroupRole_time[group]);
  5412. }
  5413. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue != 0)
  5414. {
  5415. SlaveStopChargingProcess(group);
  5416. }
  5417. else
  5418. {
  5419. if(IsPsuGroupAllAlarm(group) || IsPsuGroupAllFault(group))
  5420. {
  5421. if(!ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.SlavePowerOffRequest)
  5422. {
  5423. LOG_INFO("Gun %d PsuGroup (slave) All Alarm/Fault, Need Power Off", group + 1);
  5424. }
  5425. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.SlavePowerOffRequest = true;
  5426. }
  5427. else
  5428. {
  5429. SetPsuGroupOutput(group);
  5430. }
  5431. }
  5432. break;
  5433. case _GROLE_PREPARE_SWITCH_OFF:
  5434. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5435. {
  5436. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5437. PSU_LOG("===== PSU Group[%02X] ===== Prepare Off", group);
  5438. GetClockTime(&_PsuGroupRole_time[group]);
  5439. }
  5440. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  5441. if(time >= MAX_PREPARE_SWITCH_OFF_TIME)
  5442. {
  5443. // timeout shall not happen
  5444. SetPsuGroupRole(group, _GROLE_SLAVE_POWER_OFF);
  5445. }
  5446. else
  5447. {
  5448. SetPsuGroupOutput(group);
  5449. }
  5450. break;
  5451. case _GROLE_SLAVE_POWER_OFF:
  5452. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5453. {
  5454. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5455. PSU_LOG("===== PSU Group[%02X] ===== Slave Power Off", group);
  5456. GetClockTime(&_PsuGroupRole_time[group]);
  5457. }
  5458. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  5459. if(time >= MIN_POWER_OFF_TIME &&
  5460. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT || _isTriggerShutdown))
  5461. {
  5462. SetPsuGroupRole(group, _GROLE_SWITCH_OFF_OK);
  5463. }
  5464. else
  5465. {
  5466. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  5467. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  5468. SetPsuGroupOutput(group);
  5469. }
  5470. break;
  5471. case _GROLE_SWITCH_OFF_OK:
  5472. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5473. {
  5474. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5475. PSU_LOG("===== PSU Group[%02X] ===== Switch Off OK, %d.%d V, %d.%d A", group,
  5476. (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10),
  5477. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent % 10));
  5478. GetClockTime(&_PsuGroupRole_time[group]);
  5479. }
  5480. break;
  5481. case _GROLE_WAIT_IDLE:
  5482. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5483. {
  5484. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5485. PSU_LOG("===== PSU Group[%02X] ===== Wait Idle, %d.%d V, %d.%d A", group,
  5486. (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10),
  5487. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent % 10));
  5488. GetClockTime(&_PsuGroupRole_time[group]);
  5489. }
  5490. break;
  5491. case _GROLE_WAIT_SLAVE:
  5492. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5493. {
  5494. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5495. PSU_LOG("===== PSU Group[%02X] ===== Wait Slave", group);
  5496. GetClockTime(&_PsuGroupRole_time[group]);
  5497. }
  5498. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  5499. if(ShmPsuGrouping->GroupCollection[group].ReservedTarget != 0)
  5500. {
  5501. if(time >= WAIT_SLAVE_TO_CHARGING && (ShmPsuGrouping->GroupCollection[group].ReservedTarget - 1) == group)
  5502. {
  5503. SetPsuGroupRole(group, _GROLE_REQUEST_TO_CHARGING);
  5504. ShmPsuGrouping->GroupCollection[group].GroupCtrl.bits.ChargingRequest = true;
  5505. ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.SlaveCtrlValue = 0;
  5506. break;
  5507. }
  5508. }
  5509. if(time >= WAIT_SLAVE_TIMEOUT)
  5510. {
  5511. PSU_LOG("Group[%02X] Wait Slave Timeout", group);
  5512. SetPsuGroupToIdle(group);
  5513. }
  5514. break;
  5515. case _GROLE_PREPARE_ATTACH_ON:
  5516. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5517. {
  5518. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5519. PSU_LOG("===== PSU Group[%02X] ===== Prepare Attach On", group);
  5520. GetClockTime(&_PsuGroupRole_time[group]);
  5521. }
  5522. SetPsuGroupOutput(group);
  5523. break;
  5524. case _GROLE_PRECHARGE_READY:
  5525. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5526. {
  5527. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5528. PSU_LOG("===== PSU Group[%02X] ===== Precharge %d.%d V Ready",
  5529. group, (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10));
  5530. GetClockTime(&_PsuGroupRole_time[group]);
  5531. }
  5532. break;
  5533. case _GROLE_EXTEND_STOP:
  5534. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5535. {
  5536. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5537. PSU_LOG("===== PSU Group[%02X] ===== Extend Stop, %d.%d V",
  5538. group, (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage / 10), (ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage % 10));
  5539. GetClockTime(&_PsuGroupRole_time[group]);
  5540. }
  5541. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  5542. if(time >= MIN_POWER_OFF_TIME ||
  5543. (ShmPsuData->PsuGroup[group].GroupPresentOutputCurrent <= MAX_PSU_POWER_OFF_CURRENT &&
  5544. ShmPsuData->PsuGroup[group].GroupPresentOutputVoltage <= MAX_PSU_POWER_OFF_VOLTAGE))
  5545. {
  5546. SetPsuGroupToIdle(group);
  5547. }
  5548. else
  5549. {
  5550. ShmPsuGrouping->GroupOutput[group].GTargetVoltage = 0;
  5551. ShmPsuGrouping->GroupOutput[group].GTargetCurrent = 0;
  5552. SetPsuGroupOutput(group);
  5553. }
  5554. break;
  5555. case _GROLE_REQUEST_TO_CHARGING:
  5556. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5557. {
  5558. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5559. PSU_LOG("===== PSU Group[%02X] ===== Request To Charging", group);
  5560. GetClockTime(&_PsuGroupRole_time[group]);
  5561. }
  5562. if(ShmPsuGrouping->GroupCollection[group].GroupCtrl.RoleCtrl.IdleCtrlValue != 0)
  5563. {
  5564. ChargingRequestProcess(group);
  5565. }
  5566. break;
  5567. case _GROLE_TERMINATE:
  5568. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5569. {
  5570. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5571. PSU_LOG("===== PSU Group[%02X] ===== Terminate", group);
  5572. GetClockTime(&_PsuGroupRole_time[group]);
  5573. }
  5574. break;
  5575. case _GROLE_WAIT_TERMINATED:
  5576. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5577. {
  5578. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5579. PSU_LOG("===== PSU Group[%02X] ===== Wait Terminated", group);
  5580. GetClockTime(&_PsuGroupRole_time[group]);
  5581. }
  5582. break;
  5583. case _GROLE_DUMMY_MASTER:
  5584. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5585. {
  5586. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5587. PSU_LOG("===== PSU Group[%02X] ===== Dummy Master", group);
  5588. GetClockTime(&_PsuGroupRole_time[group]);
  5589. }
  5590. DummuMasterProcess(group);
  5591. break;
  5592. case _GROLE_DUMMY_FAULT:
  5593. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5594. {
  5595. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5596. PSU_LOG("===== PSU Group[%02X] ===== Dummy Master Fault", group);
  5597. GetClockTime(&_PsuGroupRole_time[group]);
  5598. }
  5599. time = GetTimeoutValue(_PsuGroupRole_time[group]) / uSEC_VAL;
  5600. if(time >= WAIT_DUMMY_FAULT_TIME)
  5601. {
  5602. SetPsuGroupToIdle(group);
  5603. }
  5604. break;
  5605. case _GROLE_NONE:
  5606. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5607. {
  5608. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5609. PSU_LOG("===== PSU Group[%02X] ===== None", group);
  5610. GetClockTime(&_PsuGroupRole_time[group]);
  5611. }
  5612. break;
  5613. case _GROLE_NO_RESOURCE:
  5614. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5615. {
  5616. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5617. PSU_LOG("===== PSU Group[%02X] ===== No Resource", group);
  5618. GetClockTime(&_PsuGroupRole_time[group]);
  5619. }
  5620. if(GetPsuGroupAvailable(group) > 0)
  5621. {
  5622. SetPsuGroupRole(group, _GROLE_IDLE);
  5623. }
  5624. else if(CheckAlternateMaster(group, &partner, YES))
  5625. {
  5626. SetPsuGroupRole(group, _GROLE_IDLE);
  5627. }
  5628. break;
  5629. default:
  5630. if(ShmPsuGrouping->GroupCollection[group].PreRole != role)
  5631. {
  5632. ShmPsuGrouping->GroupCollection[group].PreRole = role;
  5633. PSU_LOG("===== PSU Group[%02X] ===== Unknown Role = %d", group, role);
  5634. GetClockTime(&_PsuGroupRole_time[group]);
  5635. }
  5636. SetPsuGroupRole(group, _GROLE_NONE);
  5637. break;
  5638. }
  5639. }
  5640. }
  5641. // only for test & debug purpose
  5642. void PsuSimulationSetting(void)
  5643. {
  5644. ShmPsuData->Work_Step = _WORK_CHARGING;
  5645. ShmPsuData->PsuGroup[0].GroupPresentPsuQuantity = 3;
  5646. ShmPsuData->PsuGroup[1].GroupPresentPsuQuantity = 3;
  5647. ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity = 3;
  5648. ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity = 3;
  5649. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].RemoteMaxPhysicalVoltage = 9500;
  5650. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[0].RemoteMaxPhysicalCurrent = 5000;
  5651. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].RemoteMaxPhysicalVoltage = 9500;
  5652. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[1].RemoteMaxPhysicalCurrent = 2000;
  5653. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[2].RemoteMaxPhysicalVoltage = 9500;
  5654. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[2].RemoteMaxPhysicalCurrent = 5000;
  5655. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[3].RemoteMaxPhysicalVoltage = 9500;
  5656. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[3].RemoteMaxPhysicalCurrent = 2500;
  5657. }
  5658. // only for test & debug purpose
  5659. void PsuGroupOutputSimulation(void)
  5660. {
  5661. byte master = 0;
  5662. unsigned short pVoltage = 0, pCurrent = 0;
  5663. unsigned short gVoltage = 0, gCurrent = 0;
  5664. // psu output voltage & current simulation
  5665. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  5666. {
  5667. pVoltage = ShmPsuGrouping->GroupOutput[i].GTargetVoltage;
  5668. pCurrent = ShmPsuGrouping->GroupOutput[i].GTargetCurrent;
  5669. ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage = pVoltage;
  5670. ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent = pCurrent;
  5671. ShmPsuData->PsuGroup[i].GroupPresentOutputPower = (pVoltage * pCurrent) / 100 / 100;
  5672. }
  5673. // psu AvailableCurrent & GroupAvailablePower simulation
  5674. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  5675. {
  5676. if(ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage == 0)
  5677. {
  5678. ShmPsuData->PsuGroup[i].GroupAvailableCurrent = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity * 1000;
  5679. }
  5680. else
  5681. {
  5682. if(ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage <= 3000)
  5683. {
  5684. ShmPsuData->PsuGroup[i].GroupAvailableCurrent = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity * 1000;
  5685. }
  5686. else
  5687. {
  5688. ShmPsuData->PsuGroup[i].GroupAvailableCurrent = (30000 * 100 * ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity) / ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage;
  5689. }
  5690. }
  5691. ShmPsuData->PsuGroup[i].GroupAvailablePower = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity * 300;
  5692. }
  5693. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  5694. {
  5695. master = ShmChargerInfo->PsuGrouping.GroupCollection[i].TargetGroup;
  5696. if(master == 0)
  5697. {
  5698. // calculate PresentChargingVoltage and PresentChargingCurrent of self group
  5699. chargingInfo[i]->PresentChargingVoltage = (float)ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage / 10;
  5700. chargingInfo[i]->PresentChargingCurrent = (float)ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent / 10;
  5701. }
  5702. else
  5703. {
  5704. if(i == (master - 1))
  5705. {
  5706. gVoltage = ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage;
  5707. gCurrent = ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent;
  5708. for(byte j = 0; j < ShmPsuGrouping->GroupCollection[i].Partner.Quantity; j++)
  5709. {
  5710. byte slave = ShmPsuGrouping->GroupCollection[i].Partner.Member[j];
  5711. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  5712. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF ||
  5713. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE_POWER_OFF)
  5714. {
  5715. gCurrent += ShmPsuData->PsuGroup[slave].GroupPresentOutputCurrent;
  5716. }
  5717. }
  5718. chargingInfo[i]->PresentChargingVoltage = (float)gVoltage / 10;
  5719. chargingInfo[i]->PresentChargingCurrent = (float)gCurrent / 10;
  5720. }
  5721. }
  5722. }
  5723. }
  5724. // only for test & debug purpose
  5725. void PsuGroupAvailableSimulation(void)
  5726. {
  5727. unsigned char master = 0;
  5728. int _groupCurrent = 0, _groupPower = 0;
  5729. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  5730. {
  5731. master = ShmChargerInfo->PsuGrouping.GroupCollection[i].TargetGroup;
  5732. // calculate output group capability
  5733. if(master == 0)
  5734. {
  5735. chargingInfo[i]->AvailableChargingCurrent = ShmPsuData->PsuGroup[i].GroupAvailableCurrent;
  5736. chargingInfo[i]->AvailableChargingPower = ShmPsuData->PsuGroup[i].GroupAvailablePower;
  5737. }
  5738. else
  5739. {
  5740. _groupCurrent = ShmPsuData->PsuGroup[master - 1].GroupAvailableCurrent;
  5741. _groupPower = ShmPsuData->PsuGroup[master - 1].GroupAvailablePower;
  5742. for(byte j = 0; j < ShmPsuGrouping->GroupCollection[master - 1].Partner.Quantity; j++)
  5743. {
  5744. byte slave = ShmPsuGrouping->GroupCollection[master - 1].Partner.Member[j];
  5745. if(ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_SLAVE ||
  5746. ShmPsuGrouping->GroupCollection[slave].Role == _GROLE_PREPARE_SWITCH_OFF)
  5747. {
  5748. _groupCurrent += ShmPsuData->PsuGroup[slave].GroupAvailableCurrent;
  5749. _groupPower += ShmPsuData->PsuGroup[slave].GroupAvailablePower;
  5750. }
  5751. }
  5752. if(ShmPsuGrouping->GroupCollection[master - 1].GroupCtrl.bits.DeratingConfirmed)
  5753. {
  5754. chargingInfo[master - 1]->AvailableChargingCurrent = ShmPsuGrouping->GroupCollection[master - 1].ReAssignAvailableCurrent;
  5755. }
  5756. else
  5757. {
  5758. chargingInfo[master - 1]->AvailableChargingCurrent = _groupCurrent;
  5759. }
  5760. chargingInfo[master - 1]->AvailableChargingPower = _groupPower;
  5761. chargingInfo[master - 1]->MaximumChargingVoltage = 9500;
  5762. if((master - 1) != i)
  5763. {
  5764. chargingInfo[i]->AvailableChargingCurrent = 0;
  5765. chargingInfo[i]->AvailableChargingPower = 0;
  5766. chargingInfo[i]->RealRatingPower = 0;
  5767. chargingInfo[i]->MaximumChargingVoltage = 0;
  5768. }
  5769. }
  5770. }
  5771. }
  5772. void CapabilitySimulation(void)
  5773. {
  5774. unsigned short voltage = 0, current = 0, power = 0;
  5775. unsigned short limitPower = 0, limitCurrent = 0;
  5776. unsigned char inUsingCnt = 0;
  5777. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5778. {
  5779. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  5780. {
  5781. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  5782. }
  5783. }
  5784. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  5785. {
  5786. voltage = chargingInfo[i]->MaximumChargingVoltage;
  5787. current = chargingInfo[i]->AvailableChargingCurrent;
  5788. power = chargingInfo[i]->AvailableChargingPower;
  5789. // ********** GetPhysicalLimitVoltageAndCurrent **********
  5790. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage != 0 &&
  5791. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage <= voltage)
  5792. {
  5793. voltage = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalVoltage;
  5794. }
  5795. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent != 0 &&
  5796. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent <= current)
  5797. {
  5798. current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].RemoteMaxPhysicalCurrent;
  5799. }
  5800. // ********** GetModelNameLimitPower **********
  5801. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  5802. ShmChargerInfo->Control.ChargerRatingPower != 0)
  5803. {
  5804. limitPower = ShmChargerInfo->Control.ChargerRatingPower;
  5805. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5806. {
  5807. limitPower += ShmChargerInfo->ParallelCabinet.TotalParallelPower;
  5808. }
  5809. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity) / inUsingCnt;
  5810. }
  5811. if(limitPower != 0 && limitPower <= power)
  5812. {
  5813. power = limitPower;
  5814. }
  5815. // ********** GetConfigLimitPowerAndCurrent **********
  5816. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  5817. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  5818. {
  5819. limitPower = ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10;
  5820. limitPower = (limitPower * ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity) / inUsingCnt;
  5821. }
  5822. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].MaxTotalChargingPower = limitPower;
  5823. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity != 0 && inUsingCnt != 0 &&
  5824. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  5825. {
  5826. limitCurrent = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
  5827. limitCurrent = (limitCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity) / inUsingCnt;
  5828. }
  5829. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].MaxTotalChargingCurrent = limitCurrent;
  5830. if(limitPower != 0 && limitPower <= power)
  5831. {
  5832. power = limitPower;
  5833. }
  5834. if(limitCurrent != 0 && limitCurrent <= current)
  5835. {
  5836. current = limitCurrent;
  5837. }
  5838. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  5839. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  5840. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  5841. }
  5842. }
  5843. void PsuGroupingInitial(void)
  5844. {
  5845. for(int i = 0; i < ShmPsuData->GroupCount; i++)
  5846. {
  5847. ShmPsuGrouping->GroupCollection[i].Role = 0;
  5848. ShmPsuGrouping->GroupCollection[i].PreRole = 0xFF;
  5849. memset(&ShmPsuGrouping->GroupCollection[i].Partner, 0x00, sizeof(PsuGroupPartner));
  5850. memset(&ShmPsuGrouping->GroupCollection[i].PossibleMember, 0x00, sizeof(PsuGroupPartner));
  5851. ShmPsuGrouping->GroupCollection[i].TargetGroup = 0;
  5852. ShmPsuGrouping->GroupCollection[i].ReservedTarget = 0;
  5853. memset(&ShmPsuGrouping->GroupCollection[i].GroupCtrl, 0x00, sizeof(PsuGroupControl));
  5854. ShmPsuGrouping->GroupCollection[i].ReAssignAvailableCurrent = 0;
  5855. ShmPsuGrouping->GroupCollection[i].ParallelCheck = 0;
  5856. ShmPsuGrouping->GroupCollection[i].GunLoading = 0;
  5857. memset(&ShmPsuGrouping->GroupOutput[i], 0x00, sizeof(GroupOutputConfigInfo));
  5858. }
  5859. }
  5860. void PsuCommLostCheck(void)
  5861. {
  5862. if(ShmPsuData->Work_Step != INITIAL_START &&
  5863. ShmPsuData->Work_Step != _NO_WORKING &&
  5864. ShmPsuData->Work_Step != _INIT_PSU_STATUS)
  5865. {
  5866. if(!isCommStart)
  5867. {
  5868. isCommStart = true;
  5869. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt = 0;
  5870. lostCnt = 0;
  5871. GetClockTime(&_PsuCommCheck_time);
  5872. }
  5873. if((GetTimeoutValue(_PsuCommCheck_time) / uSEC_VAL) >= PSU_COMM_CHECK_TIME)
  5874. {
  5875. ShmChargerInfo->Control.CommInfo.PsuComm.CommCnt = ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt;
  5876. ShmChargerInfo->Control.CommInfo.PsuComm.RxCnt = 0;
  5877. if(ShmChargerInfo->Control.CommInfo.PsuComm.CommCnt > COMM_LOST_CRITICAL)
  5878. {
  5879. if(ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost)
  5880. {
  5881. LOG_INFO("Psu Communication Recovery");
  5882. }
  5883. ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost = NO;
  5884. lostCnt = 0;
  5885. }
  5886. else
  5887. {
  5888. lostCnt++;
  5889. if(lostCnt >= LOST_CNT_CRITICAL)
  5890. {
  5891. if(!ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost)
  5892. {
  5893. LOG_INFO("Psu Communication Lost");
  5894. }
  5895. ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost = YES;
  5896. }
  5897. }
  5898. GetClockTime(&_PsuCommCheck_time);
  5899. }
  5900. }
  5901. else
  5902. {
  5903. isCommStart = false;
  5904. ShmChargerInfo->Control.PsuCtrl.bits.CommunicationLost = NO;
  5905. lostCnt = 0;
  5906. /*
  5907. if(ShmChargerInfo->Control.PsuCtrl.bits.Paused)
  5908. {
  5909. system("ifconfig can1 down");
  5910. sleep(1);
  5911. system("ifconfig can1 up");
  5912. sleep(1);
  5913. LOG_INFO("Psu Can Port Down & Up");
  5914. }
  5915. ShmChargerInfo->Control.PsuCtrl.bits.Paused = NO;
  5916. */
  5917. }
  5918. }
  5919. void InitialGunLimitSequence(void)
  5920. {
  5921. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  5922. {
  5923. _GunIndexOfPrioritySequence[i] = i;
  5924. }
  5925. }
  5926. bool IsShutdownTrigger(void)
  5927. {
  5928. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == YES ||
  5929. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency == YES)
  5930. {
  5931. if(!_isTriggerShutdown)
  5932. {
  5933. PSU_LOG("Psu Shutdown Flag Is Trigger!");
  5934. }
  5935. _isTriggerShutdown = true;
  5936. return true;
  5937. }
  5938. if(_isTriggerShutdown)
  5939. {
  5940. PSU_LOG("Psu Shutdown Flag Recover");
  5941. }
  5942. _isTriggerShutdown = false;
  5943. return false;
  5944. }
  5945. bool IsShutdownCompleted(void)
  5946. {
  5947. bool done = false;
  5948. if(IsShutdownTrigger())
  5949. {
  5950. done = true;
  5951. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5952. {
  5953. if(ShmPsuGrouping->GroupCollection[i].Role != _GROLE_IDLE)
  5954. {
  5955. done = false;
  5956. break;
  5957. }
  5958. }
  5959. if(done)
  5960. {
  5961. if(!_isShutdownCompleted)
  5962. {
  5963. PSU_LOG("Psu Shutdown Completed!");
  5964. }
  5965. _isShutdownCompleted = true;
  5966. }
  5967. return done;
  5968. }
  5969. if(_isShutdownCompleted)
  5970. {
  5971. PSU_LOG("Psu Shutdown Clear!");
  5972. }
  5973. _isShutdownCompleted = false;
  5974. return done;
  5975. }
  5976. void PsuIndication(void)
  5977. {
  5978. if(ShmChargerInfo->Control.LibCtrl.bits.InfyPwrLib)
  5979. {
  5980. for(int i = 0; i < MAX_PSU_MODULE_QUANTITY; i++)
  5981. {
  5982. if(ShmPsuPosition->SingleInfyPwrState[i].InfyPwrStateFlag.bits.Indicated)
  5983. {
  5984. LedSingleOnOff(i, _PSU_ON);
  5985. LOG_INFO("Set Psu[%2d] Led Blinking", i);
  5986. _Psu_Indication_Enable[i] = true;
  5987. _Psu_Indication_Count[i] = PSU_INDICATION_TIME;
  5988. ShmPsuPosition->SingleInfyPwrState[i].InfyPwrStateFlag.bits.Indicated = false;
  5989. }
  5990. else
  5991. {
  5992. if(_Psu_Indication_Enable[i])
  5993. {
  5994. _Psu_Indication_Count[i]--;
  5995. if(_Psu_Indication_Count[i] == 0)
  5996. {
  5997. LedSingleOnOff(i, _PSU_OFF);
  5998. LOG_INFO("Set Psu[%2d] Led Static", i);
  5999. _Psu_Indication_Enable[i] = false;
  6000. }
  6001. }
  6002. }
  6003. }
  6004. }
  6005. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  6006. {
  6007. }
  6008. }
  6009. int main(void)
  6010. {
  6011. byte _TotalModuleCount = 0;
  6012. byte _PrePsuWorkStep = 0;
  6013. byte isInitialComp = NO;
  6014. if(InitShareMemory() == FAIL)
  6015. {
  6016. #ifdef SystemLogMessage
  6017. LOG_ERROR("InitShareMemory NG");
  6018. #endif
  6019. if(ShmStatusCodeData != NULL)
  6020. {
  6021. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  6022. }
  6023. sleep(5);
  6024. return 0;
  6025. }
  6026. LOG_INFO("Psu Task PID = %d", getpid());
  6027. signal(SIGCHLD, SIG_IGN);
  6028. // register callback function
  6029. RefreshPsuIndex(&FindPsuIndex);
  6030. RefreshGroup(&GetGroupCallback);
  6031. RefreshAlarmStatus(&GetAlarmStatusCallback);
  6032. RefreshInfyPwrAlarmFlag(&GetInfyPwrAlarmFlagCallback);
  6033. RefreshPhPwrAlarmFlag(&GetPhPwrAlarmFlagCallback);
  6034. RefreshAmbient(&GetAmbientCallback);
  6035. RefreshModuleCount(&GetModuleCountCallback);
  6036. RefreshSysModuleCount(&GetSysModuleCountCallback);
  6037. RefreshAvailableCap(&GetAvailableCapCallback);
  6038. RefreshFwDcVersion(&GetDcFwCallback);
  6039. RefreshFwPfcVersion(&GetPfcFwCallback);
  6040. RefreshInputVol(&GetInputVoltageCallback);
  6041. RefreshGetOutput(&GetPresentOutputCallback);
  6042. RefreshFanSpeedInfo(&GetFanSpeedCallback);
  6043. RefreshInletTemp(&GetInletTempCallback);
  6044. RefreshExletTemp(&GetExletTempCallback);
  6045. RefreshOutletTemp(&GetOutletTempCallback);
  6046. RefreshOtherTemp(&GetOtherTempCallback);
  6047. RefreshIavailable(&GetIavailableCallback);
  6048. RefreshGetOutputF(&GetPresentOutputFCallback);
  6049. RefreshGetModuleOutput(&GetModuleOutputCallback);
  6050. RefreshGetErrorRecord(&GetErrorRecordCallback);
  6051. // GetPresentOutputCallback & GetStatusCallback
  6052. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  6053. // GetStatusCallback
  6054. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  6055. // GetInputVoltageCallback
  6056. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  6057. sleep(2);
  6058. InitialGunLimitSequence();
  6059. _gunCount = GENERAL_GUN_QUANTITY;
  6060. _maxGroupCount = MAX_GROUP_QUANTITY;
  6061. _PrePsuWorkStep = _INIT_PSU_STATUS;
  6062. ShmPsuData->Work_Step = INITIAL_START;
  6063. isInitialComp = NO;
  6064. isCommStart = false;
  6065. _isTriggerShutdown = false;
  6066. _isShutdownCompleted = false;
  6067. LOG_INFO("PSU Work Status = %d", ShmPsuData->Work_Step);
  6068. // initial object
  6069. Initialization();
  6070. InitialPsuData();
  6071. //libInitialize = InitialCommunication();
  6072. Run_PsuRxCommProcess();
  6073. //main loop
  6074. while (1)
  6075. {
  6076. PsuCommLostCheck();
  6077. IsShutdownCompleted();
  6078. // 斷電狀態
  6079. if (ShmChargerInfo->Control.RelayCtrl.bits.AcContactor == NO ||
  6080. ShmChargerInfo->Control.PsuCtrl.bits.NeedSelfTest == YES ||
  6081. ShmChargerInfo->Control.PsuCtrl.bits.Paused == YES ||
  6082. _isShutdownCompleted)
  6083. {
  6084. //一但 AC Off PSU 斷電全部的 PSU Group ID 會全部清 0
  6085. if (!isInitialComp)
  6086. {
  6087. _PrePsuWorkStep = _INIT_PSU_STATUS;
  6088. ShmPsuData->Work_Step = INITIAL_START;
  6089. psuCmdSeq = _PSU_CMD_STATUS;
  6090. _TotalModuleCount = 0;
  6091. PsuGroupingInitial();
  6092. sleep(2);
  6093. InitialPsuData();
  6094. isInitialComp = YES;
  6095. }
  6096. if(ShmChargerInfo->Control.PsuCtrl.bits.NeedSelfTest)
  6097. {
  6098. ShmChargerInfo->Control.PsuCtrl.bits.NeedSelfTest = NO;
  6099. ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK = NO;
  6100. memset(ShmChargerInfo->Control.PsuInitQuantity, 0x00, sizeof(ShmChargerInfo->Control.PsuInitQuantity));
  6101. LOG_INFO("Psu Need Execute Self Test!");
  6102. }
  6103. sleep(1);
  6104. continue;
  6105. }
  6106. else
  6107. {
  6108. if(isInitialComp)
  6109. {
  6110. system("ifconfig can1 down");
  6111. sleep(1);
  6112. system("ifconfig can1 up");
  6113. LOG_INFO("Psu Can Port Down & Up");
  6114. }
  6115. isInitialComp = NO;
  6116. }
  6117. // only for test & debug purpose
  6118. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  6119. {
  6120. PsuSimulationSetting();
  6121. PsuGroupOutputSimulation();
  6122. PsuGroupAvailableSimulation();
  6123. CapabilitySimulation();
  6124. }
  6125. // 自檢失敗
  6126. if (ShmPsuData->Work_Step == _NO_WORKING)
  6127. {
  6128. LOG_INFO("== PSU == self test fail.");
  6129. sleep(5);
  6130. }
  6131. if((GetTimeoutValue(_PsuReceiveRecoveryCheck_time) / uSEC_VAL) >= PSU_TASK_CHECK_TIME)
  6132. {
  6133. //PsuReceiveRecoveryCheck();
  6134. if(!Is_PsuRxComm_Alive())
  6135. {
  6136. Run_PsuRxCommProcess();
  6137. }
  6138. GetClockTime(&_PsuReceiveRecoveryCheck_time);
  6139. }
  6140. switch(ShmPsuData->Work_Step)
  6141. {
  6142. case INITIAL_START:
  6143. {
  6144. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6145. {
  6146. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6147. LOG_INFO("== PSU == INITIAL_START");
  6148. }
  6149. sleep(5);
  6150. PsuGroupOnOff(SYSTEM_CMD, _PSU_OFF);
  6151. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  6152. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  6153. {
  6154. isStartOutputSwitch[index] = false;
  6155. }
  6156. ShmPsuData->Work_Step = GET_PSU_COUNT;
  6157. }
  6158. break;
  6159. case GET_PSU_COUNT:
  6160. {
  6161. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6162. {
  6163. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6164. LOG_INFO("== PSU == GET_PSU_COUNT");
  6165. GetClockTime(&_PsuWorkStep_time);
  6166. GetClockTime(&_cmdSubPriority_time);
  6167. }
  6168. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  6169. int interval = GetTimeoutValue(_cmdSubPriority_time) / uSEC_VAL;
  6170. if(interval >= GET_PSU_COUNT_INTERVAL)
  6171. {
  6172. _TotalModuleCount = 0;
  6173. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  6174. {
  6175. // 總和各群模組數量
  6176. _TotalModuleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  6177. }
  6178. LOG_INFO("== PSU == indexCount = %d, moduleCount = %d, sysCount = %d",
  6179. ShmPsuData->GroupCount, _TotalModuleCount, ShmPsuData->SystemInitialPsuQuantity);
  6180. // 判斷系統數量與各群數量一致
  6181. if(_TotalModuleCount == ShmPsuData->SystemInitialPsuQuantity && _TotalModuleCount > 0 &&
  6182. time > GET_PSU_COUNT_TIME)
  6183. {
  6184. LOG_INFO("Psu Count = %d", _TotalModuleCount);
  6185. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  6186. {
  6187. ShmChargerInfo->Control.PsuInitQuantity[i] = ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity;
  6188. }
  6189. ShmPsuData->Work_Step = Get_PSU_LOCATION;
  6190. }
  6191. else
  6192. {
  6193. // 發送取得目前全部模組數量
  6194. GetSysModuleCount();
  6195. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  6196. {
  6197. // 取各群模組數量
  6198. GetModuleCount(index);
  6199. }
  6200. }
  6201. GetClockTime(&_cmdSubPriority_time);
  6202. }
  6203. }
  6204. break;
  6205. case Get_PSU_LOCATION:
  6206. {
  6207. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6208. {
  6209. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6210. LOG_INFO("== PSU == Get_PSU_LOCATION");
  6211. // clean psu location info
  6212. memset(ShmPsuPosition, 0x00, sizeof(PsuPositionInfoData));
  6213. GetClockTime(&_cmdSubPriority_time);
  6214. }
  6215. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  6216. if(interval >= GET_PSU_LOCATION_INTERVAL)
  6217. {
  6218. for(byte index = 0; index < ShmPsuData->GroupCount; index++)
  6219. {
  6220. if(ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity > 0)
  6221. {
  6222. // 取得狀態 : 支援模塊不須按照順序功能
  6223. GetStatus(index);
  6224. }
  6225. }
  6226. GetClockTime(&_cmdSubPriority_time);
  6227. }
  6228. if(ShmPsuPosition->PsuLocationInit)
  6229. {
  6230. for(int i = 0; i < _maxGroupCount; i++)
  6231. {
  6232. UpdateGunTotalPsuQuantity(i);
  6233. #if 1
  6234. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  6235. {
  6236. ShowGroupMember(i);
  6237. }
  6238. #endif
  6239. }
  6240. //ShmPsuData->Work_Step = Get_PSU_VERSION;
  6241. ShmPsuData->Work_Step = PSU_COUNT_CONFIRM;
  6242. }
  6243. if(ShmPsuPosition->ReInitPsuLocation)
  6244. {
  6245. LOG_INFO("Retry Psu Location Initialization");
  6246. ShmPsuData->Work_Step = GET_PSU_COUNT;
  6247. }
  6248. }
  6249. break;
  6250. case PSU_COUNT_CONFIRM:
  6251. {
  6252. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6253. {
  6254. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6255. LOG_INFO("== PSU == PSU_COUNT_CONFIRM");
  6256. GetClockTime(&_PsuWorkStep_time);
  6257. GetClockTime(&_cmdSubPriority_time);
  6258. }
  6259. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  6260. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  6261. if(interval > PSU_COUNT_CONFIRM_INTERVAL)
  6262. {
  6263. _TotalModuleCount = 0;
  6264. for (byte index = 0; index < _maxGroupCount; index++)
  6265. {
  6266. // 總和各群模組數量
  6267. _TotalModuleCount += ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity;
  6268. }
  6269. if(time > PSU_COUNT_CONFIRM_TIME)
  6270. {
  6271. if(_TotalModuleCount == ShmPsuData->SystemInitialPsuQuantity && _TotalModuleCount > 0)
  6272. {
  6273. ShmPsuData->Work_Step = Get_PSU_VERSION;
  6274. }
  6275. else
  6276. {
  6277. LOG_INFO("Total PSU = %d, System PSU Quantity = %d", _TotalModuleCount, ShmPsuData->SystemInitialPsuQuantity);
  6278. LOG_INFO("PSU Quantity Confirm Fail");
  6279. ShmPsuData->Work_Step = GET_PSU_COUNT;
  6280. }
  6281. break;
  6282. }
  6283. // 發送取得目前全部模組數量
  6284. GetSysModuleCount();
  6285. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  6286. {
  6287. // 取各群模組數量
  6288. GetModuleCount(index);
  6289. }
  6290. GetClockTime(&_cmdSubPriority_time);
  6291. }
  6292. }
  6293. break;
  6294. case Get_PSU_VERSION:
  6295. {
  6296. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6297. {
  6298. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6299. LOG_INFO("== PSU == Get_PSU_VERSION");
  6300. GetClockTime(&_cmdSubPriority_time);
  6301. // clean version info
  6302. memset(ShmPsuData->PsuVersion, 0x00, sizeof(ShmPsuData->PsuVersion));
  6303. }
  6304. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  6305. if (interval >= GET_PSU_VERSION_INTERVAL)
  6306. {
  6307. bool isGetVersion = true;
  6308. for(byte psu = 0; psu < ShmPsuData->SystemInitialPsuQuantity; psu++)
  6309. {
  6310. if (strcmp((char *)ShmPsuData->PsuVersion[psu].FwPrimaryVersion, "") == EQUAL)
  6311. {
  6312. isGetVersion = false;
  6313. break;
  6314. }
  6315. }
  6316. if(isGetVersion)
  6317. {
  6318. #if 1
  6319. for(int i = 0; i < _maxGroupCount; i++)
  6320. {
  6321. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  6322. {
  6323. ShowPsuVersion(i);
  6324. }
  6325. }
  6326. #endif
  6327. ShmPsuData->Work_Step = GET_SYS_CAP;
  6328. }
  6329. else
  6330. {
  6331. for(byte group = 0; group < _maxGroupCount; group++)
  6332. {
  6333. if(ShmPsuData->PsuGroup[group].GroupPresentPsuQuantity > 0)
  6334. {
  6335. // 取版號
  6336. GetAllModuleVer(group);
  6337. }
  6338. }
  6339. }
  6340. GetClockTime(&_cmdSubPriority_time);
  6341. }
  6342. }
  6343. break;
  6344. case GET_SYS_CAP:
  6345. {
  6346. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6347. {
  6348. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6349. LOG_INFO("== PSU == GET_SYS_CAP");
  6350. GetClockTime(&_PsuWorkStep_time);
  6351. GetClockTime(&_cmdSubPriority_time);
  6352. }
  6353. int time = GetTimeoutValue(_PsuWorkStep_time) / uSEC_VAL;
  6354. int interval = GetTimeoutValue(_cmdSubPriority_time) / mSEC_VAL;
  6355. if (interval >= GET_PSU_CAP_INTERVAL)
  6356. {
  6357. if(time > GET_PSU_CAP_TIME && ShmPsuData->SystemAvailablePower > 0 &&
  6358. ShmPsuData->SystemAvailableCurrent > 0)
  6359. {
  6360. #if 1
  6361. for(int i = 0; i < _maxGroupCount; i++)
  6362. {
  6363. if(ShmPsuPosition->GroupLocationInfo[i].GroupPsuQuantity > 0)
  6364. {
  6365. ShowGroupAvailableCurrentPower(i);
  6366. }
  6367. }
  6368. #endif
  6369. // 判斷系統輸出額定功率與電流
  6370. LOG_INFO("SystemAvailableCurrent = %d, SystemAvailablePower = %d",
  6371. ShmPsuData->SystemAvailableCurrent, ShmPsuData->SystemAvailablePower);
  6372. ShmPsuData->Work_Step = BOOTING_COMPLETE;
  6373. }
  6374. else
  6375. {
  6376. for(byte index = 0; index < ShmPsuData->GroupCount; index++)
  6377. {
  6378. if(ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity > 0)
  6379. {
  6380. // 取系統總輸出能力
  6381. GetModuleCap(index);
  6382. }
  6383. }
  6384. }
  6385. GetClockTime(&_cmdSubPriority_time);
  6386. }
  6387. }
  6388. break;
  6389. case BOOTING_COMPLETE:
  6390. {
  6391. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6392. {
  6393. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6394. LOG_INFO("== PSU == BOOTING_COMPLETE");
  6395. }
  6396. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  6397. {
  6398. ShmPsuData->Work_Step = _WORK_CHARGING;
  6399. }
  6400. else
  6401. {
  6402. LOG_INFO("== PSU == Self Test OK!");
  6403. }
  6404. ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK = true;
  6405. }
  6406. break;
  6407. case _WORK_CHARGING:
  6408. {
  6409. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6410. {
  6411. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6412. LOG_INFO("== PSU == _WORK_CHARGING");
  6413. GetClockTime(&_PsuWorkStep_time);
  6414. GetClockTime(&_cmdSubPriority_time);
  6415. PsuGroupingInitial();
  6416. }
  6417. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  6418. // 低 Priority 的指令
  6419. if (time >= 1000)
  6420. {
  6421. GetSysModuleCount();
  6422. for(int i = 0; i < MAX_GROUP_QUANTITY; i++)
  6423. {
  6424. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  6425. {
  6426. GetStatus(i);
  6427. GetModuleCount(i);
  6428. GetModuleInputVol(i);
  6429. if(IsPsuGroupAnyAlarm(i) || IsPsuGroupAnyFault(i))
  6430. {
  6431. GetErrorRecord(i);
  6432. }
  6433. }
  6434. }
  6435. PsuIndication();
  6436. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  6437. {
  6438. UpdateGunTotalPsuQuantity(i);
  6439. }
  6440. GetClockTime(&_cmdSubPriority_time);
  6441. }
  6442. CheckPsuAlarmStatus();
  6443. CheckPsuErrRecord();
  6444. PsuGroupRoutineQuery();
  6445. PsuGroupControlProcess();
  6446. break;
  6447. }
  6448. case _TEST_MODE:
  6449. {
  6450. // 在測試模式中,保持與模塊的通訊
  6451. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  6452. if (time > 1500)
  6453. {
  6454. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  6455. {
  6456. // 取系統總輸出能力
  6457. GetModuleCap(index);
  6458. // 取各群輸出電壓電流 (float)
  6459. GetModuleOutputF(index);
  6460. }
  6461. GetClockTime(&_cmdSubPriority_time);
  6462. }
  6463. byte _switch = 0x00;
  6464. if ((chargingInfo[0]->EvBatterytargetVoltage * 10) > 0 && (chargingInfo[0]->EvBatterytargetCurrent * 10) > 0)
  6465. _switch = 0x01;
  6466. for (byte _groupCount_1 = 0; _groupCount_1 < conn_1_count; _groupCount_1++)
  6467. {
  6468. SetDirModulePresentOutput(connector_1[_groupCount_1],
  6469. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  6470. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  6471. _switch, _switch);
  6472. }
  6473. for (byte _groupCount_2 = 0; _groupCount_2 < conn_2_count; _groupCount_2++)
  6474. {
  6475. SetDirModulePresentOutput(connector_2[_groupCount_2],
  6476. (chargingInfo[0]->EvBatterytargetVoltage * 10),
  6477. (chargingInfo[0]->EvBatterytargetCurrent * 10),
  6478. _switch, _switch);
  6479. }
  6480. }
  6481. break;
  6482. case _INIT_PSU_STATUS:
  6483. if(_PrePsuWorkStep != ShmPsuData->Work_Step)
  6484. {
  6485. _PrePsuWorkStep = ShmPsuData->Work_Step;
  6486. LOG_INFO("== PSU == _INIT_PSU_STATUS");
  6487. GetClockTime(&_cmdSubPriority_time);
  6488. }
  6489. break;
  6490. default:
  6491. break;
  6492. }
  6493. usleep(20000);
  6494. }
  6495. return FAIL;
  6496. }