Module_PsuComm.c 174 KB

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