fix(vesc): recover cancelled controller preparation without rearming

This commit is contained in:
DCCONSTRUCTIONS
2026-11-21 20:58:25 +03:00
parent 1635299252
commit 3d04fa45d3
5 changed files with 243 additions and 11 deletions
+2 -1
View File
@@ -6,8 +6,9 @@ from .release_observation import ReleaseObservation
class PreflightRecovery:
def __init__(self, profile):
def __init__(self, profile, *, cancelled=False):
self.profile = copy.deepcopy(profile)
self.cancelled = cancelled
self.proof = None
def reset(self):
+35 -8
View File
@@ -240,9 +240,9 @@ class RemoteControl:
self.pending_release = ReleaseObservation(devices, time.monotonic())
def _reconcile_preflight(self, devices):
# Only the missing-device preflight path creates this observer. No
# motor output/configuration was attempted, so it cannot invent a
# historical release ACK or clear an unrelated execution fault.
# Missing-device failures and cancelled preparation both reuse passive
# whole-profile proof. Neither path revives a retired input lease;
# historical hardware faults remain terminal in last_stop.
with self.lock:
pending = self.pending_availability
if pending is None:
@@ -253,9 +253,23 @@ class RemoteControl:
or self.service.motor.active or firmware_active):
pending.reset()
return
if not pending.check(self.service.drive.value, devices, time.monotonic(),
self.service.motor.receiver_active, self.service.motor.limits.pending):
ready = pending.check(self.service.drive.value, devices, time.monotonic(),
self.service.motor.receiver_active, self.service.motor.limits.pending)
if (pending.cancelled and self.state == "stopping" and pending.proof is not None
and pending.proof.faulted(devices, time.monotonic())):
self.state = "fault"
self.message = "VESC сообщил об ошибке. Проверьте состояние перед включением."
if self.last_stop is not None:
self.last_stop.update(state="fault", message=self.message)
self.trace.add("preparation_recovery_fault", session_id=self.lease.id)
if not ready:
return
if pending.cancelled:
# Current neutral proof, not an invented ACK for an output that
# was never claimed. Limits must already be restored by owner.
self.release_confirmed = True
self.trace.add("preparation_neutral_confirmed", session_id=self.lease.id,
attachments=pending.proof.attachments, samples=pending.proof.count)
self.pending_availability = None
self.state, self.message = "stopped", None
if self.last_stop is not None:
@@ -313,6 +327,7 @@ class RemoteControl:
owner = self.service.motor
acquired = False
devices = []
profile = copy.deepcopy(self.service.drive.value)
try:
# Observation also owns this lock. A nonblocking attempt made
# arming depend on which thread happened to read first. Wait only
@@ -326,7 +341,7 @@ class RemoteControl:
acquired = self.service.operation_lock.acquire(timeout=min(.02, remaining))
self.ensure_live()
with self.service.lock: devices = list(self.service.devices.values())
profile = self.service.drive.value
profile = copy.deepcopy(self.service.drive.value)
slots = LAYOUTS.get(profile["layout"], ())
if not slots or set(profile["bindings"]) != set(slots):
raise ValueError("Назначьте все моторы в настройках борта.")
@@ -352,7 +367,18 @@ class RemoteControl:
# drive() classifies only successful, finite release readback
# as pending. Keep that state while the passive observer proves
# standstill; an expired input is never granted more lifetime.
if self.state != "stopping" or not settlement_pending(self.release_evidence):
if (self.release_confirmed is None and self.message is None
and self.pending_release is None):
# Input may expire before acquiring the operation lock, in
# preflight, or before drive claims output. A null release
# must not leave Core waiting forever. Reuse passive proof
# of the exact bound profile; never grant/replay input here.
self.pending_availability = PreflightRecovery(profile, cancelled=True)
self.state = "stopping"
self.trace.add("preparation_cancelled_waiting", session_id=envelope["id"],
reason=str(error), operation_acquired=acquired,
waiting_for="fresh_neutral_pair_and_restored_limits")
elif self.state != "stopping" or not settlement_pending(self.release_evidence):
self.state = "fault" if self.message or self.release_confirmed is False else "stopped"
except (OSError, ValueError, TimeoutError, KeyError) as error:
with self.lock:
@@ -378,7 +404,8 @@ class RemoteControl:
with self.lock:
if not self.lease.live():
self.trace.add("lease_expired",session_id=self.lease.id,sequence=self.lease.sequence,
remaining_ms=(self.lease.until-time.monotonic())*1000)
reason="withdrawn" if self.lease.until == 0 else "deadline_expired",
remaining_ms=max(0,(self.lease.until-time.monotonic())*1000))
raise ControlEnded("Управление остановлено: команда больше не подтверждается.")
def drive(self, owner, command, devices, moving, originals, unchanged):