Module_PsuComm.c 254 KB

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