diff --git a/.changeset/a6-one-ev-offer-log.md b/.changeset/a6-one-ev-offer-log.md new file mode 100644 index 000000000..8779c2a08 --- /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. 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/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..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 @@ -187,6 +190,30 @@ 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. 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 { + settle := evSettle + if c.declared { + settle = c.settle + } + unshown, _ := c.log.unshown(at, 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 { @@ -258,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) @@ -368,9 +395,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 +413,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..91b583532 --- /dev/null +++ b/go/internal/control/ev_slow_charger_battery_test.go @@ -0,0 +1,86 @@ +package control + +import ( + "fmt" + "testing" + "time" + + "github.com/srcfl/ftw/go/internal/loadpoint" + "github.com/srcfl/ftw/go/internal/sitesim" +) + +// 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, + 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: 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) + 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) + } + } +} + +// #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