Module_PsuComm.c 171 KB

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