StopState implementation
Prevent hard Servo movements to Idle Position
This commit is contained in:
@@ -217,6 +217,9 @@ class Robot:
|
|||||||
return kin.ikpyForward(rads)
|
return kin.ikpyForward(rads)
|
||||||
|
|
||||||
def transition_to(self, next_state_key: str) -> None:
|
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:
|
if next_state_key in STATE_REGISTRY and next_state_key != self.current_state_key:
|
||||||
self.current_state.exit(self)
|
self.current_state.exit(self)
|
||||||
self.current_state_key = next_state_key
|
self.current_state_key = next_state_key
|
||||||
@@ -240,6 +243,18 @@ class Robot:
|
|||||||
self.apply_rl_action_delta(action)
|
self.apply_rl_action_delta(action)
|
||||||
|
|
||||||
else: # "kinematics" / standard State Machine execution
|
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)
|
next_state_key = self.current_state.execute(self)
|
||||||
if next_state_key:
|
if next_state_key:
|
||||||
self.transition_to(next_state_key)
|
self.transition_to(next_state_key)
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
class IdleState(State):
|
class IdleState(State):
|
||||||
def enter(self, robot: "Robot") -> None:
|
def enter(self, robot: "Robot") -> None:
|
||||||
robot.reset_to_init()
|
pass
|
||||||
|
|
||||||
def execute(self, robot: "Robot") -> Optional[str]:
|
def execute(self, robot: "Robot") -> Optional[str]:
|
||||||
# Check transition conditions based on vector_dirmov or command flags
|
# Check transition conditions based on vector_dirmov or command flags
|
||||||
|
|||||||
@@ -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
|
||||||
+10
-1
@@ -73,7 +73,7 @@ class WalkingState(State):
|
|||||||
and abs(omega) < 0.01
|
and abs(omega) < 0.01
|
||||||
and self.current_tick == 0
|
and self.current_tick == 0
|
||||||
):
|
):
|
||||||
return "idle"
|
return "stopping"
|
||||||
|
|
||||||
t = self.current_tick / float(self.ticks)
|
t = self.current_tick / float(self.ticks)
|
||||||
tick_pos_temp = []
|
tick_pos_temp = []
|
||||||
@@ -165,6 +165,15 @@ class WalkingFourState(State):
|
|||||||
self.start_pos = dt.PosArray(np.copy(robot.current_pos.data))
|
self.start_pos = dt.PosArray(np.copy(robot.current_pos.data))
|
||||||
|
|
||||||
def execute(self, robot: Robot) -> Optional[str]:
|
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)
|
t = self.current_tick / float(self.ticks)
|
||||||
tick_pos_temp = []
|
tick_pos_temp = []
|
||||||
dirmov = robot.vector_dirmov
|
dirmov = robot.vector_dirmov
|
||||||
|
|||||||
+3
-1
@@ -4,10 +4,12 @@ states/__init__.py - State Machine Initialization
|
|||||||
from states.State import State, STATE_REGISTRY
|
from states.State import State, STATE_REGISTRY
|
||||||
from states.IdleState import IdleState
|
from states.IdleState import IdleState
|
||||||
from states.WalkingState import WalkingState, WalkingFourState
|
from states.WalkingState import WalkingState, WalkingFourState
|
||||||
|
from states.StopState import StopState
|
||||||
|
|
||||||
# Register available state instances
|
# Register available state instances
|
||||||
STATE_REGISTRY["idle"] = IdleState()
|
STATE_REGISTRY["idle"] = IdleState()
|
||||||
|
STATE_REGISTRY["stopping"] = StopState()
|
||||||
STATE_REGISTRY["walking"] = WalkingState()
|
STATE_REGISTRY["walking"] = WalkingState()
|
||||||
STATE_REGISTRY["walking_four"] = WalkingFourState()
|
STATE_REGISTRY["walking_four"] = WalkingFourState()
|
||||||
|
|
||||||
__all__ = ["State", "STATE_REGISTRY", "IdleState", "WalkingState", "WalkingFourState"]
|
__all__ = ["State", "STATE_REGISTRY", "IdleState", "StopState", "WalkingState", "WalkingFourState"]
|
||||||
Reference in New Issue
Block a user