From 261d1db1e68908d9ebf43489aad06e88f34ffcfb Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Tue, 27 Oct 2026 14:30:53 +0100 Subject: [PATCH] fix(loadpoint): mark a one-phase car only from an order rise The steady check had no baseline. A 3 kW load on L1 and solar on L2/L3 read [17, 4.5, 4.5] A next to a three-phase car at 6 A, which it took for a one-phase car. The order then tripled from 4140 W to 11040 W and swung back on the next rise test. Delete the steady check and keep the rise test, which compares against the meter before the rise. Co-Authored-By: Claude Opus 5.5 --- .changeset/d5-one-phase-drop-steady-check.md | 5 + go/internal/loadpoint/one_phase_car.go | 50 +------- go/internal/loadpoint/one_phase_car_test.go | 34 ++++-- .../loadpoint/one_phase_house_load_test.go | 115 ++++++++++++++++++ 4 files changed, 147 insertions(+), 57 deletions(-) create mode 100644 .changeset/d5-one-phase-drop-steady-check.md create mode 100644 go/internal/loadpoint/one_phase_house_load_test.go diff --git a/.changeset/d5-one-phase-drop-steady-check.md b/.changeset/d5-one-phase-drop-steady-check.md new file mode 100644 index 000000000..9b4bc4473 --- /dev/null +++ b/.changeset/d5-one-phase-drop-steady-check.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +Stop a heavy load on one phase from marking a three-phase car as one-phase. FTW now marks a one-phase car only when its own order rises and one phase follows. A three-phase car no longer gets triple its planned order or swings between 6 A and 16 A. After a restart, a one-phase car is marked again at its next order rise. diff --git a/go/internal/loadpoint/one_phase_car.go b/go/internal/loadpoint/one_phase_car.go index ca2c1d816..031bbeed3 100644 --- a/go/internal/loadpoint/one_phase_car.go +++ b/go/internal/loadpoint/one_phase_car.go @@ -19,10 +19,9 @@ import ( // A house load does not follow FTW's orders, and the charger's own reading // must have changed by what the phases show, so a kettle cannot mark a car // and a heat pump cannot clear one. A two-phase car moves two phases and is -// never marked. With no reading from before the car drew, as after a Core -// restart, the charger's own draw is the reference: one phase carrying all -// of it and the other two less than a third, which no three- or two-phase -// car can show. +// never marked. Without a rise there is no baseline: a house load on one +// phase and solar on the other two can look like a one-phase car, so after +// a Core restart the car stays unmarked until the next rise. const ( onePhaseStepA = 2.0 // the smallest order rise that is a test @@ -67,8 +66,6 @@ type carPhases struct { lastDrawA float64 seen bool test *phaseTest - steady int // the one phase the charger's whole draw shows on, 0 none - since time.Time phase int } @@ -113,28 +110,6 @@ func phaseVerdict(amps, baseline [3]float64, riseA, drawA float64) int { return 0 } -// steadyVerdict is the check with no baseline: the phase (1-3) that carries -// all of the charger's draw while the other two carry less than a third of -// it, or 0. drawA is the charger's draw over one phase's voltage. -func steadyVerdict(amps [3]float64, drawA float64) int { - if !finite(drawA) || drawA < 6 { - return 0 - } - phase, quiet := 0, 0 - for i, a := range amps { - switch { - case a >= drawA-1: - phase = i + 1 - case a < drawA/3-1: - quiet++ - } - } - if quiet == 2 { - return phase - } - return 0 -} - // SetSitePhases wires the site meter's phase currents for the one-phase car // check. nil turns the check off. func (c *Controller) SetSitePhases(f func() (SitePhases, bool)) { @@ -187,7 +162,7 @@ func (m *Manager) observePhases(id string, meter SitePhases, drawA, voltage floa previous, previousDrawA, seen := s.last, s.lastDrawA, s.seen s.last, s.lastDrawA, s.seen = meter.A, drawA, true if !lp.commandedKnown || lp.commandedPhases != 3 || !finite(lp.commandedW) { - s.test, s.rising, s.steady = nil, false, 0 + s.test, s.rising = nil, false return } orderA := math.Max(0, lp.commandedW) / (3 * voltage) @@ -204,12 +179,11 @@ func (m *Manager) observePhases(id string, meter SitePhases, drawA, voltage floa s.rising, s.before, s.beforeDrawA = true, previous, previousDrawA } if !s.rising || orderA-s.orderA < onePhaseStepA { - m.observeSteadyPhase(s, meter.A, drawA, now) return } s.test = &phaseTest{at: now, until: now.Add(settle + onePhaseHold), baseline: s.before, baseDrawA: s.beforeDrawA, fromA: s.orderA} - s.orderA, s.rising, s.steady = orderA, false, 0 + s.orderA, s.rising = orderA, false } t := s.test s.orderA = max(s.orderA, orderA) @@ -226,20 +200,6 @@ func (m *Manager) observePhases(id string, meter SitePhases, drawA, voltage floa s.test = nil } -// observeSteadyPhase marks an unmarked car whose charger's whole draw has -// shown on one phase alone for onePhaseHold. Caller holds m.mu. -func (m *Manager) observeSteadyPhase(s *carPhases, amps [3]float64, drawA float64, now time.Time) { - if s.phase != 0 { - return - } - if v := steadyVerdict(amps, drawA); v != s.steady { - s.steady, s.since = v, now - } - if s.steady != 0 && now.Sub(s.since) >= onePhaseHold { - s.phase = s.steady - } -} - // chargerPhaseMaxA is the most current a charger offers on each phase of a // three-phase order: its maximum over three phases, within the site fuse. func chargerPhaseMaxA(cfg Config, site SiteFuse) float64 { diff --git a/go/internal/loadpoint/one_phase_car_test.go b/go/internal/loadpoint/one_phase_car_test.go index 35a268884..ae002be08 100644 --- a/go/internal/loadpoint/one_phase_car_test.go +++ b/go/internal/loadpoint/one_phase_car_test.go @@ -219,28 +219,38 @@ func TestHeatPumpDoesNotClearAOnePhaseCar(t *testing.T) { } } -// Review M3: after a Core restart the order is out and the car draws before -// the first reading. With no baseline, a one-phase car still shows its whole -// draw on one phase and less than a third on the others; a three-phase car -// never does. -func TestOnePhaseCarMarkedAfterARestart(t *testing.T) { +// After a Core restart the order is out and the car draws before the first +// reading, so there is no baseline. No car is marked from one reading: a +// house load on L1 and solar on L2/L3 look the same as a one-phase car. The +// next rise marks a one-phase car. +func TestOnePhaseCarMarkedOnTheRiseAfterARestart(t *testing.T) { + still := func(a [3]float64) func(time.Duration) [3]float64 { return func(time.Duration) [3]float64 { return a } } + draw := func(w float64) func(time.Duration) float64 { return func(time.Duration) float64 { return w } } for _, c := range []struct { name string amps [3]float64 drawW float64 - want int }{ - {"one-phase car at 16 A on L1, house 3/1/1 A", [3]float64{19, 1, 1}, 3680, 1}, - {"three-phase car at 16 A, house 3/1/1 A", [3]float64{19, 17, 17}, 11040, 0}, - {"two-phase car at 16 A, house 3/1/1 A", [3]float64{19, 17, 1}, 7360, 0}, + {"one-phase car at 16 A on L1, house 3/1/1 A", [3]float64{19, 1, 1}, 3680}, + {"three-phase car at 16 A, house 3/1/1 A", [3]float64{19, 17, 17}, 11040}, + {"two-phase car at 16 A, house 3/1/1 A", [3]float64{19, 17, 1}, 7360}, } { r := newPhaseRig() r.order(11040) - r.run(0, 60*time.Second, func(time.Duration) [3]float64 { return c.amps }, func(time.Duration) float64 { return c.drawW }) - if got := r.carPhases(); got != c.want { - t.Errorf("%s: car phases %d, want %d", c.name, got, c.want) + r.run(0, 60*time.Second, still(c.amps), draw(c.drawW)) + if got := r.carPhases(); got != 0 { + t.Errorf("%s: marked %d-phase with no rise", c.name, got) } } + r := newPhaseRig() + r.order(6 * 690) + r.run(0, 60*time.Second, still([3]float64{9, 1, 1}), draw(6*230)) + r.order(8 * 690) + r.run(65*time.Second, 70*time.Second, still([3]float64{9, 1, 1}), draw(6*230)) + r.run(75*time.Second, 200*time.Second, still([3]float64{11, 1, 1}), draw(8*230)) + if got := r.carPhases(); got != 1 { + t.Fatalf("a 2 A rise on L1 alone after a restart: car phases %d, want 1", got) + } } // Device-simulator rig s14 on 2e847104: a three-phase charger with the diff --git a/go/internal/loadpoint/one_phase_house_load_test.go b/go/internal/loadpoint/one_phase_house_load_test.go new file mode 100644 index 000000000..1f87be658 --- /dev/null +++ b/go/internal/loadpoint/one_phase_house_load_test.go @@ -0,0 +1,115 @@ +package loadpoint + +import ( + "context" + "encoding/json" + "testing" + "time" +) + +// A three-phase car at 6 A (4140 W, 18 A over one phase's voltage), an oven +// of 3 kW on L1 and a little PV on every phase: the meter reads +// [17, 4.5, 4.5] A. No three-phase car should be marked one-phase. +func TestHouseLoadDoesNotMarkAThreePhaseCar(t *testing.T) { + r := newPhaseRig() + r.order(4140) + r.run(0, 60*time.Second, func(time.Duration) [3]float64 { return [3]float64{17, 4.5, 4.5} }, + func(time.Duration) float64 { return 4140 }) + if got := r.carPhases(); got != 0 { + t.Errorf("three-phase car at 6 A marked as %d-phase from meter [17 4.5 4.5] A", got) + } +} + +// End to end through Controller.Tick: a planned 4140 W three-phase slot, +// the same meter and charger readings every 5 s. The order must stay near +// 4140 W. +func TestHouseLoadKeepsAPlannedThreePhaseOrder(t *testing.T) { + cfg := Config{ID: "garage", DriverName: "easee", MinChargeW: 4140, MaxChargeW: 11040, PhaseMode: "3p"} + start := time.Now() + slot := start.Add(-time.Second) + dir := Directive{SlotStart: slot, SlotEnd: slot.Add(time.Hour), LoadpointEnergyWh: map[string]float64{cfg.ID: 4140}} + var orders []float64 + send := func(_ context.Context, _ string, p []byte) error { + var payload map[string]any + _ = json.Unmarshal(p, &payload) + w, _ := payload["power_w"].(float64) + orders = append(orders, w) + return nil + } + var now time.Time + c := NewController(NewManager(), PlanFunc(func(time.Time) (Directive, bool) { return dir, true }), + TelemetryFunc(func(string) (EVSample, bool) { + return EVSample{Connected: true, RequestActive: true, PowerW: 4140, PowerAt: now, PowerMaxAge: 10 * time.Second}, true + }), send) + c.manager.Load([]Config{cfg}) + c.SetSiteFuse(SiteFuse{MaxAmps: 25, Voltage: 230, PhaseCnt: 3}) + c.SetSitePhases(func() (SitePhases, bool) { return SitePhases{A: [3]float64{17, 4.5, 4.5}, At: now}, true }) + c.manager.Observe(cfg.ID, true, 0, 0, true) + c.manager.SetSchedule(cfg.ID, Schedule{SoC: .8}) + for d := time.Duration(0); d <= 300*time.Second; d += 5 * time.Second { + now = start.Add(d) + c.Tick(context.Background(), now) + } + for _, w := range orders { + if w > 6000 { + t.Fatalf("a planned 4140 W three-phase slot went out as %.0f W", w) + } + } +} + +// The same site with a closed loop: a real three-phase car takes the order +// on all three phases, so the meter is the house [12, -1.5, -1.5] A plus the +// order's current on each phase. The order must not swing between 6 A and +// 16 A. +func TestHouseLoadDoesNotSwingAThreePhaseOrder(t *testing.T) { + cfg := Config{ID: "garage", DriverName: "easee", MinChargeW: 4140, MaxChargeW: 11040, PhaseMode: "3p"} + start := time.Now() + slot := start.Add(-time.Second) + dir := Directive{SlotStart: slot, SlotEnd: slot.Add(time.Hour), LoadpointEnergyWh: map[string]float64{cfg.ID: 4140}} + var orders []float64 + drawW := 0.0 + send := func(_ context.Context, _ string, p []byte) error { + var payload map[string]any + _ = json.Unmarshal(p, &payload) + w, _ := payload["power_w"].(float64) + orders = append(orders, w) + return nil + } + var now time.Time + c := NewController(NewManager(), PlanFunc(func(time.Time) (Directive, bool) { return dir, true }), + TelemetryFunc(func(string) (EVSample, bool) { + return EVSample{Connected: true, RequestActive: true, PowerW: drawW, PowerAt: now, PowerMaxAge: 10 * time.Second}, true + }), send) + c.manager.Load([]Config{cfg}) + c.SetSiteFuse(SiteFuse{MaxAmps: 35, Voltage: 230, PhaseCnt: 3}) + c.SetSitePhases(func() (SitePhases, bool) { + a := drawW / 690 + return SitePhases{A: [3]float64{12 + a, -1.5 + a, -1.5 + a}, At: now}, true + }) + c.manager.Observe(cfg.ID, true, 0, 0, true) + c.manager.SetSchedule(cfg.ID, Schedule{SoC: .8}) + marks, prev := 0, 0 + for d := time.Duration(0); d <= 15*time.Minute; d += 5 * time.Second { + now = start.Add(d) + c.Tick(context.Background(), now) + if len(orders) > 0 { + drawW = orders[len(orders)-1] // the car follows the order by the next tick + } + st, _ := c.manager.State(cfg.ID) + if st.CarPhases != prev { + if st.CarPhases == 1 { + marks++ + } + prev = st.CarPhases + } + } + hi := 0 + for _, w := range orders { + if w > 6000 { + hi++ + } + } + if marks > 0 || hi > 0 { + t.Fatalf("three-phase car marked one-phase %d times; %d of %d orders above 6 kW on a 4140 W plan", marks, hi, len(orders)) + } +}