diff --git a/.changeset/c8-heat-filter-cold-start.md b/.changeset/c8-heat-filter-cold-start.md new file mode 100644 index 000000000..5a478bb7e --- /dev/null +++ b/.changeset/c8-heat-filter-cold-start.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +After a restart, one high site reading no longer holds an idle car back for close to a minute. The fuse heat model used to start at the first reading's current, as if that overload had lasted. In seed 207539 one reading of 27.94 A on a 20 A fuse delayed the car's start by about 43 s. The model now starts at the first reading's current, at most the working limit. A sustained overload after a restart is still cut on the next reading, and readings above 1.2 × the fuse rating are still cut at once. diff --git a/go/internal/control/ev_battery_priority_test.go b/go/internal/control/ev_battery_priority_test.go index 5b0e43068..6564016ea 100644 --- a/go/internal/control/ev_battery_priority_test.go +++ b/go/internal/control/ev_battery_priority_test.go @@ -34,6 +34,7 @@ func issue1868Site(t *testing.T) (*telemetry.Store, *State, []loadpoint.State, [ 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) + lastingFuseHeat(tel, st) // The overage has lasted, as in the field. 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}}} return tel, st, points, configs @@ -107,8 +108,9 @@ func TestEVPriorityFallsBackWhenTheBatteryCannotYield(t *testing.T) { {"stale meter", func(tel *telemetry.Store, _ *State) { tel.Get("meter", telemetry.DerMeter).UpdatedAt = time.Now().Add(-time.Minute) }}, - {"over the working limit", func(tel *telemetry.Store, _ *State) { + {"over the working limit", func(tel *telemetry.Store, st *State) { tel.Update("meter", telemetry.DerMeter, 9000, nil, []byte(`{"l1_a":15.6,"l2_a":12.5,"l3_a":11.9}`)) + lastingFuseHeat(tel, st) // the overage has lasted (I²t) }}, {"unseen battery rise", func(_ *telemetry.Store, st *State) { sent(st, "battery", 0, 4000) }}, } { diff --git a/go/internal/control/ev_fuse_budget_test.go b/go/internal/control/ev_fuse_budget_test.go index 3c7e56ac4..997bdd838 100644 --- a/go/internal/control/ev_fuse_budget_test.go +++ b/go/internal/control/ev_fuse_budget_test.go @@ -704,10 +704,11 @@ func evMeterBudgetFixture(t *testing.T, gridW float64, phaseA []float64, offerW } // A 3p car offered 15 A beside 11 A of house load on L1: 26 A on the 25 A -// working limit (no safety margin). With no earlier reading the fuse heat -// starts at 26 A, so one amp on three phases comes off the offer at once. +// working limit, long enough to spend the heat budget, so one amp on three +// phases comes off the offer. func TestEVHardLimitTrimsPhaseOverageFromOffer(t *testing.T) { store, state, points, configs := evMeterBudgetFixture(t, 8280, []float64{26, 20, 20}, 10350, "3p") + lastingFuseHeat(store, state) got, known := EVFuseBudget(store, state, points, configs, 17250) if !known || math.Abs(got-9660) > .01 { t.Fatalf("cap %.1f W, want 10,350 - 1 A x 690 = 9,660 W", got) diff --git a/go/internal/control/ev_fuse_cut_netting_test.go b/go/internal/control/ev_fuse_cut_netting_test.go index fdc60ce6a..8cbab6d6e 100644 --- a/go/internal/control/ev_fuse_cut_netting_test.go +++ b/go/internal/control/ev_fuse_cut_netting_test.go @@ -344,9 +344,9 @@ func TestEVFusePhaseChangeCoversNothing(t *testing.T) { } // After a restart, the offer Core finds in force is one it did not send in -// this run. A cut takes it as shown: the first reading over the limit trims -// the overage from it, and while that cut travels to a slow charger no -// further reading counts the found offer as a raise on its way. +// this run. A cut takes it as shown: the first reading that spends the heat +// trims the overage from it, and while that cut travels to a slow charger +// no further reading counts the found offer as a raise on its way. func TestEVFuseRestartTakesFoundOfferAsShown(t *testing.T) { s := fuseHeatSite(t) s.chargerDelay, s.chargerMaxAgeS = time.Minute, 180 @@ -354,8 +354,8 @@ func TestEVFuseRestartTakesFoundOfferAsShown(t *testing.T) { s.run(30 * time.Second) s.boot() bootAt := s.now - if took, offer := s.firstCut(5 * time.Second); took != 0 || offer != 8970 { - t.Fatalf("restart at 26 A cut after %v to %.0f W, want the first reading to 8,970 W", took, offer) + if took, offer := s.firstCut(5 * time.Second); took != time.Second || offer != 8970 { + t.Fatalf("restart at 26 A cut after %v to %.0f W, want the second reading to 8,970 W", took, offer) } s.run(50 * time.Second) if cuts := offerWatts(s.cutsSince(bootAt)); len(cuts) != 1 { diff --git a/go/internal/control/fuse_heat.go b/go/internal/control/fuse_heat.go index 32d646946..0ac83b55e 100644 --- a/go/internal/control/fuse_heat.go +++ b/go/internal/control/fuse_heat.go @@ -28,9 +28,18 @@ const fuseInstantFactor = 1.2 // // A reading stands for the time until the next one. A meter gap counts at // the last current seen: the reading after it neither fills the gap with its -// own current nor erases an overload from before it. A Core restart starts -// the model at the first reading's current, with no memory of the minutes -// before. +// own current nor erases an overload from before it. +// +// A Core restart loses the model, but not the fuse's heat: a fuse cools over +// minutes and a restart takes seconds. The model starts at the first +// reading's current, at most the working limit (start). Starting cold would +// grant a full fresh budget to an overload already under way: from cold, +// 1.15 × In takes about 85 s to reach In. Starting at the line, the first +// reading over it spends the budget at once, and a sustained overload is cut +// on the next reading. One reading cannot show that an overage has lasted, +// so it is not charged as if it had; its current still counts for the time +// until the next reading, and the instant cut above fuseInstantFactor +// applies to it as to any other. type fuseHeat struct { set bool at time.Time // the last reading @@ -38,6 +47,15 @@ type fuseHeat struct { sq [3]float64 // filtered I² per phase up to at, A² } +// start seeds the model from the first reading after Core starts: each +// phase at its current, at most lineA. +func (h *fuseHeat) start(at time.Time, amps []float64, lineA float64) { + h.step(at, amps) + for i, sq := range h.sq { + h.sq[i] = math.Min(sq, lineA*lineA) + } +} + // step folds the time since the last reading into the model at that // reading's currents, then holds the new ones. func (h *fuseHeat) step(at time.Time, amps []float64) { @@ -118,9 +136,15 @@ func (s *State) stepFuseHeat(meter *telemetry.DerReading, totalW float64) { if s.fuseHeat.set && !meter.UpdatedAt.After(s.fuseHeat.at) { return } - if amps, _, valid := phaseCurrentsA(meter, s, totalW); valid { - s.fuseHeat.step(meter.UpdatedAt, amps) + amps, _, valid := phaseCurrentsA(meter, s, totalW) + if !valid { + return + } + if !s.fuseHeat.set { + s.fuseHeat.start(meter.UpdatedAt, amps, s.fuseLineA()) // evMeterRoom's limitA + return } + s.fuseHeat.step(meter.UpdatedAt, amps) } // phaseCurrentsA reads the site's configured phases, import positive. A diff --git a/go/internal/control/fuse_heat_test.go b/go/internal/control/fuse_heat_test.go index b8ddd1038..0dc8ef8af 100644 --- a/go/internal/control/fuse_heat_test.go +++ b/go/internal/control/fuse_heat_test.go @@ -107,12 +107,62 @@ func TestFuseHeatCountsGapAtLastCurrentSeen(t *testing.T) { } } -// A Core restart has no memory: the first reading sets the heat. +// A Core restart has no memory: the first reading sets the heat, at most +// the working limit. One reading cannot show that an overage has lasted. func TestFuseHeatStartsAtFirstReadingAfterRestart(t *testing.T) { store, state := fuseHeatState() state.stepFuseHeat0(meterAt(store, time.Unix(1_800_000_000, 0), [3]float64{26, 3, 0})) - if state.fuseHeat.sq != [3]float64{676, 9, 0} { - t.Fatalf("first reading set %v", state.fuseHeat.sq) + if state.fuseHeat.sq != [3]float64{625, 9, 0} { + t.Fatalf("first reading set %v, want L1 at the 25 A working limit", state.fuseHeat.sq) + } + if state.fuseHeat.amps != [3]float64{26, 3, 0} { + t.Fatalf("first reading held %v, want its own currents", state.fuseHeat.amps) + } +} + +// firstStart runs until the first offer above 0 W and returns how long +// after the first reading of the run that was. +func (s *evMeterSite) firstStart(limit time.Duration) time.Duration { + s.t.Helper() + first := s.now.Add(time.Second) + for s.now.Sub(first) < limit { + s.step() + if s.offer() > 0 { + return s.now.Sub(first) + } + } + return -1 +} + +// Seed 207539: Core starts while one reading shows 1.4 × In on L1, then the +// house is back at 10 A per phase. A reading without room starts the idle +// car's dwell, as it always does; the heat that one second added is gone +// within a couple of readings. The car must not also wait the ~52 s a fuse +// already at 35 A would need to cool under the working limit. +func TestEVStartAfterRestartDoesNotWaitOutOneHighReading(t *testing.T) { + s := newEVMeterSite(t, 0, 6900) + s.addHouseA(.4*25+25-10, 0, 0) // 35 A on L1 + s.step() // Core's first reading + s.addHouseA(-(.4*25 + 25 - 10), 0, 0) + got := s.firstStart(3 * time.Minute) + if got < 0 || got > evRestartDwell+3*time.Second { + t.Fatalf("after one 35 A reading the car started after %v, want the %v dwell and at most 3 s of heat", got, evRestartDwell) + } +} + +// A sustained 1.15 × In on L1 right after a restart gets no fresh budget: +// the heat starts at the working limit and the second reading cuts. A cold +// model would let it run for about 72 s before it reached the 24 A line. +func TestEVHardLimitSustainedOverageAfterRestartCuts(t *testing.T) { + if cold := secondsToSpend(0, 1.15*25, 24); cold < 60 { + t.Fatalf("setup: a cold model spends the budget after %d s", cold) + } + s := fuseHeatSite(t) + s.boot() + s.addHouseA(1.15*25-20, 0, 0) // 28.75 A, under the instant cut + took, offer := s.firstCut(2 * time.Minute) + if took < 0 || took > time.Second || offer >= 10350 { + t.Fatalf("1.15 × In after a restart cut after %v to %.0f W, want the second reading", took, offer) } } @@ -275,9 +325,10 @@ func TestEVHardLimitHeatBudgetAcrossMeterGap(t *testing.T) { } } -// After a Core restart the heat starts at the first reading. Restarting -// 30 s into 26 A on L1 counts the fuse as already at 26 A and cuts at once, -// which is conservative only because that reading is high. +// After a Core restart the heat starts at the first reading, at most the +// working limit. Restarting 30 s into 26 A on L1, with about 31 s of budget +// left, counts the fuse as at the line: the second reading spends the +// budget and cuts. func TestEVHardLimitRestartStartsHeatAtFirstReading(t *testing.T) { s := fuseHeatSite(t) s.addHouseA(6, 0, 0) @@ -287,8 +338,8 @@ func TestEVHardLimitRestartStartsHeatAtFirstReading(t *testing.T) { } s.boot() took, offer := s.firstCut(5 * time.Second) - if took != 0 || offer != 8970 { - t.Fatalf("restart at 26 A cut after %v to %.0f W, want the first reading to 8,970 W", took, offer) + if took != time.Second || offer != 8970 { + t.Fatalf("restart at 26 A cut after %v to %.0f W, want the second reading to 8,970 W", took, offer) } } @@ -567,6 +618,20 @@ func TestExportingPhaseLevelIsItsReading(t *testing.T) { } } +// lastingFuseHeat sets the heat model as if the site meter's last reading +// had held for an hour: the case under test has lasted. +func lastingFuseHeat(store *telemetry.Store, state *State) { + meter := store.Get(state.SiteMeterDriver, telemetry.DerMeter) + amps, _, valid := phaseCurrentsA(meter, state, meter.RawW) + if !valid { + panic("lastingFuseHeat: no usable phase currents") + } + state.fuseHeat = fuseHeat{set: true, at: meter.UpdatedAt.Add(-time.Hour)} + for i, a := range amps { + state.fuseHeat.amps[i], state.fuseHeat.sq[i] = a, a*a + } +} + // stepFuseHeat0 folds a reading with phase currents but no phase power, so // its total does not matter. func (s *State) stepFuseHeat0(meter *telemetry.DerReading) { s.stepFuseHeat(meter, 0) }