Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/a6-one-ev-offer-log.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 5 additions & 11 deletions go/internal/control/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
8 changes: 2 additions & 6 deletions go/internal/control/ev_battery_headroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion go/internal/control/ev_battery_headroom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go/internal/control/ev_battery_priority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}
Expand Down
42 changes: 35 additions & 7 deletions go/internal/control/ev_fuse_budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
86 changes: 86 additions & 0 deletions go/internal/control/ev_slow_charger_battery_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
5 changes: 4 additions & 1 deletion go/internal/control/sitesim_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,17 @@ 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
}

// 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}
Expand Down
3 changes: 3 additions & 0 deletions go/internal/control/sitesim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading