From 8f93f90fb826872a63aeb3ce36cf1f2854a43819 Mon Sep 17 00:00:00 2001 From: JackM323 Date: Sat, 24 Oct 2026 22:15:34 +0200 Subject: [PATCH] StopState implementation Prevent hard Servo movements to Idle Position --- Robot.py | 15 +++++++++++++++ states/IdleState.py | 2 +- states/StopState.py | 35 +++++++++++++++++++++++++++++++++++ states/WalkingState.py | 11 ++++++++++- states/__init__.py | 4 +++- 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 states/StopState.py diff --git a/Robot.py b/Robot.py index d499e13..f1b8886 100644 --- a/Robot.py +++ b/Robot.py @@ -217,6 +217,9 @@ class Robot: return kin.ikpyForward(rads) def transition_to(self, next_state_key: str) -> None: + if next_state_key == "idle" and self.current_state_key not in ("idle", "stopping"): + next_state_key = "stopping" + if next_state_key in STATE_REGISTRY and next_state_key != self.current_state_key: self.current_state.exit(self) self.current_state_key = next_state_key @@ -240,6 +243,18 @@ class Robot: self.apply_rl_action_delta(action) else: # "kinematics" / standard State Machine execution + has_motion_command = ( + abs(vx) > 0.01 + or abs(vy) > 0.01 + or abs(omega) > 0.01 + or self.robot_state in ("wave", "laola") + ) + if ( + self.current_state_key not in ("idle", "stopping", "walking", "walking_four") + and not has_motion_command + ): + self.transition_to("stopping") + next_state_key = self.current_state.execute(self) if next_state_key: self.transition_to(next_state_key) diff --git a/states/IdleState.py b/states/IdleState.py index 4dd1176..0f1bd99 100644 --- a/states/IdleState.py +++ b/states/IdleState.py @@ -11,7 +11,7 @@ if TYPE_CHECKING: class IdleState(State): def enter(self, robot: "Robot") -> None: - robot.reset_to_init() + pass def execute(self, robot: "Robot") -> Optional[str]: # Check transition conditions based on vector_dirmov or command flags diff --git a/states/StopState.py b/states/StopState.py new file mode 100644 index 0000000..3c24c0a --- /dev/null +++ b/states/StopState.py @@ -0,0 +1,35 @@ +"""Smoothly returns the robot to its configured idle joint pose.""" +from __future__ import annotations +from typing import TYPE_CHECKING, Optional +import numpy as np + +import DataTypes as dt +import kinematics as kin +import robot_init as ri +from states.State import State + +if TYPE_CHECKING: + from Robot import Robot + + +class StopState(State): + def enter(self, robot: "Robot") -> None: + pass + + def execute(self, robot: "Robot") -> Optional[str]: + vx, vy, omega = robot.vector_dirmov + if abs(vx) > 0.01 or abs(vy) > 0.01 or abs(omega) > 0.01: + return "walking" + + idle_pose = ri.init_deg if robot.start_pose == "init_deg" else ri.init90_deg + target_rad = idle_pose.to_rad() + robot.set_joint_angles(target_rad) + robot.current_pos = dt.PosArray(kin.ikpyForward(robot.current_rad).data.copy()) + + if np.allclose(robot.current_rad.data, target_rad.data, atol=1e-6): + return "idle" + + return None + + def exit(self, robot: "Robot") -> None: + pass \ No newline at end of file diff --git a/states/WalkingState.py b/states/WalkingState.py index 98f4bc1..4eadfea 100644 --- a/states/WalkingState.py +++ b/states/WalkingState.py @@ -73,7 +73,7 @@ class WalkingState(State): and abs(omega) < 0.01 and self.current_tick == 0 ): - return "idle" + return "stopping" t = self.current_tick / float(self.ticks) tick_pos_temp = [] @@ -165,6 +165,15 @@ class WalkingFourState(State): self.start_pos = dt.PosArray(np.copy(robot.current_pos.data)) def execute(self, robot: Robot) -> Optional[str]: + vx, vy, omega = robot.vector_dirmov + if ( + abs(vx) < 0.01 + and abs(vy) < 0.01 + and abs(omega) < 0.01 + and self.current_tick == 0 + ): + return "stopping" + t = self.current_tick / float(self.ticks) tick_pos_temp = [] dirmov = robot.vector_dirmov diff --git a/states/__init__.py b/states/__init__.py index c0b9e4d..72a95de 100644 --- a/states/__init__.py +++ b/states/__init__.py @@ -4,10 +4,12 @@ states/__init__.py - State Machine Initialization from states.State import State, STATE_REGISTRY from states.IdleState import IdleState from states.WalkingState import WalkingState, WalkingFourState +from states.StopState import StopState # Register available state instances STATE_REGISTRY["idle"] = IdleState() +STATE_REGISTRY["stopping"] = StopState() STATE_REGISTRY["walking"] = WalkingState() STATE_REGISTRY["walking_four"] = WalkingFourState() -__all__ = ["State", "STATE_REGISTRY", "IdleState", "WalkingState", "WalkingFourState"] \ No newline at end of file +__all__ = ["State", "STATE_REGISTRY", "IdleState", "StopState", "WalkingState", "WalkingFourState"] \ No newline at end of file