chromapi 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chromapi/__init__.py +2 -0
- chromapi/__main__.py +6 -0
- chromapi/assets/audio/alert.wav +0 -0
- chromapi/assets/audio/chromapi_scream.wav +0 -0
- chromapi/assets/audio/confused.wav +0 -0
- chromapi/assets/audio/dizzy.wav +0 -0
- chromapi/assets/audio/excited.wav +0 -0
- chromapi/assets/audio/greeting.wav +0 -0
- chromapi/assets/audio/happy.wav +0 -0
- chromapi/assets/audio/no.wav +0 -0
- chromapi/assets/audio/purring.wav +0 -0
- chromapi/assets/audio/sad.wav +0 -0
- chromapi/assets/audio/scared.wav +0 -0
- chromapi/assets/audio/sleepy.wav +0 -0
- chromapi/assets/audio/squeak.wav +0 -0
- chromapi/assets/audio/surprised.wav +0 -0
- chromapi/assets/audio/thinking.wav +0 -0
- chromapi/assets/audio/upset.wav +0 -0
- chromapi/assets/audio/yes.wav +0 -0
- chromapi/assets/favicon.ico +0 -0
- chromapi/assets/firmware/chromapi_stm32_core_v2.0.0.hex +2222 -0
- chromapi/assets/firmware/flash.sh +88 -0
- chromapi/assets/moves/README.md +48 -0
- chromapi/assets/moves/angry.json +1 -0
- chromapi/assets/moves/confused.json +1 -0
- chromapi/assets/moves/dizzy.json +1 -0
- chromapi/assets/moves/excited.json +1 -0
- chromapi/assets/moves/greeting.json +1 -0
- chromapi/assets/moves/happy.json +1 -0
- chromapi/assets/moves/joy.json +1 -0
- chromapi/assets/moves/kick.json +1 -0
- chromapi/assets/moves/metadata.jsonl +15 -0
- chromapi/assets/moves/no.json +1 -0
- chromapi/assets/moves/purring.json +1 -0
- chromapi/assets/moves/sad.json +1 -0
- chromapi/assets/moves/scared.json +1 -0
- chromapi/assets/moves/surprised.json +1 -0
- chromapi/assets/moves/tired.json +1 -0
- chromapi/assets/moves/yes.json +1 -0
- chromapi/chromapi.py +1460 -0
- chromapi/cli.py +106 -0
- chromapi/config/config.yaml +118 -0
- chromapi/estimation/__init__.py +6 -0
- chromapi/estimation/attitude.py +107 -0
- chromapi/hardware/__init__.py +2 -0
- chromapi/hardware/battery.py +54 -0
- chromapi/hardware/fan.py +165 -0
- chromapi/hardware/motherboard_bridge.py +345 -0
- chromapi/input/__init__.py +2 -0
- chromapi/input/gamepad.py +379 -0
- chromapi/input/pairing.py +98 -0
- chromapi/kinematics/__init__.py +10 -0
- chromapi/kinematics/kromatics.py +324 -0
- chromapi/kinematics/poses.py +29 -0
- chromapi/kinematics/topology.py +26 -0
- chromapi/kinematics/types.py +21 -0
- chromapi/locomotion/__init__.py +7 -0
- chromapi/locomotion/balance.py +284 -0
- chromapi/locomotion/gait.py +595 -0
- chromapi/locomotion/recorded_move.py +290 -0
- chromapi/media/__init__.py +2 -0
- chromapi/media/audio.py +424 -0
- chromapi/media/led.py +461 -0
- chromapi/model/LICENSE +438 -0
- chromapi/model/mjcf/additional.xml +21 -0
- chromapi/model/mjcf/assets/18650_li_ion_battery.msh +0 -0
- chromapi/model/mjcf/assets/18650_li_ion_battery.part +13 -0
- chromapi/model/mjcf/assets/battery_holder.msh +0 -0
- chromapi/model/mjcf/assets/battery_holder.part +13 -0
- chromapi/model/mjcf/assets/camera_imx219_d160.msh +0 -0
- chromapi/model/mjcf/assets/camera_imx219_d160.part +13 -0
- chromapi/model/mjcf/assets/chromapi_logo.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo.part +13 -0
- chromapi/model/mjcf/assets/chromapi_logo_circle.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo_inner.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo_outer.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_motherboard.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_motherboard.part +13 -0
- chromapi/model/mjcf/assets/composant2_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant2_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/composant3_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant3_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/composant4_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant4_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1_1.msh +0 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1_1.part +13 -0
- chromapi/model/mjcf/assets/corps14.msh +0 -0
- chromapi/model/mjcf/assets/corps14.part +13 -0
- chromapi/model/mjcf/assets/fan_nf_a4x10.msh +0 -0
- chromapi/model/mjcf/assets/fan_nf_a4x10.part +13 -0
- chromapi/model/mjcf/assets/foot.msh +0 -0
- chromapi/model/mjcf/assets/foot.part +13 -0
- chromapi/model/mjcf/assets/left_coxa.msh +0 -0
- chromapi/model/mjcf/assets/left_coxa.part +13 -0
- chromapi/model/mjcf/assets/left_femur.msh +0 -0
- chromapi/model/mjcf/assets/left_femur.part +13 -0
- chromapi/model/mjcf/assets/left_tibia.msh +0 -0
- chromapi/model/mjcf/assets/left_tibia.part +13 -0
- chromapi/model/mjcf/assets/microswitch.msh +0 -0
- chromapi/model/mjcf/assets/microswitch.part +13 -0
- chromapi/model/mjcf/assets/power_switch.msh +0 -0
- chromapi/model/mjcf/assets/power_switch.part +13 -0
- chromapi/model/mjcf/assets/raspberry_pi_4_model_b.msh +0 -0
- chromapi/model/mjcf/assets/raspberry_pi_4_model_b.part +13 -0
- chromapi/model/mjcf/assets/right_coxa.msh +0 -0
- chromapi/model/mjcf/assets/right_coxa.part +13 -0
- chromapi/model/mjcf/assets/right_femur.msh +0 -0
- chromapi/model/mjcf/assets/right_femur.part +13 -0
- chromapi/model/mjcf/assets/right_tibia.msh +0 -0
- chromapi/model/mjcf/assets/right_tibia.part +13 -0
- chromapi/model/mjcf/assets/robot_case_bottom.msh +0 -0
- chromapi/model/mjcf/assets/robot_case_bottom.part +13 -0
- chromapi/model/mjcf/assets/robot_case_top.msh +0 -0
- chromapi/model/mjcf/assets/robot_case_top.part +13 -0
- chromapi/model/mjcf/assets/rpi_battery_bracket.msh +0 -0
- chromapi/model/mjcf/assets/rpi_battery_bracket.part +13 -0
- chromapi/model/mjcf/assets/speaker.msh +0 -0
- chromapi/model/mjcf/assets/speaker.part +13 -0
- chromapi/model/mjcf/assets/vis_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/vis_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/vis_1__corps1_1.msh +0 -0
- chromapi/model/mjcf/assets/vis_1__corps1_1.part +13 -0
- chromapi/model/mjcf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/ws2812b_cob_pixel_ring_27mm.msh +0 -0
- chromapi/model/mjcf/assets/ws2812b_cob_pixel_ring_27mm.part +13 -0
- chromapi/model/mjcf/config.json +14 -0
- chromapi/model/mjcf/robot.xml +639 -0
- chromapi/model/mjcf/scene.xml +24 -0
- chromapi/model/urdf/assets/18650_li_ion_battery.part +13 -0
- chromapi/model/urdf/assets/18650_li_ion_battery.stl +0 -0
- chromapi/model/urdf/assets/battery_holder.part +13 -0
- chromapi/model/urdf/assets/battery_holder.stl +0 -0
- chromapi/model/urdf/assets/camera_imx219_d160.part +13 -0
- chromapi/model/urdf/assets/camera_imx219_d160.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo.part +13 -0
- chromapi/model/urdf/assets/chromapi_logo.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_circle.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_inner.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_outer.stl +0 -0
- chromapi/model/urdf/assets/chromapi_motherboard.part +13 -0
- chromapi/model/urdf/assets/chromapi_motherboard.stl +0 -0
- chromapi/model/urdf/assets/composant2_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant2_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/composant3_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant3_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/composant4_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant4_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/connector_f_1__corps1.part +13 -0
- chromapi/model/urdf/assets/connector_f_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/connector_f_1__corps1_1.part +13 -0
- chromapi/model/urdf/assets/connector_f_1__corps1_1.stl +0 -0
- chromapi/model/urdf/assets/corps14.part +13 -0
- chromapi/model/urdf/assets/corps14.stl +0 -0
- chromapi/model/urdf/assets/fan_nf_a4x10.part +13 -0
- chromapi/model/urdf/assets/fan_nf_a4x10.stl +0 -0
- chromapi/model/urdf/assets/foot.part +13 -0
- chromapi/model/urdf/assets/foot.stl +0 -0
- chromapi/model/urdf/assets/left_coxa.part +13 -0
- chromapi/model/urdf/assets/left_coxa.stl +0 -0
- chromapi/model/urdf/assets/left_femur.part +13 -0
- chromapi/model/urdf/assets/left_femur.stl +0 -0
- chromapi/model/urdf/assets/left_tibia.part +13 -0
- chromapi/model/urdf/assets/left_tibia.stl +0 -0
- chromapi/model/urdf/assets/microswitch.part +13 -0
- chromapi/model/urdf/assets/microswitch.stl +0 -0
- chromapi/model/urdf/assets/power_switch.part +13 -0
- chromapi/model/urdf/assets/power_switch.stl +0 -0
- chromapi/model/urdf/assets/raspberry_pi_4_model_b.part +13 -0
- chromapi/model/urdf/assets/raspberry_pi_4_model_b.stl +0 -0
- chromapi/model/urdf/assets/right_coxa.part +13 -0
- chromapi/model/urdf/assets/right_coxa.stl +0 -0
- chromapi/model/urdf/assets/right_femur.part +13 -0
- chromapi/model/urdf/assets/right_femur.stl +0 -0
- chromapi/model/urdf/assets/right_tibia.part +13 -0
- chromapi/model/urdf/assets/right_tibia.stl +0 -0
- chromapi/model/urdf/assets/robot_case_bottom.part +13 -0
- chromapi/model/urdf/assets/robot_case_bottom.stl +0 -0
- chromapi/model/urdf/assets/robot_case_top.part +13 -0
- chromapi/model/urdf/assets/robot_case_top.stl +0 -0
- chromapi/model/urdf/assets/rpi_battery_bracket.part +13 -0
- chromapi/model/urdf/assets/rpi_battery_bracket.stl +0 -0
- chromapi/model/urdf/assets/speaker.part +13 -0
- chromapi/model/urdf/assets/speaker.stl +0 -0
- chromapi/model/urdf/assets/vis_1__corps1.part +13 -0
- chromapi/model/urdf/assets/vis_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/vis_1__corps1_1.part +13 -0
- chromapi/model/urdf/assets/vis_1__corps1_1.stl +0 -0
- chromapi/model/urdf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.part +13 -0
- chromapi/model/urdf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/ws2812b_cob_pixel_ring_27mm.part +13 -0
- chromapi/model/urdf/assets/ws2812b_cob_pixel_ring_27mm.stl +0 -0
- chromapi/model/urdf/config.json +8 -0
- chromapi/model/urdf/robot.urdf +2587 -0
- chromapi/utils/calibrate_imu_mount.py +80 -0
- chromapi/utils/calibrate_servo_centers.py +432 -0
- chromapi/utils/flash.py +141 -0
- chromapi/utils/health.py +210 -0
- chromapi/utils/report.py +75 -0
- chromapi/utils/setup_motors.py +189 -0
- chromapi/utils/sim_server.py +126 -0
- chromapi/web/__init__.py +2 -0
- chromapi/web/access.py +167 -0
- chromapi/web/app.py +1136 -0
- chromapi/web/camera.py +202 -0
- chromapi/web/static/fonts/varela-round.woff2 +0 -0
- chromapi/web/static/index.html +1079 -0
- chromapi/web/static/logo.png +0 -0
- chromapi/web/static/motherboard_front.png +0 -0
- chromapi/web/static/socket.io.min.js +7 -0
- chromapi-1.0.0.dist-info/METADATA +222 -0
- chromapi-1.0.0.dist-info/RECORD +219 -0
- chromapi-1.0.0.dist-info/WHEEL +5 -0
- chromapi-1.0.0.dist-info/entry_points.txt +2 -0
- chromapi-1.0.0.dist-info/licenses/LICENSE +201 -0
- chromapi-1.0.0.dist-info/licenses/src/chromapi/model/LICENSE +438 -0
- chromapi-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Gait engine for Chromapi."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from dataclasses import dataclass, field, replace
|
|
7
|
+
from typing import Dict, Optional, Tuple
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
import numpy.typing as npt
|
|
11
|
+
|
|
12
|
+
from chromapi.chromapi import MotorCommand, Move, RobotState, _minimum_jerk
|
|
13
|
+
from chromapi.kinematics.kromatics import Kromatics
|
|
14
|
+
from chromapi.kinematics.topology import LEG_NAMES
|
|
15
|
+
from chromapi.kinematics.types import FootTargets, JointDict, Vector3
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"WALK_POSTURE",
|
|
19
|
+
"GaitEngine",
|
|
20
|
+
"GaitParams",
|
|
21
|
+
"WalkMove",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
WALK_POSTURE: Tuple[float, float, float] = (0.09, 0.11, 0.12) # (x_reach, y_reach, body_height) [m]
|
|
25
|
+
|
|
26
|
+
# Trot: diagonal pairs (tl+br), (tr+bl).
|
|
27
|
+
_TROT_PHASE_OFFSETS: Dict[str, float] = {"tl": 0.0, "br": 0.0, "tr": 0.5, "bl": 0.5}
|
|
28
|
+
|
|
29
|
+
# Crawl gait order: bl -> tl -> br -> tr.
|
|
30
|
+
_CRAWL_PHASE_OFFSETS: Dict[str, float] = {"bl": 0.75, "tl": 0.5, "br": 0.25, "tr": 0.0}
|
|
31
|
+
|
|
32
|
+
# A small epsilon for angular velocity to avoid divide-by-zero in integration
|
|
33
|
+
_OMEGA_EPS = 1e-4 # [rad.s⁻¹] on a yaw rate, [rad] on a yaw step
|
|
34
|
+
|
|
35
|
+
# Gait pattern heading in the odometric frame (chassis +X = forward)
|
|
36
|
+
_PATTERN_HEADING = 0.0 # [rad]
|
|
37
|
+
|
|
38
|
+
# Linear speed (fraction of max_speed_mps) above which the travel direction picks the leg order
|
|
39
|
+
_ORDER_SPEED_FRACTION = 0.2
|
|
40
|
+
|
|
41
|
+
# Extra angle past the 45 deg quadrant boundary before the leg order switches
|
|
42
|
+
_ORDER_HYSTERESIS_RAD = np.radians(10.0) # [rad]
|
|
43
|
+
|
|
44
|
+
# How far a stance foot may drift out of its nominal footprint before the body waits for it to swing back in
|
|
45
|
+
_STANCE_DRIFT_SLACK = 1.2
|
|
46
|
+
|
|
47
|
+
# Samples used to average the velocity ramp over an upcoming stance.
|
|
48
|
+
_PLAN_SAMPLES = 10
|
|
49
|
+
|
|
50
|
+
# Cap on how fast a re-planned touch-down point may move
|
|
51
|
+
_REPLAN_SPEED_FACTOR = 2.0
|
|
52
|
+
|
|
53
|
+
#: Once stopped, a foot this close to its neutral footprint [m] counts as settled (see GaitEngine._settled).
|
|
54
|
+
_SETTLED_TOLERANCE_M = 0.005
|
|
55
|
+
|
|
56
|
+
#: ... and a swinging foot this low [m] counts as down: the crawl always has one leg in swing.
|
|
57
|
+
_SETTLED_CLEARANCE_M = 0.0005
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _quintic(tau: float) -> float:
|
|
61
|
+
"""Normalized quintic polynomial time-scaling (``tau`` normalized time in [0, 1])."""
|
|
62
|
+
t = float(np.clip(tau, 0.0, 1.0))
|
|
63
|
+
return 6.0 * t**5 - 15.0 * t**4 + 10.0 * t**3
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _swing_apex_profile(tau: float) -> float:
|
|
67
|
+
"""Ground-clearance profile."""
|
|
68
|
+
t = float(np.clip(tau, 0.0, 1.0))
|
|
69
|
+
return _quintic(2.0 * t) if t <= 0.5 else _quintic(2.0 - 2.0 * t)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _rotate2(vec_xy: npt.NDArray[np.float64], angle: float) -> npt.NDArray[np.float64]:
|
|
73
|
+
"""Rotate a 2-vector by ``angle`` [rad] (counterclockwise, +Z-up)."""
|
|
74
|
+
c, s = np.cos(angle), np.sin(angle)
|
|
75
|
+
return np.array([c * vec_xy[0] - s * vec_xy[1], s * vec_xy[0] + c * vec_xy[1]])
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _integrate_se2(
|
|
79
|
+
xy: npt.NDArray[np.float64], yaw: float, vx: float, vy: float, wz: float, dt: float
|
|
80
|
+
) -> Tuple[npt.NDArray[np.float64], float]:
|
|
81
|
+
"""Integrate a planar odometric pose (``xy`` [m], ``yaw`` [rad]) by one body-frame twist step.
|
|
82
|
+
|
|
83
|
+
``vx``, ``vy`` [m.s⁻¹], ``wz`` [rad.s⁻¹], ``dt`` [s].
|
|
84
|
+
"""
|
|
85
|
+
d_yaw = wz * dt # [rad]
|
|
86
|
+
if abs(d_yaw) < _OMEGA_EPS:
|
|
87
|
+
d_body = np.array([vx, vy]) * dt # [m]
|
|
88
|
+
else:
|
|
89
|
+
d_body = np.array(
|
|
90
|
+
[
|
|
91
|
+
(vx * np.sin(d_yaw) + vy * (np.cos(d_yaw) - 1.0)) / wz,
|
|
92
|
+
(vx * (1.0 - np.cos(d_yaw)) + vy * np.sin(d_yaw)) / wz,
|
|
93
|
+
]
|
|
94
|
+
)
|
|
95
|
+
new_xy = xy + _rotate2(d_body, yaw) # [m]
|
|
96
|
+
new_yaw = yaw + d_yaw # [rad]
|
|
97
|
+
return new_xy, new_yaw
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass
|
|
101
|
+
class GaitParams:
|
|
102
|
+
"""Gait Parameters.
|
|
103
|
+
|
|
104
|
+
Attributes:
|
|
105
|
+
pattern: Gait pattern name, ``"crawl"`` or ``"trot"``.
|
|
106
|
+
step_frequency: Gait cycle frequency [Hz].
|
|
107
|
+
duty_factor: Fraction of the cycle each leg spends in stance. Needs to be at least 0.75 for guaranteed 3-leg support at all times.
|
|
108
|
+
swing_height: Apex ground clearance during swing [m].
|
|
109
|
+
body_height: Nominal hip-to-foot drop [m].
|
|
110
|
+
x_reach: Nominal footprint half-extent along chassis X [m].
|
|
111
|
+
y_reach: Nominal footprint half-extent along chassis Y [m].
|
|
112
|
+
phase_offsets: Phase offsets for each leg, in [0, 1[.
|
|
113
|
+
workspace_reach_m: Usable leg workspace (stride length), defined by hardware limitations [m].
|
|
114
|
+
max_acceleration_mps2: Linear-velocity ramp limit [m.s⁻²].
|
|
115
|
+
max_angular_acceleration_rad_s2: Angular-velocity ramp limit [rad.s⁻²].
|
|
116
|
+
diagonal_speed_ratio: Diagonal speed limit, ]0.5, 1.0].
|
|
117
|
+
ik_iterations: Number of iterations for the QP solver.
|
|
118
|
+
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
pattern: str = "crawl"
|
|
122
|
+
step_frequency: float = 0.45 # [Hz]
|
|
123
|
+
duty_factor: float = 0.75
|
|
124
|
+
swing_height: float = 0.018 # [m]
|
|
125
|
+
body_height: float = WALK_POSTURE[2] # [m]
|
|
126
|
+
x_reach: float = WALK_POSTURE[0] # [m]
|
|
127
|
+
y_reach: float = WALK_POSTURE[1] # [m]
|
|
128
|
+
phase_offsets: Dict[str, float] = field(default_factory=lambda: dict(_CRAWL_PHASE_OFFSETS))
|
|
129
|
+
workspace_reach_m: float = 0.116 # [m]
|
|
130
|
+
max_acceleration_mps2: float = 0.15 # [m.s⁻²]
|
|
131
|
+
max_angular_acceleration_rad_s2: float = 1.0 # [rad.s⁻²]
|
|
132
|
+
diagonal_speed_ratio: float = 0.78
|
|
133
|
+
ik_iterations: Optional[int] = None
|
|
134
|
+
|
|
135
|
+
def __post_init__(self) -> None:
|
|
136
|
+
"""Validate ``phase_offsets`` and ``duty_factor``."""
|
|
137
|
+
missing = set(LEG_NAMES) - set(self.phase_offsets)
|
|
138
|
+
if missing:
|
|
139
|
+
raise ValueError(f"GaitParams.phase_offsets is missing legs: {sorted(missing)}")
|
|
140
|
+
if not 0.0 < self.duty_factor < 1.0:
|
|
141
|
+
raise ValueError(f"GaitParams.duty_factor must be in (0, 1), got {self.duty_factor}")
|
|
142
|
+
|
|
143
|
+
@classmethod
|
|
144
|
+
def crawl(cls) -> "GaitParams":
|
|
145
|
+
"""Crawl tuning for stable walking. Move one leg at a time."""
|
|
146
|
+
return cls()
|
|
147
|
+
|
|
148
|
+
@classmethod
|
|
149
|
+
def trot(cls) -> "GaitParams":
|
|
150
|
+
"""Trot tuning for faster walking. Move diagonal leg pairs at a time."""
|
|
151
|
+
return cls(
|
|
152
|
+
pattern="trot",
|
|
153
|
+
step_frequency=1.2,
|
|
154
|
+
duty_factor=0.61,
|
|
155
|
+
swing_height=0.021,
|
|
156
|
+
body_height=WALK_POSTURE[2],
|
|
157
|
+
x_reach=WALK_POSTURE[0],
|
|
158
|
+
y_reach=WALK_POSTURE[1],
|
|
159
|
+
phase_offsets=dict(_TROT_PHASE_OFFSETS),
|
|
160
|
+
workspace_reach_m=0.118,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def cycle_period_s(self) -> float:
|
|
165
|
+
r"""Walk cycle period [s]."""
|
|
166
|
+
return 1.0 / self.step_frequency
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def stance_duration_s(self) -> float:
|
|
170
|
+
r"""Stance duration [s]."""
|
|
171
|
+
return self.duty_factor * self.cycle_period_s
|
|
172
|
+
|
|
173
|
+
@property
|
|
174
|
+
def max_speed_mps(self) -> float:
|
|
175
|
+
"""Max sustainable body speed [m.s⁻¹]."""
|
|
176
|
+
return self.workspace_reach_m / self.stance_duration_s
|
|
177
|
+
|
|
178
|
+
@property
|
|
179
|
+
def max_yaw_rate_rad_s(self) -> float:
|
|
180
|
+
"""Max sustainable yaw rate [rad.s⁻¹]."""
|
|
181
|
+
radius = max(float(np.hypot(self.x_reach, self.y_reach)), 1e-6) # [m]
|
|
182
|
+
return self.workspace_reach_m / (self.stance_duration_s * radius)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@dataclass
|
|
186
|
+
class _LegSwingState:
|
|
187
|
+
"""Per-leg swing bookkeeping - odometric-frame (x, y, z) points."""
|
|
188
|
+
|
|
189
|
+
lift_off: Vector3 # [m]
|
|
190
|
+
touch_down: Vector3 # [m]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
class GaitEngine:
|
|
194
|
+
"""Gait algorithm implementation."""
|
|
195
|
+
|
|
196
|
+
def __init__(self, kinematics: Kromatics, params: Optional[GaitParams] = None) -> None:
|
|
197
|
+
"""Build the engine and reset it to a stand still pose."""
|
|
198
|
+
self.kinematics = kinematics
|
|
199
|
+
self.params = params or GaitParams.trot()
|
|
200
|
+
self.reset()
|
|
201
|
+
|
|
202
|
+
# -- setup / state ------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
def reset(self) -> None:
|
|
205
|
+
"""Return to a stand still pose."""
|
|
206
|
+
self._nominal_footprint: FootTargets = self.kinematics.stance_targets( # [m]
|
|
207
|
+
self.params.body_height, self.params.x_reach, self.params.y_reach
|
|
208
|
+
)
|
|
209
|
+
# Twists are about the footprint center, not the chassis origin (offset from it).
|
|
210
|
+
self._pivot = np.mean([p[:2] for p in self._nominal_footprint.values()], axis=0) # [m]
|
|
211
|
+
self._t = 0.0 # [s]
|
|
212
|
+
self._odom_xy = np.zeros(2) # [m]
|
|
213
|
+
self._odom_yaw = 0.0 # [rad]
|
|
214
|
+
self._target_twist = np.zeros(3) # (vx, vy, wz) desired [m.s⁻¹, m.s⁻¹, rad.s⁻¹]
|
|
215
|
+
self._twist = np.zeros(3) # (vx, vy, wz) actual [m.s⁻¹, m.s⁻¹, rad.s⁻¹]
|
|
216
|
+
self._swing: Dict[str, _LegSwingState] = {
|
|
217
|
+
leg: _LegSwingState(lift_off=pos.copy(), touch_down=pos.copy())
|
|
218
|
+
for leg, pos in self._nominal_footprint.items()
|
|
219
|
+
}
|
|
220
|
+
self._roles_by_turn = self._quarter_turn_roles()
|
|
221
|
+
self._turn = 0
|
|
222
|
+
self._phase_shift = 0.0
|
|
223
|
+
self._prev_in_stance: Dict[str, bool] = {
|
|
224
|
+
leg: self._phase(leg, 0.0) < self.params.duty_factor for leg in LEG_NAMES
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
def set_body_height(self, body_height: float) -> None:
|
|
228
|
+
"""Change ``params.body_height`` live, without :meth:`reset` (keeps phase and foot anchors).
|
|
229
|
+
|
|
230
|
+
Every stored foot z (nominal footprint, swing anchors) moves by the height change; keep
|
|
231
|
+
each call small (ramp it), since the joints follow within a single tick.
|
|
232
|
+
"""
|
|
233
|
+
dz = body_height - self.params.body_height # [m]
|
|
234
|
+
points = list(self._nominal_footprint.values())
|
|
235
|
+
points += [p for s in self._swing.values() for p in (s.lift_off, s.touch_down)]
|
|
236
|
+
for p in {id(p): p for p in points}.values():
|
|
237
|
+
p[2] -= dz
|
|
238
|
+
self.params = replace(self.params, body_height=body_height)
|
|
239
|
+
|
|
240
|
+
def set_velocity(self, vx: float, vy: float, wz: float) -> None:
|
|
241
|
+
"""Set the desired body-frame twist, clamped to :class:`GaitParams`'s speed limits.
|
|
242
|
+
|
|
243
|
+
``vx``, ``vy`` [m.s⁻¹] and ``wz`` [rad.s⁻¹], about the footprint center.
|
|
244
|
+
"""
|
|
245
|
+
ratio = float(np.clip(self.params.diagonal_speed_ratio, 0.5, 1.0))
|
|
246
|
+
p = 1.0 / (0.5 + np.log2(1.0 / ratio))
|
|
247
|
+
speed = float((abs(vx) ** p + abs(vy) ** p) ** (1.0 / p)) # [m.s⁻¹]
|
|
248
|
+
if speed > self.params.max_speed_mps and speed > 0.0:
|
|
249
|
+
scale = self.params.max_speed_mps / speed
|
|
250
|
+
vx *= scale
|
|
251
|
+
vy *= scale
|
|
252
|
+
max_wz = self.max_yaw_rate_rad_s # [rad.s⁻¹]
|
|
253
|
+
wz = float(np.clip(wz, -max_wz, max_wz))
|
|
254
|
+
foot_speed = max( # [m.s⁻¹]
|
|
255
|
+
float(np.hypot(vx - wz * (p[1] - self._pivot[1]), vy + wz * (p[0] - self._pivot[0])))
|
|
256
|
+
for p in self._nominal_footprint.values()
|
|
257
|
+
)
|
|
258
|
+
if foot_speed > self.params.max_speed_mps:
|
|
259
|
+
scale = self.params.max_speed_mps / foot_speed
|
|
260
|
+
vx, vy, wz = vx * scale, vy * scale, wz * scale
|
|
261
|
+
self._target_twist = np.array([vx, vy, wz])
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
def twist(self) -> npt.NDArray[np.float64]:
|
|
265
|
+
"""Actual (ramped) body-frame twist (vx, vy, wz) [m.s⁻¹, m.s⁻¹, rad.s⁻¹]."""
|
|
266
|
+
return self._twist.copy()
|
|
267
|
+
|
|
268
|
+
@property
|
|
269
|
+
def max_yaw_rate_rad_s(self) -> float:
|
|
270
|
+
"""Max sustainable yaw rate about the footprint center [rad.s⁻¹]."""
|
|
271
|
+
radius = max( # [m]
|
|
272
|
+
float(np.linalg.norm(p[:2] - self._pivot)) for p in self._nominal_footprint.values()
|
|
273
|
+
)
|
|
274
|
+
own = self.params.workspace_reach_m / (self.params.stance_duration_s * max(radius, 1e-6))
|
|
275
|
+
return min(own, self.params.max_yaw_rate_rad_s)
|
|
276
|
+
|
|
277
|
+
def _origin_twist(self, twist: Optional[npt.NDArray[np.float64]] = None) -> Tuple[float, float, float]:
|
|
278
|
+
"""Twist [m.s⁻¹, m.s⁻¹, rad.s⁻¹], moved from the footprint center to the chassis origin."""
|
|
279
|
+
vx, vy, wz = self._twist if twist is None else twist
|
|
280
|
+
return vx + wz * self._pivot[1], vy - wz * self._pivot[0], wz
|
|
281
|
+
|
|
282
|
+
# -- phase ----------------------------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
def _phase(self, leg: str, t: float, turn: Optional[int] = None, shift: Optional[float] = None) -> float:
|
|
285
|
+
"""Phase variable, in [0,1[, at time ``t`` [s]."""
|
|
286
|
+
turn = self._turn if turn is None else turn
|
|
287
|
+
shift = self._phase_shift if shift is None else shift
|
|
288
|
+
role = self._roles_by_turn[turn][leg]
|
|
289
|
+
cycles = t / self.params.cycle_period_s + self.params.phase_offsets[role] + shift
|
|
290
|
+
return float(cycles % 1.0)
|
|
291
|
+
|
|
292
|
+
# -- direction-dependent leg order ----------------------------------------------------
|
|
293
|
+
|
|
294
|
+
def _quarter_turn_roles(self) -> Dict[int, Dict[str, str]]:
|
|
295
|
+
"""For k quarter turns, map each leg to the leg whose phase offset it takes."""
|
|
296
|
+
center = np.mean([p[:2] for p in self._nominal_footprint.values()], axis=0) # [m]
|
|
297
|
+
rel = {leg: p[:2] - center for leg, p in self._nominal_footprint.items()} # [m]
|
|
298
|
+
roles: Dict[int, Dict[str, str]] = {}
|
|
299
|
+
for k in range(4):
|
|
300
|
+
roles[k] = {}
|
|
301
|
+
for leg in LEG_NAMES:
|
|
302
|
+
source = _rotate2(rel[leg], -k * np.pi / 2) # [m]
|
|
303
|
+
roles[k][leg] = min(
|
|
304
|
+
LEG_NAMES, key=lambda other: float(np.linalg.norm(rel[other] - source))
|
|
305
|
+
)
|
|
306
|
+
return roles
|
|
307
|
+
|
|
308
|
+
def _wanted_turn(self) -> int:
|
|
309
|
+
"""Quarter turn matching the actual travel direction."""
|
|
310
|
+
vx, vy, _ = self._twist # [m.s⁻¹]
|
|
311
|
+
if np.hypot(vx, vy) < _ORDER_SPEED_FRACTION * self.params.max_speed_mps:
|
|
312
|
+
return self._turn
|
|
313
|
+
rel = float(np.arctan2(vy, vx)) - _PATTERN_HEADING # [rad]
|
|
314
|
+
current_heading = self._turn * np.pi / 2 # [rad]
|
|
315
|
+
off = (rel - current_heading + np.pi) % (2 * np.pi) - np.pi # [rad]
|
|
316
|
+
if abs(off) <= np.pi / 4 + _ORDER_HYSTERESIS_RAD:
|
|
317
|
+
return self._turn
|
|
318
|
+
return int(np.round(rel / (np.pi / 2))) % 4
|
|
319
|
+
|
|
320
|
+
def _update_leg_order(self) -> None:
|
|
321
|
+
"""Switch to the leg order of the travel direction, without a jump in any swing.
|
|
322
|
+
|
|
323
|
+
The new order comes with a phase shift chosen so that a swinging leg keeps its phase
|
|
324
|
+
and every stance leg stays in stance.
|
|
325
|
+
"""
|
|
326
|
+
turn = self._wanted_turn()
|
|
327
|
+
if turn == self._turn:
|
|
328
|
+
return
|
|
329
|
+
duty = self.params.duty_factor
|
|
330
|
+
tol = 1e-9
|
|
331
|
+
current = {leg: self._phase(leg, self._t) for leg in LEG_NAMES}
|
|
332
|
+
swinging = [leg for leg in LEG_NAMES if current[leg] >= duty]
|
|
333
|
+
for pivot in swinging or LEG_NAMES:
|
|
334
|
+
shift = self._phase_shift + current[pivot] - self._phase(pivot, self._t, turn=turn)
|
|
335
|
+
new = {leg: self._phase(leg, self._t, turn=turn, shift=shift) for leg in LEG_NAMES}
|
|
336
|
+
ok = all(
|
|
337
|
+
abs((new[leg] - current[leg] + 0.5) % 1.0 - 0.5) < tol
|
|
338
|
+
if leg in swinging
|
|
339
|
+
else new[leg] < duty
|
|
340
|
+
for leg in LEG_NAMES
|
|
341
|
+
)
|
|
342
|
+
if ok:
|
|
343
|
+
self._turn, self._phase_shift = turn, shift % 1.0
|
|
344
|
+
return
|
|
345
|
+
|
|
346
|
+
# -- odometric-frame <-> chassis-frame ------------------------------------------------
|
|
347
|
+
|
|
348
|
+
def _chassis_from_odom(
|
|
349
|
+
self, p_odom: Vector3, pose: Optional[Tuple[npt.NDArray[np.float64], float]] = None
|
|
350
|
+
) -> Vector3:
|
|
351
|
+
"""Convert an odometric-frame point [m] to the chassis frame (current, or at ``pose``)."""
|
|
352
|
+
odom_xy, odom_yaw = pose if pose is not None else (self._odom_xy, self._odom_yaw) # [m], [rad]
|
|
353
|
+
xy = _rotate2(p_odom[:2] - odom_xy, -odom_yaw) # [m]
|
|
354
|
+
return np.array([xy[0], xy[1], p_odom[2]])
|
|
355
|
+
|
|
356
|
+
def _odom_from_chassis(
|
|
357
|
+
self, p_chassis: Vector3, pose: Optional[Tuple[npt.NDArray[np.float64], float]] = None
|
|
358
|
+
) -> Vector3:
|
|
359
|
+
"""Convert a chassis-frame point [m] (current chassis, or at ``pose``) to the odometric frame."""
|
|
360
|
+
odom_xy, odom_yaw = pose if pose is not None else (self._odom_xy, self._odom_yaw) # [m], [rad]
|
|
361
|
+
xy = odom_xy + _rotate2(p_chassis[:2], odom_yaw) # [m]
|
|
362
|
+
return np.array([xy[0], xy[1], p_chassis[2]])
|
|
363
|
+
|
|
364
|
+
# -- foothold placement -------------- ------------------------------------------------
|
|
365
|
+
|
|
366
|
+
def _touchdown_liftoff(
|
|
367
|
+
self, leg: str, twist: Optional[npt.NDArray[np.float64]] = None
|
|
368
|
+
) -> Tuple[Vector3, Vector3]:
|
|
369
|
+
"""Symmetric lift-off/touch-down targets [m] around this leg's nominal footprint (in chassis frame)."""
|
|
370
|
+
vx, vy, wz = self._origin_twist(twist) # [m.s⁻¹], [m.s⁻¹], [rad.s⁻¹]
|
|
371
|
+
nominal = self._nominal_footprint[leg] # [m]
|
|
372
|
+
half_stance = self.params.stance_duration_s / 2.0 # [s]
|
|
373
|
+
|
|
374
|
+
if abs(wz) < _OMEGA_EPS:
|
|
375
|
+
delta = np.array([vx, vy]) * half_stance # [m]
|
|
376
|
+
touch_down_xy = nominal[:2] + delta # [m]
|
|
377
|
+
lift_off_xy = nominal[:2] - delta # [m]
|
|
378
|
+
else:
|
|
379
|
+
center = (1.0 / wz) * np.array([-vy, vx]) # [m]
|
|
380
|
+
angle = wz * half_stance # [rad]
|
|
381
|
+
touch_down_xy = center + _rotate2(nominal[:2] - center, angle)
|
|
382
|
+
lift_off_xy = center + _rotate2(nominal[:2] - center, -angle)
|
|
383
|
+
|
|
384
|
+
touch_down = np.array([touch_down_xy[0], touch_down_xy[1], nominal[2]])
|
|
385
|
+
lift_off = np.array([lift_off_xy[0], lift_off_xy[1], nominal[2]])
|
|
386
|
+
return lift_off, touch_down
|
|
387
|
+
|
|
388
|
+
def _planned_stance_twist(self, delay_s: float) -> npt.NDArray[np.float64]:
|
|
389
|
+
"""Mean twist [m.s⁻¹, m.s⁻¹, rad.s⁻¹] over a stance starting in ``delay_s`` [s], following the velocity ramp."""
|
|
390
|
+
twist = self._twist.copy() # [m.s⁻¹, m.s⁻¹, rad.s⁻¹]
|
|
391
|
+
dt = delay_s / _PLAN_SAMPLES # [s]
|
|
392
|
+
for _ in range(_PLAN_SAMPLES):
|
|
393
|
+
twist = self._ramped(twist, dt)
|
|
394
|
+
dt = self.params.stance_duration_s / _PLAN_SAMPLES # [s]
|
|
395
|
+
total = np.zeros(3) # [m.s⁻¹, m.s⁻¹, rad.s⁻¹]
|
|
396
|
+
for _ in range(_PLAN_SAMPLES):
|
|
397
|
+
nxt = self._ramped(twist, dt)
|
|
398
|
+
total += 0.5 * (twist + nxt)
|
|
399
|
+
twist = nxt
|
|
400
|
+
return total / _PLAN_SAMPLES
|
|
401
|
+
|
|
402
|
+
def _stance_speed_scale(self, dt: float) -> float:
|
|
403
|
+
"""Largest fraction of the current twist that keeps every stance foot in its workspace over ``dt`` [s]."""
|
|
404
|
+
limit = _STANCE_DRIFT_SLACK * 0.5 * self.params.workspace_reach_m # [m]
|
|
405
|
+
stance = [leg for leg in LEG_NAMES if self._prev_in_stance[leg]]
|
|
406
|
+
twist = np.array(self._origin_twist()) # [m.s⁻¹, m.s⁻¹, rad.s⁻¹]
|
|
407
|
+
now = { # [m]
|
|
408
|
+
leg: float(np.linalg.norm(
|
|
409
|
+
self._chassis_from_odom(self._swing[leg].touch_down)[:2]
|
|
410
|
+
- self._nominal_footprint[leg][:2]
|
|
411
|
+
))
|
|
412
|
+
for leg in stance
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
def ok(scale: float) -> bool:
|
|
416
|
+
"""Check if the stance feet stay within the workspace when moving at ``scale * twist`` for ``dt`` seconds."""
|
|
417
|
+
pose = _integrate_se2(
|
|
418
|
+
self._odom_xy,
|
|
419
|
+
self._odom_yaw,
|
|
420
|
+
float(scale * twist[0]),
|
|
421
|
+
float(scale * twist[1]),
|
|
422
|
+
float(scale * twist[2]),
|
|
423
|
+
dt,
|
|
424
|
+
) # [m], [rad]
|
|
425
|
+
for leg in stance:
|
|
426
|
+
p = self._chassis_from_odom(self._swing[leg].touch_down, pose) # [m]
|
|
427
|
+
drift = float(np.linalg.norm(p[:2] - self._nominal_footprint[leg][:2])) # [m]
|
|
428
|
+
if drift > limit and drift > now[leg]:
|
|
429
|
+
return False
|
|
430
|
+
return True
|
|
431
|
+
|
|
432
|
+
if ok(1.0):
|
|
433
|
+
return 1.0
|
|
434
|
+
lo, hi = 0.0, 1.0
|
|
435
|
+
for _ in range(8):
|
|
436
|
+
mid = 0.5 * (lo + hi)
|
|
437
|
+
lo, hi = (mid, hi) if ok(mid) else (lo, mid)
|
|
438
|
+
return lo
|
|
439
|
+
|
|
440
|
+
# -- solving --------------------------------------------------------------------------
|
|
441
|
+
|
|
442
|
+
def _solve_ik(self, foot_targets: FootTargets) -> JointDict:
|
|
443
|
+
"""Whole-body QP IK for the 4 feet."""
|
|
444
|
+
q, _ = self.kinematics.inverse_kinematics(
|
|
445
|
+
foot_targets, iterations=self.params.ik_iterations
|
|
446
|
+
)
|
|
447
|
+
return q
|
|
448
|
+
|
|
449
|
+
# -- main tick --------------------------------------------------------------------------
|
|
450
|
+
|
|
451
|
+
def step(self, dt: float) -> JointDict:
|
|
452
|
+
"""Advance the gait by ``dt`` [s] and return the resulting 12 joint angles [rad]."""
|
|
453
|
+
self._twist = self._ramped(self._twist, dt)
|
|
454
|
+
if self._settled():
|
|
455
|
+
dt = 0.0 # stand still rather than step in place; the clock resumes with the next velocity
|
|
456
|
+
scale = self._stance_speed_scale(dt)
|
|
457
|
+
vx, vy, wz = (scale * c for c in self._origin_twist()) # [m.s⁻¹], [m.s⁻¹], [rad.s⁻¹]
|
|
458
|
+
self._odom_xy, self._odom_yaw = _integrate_se2(
|
|
459
|
+
self._odom_xy, self._odom_yaw, vx, vy, wz, dt
|
|
460
|
+
)
|
|
461
|
+
self._t += dt
|
|
462
|
+
self._update_leg_order()
|
|
463
|
+
|
|
464
|
+
foot_targets: FootTargets = {} # [m]
|
|
465
|
+
for leg in LEG_NAMES:
|
|
466
|
+
in_stance = self._phase(leg, self._t) < self.params.duty_factor
|
|
467
|
+
lifted_off = not in_stance and self._prev_in_stance[leg]
|
|
468
|
+
if in_stance and not self._prev_in_stance[leg]:
|
|
469
|
+
anchor = self._swing[leg].touch_down
|
|
470
|
+
self._swing[leg] = _LegSwingState(lift_off=anchor, touch_down=anchor)
|
|
471
|
+
elif lifted_off:
|
|
472
|
+
anchor = self._swing[leg].touch_down
|
|
473
|
+
self._swing[leg] = _LegSwingState(lift_off=anchor, touch_down=anchor.copy())
|
|
474
|
+
self._prev_in_stance[leg] = in_stance
|
|
475
|
+
|
|
476
|
+
if in_stance:
|
|
477
|
+
foot_targets[leg] = self._chassis_from_odom(self._swing[leg].touch_down)
|
|
478
|
+
else:
|
|
479
|
+
remaining_swing_s = ( # [s]
|
|
480
|
+
1.0 - self._phase(leg, self._t)
|
|
481
|
+
) * self.params.cycle_period_s
|
|
482
|
+
_, touch_down_chassis = self._touchdown_liftoff(
|
|
483
|
+
leg, self._planned_stance_twist(remaining_swing_s)
|
|
484
|
+
)
|
|
485
|
+
pose_at_touch_down = _integrate_se2( # [m], [rad]
|
|
486
|
+
self._odom_xy, self._odom_yaw, vx, vy, wz, remaining_swing_s
|
|
487
|
+
)
|
|
488
|
+
planned = self._odom_from_chassis(touch_down_chassis, pose_at_touch_down) # [m]
|
|
489
|
+
swing_s = (1.0 - self.params.duty_factor) * self.params.cycle_period_s # [s]
|
|
490
|
+
max_move = _REPLAN_SPEED_FACTOR * self.params.workspace_reach_m / swing_s * dt # [m]
|
|
491
|
+
move = planned[:2] - self._swing[leg].touch_down[:2] # [m]
|
|
492
|
+
norm = float(np.linalg.norm(move)) # [m]
|
|
493
|
+
# On the lift-off tick sigma(tau) ~ 0, so the whole plan can be taken at once without moving
|
|
494
|
+
# the foot; capping it there would slew it during the swing and stop dead -> a joint kink.
|
|
495
|
+
if norm > max_move and not lifted_off:
|
|
496
|
+
move *= max_move / norm
|
|
497
|
+
self._swing[leg].touch_down[:2] += move
|
|
498
|
+
tau = (self._phase(leg, self._t) - self.params.duty_factor) / (
|
|
499
|
+
1.0 - self.params.duty_factor
|
|
500
|
+
)
|
|
501
|
+
swing = self._swing[leg]
|
|
502
|
+
sigma = _quintic(tau)
|
|
503
|
+
xy = swing.lift_off[:2] + sigma * (swing.touch_down[:2] - swing.lift_off[:2]) # [m]
|
|
504
|
+
ground_z = swing.lift_off[2] # [m], == touch_down[2]: flat-ground assumption
|
|
505
|
+
z = ground_z + self.params.swing_height * _swing_apex_profile(tau) # [m]
|
|
506
|
+
foot_targets[leg] = self._chassis_from_odom(np.array([xy[0], xy[1], z]))
|
|
507
|
+
|
|
508
|
+
return self._solve_ik(foot_targets)
|
|
509
|
+
|
|
510
|
+
def _settled(self) -> bool:
|
|
511
|
+
"""True once stopped with every foot down (or barely lifted) on its neutral footprint."""
|
|
512
|
+
if self._target_twist.any() or self._twist.any():
|
|
513
|
+
return False
|
|
514
|
+
for leg in LEG_NAMES:
|
|
515
|
+
foot = self._chassis_from_odom(self._swing[leg].touch_down) # [m]
|
|
516
|
+
if float(np.linalg.norm(foot[:2] - self._nominal_footprint[leg][:2])) >= _SETTLED_TOLERANCE_M:
|
|
517
|
+
return False
|
|
518
|
+
tau = (self._phase(leg, self._t) - self.params.duty_factor) / (1.0 - self.params.duty_factor)
|
|
519
|
+
if tau > 0.0 and self.params.swing_height * _swing_apex_profile(tau) >= _SETTLED_CLEARANCE_M:
|
|
520
|
+
return False
|
|
521
|
+
return True
|
|
522
|
+
|
|
523
|
+
def _ramped(self, twist: npt.NDArray[np.float64], dt: float) -> npt.NDArray[np.float64]:
|
|
524
|
+
"""Move actual twist [m.s⁻¹, m.s⁻¹, rad.s⁻¹] to desired within the acceleration limits, over ``dt`` [s]."""
|
|
525
|
+
twist = twist.copy()
|
|
526
|
+
linear_error = self._target_twist[:2] - twist[:2] # [m.s⁻¹]
|
|
527
|
+
linear_step = self.params.max_acceleration_mps2 * dt # [m.s⁻¹]
|
|
528
|
+
error_norm = float(np.linalg.norm(linear_error)) # [m.s⁻¹]
|
|
529
|
+
if error_norm <= linear_step or error_norm == 0.0:
|
|
530
|
+
twist[:2] = self._target_twist[:2]
|
|
531
|
+
else:
|
|
532
|
+
twist[:2] += linear_error / error_norm * linear_step
|
|
533
|
+
|
|
534
|
+
angular_error = self._target_twist[2] - twist[2] # [rad.s⁻¹]
|
|
535
|
+
angular_step = self.params.max_angular_acceleration_rad_s2 * dt # [rad.s⁻¹]
|
|
536
|
+
if abs(angular_error) <= angular_step:
|
|
537
|
+
twist[2] = self._target_twist[2]
|
|
538
|
+
else:
|
|
539
|
+
twist[2] += np.sign(angular_error) * angular_step
|
|
540
|
+
return twist
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
WALK_BLEND_S = 0.6
|
|
544
|
+
BODY_HEIGHT_RATE_MPS = 0.03 # [m.s⁻¹] ramp of WalkMove.set_body_height: 1 cm in a third of a second
|
|
545
|
+
_WALK_BLEND_SKIP_RAD = np.radians(2.0)
|
|
546
|
+
|
|
547
|
+
class WalkMove(Move):
|
|
548
|
+
"""Walk Move class."""
|
|
549
|
+
|
|
550
|
+
def __init__(self, kinematics: Kromatics, params: Optional[GaitParams] = None) -> None:
|
|
551
|
+
"""Build a fresh :class:`GaitEngine` (stand still pose) for ``kinematics``/``params``."""
|
|
552
|
+
self.gait = GaitEngine(kinematics, params)
|
|
553
|
+
self._blend_from: Optional[JointDict] = None
|
|
554
|
+
self._blend_t = WALK_BLEND_S # [s]
|
|
555
|
+
self.height_target = self.gait.params.body_height # [m]
|
|
556
|
+
|
|
557
|
+
def set_body_height(self, body_height: float) -> None:
|
|
558
|
+
"""Ramp the hip-to-foot height towards ``body_height`` [m], standing or walking (applied in the control loop)."""
|
|
559
|
+
self.height_target = body_height
|
|
560
|
+
|
|
561
|
+
def on_start(self) -> None:
|
|
562
|
+
"""Ease into the gait from wherever the robot stands (see :data:`WALK_BLEND_S`)."""
|
|
563
|
+
self._blend_from = None
|
|
564
|
+
self._blend_t = 0.0
|
|
565
|
+
|
|
566
|
+
def set_velocity(self, vx: float, vy: float, wz: float) -> None:
|
|
567
|
+
"""Forward to :meth:`GaitEngine.set_velocity` (``vx``, ``vy`` [m.s⁻¹], ``wz`` [rad.s⁻¹])."""
|
|
568
|
+
self.gait.set_velocity(vx, vy, wz)
|
|
569
|
+
|
|
570
|
+
@property
|
|
571
|
+
def settled(self) -> bool:
|
|
572
|
+
"""Stopped, every foot down on its neutral footprint: safe to lift a leg."""
|
|
573
|
+
return self.gait._settled()
|
|
574
|
+
|
|
575
|
+
def step(self, state: RobotState, command: MotorCommand, dt: float) -> None:
|
|
576
|
+
"""Advance the gait and write the resulting joint targets into ``command``."""
|
|
577
|
+
del state # unused - GaitEngine is pure dead reckoning
|
|
578
|
+
dh = self.height_target - self.gait.params.body_height # [m]
|
|
579
|
+
if dh:
|
|
580
|
+
step = BODY_HEIGHT_RATE_MPS * dt # [m]
|
|
581
|
+
self.gait.set_body_height(self.gait.params.body_height + float(np.clip(dh, -step, step)))
|
|
582
|
+
if self._blend_t >= WALK_BLEND_S:
|
|
583
|
+
command.target_angles.update(self.gait.step(dt))
|
|
584
|
+
return
|
|
585
|
+
stance = self.gait.step(0.0) # frozen until the pose has caught up
|
|
586
|
+
if self._blend_from is None:
|
|
587
|
+
self._blend_from = dict(command.target_angles)
|
|
588
|
+
if max(abs(q - self._blend_from.get(j, q)) for j, q in stance.items()) < _WALK_BLEND_SKIP_RAD:
|
|
589
|
+
self._blend_t = WALK_BLEND_S
|
|
590
|
+
command.target_angles.update(stance)
|
|
591
|
+
return
|
|
592
|
+
self._blend_t = min(self._blend_t + dt, WALK_BLEND_S)
|
|
593
|
+
s = _minimum_jerk(self._blend_t / WALK_BLEND_S)
|
|
594
|
+
start = self._blend_from
|
|
595
|
+
command.target_angles.update({j: start.get(j, q) + s * (q - start.get(j, q)) for j, q in stance.items()})
|