Module_PsuComm.c 178 KB

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