Module_PsuComm.c 163 KB

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