Preserve installed operator workspace before consolidation
Local preservation snapshot, not a new deployment or simulation acceptance.
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
"""Vendor-neutral two-side speed intent. No transport, motor calls or body motion.
|
||||
|
||||
Positive speed is forward; positive yaw turns left. Physical adapters must
|
||||
declare how this intent maps to their actuator, including zero/coast/brake.
|
||||
"""
|
||||
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
def finite(*values):
|
||||
if not all(math.isfinite(value) for value in values):
|
||||
raise ValueError("Drive values must be finite")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SideSpeeds:
|
||||
left_mps: float = 0.0
|
||||
right_mps: float = 0.0
|
||||
|
||||
def __post_init__(self):
|
||||
finite(self.left_mps, self.right_mps)
|
||||
|
||||
@classmethod
|
||||
def from_twist(cls, velocity_mps, yaw_rps, track_centres_m):
|
||||
finite(velocity_mps, yaw_rps, track_centres_m)
|
||||
if track_centres_m <= 0:
|
||||
raise ValueError("Track centre distance must be positive")
|
||||
return cls(
|
||||
velocity_mps - yaw_rps * track_centres_m / 2,
|
||||
velocity_mps + yaw_rps * track_centres_m / 2,
|
||||
)
|
||||
|
||||
def twist(self, track_centres_m):
|
||||
finite(track_centres_m)
|
||||
if track_centres_m <= 0:
|
||||
raise ValueError("Track centre distance must be positive")
|
||||
return (
|
||||
(self.left_mps + self.right_mps) / 2,
|
||||
(self.right_mps - self.left_mps) / track_centres_m,
|
||||
)
|
||||
|
||||
def bounded(self, maximum_mps):
|
||||
finite(maximum_mps)
|
||||
if maximum_mps <= 0:
|
||||
raise ValueError("Side speed limit must be positive")
|
||||
scale = max(1.0, abs(self.left_mps) / maximum_mps, abs(self.right_mps) / maximum_mps)
|
||||
return SideSpeeds(self.left_mps / scale, self.right_mps / scale)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DriveLimits:
|
||||
track_centres_m: float
|
||||
max_side_speed_mps: float
|
||||
acceleration_mps2: float
|
||||
deceleration_mps2: float
|
||||
max_step_seconds: float
|
||||
|
||||
def __post_init__(self):
|
||||
values = tuple(vars(self).values())
|
||||
finite(*values)
|
||||
if min(values) <= 0:
|
||||
raise ValueError("Drive limits must be positive")
|
||||
|
||||
|
||||
def tank_intent(left, right, limits):
|
||||
finite(left, right)
|
||||
if max(abs(left), abs(right)) > 1:
|
||||
raise ValueError("Stick inputs must be within [-1, 1]")
|
||||
return SideSpeeds(left * limits.max_side_speed_mps, right * limits.max_side_speed_mps)
|
||||
|
||||
|
||||
def arcade_intent(throttle, steering, limits, max_yaw_rps):
|
||||
finite(throttle, steering, max_yaw_rps)
|
||||
if max(abs(throttle), abs(steering)) > 1 or max_yaw_rps <= 0:
|
||||
raise ValueError("Invalid arcade input or yaw limit")
|
||||
return SideSpeeds.from_twist(
|
||||
throttle * limits.max_side_speed_mps, steering * max_yaw_rps, limits.track_centres_m
|
||||
).bounded(limits.max_side_speed_mps)
|
||||
|
||||
|
||||
class TwoSideDrive:
|
||||
"""Slew both targets on one clock, with explicit immediate zero intent.
|
||||
|
||||
Each reversing side reaches zero before accelerating in its new direction.
|
||||
The other side need not stop. A late update cannot create a large speed jump.
|
||||
This limits commands, not measured chassis acceleration or stopping distance.
|
||||
"""
|
||||
|
||||
def __init__(self, limits):
|
||||
self.limits = limits
|
||||
self.applied = SideSpeeds()
|
||||
|
||||
def step(self, requested, dt, *, stop=False):
|
||||
finite(dt)
|
||||
if dt < 0:
|
||||
raise ValueError("Drive step must not be negative")
|
||||
target = requested.bounded(self.limits.max_side_speed_mps)
|
||||
if stop or target == SideSpeeds():
|
||||
self.applied = SideSpeeds()
|
||||
return self.applied
|
||||
dt = min(dt, self.limits.max_step_seconds)
|
||||
old = (self.applied.left_mps, self.applied.right_mps)
|
||||
goal = (target.left_mps, target.right_mps)
|
||||
goal = tuple(0.0 if a * b < 0 else b for a, b in zip(old, goal, strict=True))
|
||||
fraction = 1.0
|
||||
for a, b in zip(old, goal, strict=True):
|
||||
rate = (
|
||||
self.limits.acceleration_mps2 if abs(b) > abs(a) else self.limits.deceleration_mps2
|
||||
)
|
||||
if a != b:
|
||||
fraction = min(fraction, rate * dt / abs(b - a))
|
||||
self.applied = SideSpeeds(*(a + (b - a) * fraction for a, b in zip(old, goal, strict=True)))
|
||||
return self.applied
|
||||
Reference in New Issue
Block a user