Local preservation snapshot, not a new deployment or simulation acceptance.
166 lines
7.2 KiB
Python
166 lines
7.2 KiB
Python
"""Physics-clock adapter for the common two-side command contract.
|
|
|
|
The profile declares simulation-only limits and the contact proxy's stop mode.
|
|
Zero intent is immediate; actual deceleration belongs to the physics adapter.
|
|
"""
|
|
|
|
from rover_profile import ROBOT_DOCUMENT
|
|
|
|
CONTROL_PROFILE = ROBOT_DOCUMENT["control"]
|
|
|
|
|
|
def reserve_chassis_speed(velocity, yaw_rate, maximum_mps, reserve_mps):
|
|
"""Leave declared tracking-error room below a run's body-speed ceiling.
|
|
|
|
Scale moving arcs together; stationary pivots and safety zero retain their
|
|
meaning. This is a planning reserve, not a guarantee about physical speed.
|
|
"""
|
|
from k1link.fleet.differential_drive import finite
|
|
|
|
finite(velocity, yaw_rate, maximum_mps, reserve_mps)
|
|
if not 0 <= reserve_mps < maximum_mps:
|
|
raise ValueError("Speed reserve must be below the run speed ceiling")
|
|
scale = min(1.0, (maximum_mps - reserve_mps) / max(abs(velocity), 1e-12))
|
|
return velocity * scale, yaw_rate * scale
|
|
|
|
|
|
class DriveEnvelope:
|
|
def __init__(self, track_width=None, acceleration=None, *, control=None):
|
|
from k1link.fleet.differential_drive import DriveLimits, TwoSideDrive
|
|
|
|
control = CONTROL_PROFILE if control is None else control
|
|
self.limits = DriveLimits(
|
|
track_centres_m=(
|
|
ROBOT_DOCUMENT["physics"]["track_width_m"] if track_width is None else track_width
|
|
),
|
|
max_side_speed_mps=control["max_side_speed_mps"],
|
|
acceleration_mps2=(
|
|
control["acceleration_mps2"] if acceleration is None else acceleration
|
|
),
|
|
deceleration_mps2=control["deceleration_mps2"],
|
|
max_step_seconds=control["max_step_seconds"],
|
|
)
|
|
self.drive = TwoSideDrive(self.limits)
|
|
self.requested = self.drive.applied
|
|
|
|
@property
|
|
def wheels(self):
|
|
return self.drive.applied.left_mps, self.drive.applied.right_mps
|
|
|
|
def step(self, velocity, yaw_rate, dt, *, stop=False):
|
|
from k1link.fleet.differential_drive import SideSpeeds
|
|
|
|
self.requested = SideSpeeds.from_twist(velocity, yaw_rate, self.limits.track_centres_m)
|
|
return self.drive.step(self.requested, dt, stop=stop).twist(self.limits.track_centres_m)
|
|
|
|
|
|
class ChassisSpeedFeedback:
|
|
"""Inner speed correction for autonomous chassis intent on a pitching body.
|
|
|
|
The lower contact moves at v_COM - height * pitch_rate. A belt encoder
|
|
alone therefore does not regulate COM speed over uneven terrain. This
|
|
controller uses measured forward speed and angular velocity; it only
|
|
changes bounded belt targets. Native forces still own all body motion.
|
|
"""
|
|
|
|
def __init__(self, profile, contact_height_m, track_centres_m, max_side_mps):
|
|
from k1link.fleet.differential_drive import finite
|
|
|
|
self.gain = profile["proportional_gain"]
|
|
self.maximum = profile["max_correction_mps"]
|
|
self.height = contact_height_m
|
|
self.track_centres = track_centres_m
|
|
self.max_side = max_side_mps
|
|
finite(self.gain, self.maximum, self.height, self.track_centres, self.max_side)
|
|
if min(self.gain, self.maximum, self.height, self.track_centres, self.max_side) <= 0:
|
|
raise ValueError("Invalid chassis feedback parameters")
|
|
self.metrics = {}
|
|
|
|
def step(self, velocity, yaw_rate, measured_forward_mps, pitch_rate_rps):
|
|
from k1link.fleet.differential_drive import SideSpeeds, finite
|
|
|
|
finite(velocity, yaw_rate, measured_forward_mps, pitch_rate_rps)
|
|
correction = 0.0
|
|
if velocity != 0.0 or yaw_rate != 0.0:
|
|
correction = max(
|
|
-self.maximum,
|
|
min(
|
|
self.maximum,
|
|
-self.height * pitch_rate_rps + self.gain * (velocity - measured_forward_mps),
|
|
),
|
|
)
|
|
if velocity == 0.0 and yaw_rate != 0.0:
|
|
# A pivot requests zero body translation, not zero feedback.
|
|
# Counter measured drift with a common belt adjustment, bounded
|
|
# by the opposed-side intent so neither side reverses and the
|
|
# requested yaw differential is retained. Safety zero stays zero.
|
|
pivot_room = abs(yaw_rate) * self.track_centres / 2
|
|
correction = max(-pivot_room, min(pivot_room, correction))
|
|
corrected = velocity + correction
|
|
# Feedback may reduce translation to zero, but cannot request an
|
|
# unplanned reversal. A pivot retains its opposed side directions.
|
|
if velocity * corrected < 0:
|
|
corrected = 0.0
|
|
nominal = SideSpeeds.from_twist(velocity, yaw_rate, self.track_centres)
|
|
proposed = SideSpeeds.from_twist(corrected, yaw_rate, self.track_centres)
|
|
# The nominal envelope owns each side's reversal through zero. A
|
|
# common correction must also respect a stationary/turning inner belt.
|
|
target = SideSpeeds(
|
|
*(
|
|
value if intent * value > 0 else 0.0
|
|
for intent, value in zip(
|
|
vars(nominal).values(), vars(proposed).values(), strict=True
|
|
)
|
|
)
|
|
).bounded(self.max_side)
|
|
applied = target.twist(self.track_centres)
|
|
self.metrics = dict(
|
|
nominal_twist=[velocity, yaw_rate],
|
|
measured_forward_mps=measured_forward_mps,
|
|
pitch_rate_rps=pitch_rate_rps,
|
|
correction_mps=correction,
|
|
actuator_twist=list(applied),
|
|
)
|
|
return applied
|
|
|
|
|
|
class RoverMotionControl:
|
|
"""The same autonomous intent -> belt target path for runtime and rigs.
|
|
|
|
Physics remains the sole owner of actual body motion. A rig supplies the
|
|
same measured forward velocity and pitch rate as the live callback.
|
|
"""
|
|
|
|
def __init__(self, robot, maximum_mps):
|
|
self.maximum = maximum_mps
|
|
physics, control = robot["physics"], robot["control"]
|
|
self.envelope = DriveEnvelope(track_width=physics["track_width_m"], control=control)
|
|
settings = control.get("chassis_speed_feedback", {})
|
|
self.feedback = None
|
|
self.reserve = 0.0
|
|
if settings.get("enabled", False):
|
|
self.feedback = ChassisSpeedFeedback(
|
|
settings,
|
|
robot.get("tracked_contact", {}).get(
|
|
"chassis_com_height_m", physics["body_contact_height_m"]
|
|
),
|
|
physics["track_width_m"],
|
|
control["max_side_speed_mps"],
|
|
)
|
|
self.reserve = (
|
|
maximum_mps * settings["body_speed_reserve_fraction"]
|
|
if "body_speed_reserve_fraction" in settings
|
|
else settings.get("body_speed_reserve_mps", 0.0)
|
|
)
|
|
reserve_chassis_speed(0, 0, maximum_mps, self.reserve)
|
|
|
|
def step(self, velocity, yaw_rate, dt, forward_mps, pitch_rps, *, stop=False):
|
|
if self.feedback is not None:
|
|
velocity, yaw_rate = reserve_chassis_speed(
|
|
velocity, yaw_rate, self.maximum, self.reserve
|
|
)
|
|
velocity, yaw_rate = self.envelope.step(velocity, yaw_rate, dt, stop=stop)
|
|
if self.feedback is not None:
|
|
velocity, yaw_rate = self.feedback.step(velocity, yaw_rate, forward_mps, pitch_rps)
|
|
return velocity, yaw_rate
|