Module_PsuComm.c 221 KB

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