StopState implementation
Prevent hard Servo movements to Idle Position
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
@@ -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 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
|
||||
|
||||
+3
-1
@@ -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"]
|
||||
__all__ = ["State", "STATE_REGISTRY", "IdleState", "StopState", "WalkingState", "WalkingFourState"]
|
||||
Reference in New Issue
Block a user