EV-22: Solar and Manual never write over another writer; one rule of control
C7: the other-writer watch now runs in every step of a phase change (zero, select, settle) in both writers, with the change's own 0 A and every closing current it tried counted as own. While a difference or a hold stands, nothing non-zero is written over it: no settle close, no EV-28 restore, no select, no Manual undo or abandon. The 6 A raise check stays and runs first. D4 (Solar): the read-back phases are judged like the current, against the phases Solar itself chose (start state = first known read-back outside an own select window, or an own select seen taken), never the plan's adopted n. Same mismatch_since, hold and CHARGER_OTHER; lift only when current and phases both match, or a press. The window of an own select is SETTLE_S from the try's return; a take seen after it is another writer's. DA1: a press that lifts a difference inside a phase change ends the change at its 0 A (both writers); the raise check never holds over a value the press took. DA2: the not-writing path does not judge the current with the reading from before its own 0 A. test_ev22.py (T1-T49, review row T48 (stale)), test_ev22_fuzz.py (T38, N1-N10). test_ev20_writers T46 (a)(b)(c) edited as ruled (PM, v3). Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -150,11 +150,20 @@ class ManualWriter:
|
||||
self._late = None # (a, was, why, n) of a write not taken
|
||||
self._rec_a = None # EV-56: the raised value already logged
|
||||
self._close_k = 0 # EV-59: not-taken closes of the value owed
|
||||
self._ph_diff = False # EV-22: the phases differ from our own
|
||||
# (Solar only; Manual never sets it)
|
||||
|
||||
# ------------------------------------------------------------------ user
|
||||
def user_changed(self) -> None:
|
||||
"""The user changed the mode or a value: one more write is allowed,
|
||||
and another writer's hold is lifted."""
|
||||
and another writer's hold is lifted. EV-22 (DA1): a change that lifts
|
||||
a difference inside a phase change ends the change at its 0 A; the
|
||||
raise check never holds over the value the user's change took."""
|
||||
if self.seq is not None:
|
||||
if self._other_stands():
|
||||
self.seq = None
|
||||
else:
|
||||
self.seq["pressed"] = True # the raise check holds off
|
||||
self.boot = False
|
||||
self.rev_applied = None
|
||||
self.hold = self.other = False
|
||||
@@ -192,6 +201,9 @@ class ManualWriter:
|
||||
self._rec_a = None # EV-56 (G2): logged again
|
||||
cur = r.allowed_a if r.allowed_a is not None else r.setpoint_a
|
||||
if self.seq is not None:
|
||||
# EV-22 (C7): the watch runs in every step of a phase change; the
|
||||
# change's own values count as own (`_own_values`).
|
||||
self._watch(cur, now)
|
||||
if self.seq["stage"] in ("zero", "select") \
|
||||
and (mode != MANUAL or not writing):
|
||||
await self._undo(drv, now, tracker, clamp, shadow)
|
||||
@@ -275,26 +287,54 @@ class ManualWriter:
|
||||
self.fails = max(min(self._close_k, MAX_TRIES) - 1, 0)
|
||||
self._failed(now)
|
||||
|
||||
def _watch(self, cur, now) -> None:
|
||||
def _other_stands(self) -> bool:
|
||||
"""EV-22: another writer's value is held, or its count is running:
|
||||
nothing non-zero is written over it."""
|
||||
return bool(self.hold or self.mismatch_since is not None)
|
||||
|
||||
def _own_values(self) -> list:
|
||||
"""The currents that are ours: what we wrote, tried or saw before a
|
||||
try, and (EV-22, C7) every value of the phase change under way: its
|
||||
0 A and each closing current it tried (also after a user change
|
||||
cleared `attempted_a`)."""
|
||||
vals = [x for x in (self.last_a, self.attempted_a, self.prior_a)
|
||||
if x is not None]
|
||||
if self.seq is not None:
|
||||
vals += self.seq.get("own", ())
|
||||
return vals
|
||||
|
||||
def _watch(self, cur, now, judge=True) -> None:
|
||||
"""Is the read-back what we wrote? A difference that stands for
|
||||
`WRITER_S` is another writer."""
|
||||
known = [x for x in (self.last_a, self.attempted_a, self.prior_a)
|
||||
if x is not None]
|
||||
if not known or cur is None:
|
||||
self.mismatch_since = None
|
||||
`WRITER_S` is another writer. EV-22: one count and one hold for the
|
||||
current and (Solar) the phases; `judge=False`: the reading is from
|
||||
before our own write of this step, so the current is not judged and
|
||||
the count stands."""
|
||||
known, diff = [], None
|
||||
if judge:
|
||||
known = self._own_values()
|
||||
if known and cur is not None:
|
||||
diff = not any(abs(cur - k) <= TOL_A for k in known)
|
||||
if diff or self._ph_diff:
|
||||
if self.mismatch_since is None:
|
||||
self.mismatch_since = now
|
||||
elif now - self.mismatch_since >= WRITER_S and not self.hold:
|
||||
self.hold = self.other = True
|
||||
if diff:
|
||||
_LOG.info("Something else changes the charger (read-back "
|
||||
"%.0f A, the add-on set %.0f A), so the add-on "
|
||||
"stops writing to it until you change it.",
|
||||
cur, known[0])
|
||||
else:
|
||||
_LOG.info("Something else changes the charger (the "
|
||||
"phases are not the ones the add-on chose), so "
|
||||
"the add-on stops writing to it until you "
|
||||
"change it.")
|
||||
return
|
||||
if any(abs(cur - k) <= TOL_A for k in known):
|
||||
self.mismatch_since = None
|
||||
if self.hold:
|
||||
self.hold = self.other = False
|
||||
if not judge:
|
||||
return
|
||||
if self.mismatch_since is None:
|
||||
self.mismatch_since = now
|
||||
elif now - self.mismatch_since >= WRITER_S and not self.hold:
|
||||
self.hold = self.other = True
|
||||
_LOG.info("Something else changes the charger (read-back %.0f A, "
|
||||
"the add-on set %.0f A), so the add-on stops writing "
|
||||
"to it until you change it.", cur, known[0])
|
||||
self.mismatch_since = None
|
||||
if diff is False and self.hold:
|
||||
self.hold = self.other = False
|
||||
|
||||
# ----------------------------------------------------- EV-52: the info line
|
||||
def _why_default(self) -> str:
|
||||
@@ -410,7 +450,8 @@ class ManualWriter:
|
||||
self.undone += 1
|
||||
self.rev_applied, self.reapply = None, True
|
||||
a = self._restore_a(q.get("prev"), clamp)
|
||||
if a is not None:
|
||||
if a is not None and not self._other_stands(): # EV-22: not over theirs
|
||||
q.setdefault("own", []).append(a)
|
||||
await self._write(drv, a, 0.0, now, tracker, why="Manual")
|
||||
|
||||
async def _begin(self, drv, r, cur, n, now, tracker) -> None:
|
||||
@@ -428,7 +469,7 @@ class ManualWriter:
|
||||
self._failed(now)
|
||||
return
|
||||
self.seq = {"stage": "zero", "t0": now, "n": n, "tries": 0,
|
||||
"prev": cur}
|
||||
"prev": cur, "own": [0.0]}
|
||||
|
||||
async def abandon(self, drv, now, tracker, clamp=None) -> None:
|
||||
"""The add-on is stopping in the middle of a phase change: give the car
|
||||
@@ -436,11 +477,19 @@ class ManualWriter:
|
||||
like every write. The phases stay as the select left them."""
|
||||
q, self.seq = self.seq, None
|
||||
a = None if q is None else self._restore_a(q.get("prev"), clamp)
|
||||
if a is not None:
|
||||
if a is not None and not self._other_stands(): # EV-22: not over theirs
|
||||
self._clock = self._clock or (lambda: now)
|
||||
await self._write(drv, a, 0.0, now, tracker, why="shutdown",
|
||||
strict=True)
|
||||
|
||||
def _select_try(self, n, ok) -> None:
|
||||
"""EV-22: a phase select of ours went out and returned (Solar's
|
||||
window and own phases; Manual does not judge phases)."""
|
||||
|
||||
def _took_as_is(self) -> None:
|
||||
"""EV-22 (DA1): a user's change took the charger's value as it is
|
||||
(Solar forgets what it sent)."""
|
||||
|
||||
async def _advance(self, drv, r, cur, now, tracker, want_a, clamp, rev):
|
||||
q = self.seq
|
||||
if q["stage"] in ("zero", "select") and cur is not None \
|
||||
@@ -450,11 +499,20 @@ class ManualWriter:
|
||||
# change, and leave the charger to whoever set it until the user
|
||||
# changes something.
|
||||
self.seq = None
|
||||
self.hold = self.other = True
|
||||
self.last_a = self.attempted_a = self.prior_a = None
|
||||
if q.get("pressed"):
|
||||
# EV-22 (DA1): the user's change came during this change: the
|
||||
# charger is taken as it is, never held over that value.
|
||||
self._took_as_is()
|
||||
return
|
||||
self.hold = self.other = True
|
||||
_LOG.info("Something else raised the charger's current during a "
|
||||
"phase change, so the phases were not touched.")
|
||||
return
|
||||
if self._other_stands():
|
||||
# EV-22 (C7): another writer's value stands (its count runs, or
|
||||
# it is held): no select, no restore, no closing current over it.
|
||||
return
|
||||
if q["stage"] == "zero":
|
||||
# A power reading decides; only without one does mode3 (not C,
|
||||
# not D) say the car is quiet. Still drawing at the timeout: no
|
||||
@@ -486,6 +544,7 @@ class ManualWriter:
|
||||
self.last_switch = now
|
||||
self.log.append(("phases", q["n"], now, ok))
|
||||
q["stage"], q["t1"] = "settle", self.last_done # from its return
|
||||
self._select_try(q["n"], bool(ok))
|
||||
if not ok:
|
||||
q["failed"] = True # EV-15: no grace for the alarm
|
||||
# The charger did not take it: the car gets its current back
|
||||
@@ -506,6 +565,7 @@ class ManualWriter:
|
||||
# on whether the phases changed); Manual a number, as before.
|
||||
want = want_a(q) if callable(want_a) else want_a
|
||||
a = min(max(float(want), CH.MIN_A), hi)
|
||||
q.setdefault("own", []).append(a) # EV-22: a value we tried is ours
|
||||
if await self._write(drv, a, 0.0, now, tracker,
|
||||
why=self._close_why(q),
|
||||
n=q["n"] if q.get("sel_ok") else None):
|
||||
@@ -907,6 +967,13 @@ class SolarWriter(ManualWriter):
|
||||
self.sent_cut = False # `sent_a` went out as a cut try
|
||||
self.refused_cut = None # a cut value the charger did not take
|
||||
self._cap_end = False # EV-60: a capped close ended the change
|
||||
# EV-22 (D4): the phases Solar itself chose. `_own_n`: the last it
|
||||
# TOOK (or the start state); `_sel_n` and `_win_end`: its latest
|
||||
# select try and the SETTLE_S after that try returned, in which a
|
||||
# read-back is not judged.
|
||||
self._own_n = None
|
||||
self._sel_n = None
|
||||
self._win_end = None
|
||||
|
||||
def owe_start(self) -> None:
|
||||
"""C1: the add-on starts in Off while an exit 0 A of its own was still
|
||||
@@ -1009,10 +1076,16 @@ class SolarWriter(ManualWriter):
|
||||
"""The user PRESSED SOLAR: another writer's hold is lifted and Solar
|
||||
takes the charger as it is (its value is no longer a difference);
|
||||
Solar's own value is decided and written afresh."""
|
||||
held = self.hold or self.mismatch_since is not None
|
||||
super().user_changed()
|
||||
held = self._other_stands()
|
||||
had_seq = self.seq is not None
|
||||
super().user_changed() # EV-22 (DA1): ends a change it lifts
|
||||
self.gave_up, self.fails, self.fail_at = False, 0, None
|
||||
self._stuck_end()
|
||||
# EV-22: the phases are taken afresh (the start state again); an own
|
||||
# select's window stays.
|
||||
self._own_n, self._ph_diff = None, False
|
||||
if held and had_seq:
|
||||
self.plan.end_switch(False)
|
||||
if held:
|
||||
self.last_a = None
|
||||
self.sent_a, self.taken = None, True
|
||||
@@ -1266,9 +1339,50 @@ class SolarWriter(ManualWriter):
|
||||
if self.live and not self.hold:
|
||||
await self._put(drv, 0.0, cur, now, tracker, stuck, out=True)
|
||||
|
||||
def _watch(self, cur, now) -> None:
|
||||
def _took_as_is(self) -> None:
|
||||
self.sent_a, self.taken = None, True
|
||||
self.refused_cut = None
|
||||
|
||||
def _reset_phases(self, window=True) -> None:
|
||||
"""EV-22: the phases Solar judges start afresh (entering Solar, the
|
||||
charger's return, a press). The window of an own select goes too
|
||||
unless it is kept."""
|
||||
self._own_n, self._ph_diff = None, False
|
||||
if window:
|
||||
self._sel_n = self._win_end = None
|
||||
|
||||
def _select_try(self, n, ok) -> None:
|
||||
"""EV-22 (DA3): our own select try returned. SETTLE_S from now a
|
||||
read-back is not judged (A7: from its return, not from its send); a
|
||||
taken select is our phases at once, one the charger did not confirm
|
||||
becomes ours if a read inside the window shows it."""
|
||||
self._sel_n = n
|
||||
self._win_end = float(self.last_done) + SETTLE_S
|
||||
if ok:
|
||||
self._own_n, self._ph_diff = n, False
|
||||
|
||||
def _judge_phases(self, r, now) -> None:
|
||||
"""EV-22 (D4): the read-back phases against the phases Solar itself
|
||||
chose (never the plan's adopted `n`). Unknown phases and reads inside
|
||||
a select's window start nothing and lift nothing."""
|
||||
p = getattr(r, "phases_allowed", None)
|
||||
if p not in (1, 3):
|
||||
return
|
||||
if self._win_end is not None:
|
||||
if now <= self._win_end:
|
||||
if p == self._sel_n:
|
||||
self._own_n, self._ph_diff = p, False # the take
|
||||
return
|
||||
self._sel_n = self._win_end = None
|
||||
if self._own_n is None:
|
||||
self._own_n, self._ph_diff = p, False # the start state
|
||||
return
|
||||
self._ph_diff = p != self._own_n
|
||||
|
||||
def _watch(self, cur, now, judge=True) -> None:
|
||||
was = self.hold
|
||||
super()._watch(cur, now)
|
||||
self._judge_phases(self._r, now)
|
||||
super()._watch(cur, now, judge)
|
||||
if self.hold and not was:
|
||||
_LOG.warning("Something else changes the charger, so the add-on "
|
||||
"stops writing to it until you press Solar: "
|
||||
@@ -1306,6 +1420,7 @@ class SolarWriter(ManualWriter):
|
||||
self.out, self.fault = False, None
|
||||
self.restart = True
|
||||
self.mismatch_since = None
|
||||
self._reset_phases()
|
||||
self.plan.reset(self.plan.n)
|
||||
|
||||
def _gap_ok(self, now) -> bool:
|
||||
@@ -1383,7 +1498,7 @@ class SolarWriter(ManualWriter):
|
||||
self.plan.end_switch(False)
|
||||
return
|
||||
self.seq = {"stage": "zero", "t0": now, "n": n, "tries": 0,
|
||||
"prev": None, "from": r.phases_allowed}
|
||||
"prev": None, "from": r.phases_allowed, "own": [0.0]}
|
||||
|
||||
# ------------------------------------------------------------- step
|
||||
async def held_pause(self, drv, r, *, sun, now, tracker) -> None:
|
||||
@@ -1432,6 +1547,7 @@ class SolarWriter(ManualWriter):
|
||||
# notice. The quarter's try count stays (Q5).
|
||||
self.refused_cut, self.sent_cut, self.cut_warned = None, False, \
|
||||
False
|
||||
self._reset_phases() # EV-22: a fresh entry starts clean
|
||||
if not self.engaged:
|
||||
return
|
||||
if sun is not None:
|
||||
@@ -1463,6 +1579,8 @@ class SolarWriter(ManualWriter):
|
||||
self.startup_zero = self.boot and not self._returned
|
||||
self.fresh_start = self.restart = True
|
||||
self.mismatch_since = None
|
||||
if not self.hold:
|
||||
self._reset_phases(window=False) # EV-22: start state again
|
||||
self.last_a = self.attempted_a = self.prior_a = None
|
||||
self._rec_a = None # EV-56 (G2): logged again
|
||||
cur = r.allowed_a if r.allowed_a is not None else r.setpoint_a
|
||||
@@ -1497,8 +1615,11 @@ class SolarWriter(ManualWriter):
|
||||
# owed, then nothing. The watch goes on (another writer is held
|
||||
# for every reader) and so does the plan, for the card.
|
||||
self.out = True
|
||||
n_log = len(self.log)
|
||||
await self._zero(drv, cur, now, tracker, stuck)
|
||||
self._watch(cur, now)
|
||||
# EV-22 (DA2): `cur` is from before our own 0 A of this step:
|
||||
# the current is not judged then (the phases are).
|
||||
self._watch(cur, now, judge=len(self.log) == n_log)
|
||||
self._shadow_plan(sun, now, clamp, n_now)
|
||||
self.restart = True
|
||||
return
|
||||
@@ -1571,8 +1692,8 @@ class SolarWriter(ManualWriter):
|
||||
if self.drop_ready(cur, stuck):
|
||||
# EV-54 (drop rule): the refused cut value is not sent again.
|
||||
self._drop(cur, now)
|
||||
self._watch(cur, now) # EV-22 (C7): inside a change too
|
||||
if self.seq is None:
|
||||
self._watch(cur, now)
|
||||
if self.hold or self.mismatch_since is not None:
|
||||
# Another writer's value, held or not yet: nothing is written
|
||||
# over it; the plan goes on for the card.
|
||||
|
||||
Reference in New Issue
Block a user