Skip to content
Open
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
14 changes: 14 additions & 0 deletions .changeset/declined-car-retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"ftw": minor
---

A car that stopped asking for charge is no longer left out for the rest of
the plug-in. While it stays plugged in with a goal to meet, FTW plans it
again an hour after it declined, or when the next ready time starts. A car
that is full or at its own limit declines once more and is left out again
after 90 seconds, so FTW offers it current at most once an hour. FTW does
not retry when the goal is met, when the car reports it is done or at its
limit, after a one-time ready time has passed, or on a charger it only
observes. Unplugging, a higher goal and Charge now still act at once, and
the plan now follows a refusal and its end right away instead of at the
next scheduled replan.
3 changes: 3 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ plug sessions. Completion requires a live report from the session car's own
source, made after the observed connection, and no measured charging. A car
the box has not named cannot finish the goal. A charger declining current is reported as a refusal, never
as an invented battery level or proof that the target was reached.
A car that declined stays out of the plan for an hour, or until the next
goal window, while it stays plugged in with a goal to meet. Then the plan
takes it in once more; a car that declines again costs one short offer.

### Cars

Expand Down
17 changes: 17 additions & 0 deletions go/cmd/ftw/ev_stop_replan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,20 @@ func evStopNeedsReplan(lp loadpoint.State, prevW float64) bool {
}
return !lp.CommandedKnown || lp.CommandedW >= evStopLow
}

// declineReplanReason names a replan when a plugged-in car's refusal latch
// moved since the last tick (prevDeclined): the plan leaves the car out once
// it declined, and takes it in again once the latch ends (a retry, Charge
// now, a higher goal, measured charging). A scheduled replan would leave the
// old plan offering a declining car, or holding back a car that may charge,
// for up to its whole interval.
func declineReplanReason(lp loadpoint.State, prevDeclined bool) string {
declined := lp.PluggedIn && lp.ChargingDeclined
switch {
case declined && !prevDeclined:
return "loadpoint_declined"
case prevDeclined && !declined && lp.PluggedIn:
return "loadpoint_decline_ended"
}
return ""
}
24 changes: 24 additions & 0 deletions go/cmd/ftw/ev_stop_replan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,27 @@ func TestEVStopReplanOnlyWhenTheCarStoppedItself(t *testing.T) {
}
}
}

// The plan follows the refusal latch at once: it leaves a car out as soon as
// the car has declined, and takes it in as soon as the latch ends while the
// car is still plugged in (srcfl/ftw#2080). An unplug replans on its own.
func TestDeclineEdgesReplan(t *testing.T) {
for _, tc := range []struct {
name string
prev, now, plugged bool
want string
}{
{"car declined", false, true, true, "loadpoint_declined"},
{"refusal ended", true, false, true, "loadpoint_decline_ended"},
{"unplugged", true, false, false, ""},
{"still declined", true, true, true, ""},
{"never declined", false, false, true, ""},
} {
t.Run(tc.name, func(t *testing.T) {
lp := loadpoint.State{PluggedIn: tc.plugged, ChargingDeclined: tc.now}
if got := declineReplanReason(lp, tc.prev); got != tc.want {
t.Fatalf("got %q want %q", got, tc.want)
}
})
}
}
15 changes: 15 additions & 0 deletions go/cmd/ftw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,14 @@ func main() {
health := tel.DriverHealth(name)
return health != nil && health.IsOnline()
})
// An observe-only or read-only charger is never offered current
// again after a refusal; an approved OCPP charger may be.
lpController.SetChargerControlAllowed(func(name string) bool {
if ocppSrv != nil && ocppSrv.Handler().IsOnline(name) && ocppSrv.Handler().IsApproved(name) {
return true
}
return reg.CanControl(name)
})
// Wire the site fuse so the per-phase EV clamp and the
// phase-split derivation can use the actual site voltage and
// breaker rating instead of hard-coding 230 V × 16 A.
Expand Down Expand Up @@ -2942,6 +2950,9 @@ func main() {
// Cooldown shares the fuse-replan cooldown to keep per-second flap
// from spamming the optimizer.
evDrawPrev := map[string]float64{}
// declinedPrev is each plugged-in car's refusal latch on the last tick;
// declineReplanReason replans when it moves.
declinedPrev := map[string]bool{}
var lastEVStopReplan time.Time
const evStopReplanCooldown = 60 * time.Second
var staleDefaults staleSiteDefaultTracker
Expand Down Expand Up @@ -3406,6 +3417,10 @@ func main() {
"power_unavailable", lp.PowerUnavailable)
}
evDrawPrev[lp.ID] = curr
if reason := declineReplanReason(lp, declinedPrev[lp.ID]); reason != "" {
mpcSvc.RequestReplan(reason)
}
declinedPrev[lp.ID] = lp.PluggedIn && lp.ChargingDeclined
}
}

Expand Down
38 changes: 38 additions & 0 deletions go/internal/loadpoint/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ type Controller struct {
vehicleRefreshTarget func(loadpointID string) (string, error)
vehicleChargeState func(loadpointID string) (VehicleChargeState, bool)
vehicleControlAllowed func(driver string) bool
// chargerControlAllowed is the charger driver's write authority. nil
// allows, as in narrow tests.
chargerControlAllowed func(driver string) bool

// peakRemainingSurplusW is the best PV-minus-load surplus left today.
// Below the 3Φ minimum, surplus_only locks 1Φ for the day. nil keeps 3Φ.
Expand Down Expand Up @@ -611,6 +614,40 @@ func (c *Controller) canControlVehicle(driver string) bool {
return c.vehicleControlAllowed == nil || c.vehicleControlAllowed(driver)
}

// SetChargerControlAllowed checks the charger driver's write authority. A
// charger Core only observes is never offered current again on its own.
func (c *Controller) SetChargerControlAllowed(f func(string) bool) {
if c != nil {
c.chargerControlAllowed = f
}
}

// retryDeclinedCar lets a car that declined current into the plan again
// when Manager.retryDeclinedCharging says the refusal is due for another
// offer. Charge now already retries at once, and a charger Core may not
// command, or a car whose own fresh reading is Complete or at its limit,
// is left alone. The plan then decides when the offer comes.
func (c *Controller) retryDeclinedCar(cfg Config, now time.Time) {
if st, ok := c.manager.State(cfg.ID); !ok || !st.ChargingDeclined {
return
}
if _, held := c.GetManualHold(cfg.ID, now); held {
return
}
if c.chargerControlAllowed != nil && !c.chargerControlAllowed(cfg.DriverName) {
return
}
carDone := false
if c.vehicleChargeState != nil {
if car, fresh := c.vehicleChargeState(cfg.ID); fresh {
carDone = car.State == "Complete" || car.Limit > 0 && car.SoC >= car.Limit
}
}
if c.manager.retryDeclinedCharging(cfg.ID, now, carDone) {
slog.Info("loadpoint: planning a car again that declined current", "lp", cfg.ID, "retry_interval", DeclineRetryInterval)
}
}

// SetVehicleRefreshTarget selects an authorized telemetry recipient from
// configuration. It must not require a fresh SoC or permit charging.
func (c *Controller) SetVehicleRefreshTarget(f func(string) (string, error)) {
Expand Down Expand Up @@ -1462,6 +1499,7 @@ func (c *Controller) tickOne(ctx context.Context, now time.Time, lpCfg Config, s
// but do not advance completion timers or perform wake side effects.
return
}
c.retryDeclinedCar(lpCfg, now)
// Wallbox just started delivering current: fire a wake at the
// bound vehicle so the next vehicle-driver poll comes back with
// fresh charging_state / charge_limit_pct / SoC. Gated by
Expand Down
Loading