Movement smoothing
This commit is contained in:
@@ -149,6 +149,8 @@ class Robot:
|
|||||||
self.vector_dirmov = [0.0, 0.0, 0.0] # [vx, vy, omega]
|
self.vector_dirmov = [0.0, 0.0, 0.0] # [vx, vy, omega]
|
||||||
self.robot_state = "idle"
|
self.robot_state = "idle"
|
||||||
self.leg_state = np.array(["step", "drag", "step", "drag", "step", "drag"])
|
self.leg_state = np.array(["step", "drag", "step", "drag", "step", "drag"])
|
||||||
|
self.max_joint_delta = cfg.max_joint_delta_rad
|
||||||
|
self.max_footstep_delta = cfg.max_footstep_delta_m
|
||||||
|
|
||||||
# State Machine Initialization
|
# State Machine Initialization
|
||||||
self.current_state_key: str = "idle"
|
self.current_state_key: str = "idle"
|
||||||
@@ -156,17 +158,25 @@ class Robot:
|
|||||||
self.current_state.enter(self)
|
self.current_state.enter(self)
|
||||||
|
|
||||||
def set_joint_angles(self, target_rad: dt.RadArray) -> None:
|
def set_joint_angles(self, target_rad: dt.RadArray) -> None:
|
||||||
"""Updates internal Python memory state and sends angles to active backend."""
|
"""Smooth joint motion and clamp abrupt angle jumps to protect the servos."""
|
||||||
self.current_rad = target_rad
|
target_arr = np.asarray(target_rad.data, dtype=np.float32)
|
||||||
|
current_arr = np.asarray(self.current_rad.data, dtype=np.float32)
|
||||||
|
|
||||||
|
delta = target_arr - current_arr
|
||||||
|
if np.any(np.abs(delta) > self.max_joint_delta):
|
||||||
|
delta = np.clip(delta, -self.max_joint_delta, self.max_joint_delta)
|
||||||
|
target_arr = current_arr + delta
|
||||||
|
|
||||||
|
self.current_rad = dt.RadArray(target_arr)
|
||||||
if self.backend:
|
if self.backend:
|
||||||
self.backend.send_angles(target_rad)
|
self.backend.send_angles(self.current_rad)
|
||||||
|
|
||||||
def step_sim(self) -> None:
|
def step_sim(self) -> None:
|
||||||
if self.backend:
|
if self.backend:
|
||||||
self.backend.step_simulation()
|
self.backend.step_simulation()
|
||||||
|
|
||||||
def reset_to_init(self) -> None:
|
def reset_to_init(self) -> None:
|
||||||
"""Resets kinematics state and forces instant joint alignment in backend."""
|
"""Resets kinematics state while keeping servo motion smooth and safe."""
|
||||||
pose_deg = ri.init_deg if self.start_pose == "init_deg" else ri.init90_deg
|
pose_deg = ri.init_deg if self.start_pose == "init_deg" else ri.init90_deg
|
||||||
self.current_rad = pose_deg.to_rad()
|
self.current_rad = pose_deg.to_rad()
|
||||||
self.current_pos = kin.ikpyForward(self.current_rad)
|
self.current_pos = kin.ikpyForward(self.current_rad)
|
||||||
@@ -174,13 +184,33 @@ class Robot:
|
|||||||
self.robot_state = "idle"
|
self.robot_state = "idle"
|
||||||
self.leg_state = np.array(["step", "drag", "step", "drag", "step", "drag"])
|
self.leg_state = np.array(["step", "drag", "step", "drag", "step", "drag"])
|
||||||
|
|
||||||
init_flat = self.current_rad.data.flatten()
|
|
||||||
if self.backend:
|
if self.backend:
|
||||||
self.backend.hard_reset_joints(init_flat)
|
|
||||||
self.backend.send_angles(self.current_rad)
|
self.backend.send_angles(self.current_rad)
|
||||||
|
|
||||||
|
def _clamp_cartesian_targets(self, target_pos: dt.PosArray) -> dt.PosArray:
|
||||||
|
target_arr = np.asarray(target_pos.data if hasattr(target_pos, "data") else target_pos, dtype=np.float32)
|
||||||
|
prev_arr = np.asarray(self.current_pos.data if hasattr(self.current_pos, "data") else self.current_pos, dtype=np.float32)
|
||||||
|
|
||||||
|
delta = target_arr - prev_arr
|
||||||
|
step_norms = np.linalg.norm(delta, axis=1)
|
||||||
|
limit_mask = step_norms > self.max_footstep_delta
|
||||||
|
|
||||||
|
if np.any(limit_mask):
|
||||||
|
scale = np.ones_like(step_norms, dtype=np.float32)
|
||||||
|
scale[limit_mask] = (self.max_footstep_delta / (step_norms[limit_mask] + 1e-9))
|
||||||
|
target_arr = prev_arr + delta * scale[:, None]
|
||||||
|
|
||||||
|
return dt.PosArray(target_arr)
|
||||||
|
|
||||||
def compute_ik(self, target_pos: dt.PosArray) -> dt.RadArray:
|
def compute_ik(self, target_pos: dt.PosArray) -> dt.RadArray:
|
||||||
return kin.ikpyInverse(target_pos, initial_rad=self.current_rad)
|
safe_target = self._clamp_cartesian_targets(target_pos)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return kin.ikpyInverse(safe_target, initial_rad=self.current_rad)
|
||||||
|
except ValueError as exc:
|
||||||
|
return dt.RadArray(np.array(self.current_rad.data, copy=True))
|
||||||
|
except Exception as exc:
|
||||||
|
return dt.RadArray(np.array(self.current_rad.data, copy=True))
|
||||||
|
|
||||||
def compute_fk(self, target_rad: Optional[dt.RadArray] = None) -> dt.PosArray:
|
def compute_fk(self, target_rad: Optional[dt.RadArray] = None) -> dt.PosArray:
|
||||||
rads = target_rad if target_rad is not None else self.current_rad
|
rads = target_rad if target_rad is not None else self.current_rad
|
||||||
@@ -193,7 +223,7 @@ class Robot:
|
|||||||
self.current_state = STATE_REGISTRY[next_state_key]
|
self.current_state = STATE_REGISTRY[next_state_key]
|
||||||
self.current_state.enter(self)
|
self.current_state.enter(self)
|
||||||
|
|
||||||
def tick(self, action: Optional[np.ndarray] = None) -> None:
|
def tick(self, action: Optional[np.ndarray] = None, physics_substeps: int = 1) -> None:
|
||||||
"""
|
"""
|
||||||
Unified control loop tick.
|
Unified control loop tick.
|
||||||
Processes commands through direct RL, residual RL, or State Machine kinematics.
|
Processes commands through direct RL, residual RL, or State Machine kinematics.
|
||||||
@@ -214,7 +244,8 @@ class Robot:
|
|||||||
if next_state_key:
|
if next_state_key:
|
||||||
self.transition_to(next_state_key)
|
self.transition_to(next_state_key)
|
||||||
|
|
||||||
self.step_sim()
|
for _ in range(physics_substeps):
|
||||||
|
self.step_sim()
|
||||||
|
|
||||||
def step_kinematic_gait(self, vx: float, vy: float, omega: float) -> None:
|
def step_kinematic_gait(self, vx: float, vy: float, omega: float) -> None:
|
||||||
"""Procedural Tripod Gait solver."""
|
"""Procedural Tripod Gait solver."""
|
||||||
|
|||||||
@@ -38,6 +38,10 @@ class RobotConfig:
|
|||||||
tick_rate_hz: float = 25.0 # Motion loop execution rate [ticks/sec]
|
tick_rate_hz: float = 25.0 # Motion loop execution rate [ticks/sec]
|
||||||
step_duration: float = 0.8 # Time to complete full gait stride [s]
|
step_duration: float = 0.8 # Time to complete full gait stride [s]
|
||||||
|
|
||||||
|
# Servo safety limits
|
||||||
|
max_joint_delta_rad: float = 0.12 # Maximum joint angle change per tick to protect servos
|
||||||
|
max_footstep_delta_m: float = 0.06 # Maximum Cartesian step per tick before IK target is trimmed
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def standard_tickpersec(self) -> float:
|
def standard_tickpersec(self) -> float:
|
||||||
return self.tick_rate_hz
|
return self.tick_rate_hz
|
||||||
|
|||||||
+10
-1
@@ -129,7 +129,16 @@ def ikpyInverse(target_pos: dt.PosArray, initial_rad: Optional[dt.RadArray] = No
|
|||||||
active_indices = [i for i, active in enumerate(chain.active_links_mask) if active]
|
active_indices = [i for i, active in enumerate(chain.active_links_mask) if active]
|
||||||
|
|
||||||
# Match 3 active joints to the 3 active link positions in the chain
|
# Match 3 active joints to the 3 active link positions in the chain
|
||||||
for idx, angle in zip(active_indices, initial_rad[leg_id]):
|
seed_angles = np.asarray(initial_rad[leg_id], dtype=float)
|
||||||
|
corrected_angles = []
|
||||||
|
for idx, angle in zip(active_indices, seed_angles):
|
||||||
|
bounds = chain.links[idx].bounds
|
||||||
|
if bounds is not None:
|
||||||
|
lower, upper = bounds
|
||||||
|
if not np.isfinite(angle):
|
||||||
|
angle = (lower + upper) / 2.0
|
||||||
|
angle = float(np.clip(angle, lower, upper))
|
||||||
|
corrected_angles.append(angle)
|
||||||
full_initial_position[idx] = angle
|
full_initial_position[idx] = angle
|
||||||
|
|
||||||
if full_initial_position is not None:
|
if full_initial_position is not None:
|
||||||
|
|||||||
Reference in New Issue
Block a user