diff --git a/.changeset/boost-covers-boosted-car.md b/.changeset/boost-covers-boosted-car.md new file mode 100644 index 000000000..cea534ce3 --- /dev/null +++ b/.changeset/boost-covers-boosted-car.md @@ -0,0 +1,11 @@ +--- +"ftw": patch +--- + +A battery boost now makes the home battery cover the car. In the planner +modes a boost was only a permission: a plan that kept the battery for later, +or charged it, left the boosted car on the grid. While the charger reports +the car drawing and a battery is above the boost's reserve, the battery now +discharges to cover the house and that car, down to zero import, and never +exports for car power the meter no longer shows. A planned battery charge +from the grid waits until the boost ends. diff --git a/.changeset/boost-follows-car-down.md b/.changeset/boost-follows-car-down.md new file mode 100644 index 000000000..c4f25366c --- /dev/null +++ b/.changeset/boost-follows-car-down.md @@ -0,0 +1,8 @@ +--- +"ftw": patch +--- + +A battery boost follows the car down as fast as it follows it up. When the +boosted car slowed or stopped, the battery eased down by about 1 kW every +five seconds and sold the rest to the grid for up to a minute. In the +planner modes it now drops to the house load on the next tick. diff --git a/go/internal/control/battery_boost_test.go b/go/internal/control/battery_boost_test.go index 6e7d8d6b2..7e9edfe33 100644 --- a/go/internal/control/battery_boost_test.go +++ b/go/internal/control/battery_boost_test.go @@ -1,6 +1,7 @@ package control import ( + "math" "testing" "time" @@ -109,3 +110,98 @@ func TestFuseEmergencyRemainsSuperiorToBatteryBoostReserve(t *testing.T) { t.Fatalf("fuse emergency did not override lease reserve: %+v", targets) } } + +// A boost covers the house and the boosted car even when the arbitrage plan +// keeps the battery for later (idle slot) or charges it (charge slot). The +// battery stops at grid zero: car draw the charger still reports but the +// meter no longer shows is never exported. +func TestBatteryBoostCoversCarWhatTheSlotSays(t *testing.T) { + slots := map[string]SlotDirective{ + "idle": {BatteryEnergyWh: 0, HasPlannedGridW: true, PlannedGridW: 500}, + "charge": {BatteryEnergyWh: 500, HasPlannedGridW: true, PlannedGridW: 0}, + } + for name, slot := range slots { + for _, tc := range []struct { + gridW, boostW, want float64 + }{ + {8500, 0, 0}, // no boost: the battery does not cover the car + {8500, 8000, -8500}, // car 8 kW + house 0.5 kW from the battery + {500, 8000, -500}, // the car stopped; Easee still reports it + } { + store := seedStore(tc.gridW, []struct { + name string + currentW, soc float64 + }{{"battery", 0, 0.6}}) + store.Update("easee", telemetry.DerEV, 8000, nil, nil) + store.DriverHealthMut("easee").RecordSuccess() + now := time.Now() + state := NewState(0, 0, "ferroamp") + state.Mode = ModePlannerArbitrage + state.UseEnergyDispatch = true + state.SlewRateW = 100000 + state.MinDispatchIntervalS = 0 + state.DriverLimits = map[string]PowerLimits{"battery": {MaxChargeW: 10000, MaxDischargeW: 10000}} + state.BatteryBoostEVChargingW = tc.boostW + dir := slot + dir.SlotStart, dir.SlotEnd = now, now.Add(15*time.Minute) + state.SlotDirective = func(time.Time) (SlotDirective, bool) { return dir, true } + targets := ComputeDispatch(store, state, map[string]float64{"battery": 20000}, 50000) + var got float64 + for _, tg := range targets { + got += tg.TargetW + } + // Without a boost a slot may still cover the 0.5 kW house, never the car. + if (tc.want == 0 && got < -501) || (tc.want < 0 && math.Abs(got-tc.want) > 1) { + t.Errorf("%s slot, grid %.0f, boost %.0f: target %.0f, want %.0f (%+v)", + name, tc.gridW, tc.boostW, got, tc.want, targets) + } + } + } +} + +// The battery follows a boosted car both ways within a tick: down when the +// car slows, and straight to the house (or the plan's idle) when it stops, +// instead of exporting while a controller eases it down. +func TestBatteryBoostFollowsTheCarBothWays(t *testing.T) { + now := time.Now() + state := NewState(0, 0, "ferroamp") + state.Mode = ModePlannerArbitrage + state.UseEnergyDispatch = true + state.SlewRateW = 100000 + state.MinDispatchIntervalS = 0 + state.DriverLimits = map[string]PowerLimits{"battery": {MaxChargeW: 10000, MaxDischargeW: 10000}} + state.clock = func() time.Time { return now } + dir := SlotDirective{SlotStart: now, SlotEnd: now.Add(15 * time.Minute), HasPlannedGridW: true, PlannedGridW: 500} + state.SlotDirective = func(time.Time) (SlotDirective, bool) { return dir, true } + + tick := func(batteryW, gridW, carW, boostW float64) float64 { + t.Helper() + store := seedStore(gridW, []struct { + name string + currentW, soc float64 + }{{"battery", batteryW, 0.6}}) + store.Update("easee", telemetry.DerEV, carW, nil, nil) + store.DriverHealthMut("easee").RecordSuccess() + state.BatteryBoostEVChargingW = boostW + var sum float64 + for _, tg := range ComputeDispatch(store, state, map[string]float64{"battery": 20000}, 50000) { + sum += tg.TargetW + } + now = now.Add(5 * time.Second) + return sum + } + + // House 0.5 kW, car 8 kW: the battery takes both. + if got := tick(0, 8500, 8000, 8000); math.Abs(got+8500) > 1 { + t.Fatalf("car 8 kW: target %.0f, want -8500", got) + } + // The car slows to 5 kW: the meter shows 3 kW export; back to -5.5 kW now. + if got := tick(-8500, -3000, 5000, 5000); math.Abs(got+5500) > 1 { + t.Fatalf("car 5 kW: target %.0f, want -5500", got) + } + // The car stops. The charger reading is 0 while the loadpoint still says + // 5 kW. No export: grid zero for the house, or the idle plan's 0 W. + if got := tick(-5500, -5000, 0, 5000); got < -501 || got > 0 { + t.Fatalf("car stopped: target %.0f, want between -500 and 0", got) + } +} diff --git a/go/internal/control/dispatch.go b/go/internal/control/dispatch.go index 88843fda2..c68f8eda7 100644 --- a/go/internal/control/dispatch.go +++ b/go/internal/control/dispatch.go @@ -164,6 +164,10 @@ const MaxCommandW = 5000 // elsewhere in this package for sign decisions. const evActiveThresholdW = 100.0 +// boostCoverHold is how long the battery stays on grid zero after a boosted +// car stops drawing. +const boostCoverHold = 30 * time.Second + // holdoffToleranceS is how much earlier than MinDispatchIntervalS a // dispatch may run. It absorbs jitter in the work before each tick. const holdoffToleranceS = 0.25 @@ -404,6 +408,9 @@ type State struct { // main.go refreshes both from loadpoint.Controller every dispatch tick. BatteryBoostEVChargingW float64 BatteryBoostReserveSoC float64 + // boostCoverUntil keeps the battery on grid zero for boostCoverHold + // after a boosted car stops, so it drops to the house at once. + boostCoverUntil time.Time // EVCommandedW is the frozen sum of final charger offers from this tick. // It reserves site capacity before optional battery charging, even before // an EV ramps up and in modes without an accepted optimizer plan. @@ -1635,7 +1642,20 @@ func ComputeDispatch( // battery discharging just because slew anchored from a negative live // reading. Plain self_consumption remains the classic grid-zero mode: // it may discharge to cover local load and charge from live surplus. - planNonDischargeIntent := !manualHoldActive && planHasNonDischargeIntent(state) + // A battery boost the charger reports drawing makes the battery cover + // the house and that car, whatever the plan's slot says (see below the + // path switch). The plan's no-discharge rails step aside meanwhile. + // A battery at the boost's reserve has nothing to give: the plan goes on. + // The live EV reading ends it a tick before the loadpoint's copy does. + boostCover := false + if !manualHoldActive && math.Min(state.BatteryBoostEVChargingW, state.EVChargingW) > evActiveThresholdW { + for _, b := range onlineBats { + if !b.dischargeBlocked && !b.socMissing && b.soc > state.BatteryBoostReserveSoC { + boostCover = true + } + } + } + planNonDischargeIntent := !manualHoldActive && !boostCover && planHasNonDischargeIntent(state) // planner_self gates intentionally NOT in noSelfDischarge — the // operator's "never import" floor takes precedence over the // planner's slot preference, so discharge to cover load is always @@ -2208,6 +2228,30 @@ func ComputeDispatch( } } + // Battery boost: discharge at least down to grid zero for the house and + // the boosted car. gridW leaves out car draw the boost does not cover, + // so the battery never exports for it. A plan that discharges further + // keeps its target; reserve, limits and the fuse apply downstream. + // + // The plan's reactive controller moves from the battery's live output, + // so after the car slows or stops it eased down from the boost's + // discharge by about 1 kW a tick, exporting the rest. During a boost and + // for boostCoverHold after the car stops, a planner mode's reactive path + // goes straight to its grid target instead. The energy path sets an + // absolute target and carries no boost discharge over. + if boostCover { + state.boostCoverUntil = tickAt.Add(boostCoverHold) + } + if !manualHoldActive && tickAt.Before(state.boostCoverUntil) { + target := currentTotal + totalCorrection + if boostTotal := currentTotal - gridW; boostCover && target > boostTotal { + totalCorrection = boostTotal - currentTotal + } else if gridTotal := currentTotal - (gridW - state.GridTargetW); !useEnergyPath && + state.Mode.IsPlannerMode() && target < gridTotal { + totalCorrection = gridTotal - currentTotal + } + } + // Share the site ceiling between EV and battery charge. An accepted // EV plan reserves its demand before optional battery charge, including // while the car ramps up. Unscheduled EV draw retains proportional sharing. @@ -2394,6 +2438,7 @@ func ComputeDispatch( // ---- Fuse guard (bidirectional, #145) ---- return applyDispatchSafetyPipeline(raw, store, state, driverCapacities, fuseMaxW, dispatchSafetyOptions{ manualHoldActive: manualHoldActive, + boostCover: boostCover, noSelfDischarge: noSelfDischarge, noSelfCharge: noSelfCharge, chargeBlocked: chargeBlockedDrivers(onlineBats), @@ -2563,6 +2608,7 @@ func floorBelowOwnerFloorDischarge(targets []DispatchTarget, blocked map[string] type dispatchSafetyOptions struct { manualHoldActive bool + boostCover bool // a battery boost covers a car the charger reports drawing noSelfDischarge bool noSelfCharge bool chargeBlocked map[string]bool @@ -2600,7 +2646,7 @@ func applyDispatchSafetyPipeline( // // Only applied in the energy planner modes. Manual modes have no plan to // disagree with, and manual holds intentionally override planner intent. - if !opts.manualHoldActive { + if !opts.manualHoldActive && !opts.boostCover { targets = applyPlanSignFloor(targets, state) } if opts.noSelfDischarge { diff --git a/go/internal/control/testdata/golden/seeded_ev_boost.json b/go/internal/control/testdata/golden/seeded_ev_boost.json index 89eadd288..92ed9e5cd 100644 --- a/go/internal/control/testdata/golden/seeded_ev_boost.json +++ b/go/internal/control/testdata/golden/seeded_ev_boost.json @@ -1,7 +1,7 @@ { "family": "seeded_ev_boost", "generated_by": "FTW_GOLDEN_DUMP=1 go test ./internal/control/ -run TestGoldenDump", - "ftw_commit": "303f86818a95f056f68e0b652db131a33074a421", + "ftw_commit": "b891f24ba3ce435dcf7bb0cac64b447e776a6b59", "record_count": 50, "records": [ { @@ -455,12 +455,12 @@ "per_driver_targets": [ { "driver": "ferroamp", - "target_w": 3704.0000041155554, + "target_w": 3704, "clamped": false }, { "driver": "sungrow", - "target_w": 3704.0000041155554, + "target_w": 3704, "clamped": false } ], @@ -483,9 +483,9 @@ }, "site_totals": { "battery_current_w": 5916, - "battery_target_sum_w": 7408.000008231111, + "battery_target_sum_w": 7408, "raw_grid_w": -3380, - "projected_grid_w": -1887.9999917688892 + "projected_grid_w": -1888 } }, { @@ -1695,7 +1695,7 @@ "per_driver_targets": [ { "driver": "ferroamp", - "target_w": -494.3225806451613, + "target_w": -494.32258064516134, "clamped": true }, { @@ -1726,7 +1726,7 @@ }, "site_totals": { "battery_current_w": 6936, - "battery_target_sum_w": -494.3225806451613, + "battery_target_sum_w": -494.32258064516134, "raw_grid_w": -2827, "projected_grid_w": -10257.322580645161 } @@ -2605,7 +2605,7 @@ "per_driver_targets": [ { "driver": "ferroamp", - "target_w": -4616.000004277494, + "target_w": -4616, "clamped": false } ], @@ -2628,9 +2628,9 @@ }, "site_totals": { "battery_current_w": -2895, - "battery_target_sum_w": -4616.000004277494, + "battery_target_sum_w": -4616, "raw_grid_w": -3933, - "projected_grid_w": -5654.000004277494 + "projected_grid_w": -5654 } }, { @@ -2895,7 +2895,7 @@ }, { "driver": "sungrow", - "target_w": -4955.17808723505, + "target_w": -4955.178082191781, "clamped": false } ], @@ -2920,9 +2920,9 @@ }, "site_totals": { "battery_current_w": 1004, - "battery_target_sum_w": -4955.17808723505, + "battery_target_sum_w": -4955.178082191781, "raw_grid_w": 6871, - "projected_grid_w": 911.82191276495 + "projected_grid_w": 911.821917808219 } }, { @@ -3346,7 +3346,7 @@ "per_driver_targets": [ { "driver": "ferroamp", - "target_w": 4320.0000043968, + "target_w": 4320, "clamped": false } ], @@ -3369,9 +3369,9 @@ }, "site_totals": { "battery_current_w": -1903, - "battery_target_sum_w": 4320.0000043968, + "battery_target_sum_w": 4320, "raw_grid_w": 1130, - "projected_grid_w": 7353.0000043968 + "projected_grid_w": 7353 } }, { @@ -3644,7 +3644,7 @@ }, { "driver": "sungrow", - "target_w": 3883.938938307094, + "target_w": 3883.9389373605704, "clamped": false } ], @@ -3669,9 +3669,9 @@ }, "site_totals": { "battery_current_w": -1659, - "battery_target_sum_w": 8883.938938307094, + "battery_target_sum_w": 8883.93893736057, "raw_grid_w": -325, - "projected_grid_w": 10217.938938307094 + "projected_grid_w": 10217.93893736057 } }, { @@ -4230,14 +4230,12 @@ "per_driver_targets": [ { "driver": "ferroamp", - "target_w": 5000, - "clamped": true + "target_w": -3157, + "clamped": false } ], "clamp_attributions": { - "clamped_drivers": [ - "ferroamp" - ], + "clamped_drivers": [], "all_targets_zero_clamped": false, "plan_sign_intent": 1, "plan_sign_floor_zeroed": false, @@ -4255,9 +4253,9 @@ }, "site_totals": { "battery_current_w": 1941, - "battery_target_sum_w": 5000, + "battery_target_sum_w": -3157, "raw_grid_w": 5098, - "projected_grid_w": 8157 + "projected_grid_w": 0 } }, {