Module_PsuComm.c 160 KB

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