Module_PsuComm.c 223 KB

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