From 8ad6aaa4436abda7eddfb919f12eff2475ff120b Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Tue, 27 Oct 2026 14:11:56 +0100 Subject: [PATCH 1/2] fix(control): one log of FTW's own EV offers The battery side read a 20 s pool log of EV offers (State.evOffers), while the EV fuse limit keeps each charger's offers for its declared response time, or 180 s when it declares none. After 20 s the battery counted a slow charger's unshown raise as already in the meter and charged into that room; when the car started, a 25 A site read 32.6 A per phase for 3 s. Delete State.evOffers and its two writers. evInMeterW and clampEVBatteryHeadroom read evShownW, each charger's latest offer less what it may not show yet. The hourly peak in stepEVMeterLimit takes each charger's highest offer of the last evSettle from the same log. Co-Authored-By: Claude Opus 5.5 --- .changeset/a6-one-ev-offer-log.md | 5 +++ go/internal/control/dispatch.go | 16 +++------ go/internal/control/ev_battery_headroom.go | 8 ++--- .../control/ev_battery_headroom_test.go | 3 +- .../control/ev_battery_priority_test.go | 3 ++ go/internal/control/ev_fuse_budget.go | 26 ++++++++++++--- .../control/ev_slow_charger_battery_test.go | 33 +++++++++++++++++++ 7 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 .changeset/a6-one-ev-offer-log.md create mode 100644 go/internal/control/ev_slow_charger_battery_test.go diff --git a/.changeset/a6-one-ev-offer-log.md b/.changeset/a6-one-ev-offer-log.md new file mode 100644 index 000000000..a11cc1905 --- /dev/null +++ b/.changeset/a6-one-ev-offer-log.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +Keep home battery charging out of the room a slow charger's raise still needs. The battery now counts a car's offer as not yet in the meter for the charger's own response time (180 s when it declares none), as the car's fuse limit already did, instead of 20 s. A charger that takes two minutes to start no longer pushes a 25 A phase to 32.6 A. diff --git a/go/internal/control/dispatch.go b/go/internal/control/dispatch.go index 3e01f1eea..395e52725 100644 --- a/go/internal/control/dispatch.go +++ b/go/internal/control/dispatch.go @@ -391,9 +391,8 @@ type State struct { evLimit evMeterLimit // fuseHeat is the main fuse's heat per phase, from site readings only. fuseHeat fuseHeat - // evOffers and batteryCommands are our own recent commands, the only - // device input the EV hard limit and the EV/battery split use. - evOffers commandLog + // batteryCommands and evLimit.offers are our own recent commands, the + // only device input the EV hard limit and the EV/battery split use. batteryCommands map[string]commandLog // PI controller (outer, site-level) @@ -2475,7 +2474,6 @@ func recordDispatchTargets(targets []DispatchTarget, state *State, updatePrevTar for _, t := range targets { state.batteryCommands[t.Driver] = state.batteryCommands[t.Driver].add(now, t.TargetW) } - state.evOffers = state.evOffers.add(now, math.Max(0, state.EVCommandedW)) } if updatePrevTargets { for _, t := range targets { @@ -4644,12 +4642,8 @@ func forceFuseDischargeWithSnapshot( } // evInMeterW is EV load the meter is sure to contain: the operator's declared -// load plus the lowest managed offer of the last evSettle. This tick's new -// offer is not in it yet. Charger readings take no part in the fuse share. +// load plus the managed offers already shown (evShownW). Charger readings +// take no part in the fuse share. func (s *State) evInMeterW() float64 { - low, _, _, ok := s.evOffers.span(s.now()) - if !ok { - low = 0 - } - return s.ManualEVChargingW + math.Max(0, low) + return s.ManualEVChargingW + s.evShownW(s.now()) } diff --git a/go/internal/control/ev_battery_headroom.go b/go/internal/control/ev_battery_headroom.go index 86c5cb597..f33b2a52d 100644 --- a/go/internal/control/ev_battery_headroom.go +++ b/go/internal/control/ev_battery_headroom.go @@ -10,17 +10,13 @@ import ( // still needs. It reads only the site meter and our own commands; no charger // reading takes part, and a battery's own reading only bounds how much of a // discharge we ordered it may keep. The EV still needs its demand less the -// lowest offer of the last evSettle, which the meter surely shows. +// offers the meter surely shows (evShownW). func clampEVBatteryHeadroom(targets []DispatchTarget, store *telemetry.Store, state *State, fuseMaxW float64) []DispatchTarget { if store == nil || state == nil || len(targets) == 0 || fuseMaxW <= 0 { return targets } now := state.now() - // This tick's new offer is not in the meter yet; the lowest logged one is. - offeredW, _, _, ok := state.evOffers.span(now) - if !ok { - offeredW = 0 - } + offeredW := state.evShownW(now) var demandW float64 phases := 3.0 // No phase placement promise for 1p or auto chargers. for _, demand := range state.evFuseDemands { diff --git a/go/internal/control/ev_battery_headroom_test.go b/go/internal/control/ev_battery_headroom_test.go index 9fafb4872..32b4af518 100644 --- a/go/internal/control/ev_battery_headroom_test.go +++ b/go/internal/control/ev_battery_headroom_test.go @@ -47,7 +47,8 @@ func sent(state *State, driver string, watts ...float64) { log = log.add(at, w) } if driver == "ev" { - state.evOffers, state.EVCommandedW = log, watts[len(watts)-1] + state.evLimit.offers = map[string]chargerOffers{"car": {log: log, settle: evSettle}} + state.EVCommandedW = watts[len(watts)-1] return } state.batteryCommands[driver] = log diff --git a/go/internal/control/ev_battery_priority_test.go b/go/internal/control/ev_battery_priority_test.go index f84b0d96c..3c3df469a 100644 --- a/go/internal/control/ev_battery_priority_test.go +++ b/go/internal/control/ev_battery_priority_test.go @@ -22,6 +22,9 @@ func issue1868Site(t *testing.T) (*telemetry.Store, *State, []loadpoint.State, [ tel.Update("battery", telemetry.DerBattery, 4000, &soc, nil) tel.RecordDriverSuccess("battery") sent(st, "ev", 4140) + // The car's 4140 W offer has run for longer than any charger's response time. + a, n := st.offerPhaseA(4140, 3) + st.evLimit.offers = map[string]chargerOffers{"rav4": {log: commandLog{{at: st.now().Add(-evUnknownSettle - time.Second), w: 4140, a: a, n: n}}}} sent(st, "battery", 4000) points := []loadpoint.State{{ID: "rav4", DriverName: "car", PluggedIn: true, CommandedKnown: true, CommandedW: 4140, CommandedPhases: 3}} configs := []loadpoint.Config{{ID: "rav4", DriverName: "car", PhaseMode: "3p", MinChargeW: 4140, MaxChargeW: 4140, AllowedStepsW: []float64{0, 4140}}} diff --git a/go/internal/control/ev_fuse_budget.go b/go/internal/control/ev_fuse_budget.go index e07c63e25..c3193fe8e 100644 --- a/go/internal/control/ev_fuse_budget.go +++ b/go/internal/control/ev_fuse_budget.go @@ -187,6 +187,21 @@ type pendingLoad struct { phaseA [3]float64 // on each phase } +// evShownW is the EV load a reading taken at `at` surely shows: each +// plugged charger's latest offer less what it may not show yet, kept for +// its own response time (recordEVOffers). The battery side reads this, so +// it waits for a slow charger's raise as long as the EV limit does. +func (s *State) evShownW(at time.Time) float64 { + var w float64 + for _, c := range s.evLimit.offers { + if n := len(c.log); n > 0 { + unshown, _ := c.log.unshown(at, c.settle) + w += math.Max(0, c.log[n-1].w-unshown) + } + } + return w +} + func (s *State) pendingLoad(at time.Time) pendingLoad { var p pendingLoad for _, c := range s.evLimit.offers { @@ -368,9 +383,6 @@ func EVFuseBudget(store *telemetry.Store, state *State, points []loadpoint.State *lim = evMeterLimit{} return 0, true } - if pool.offerKnown { - state.evOffers = state.evOffers.add(state.now(), pool.offerW) - } state.recordEVOffers(store, points) if !lim.set || meter.UpdatedAt.After(lim.readingAt) { state.stepEVMeterLimit(store, meter, gridW, pool, fuseMaxW) @@ -389,9 +401,13 @@ func (s *State) stepEVMeterLimit(store *telemetry.Store, meter *telemetry.DerRea } lim.set = true peakW := pool.offerW - if _, high, _, ok := s.evOffers.span(now); ok { - peakW = math.Max(peakW, high) + var logPeakW float64 + for _, c := range s.evLimit.offers { + if _, high, _, ok := c.log.span(now); ok { + logPeakW += high + } } + peakW = math.Max(peakW, logPeakW) // Our own battery orders the meter may not show yet count beside the // chargers' offers, in total import and on each phase. pending := s.pendingBatteryCommandsW(now) diff --git a/go/internal/control/ev_slow_charger_battery_test.go b/go/internal/control/ev_slow_charger_battery_test.go new file mode 100644 index 000000000..c8275988b --- /dev/null +++ b/go/internal/control/ev_slow_charger_battery_test.go @@ -0,0 +1,33 @@ +package control + +import ( + "testing" + "time" + + "github.com/srcfl/ftw/go/internal/sitesim" +) + +// A charger that declares no response time and takes delay to start, beside +// a planned 9.4 kW battery charge on a 25 A site with 3 A of house load per +// phase. The car's 11 kW offer stays unshown until the charger applies it, +// and the battery must not take that room: when the car starts, no phase +// may pass the instant limit, and the car must not sit idle with room. +func TestSlowChargerUnshownOfferKeepsBatteryOut(t *testing.T) { + for _, delay := range []time.Duration{5 * time.Second, 30 * time.Second, 40 * time.Second, 2 * time.Minute} { + sc := simScenario{name: "slow charger", run: 15 * time.Minute, + plant: func(p *sitesim.Plant) { + p.HouseW, p.EVSE.Delay = houseA(3, 3, 3), delay + p.Battery = &sitesim.Battery{Phases: 3, MaxChargeW: 9400, MaxDischargeW: 9400, CapacityWh: 20000, SoC: .3, Latency: 3 * time.Second} + }, + setup: planBatteryCharge(9400)} + s := runSimScenario(sc, simStart) + t.Logf("delay %v: peak phase %.1f A, %v over %.0f A, idle with room %v, car %.0f W, battery %.0f W", + delay, s.peakA, s.overLimit, s.p.Fuse.RatingA-evPhaseMarginA, s.idleWithRoom, s.p.EVSE.DrawW, s.p.Battery.W) + if s.failure != "" { + t.Errorf("delay %v: %s", delay, s.failure) + } + if limitA := fuseInstantFactor * s.p.Fuse.RatingA; s.peakA > limitA { + t.Errorf("delay %v: peak phase %.1f A above the instant limit %.1f A", delay, s.peakA, limitA) + } + } +} From 0af26e04610349f00d303d3e99e9932249829d27 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Tue, 27 Oct 2026 15:53:58 +0100 Subject: [PATCH 2/2] fix(control): battery waits only for a declared charger response time The first A6 change gave the battery side the EV limit's 180 s guess for every charger that declares no response time. Only Easee Cloud declares one, so on every other charger the battery reserved the car's whole demand on top of a reading that already showed the car, and stopped charging for 3 minutes after each car start or raise. On the #1868 shape (rig s18) battery charge stayed at 0 W until 180 s. evShownW now uses the charger's declared power_max_age_s, and evSettle when it declares none, as master did. Easee Cloud declares 180 s, so the slow-charger case stays fixed. Tests: the slow-charger test now declares 180 s, and a new plant test runs the s18 shape with a fast charger that declares nothing. Co-Authored-By: Claude Opus 5.5 --- .changeset/a6-one-ev-offer-log.md | 2 +- go/internal/control/ev_fuse_budget.go | 24 +++++-- .../control/ev_slow_charger_battery_test.go | 65 +++++++++++++++++-- go/internal/control/sitesim_field_test.go | 5 +- go/internal/control/sitesim_test.go | 3 + 5 files changed, 85 insertions(+), 14 deletions(-) diff --git a/.changeset/a6-one-ev-offer-log.md b/.changeset/a6-one-ev-offer-log.md index a11cc1905..8779c2a08 100644 --- a/.changeset/a6-one-ev-offer-log.md +++ b/.changeset/a6-one-ev-offer-log.md @@ -2,4 +2,4 @@ "ftw": patch --- -Keep home battery charging out of the room a slow charger's raise still needs. The battery now counts a car's offer as not yet in the meter for the charger's own response time (180 s when it declares none), as the car's fuse limit already did, instead of 20 s. A charger that takes two minutes to start no longer pushes a 25 A phase to 32.6 A. +Keep home battery charging out of the room a slow charger's raise still needs. When a charger's driver declares its response time, as Easee Cloud declares 180 s, the battery now counts a car's offer as not yet in the meter for that time, as the car's fuse limit already did, instead of 20 s. A charger that takes two minutes to start no longer pushes a 25 A phase to 32.6 A. A charger that declares no response time keeps the 20 s, so the battery and a car on a local charger still charge together within a minute of the car's start. diff --git a/go/internal/control/ev_fuse_budget.go b/go/internal/control/ev_fuse_budget.go index c3193fe8e..b74b81115 100644 --- a/go/internal/control/ev_fuse_budget.go +++ b/go/internal/control/ev_fuse_budget.go @@ -170,6 +170,9 @@ func (l commandLog) toCome(from, at time.Time, before sentCommand) (w, a float64 type chargerOffers struct { log commandLog settle time.Duration + // declared is true when the driver declares that time; settle is then + // not evUnknownSettle's guess (evShownW). + declared bool // found is the offer in force before the limit's first site reading, // after a restart: Core did not send it in this run. A rise counts the // charger as 0 before it; a cut takes it as shown, since the reading is @@ -188,14 +191,23 @@ type pendingLoad struct { } // evShownW is the EV load a reading taken at `at` surely shows: each -// plugged charger's latest offer less what it may not show yet, kept for -// its own response time (recordEVOffers). The battery side reads this, so -// it waits for a slow charger's raise as long as the EV limit does. +// plugged charger's latest offer less what it may not show yet. The battery +// side reads this. A raise stays unshown for the response time the +// charger's driver declares, so the battery waits for a slow charger's raise +// as long as the EV limit does. A charger that declares none gets evSettle, +// not evUnknownSettle: the battery would otherwise reserve a local charger's +// raise on top of the reading that already shows it, and stop charging for +// three minutes after each one. Easee Cloud, the slow charger we know, +// declares its 180 s. func (s *State) evShownW(at time.Time) float64 { var w float64 for _, c := range s.evLimit.offers { if n := len(c.log); n > 0 { - unshown, _ := c.log.unshown(at, c.settle) + settle := evSettle + if c.declared { + settle = c.settle + } + unshown, _ := c.log.unshown(at, settle) w += math.Max(0, c.log[n-1].w-unshown) } } @@ -273,10 +285,10 @@ func (s *State) recordEVOffers(store *telemetry.Store, points []loadpoint.State) continue } c := s.evLimit.offers[point.ID] - c.settle = evUnknownSettle + c.settle, c.declared = evUnknownSettle, false if r := store.Get(point.DriverName, telemetry.DerEV); r != nil { if declared := telemetry.DeclaredPowerMaxAge(r.Data); declared > 0 { - c.settle = max(evSettle, declared) + c.settle, c.declared = max(evSettle, declared), true } } w := math.Max(0, point.CommandedW) diff --git a/go/internal/control/ev_slow_charger_battery_test.go b/go/internal/control/ev_slow_charger_battery_test.go index c8275988b..91b583532 100644 --- a/go/internal/control/ev_slow_charger_battery_test.go +++ b/go/internal/control/ev_slow_charger_battery_test.go @@ -1,17 +1,20 @@ package control import ( + "fmt" "testing" "time" + "github.com/srcfl/ftw/go/internal/loadpoint" "github.com/srcfl/ftw/go/internal/sitesim" ) -// A charger that declares no response time and takes delay to start, beside -// a planned 9.4 kW battery charge on a 25 A site with 3 A of house load per -// phase. The car's 11 kW offer stays unshown until the charger applies it, -// and the battery must not take that room: when the car starts, no phase -// may pass the instant limit, and the car must not sit idle with room. +// A charger that declares a 180 s response time, as Easee Cloud does, and +// takes delay to start, beside a planned 9.4 kW battery charge on a 25 A +// site with 3 A of house load per phase. The car's 11 kW offer stays +// unshown until the charger applies it, and the battery must not take that +// room: when the car starts, no phase may pass the instant limit, and the +// car must not sit idle with room. func TestSlowChargerUnshownOfferKeepsBatteryOut(t *testing.T) { for _, delay := range []time.Duration{5 * time.Second, 30 * time.Second, 40 * time.Second, 2 * time.Minute} { sc := simScenario{name: "slow charger", run: 15 * time.Minute, @@ -19,7 +22,10 @@ func TestSlowChargerUnshownOfferKeepsBatteryOut(t *testing.T) { p.HouseW, p.EVSE.Delay = houseA(3, 3, 3), delay p.Battery = &sitesim.Battery{Phases: 3, MaxChargeW: 9400, MaxDischargeW: 9400, CapacityWh: 20000, SoC: .3, Latency: 3 * time.Second} }, - setup: planBatteryCharge(9400)} + setup: func(s *simSite) { + s.evMaxAge = evUnknownSettle + planBatteryCharge(9400)(s) + }} s := runSimScenario(sc, simStart) t.Logf("delay %v: peak phase %.1f A, %v over %.0f A, idle with room %v, car %.0f W, battery %.0f W", delay, s.peakA, s.overLimit, s.p.Fuse.RatingA-evPhaseMarginA, s.idleWithRoom, s.p.EVSE.DrawW, s.p.Battery.W) @@ -31,3 +37,50 @@ func TestSlowChargerUnshownOfferKeepsBatteryOut(t *testing.T) { } } } + +// #1868's rig shape (s18): a 16 A site with 3 A of house load, a local +// charger that declares no response time and draws within 2 s, a car that +// takes only [0, 4140] W, and a 6000 W battery charge. Once the car draws, +// the reading shows it, and the battery shares the rest of the fuse well +// before evUnknownSettle; s18's judge needs it above 1000 W 100-120 s after +// Charge now. +func TestFastChargerYoungOfferLetsBatteryShare(t *testing.T) { + for _, tc := range []struct { + name string + setup func(*simSite) + }{ + {"manual hold", func(s *simSite) { + s.state.SetBatteryManualHold(BatteryManualHold{PowerW: 6000, ExpiresAt: s.p.Now.Add(time.Hour)}) + }}, + {"plan", planBatteryCharge(6000)}, + } { + t.Run(tc.name, func(t *testing.T) { + var expect []simExpect + for _, at := range []time.Duration{60 * time.Second, 100 * time.Second, 120 * time.Second, 170 * time.Second} { + expect = append(expect, simExpect{at, func(s *simSite) error { + if s.p.EVSE.DrawW < 4100 || s.p.Battery.W < 3000 { + return fmt.Errorf("car %.0f W and battery %.0f W at %v; both should charge", s.p.EVSE.DrawW, s.p.Battery.W, at) + } + return nil + }}) + } + sc := simScenario{name: "s18 " + tc.name, run: 4 * time.Minute, + plant: func(p *sitesim.Plant) { + p.Fuse.RatingA, p.HouseW, p.EVSE.Delay, p.EVSE.ReadbackLag = 16, houseA(3, 3, 3), 2*time.Second, 0 + p.Battery = &sitesim.Battery{Phases: 3, MaxChargeW: 6000, MaxDischargeW: 6000, CapacityWh: 20000, SoC: .4, Latency: 2 * time.Second} + }, + lp: func(c *loadpoint.Config) { + c.MinChargeW, c.MaxChargeW, c.AllowedStepsW = 4140, 4140, []float64{0, 4140} + }, + setup: tc.setup, + expect: expect} + s := runSimScenario(sc, simStart) + if s.failure != "" { + t.Error(s.failure) + } + if limitA := fuseInstantFactor * s.p.Fuse.RatingA; s.peakA > limitA { + t.Errorf("peak phase %.1f A above the instant limit %.1f A", s.peakA, limitA) + } + }) + } +} diff --git a/go/internal/control/sitesim_field_test.go b/go/internal/control/sitesim_field_test.go index 29263817c..7ea788beb 100644 --- a/go/internal/control/sitesim_field_test.go +++ b/go/internal/control/sitesim_field_test.go @@ -460,7 +460,7 @@ func (s *simSite) coreEvery() time.Duration { func (s *simSite) declaredMaxAge() time.Duration { if s.field == nil { - return 0 + return s.evMaxAge } return s.field.c.timing.declared } @@ -468,6 +468,9 @@ func (s *simSite) declaredMaxAge() time.Duration { // evData is what a charger driver adds to its reading. func (s *simSite) evData() map[string]any { if s.field == nil { + if s.evMaxAge > 0 { + return map[string]any{"power_max_age_s": s.evMaxAge.Seconds()} + } return nil } d := map[string]any{"connected": s.ev.Plugged, "control_power_confirmed": true} diff --git a/go/internal/control/sitesim_test.go b/go/internal/control/sitesim_test.go index 6022cd157..0d37b6d47 100644 --- a/go/internal/control/sitesim_test.go +++ b/go/internal/control/sitesim_test.go @@ -39,6 +39,9 @@ type simSite struct { every time.Duration // control_interval_s; zero: simCoreEvery field *fieldRun // the field matrix's own checks; nil outside it + // evMaxAge is the power_max_age_s the charger driver declares outside + // the field matrix; zero declares none. + evMaxAge time.Duration downUntil, nextTick time.Time meter sitesim.MeterReading