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,324 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Kromatics - Kinematics Module for Chromapi."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
import re
|
|
8
|
+
import tempfile
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict, Optional, Sequence, Tuple, Union
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
import numpy.typing as npt
|
|
14
|
+
from scipy.spatial.transform import Rotation as SciRotation
|
|
15
|
+
|
|
16
|
+
from chromapi.kinematics.poses import APPROACH_POSE, REST_POSE, WAKE_UP_POSE, ZERO_POSE
|
|
17
|
+
from chromapi.kinematics.topology import (
|
|
18
|
+
FOOT_FRAME_NAMES,
|
|
19
|
+
JOINT_NAMES,
|
|
20
|
+
JOINT_SUFFIXES,
|
|
21
|
+
LEG_NAMES,
|
|
22
|
+
joint_name,
|
|
23
|
+
)
|
|
24
|
+
from chromapi.kinematics.types import FootTargets, JointDict, QVector, Vector3
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
import placo
|
|
28
|
+
|
|
29
|
+
PLACO_AVAILABLE = True
|
|
30
|
+
except ImportError: # pragma: no cover - optional dependency
|
|
31
|
+
placo = None
|
|
32
|
+
PLACO_AVAILABLE = False
|
|
33
|
+
|
|
34
|
+
logger = logging.getLogger(__name__)
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
"APPROACH_POSE",
|
|
38
|
+
"FOOT_FRAME_NAMES",
|
|
39
|
+
"FootTargets",
|
|
40
|
+
"JOINT_NAMES",
|
|
41
|
+
"JOINT_SUFFIXES",
|
|
42
|
+
"JointDict",
|
|
43
|
+
"Kromatics",
|
|
44
|
+
"LEG_NAMES",
|
|
45
|
+
"PLACO_AVAILABLE",
|
|
46
|
+
"QVector",
|
|
47
|
+
"REST_POSE",
|
|
48
|
+
"Vector3",
|
|
49
|
+
"WAKE_UP_POSE",
|
|
50
|
+
"ZERO_POSE",
|
|
51
|
+
"joint_name",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
_CHASSIS_FRAME = "chassis_assembly"
|
|
56
|
+
|
|
57
|
+
#: QP solve iterations per call - cheap enough (~0.15 ms at 5 iterations) that there's no
|
|
58
|
+
#: real-time-budget reason to run fewer for a per-tick gait call.
|
|
59
|
+
_DEFAULT_ITERATIONS = 8
|
|
60
|
+
|
|
61
|
+
#: A foot is considered "reached" within this distance of its target.
|
|
62
|
+
_CONVERGENCE_TOL_M = 1e-3 # [m]
|
|
63
|
+
|
|
64
|
+
_ZERO3: Vector3 = np.zeros(3) # [m] or [rad]
|
|
65
|
+
|
|
66
|
+
_GEOMETRY_TAGS = re.compile(r"<(visual|collision)\b.*?</\1>", re.S)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _load_kinematic_robot(urdf_path: Path) -> Any:
|
|
70
|
+
"""Load the URDF without its <visual>/<collision> meshes bringing the load down to a few ms."""
|
|
71
|
+
if urdf_path.is_dir():
|
|
72
|
+
urdf_path = urdf_path / "robot.urdf"
|
|
73
|
+
urdf = _GEOMETRY_TAGS.sub("", urdf_path.read_text())
|
|
74
|
+
with tempfile.NamedTemporaryFile("w", suffix=".urdf") as f:
|
|
75
|
+
f.write(urdf)
|
|
76
|
+
f.flush()
|
|
77
|
+
return placo.RobotWrapper(f.name, placo.Flags.ignore_collisions)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class Kromatics:
|
|
81
|
+
"""Whole-body kinematics for Chromapi - one QP over all 12 joints."""
|
|
82
|
+
|
|
83
|
+
_STAND_REACH = (0.0, 0.195, 0.12) # (x_reach, y_reach, height) [m]
|
|
84
|
+
|
|
85
|
+
def __init__(
|
|
86
|
+
self,
|
|
87
|
+
urdf_path: Union[str, Path],
|
|
88
|
+
dt: float = 0.01,
|
|
89
|
+
initial_pose: Optional[JointDict] = None,
|
|
90
|
+
) -> None:
|
|
91
|
+
"""Load the robot URDF and set up the per-foot QP tasks.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
urdf_path: Path to ``model/urdf/robot.urdf`` (or its containing directory).
|
|
95
|
+
dt: Solver integration time step [s] - should match the control-loop period.
|
|
96
|
+
initial_pose: Joint angles [rad] to seed ``self.robot`` with before the foot tasks are set
|
|
97
|
+
up, so their initial targets reflect this configuration rather than the URDF's
|
|
98
|
+
zero pose (legs splayed flat).
|
|
99
|
+
|
|
100
|
+
"""
|
|
101
|
+
if not PLACO_AVAILABLE:
|
|
102
|
+
raise RuntimeError(
|
|
103
|
+
"Kromatics requires the 'placo' package (pip install placo), which was not found."
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
self.robot = _load_kinematic_robot(Path(urdf_path))
|
|
107
|
+
if initial_pose is not None:
|
|
108
|
+
self._apply_q(dict(initial_pose))
|
|
109
|
+
|
|
110
|
+
self.solver = placo.KinematicsSolver(self.robot)
|
|
111
|
+
self.solver.mask_fbase(True)
|
|
112
|
+
self.solver.enable_joint_limits(True)
|
|
113
|
+
self.solver.dt = dt # [s]
|
|
114
|
+
|
|
115
|
+
self.foot_tasks: Dict[str, Any] = {}
|
|
116
|
+
for leg in LEG_NAMES:
|
|
117
|
+
frame = FOOT_FRAME_NAMES[leg]
|
|
118
|
+
target = self.robot.get_T_world_frame(frame)[:3, 3] # [m]
|
|
119
|
+
task = self.solver.add_position_task(frame, target)
|
|
120
|
+
task.configure(frame, "soft", 1.0)
|
|
121
|
+
self.foot_tasks[leg] = task
|
|
122
|
+
|
|
123
|
+
self.last_solve_error_m: float = 0.0 # [m]
|
|
124
|
+
|
|
125
|
+
# -- conversions ---------------------------------------------------------------
|
|
126
|
+
|
|
127
|
+
@staticmethod
|
|
128
|
+
def to_array(q: Union[JointDict, Sequence[float]]) -> npt.NDArray[np.float64]:
|
|
129
|
+
"""Convert a joint dict (or an already-ordered sequence) to a length-12 array."""
|
|
130
|
+
if isinstance(q, dict):
|
|
131
|
+
return np.array([q[name] for name in JOINT_NAMES], dtype=float)
|
|
132
|
+
arr = np.asarray(q, dtype=float)
|
|
133
|
+
if arr.shape != (12,):
|
|
134
|
+
raise ValueError(f"Expected 12 joint values, got shape {arr.shape}")
|
|
135
|
+
return arr
|
|
136
|
+
|
|
137
|
+
@staticmethod
|
|
138
|
+
def to_dict(q: Union[JointDict, Sequence[float]]) -> JointDict:
|
|
139
|
+
"""Convert a length-12 array (or an already-named dict) to a joint dict."""
|
|
140
|
+
if isinstance(q, dict):
|
|
141
|
+
return dict(q)
|
|
142
|
+
arr = np.asarray(q, dtype=float)
|
|
143
|
+
return dict(zip(JOINT_NAMES, arr.tolist()))
|
|
144
|
+
|
|
145
|
+
# -- low-level robot-state helpers -----------------------------------------------
|
|
146
|
+
|
|
147
|
+
def _apply_q(self, q: JointDict) -> None:
|
|
148
|
+
"""Push a joint dict into ``self.robot`` and refresh its kinematics."""
|
|
149
|
+
for name, value in q.items():
|
|
150
|
+
self.robot.set_joint(name, value)
|
|
151
|
+
self.robot.update_kinematics()
|
|
152
|
+
|
|
153
|
+
def joint_limits(self, joint: str) -> Tuple[float, float]:
|
|
154
|
+
"""Return the (lower, upper) URDF joint-limit bound for ``joint`` [rad]."""
|
|
155
|
+
lower, upper = self.robot.get_joint_limits(joint)
|
|
156
|
+
return float(lower), float(upper)
|
|
157
|
+
|
|
158
|
+
def _hip_origin_chassis(self, leg: str) -> Vector3:
|
|
159
|
+
"""Hip-yaw joint origin for ``leg``, in the chassis frame [m]."""
|
|
160
|
+
origin: Vector3 = self.robot.get_T_a_b(_CHASSIS_FRAME, joint_name(leg, 1))[:3, 3] # [m]
|
|
161
|
+
return origin
|
|
162
|
+
|
|
163
|
+
# -- forward / inverse kinematics ------------------------------------------------
|
|
164
|
+
|
|
165
|
+
def forward_kinematics(self, q: Union[JointDict, Sequence[float]]) -> FootTargets:
|
|
166
|
+
"""Compute the 4 foot positions [m] (chassis frame) for a given 12-joint configuration [rad]."""
|
|
167
|
+
self._apply_q(self.to_dict(q))
|
|
168
|
+
return {
|
|
169
|
+
leg: self.robot.get_T_a_b(_CHASSIS_FRAME, FOOT_FRAME_NAMES[leg])[:3, 3].copy()
|
|
170
|
+
for leg in LEG_NAMES
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
def _run_qp(
|
|
174
|
+
self,
|
|
175
|
+
body_xyz: Vector3,
|
|
176
|
+
body_rpy: Vector3,
|
|
177
|
+
foot_targets: FootTargets,
|
|
178
|
+
seed: Optional[JointDict],
|
|
179
|
+
n_iterations: int,
|
|
180
|
+
) -> int:
|
|
181
|
+
"""Impose the base pose/foot tasks and iterate; return how many iterations succeeded."""
|
|
182
|
+
if seed is not None:
|
|
183
|
+
self._apply_q(seed)
|
|
184
|
+
|
|
185
|
+
trunk_pose = np.eye(4) # homogeneous transform, translation [m]
|
|
186
|
+
trunk_pose[:3, :3] = SciRotation.from_euler("xyz", body_rpy).as_matrix()
|
|
187
|
+
trunk_pose[:3, 3] = body_xyz
|
|
188
|
+
self.robot.set_T_world_fbase(trunk_pose)
|
|
189
|
+
self.robot.update_kinematics()
|
|
190
|
+
|
|
191
|
+
for leg, target in foot_targets.items():
|
|
192
|
+
self.foot_tasks[leg].target_world = np.asarray(target, dtype=float)
|
|
193
|
+
|
|
194
|
+
completed = 0
|
|
195
|
+
for _ in range(n_iterations):
|
|
196
|
+
try:
|
|
197
|
+
self.solver.solve(True)
|
|
198
|
+
except RuntimeError as exc:
|
|
199
|
+
logger.warning("Kromatics: QP solve stopped early (%s)", exc)
|
|
200
|
+
break
|
|
201
|
+
self.robot.update_kinematics()
|
|
202
|
+
completed += 1
|
|
203
|
+
return completed
|
|
204
|
+
|
|
205
|
+
def _solve(
|
|
206
|
+
self,
|
|
207
|
+
body_xyz: Vector3,
|
|
208
|
+
body_rpy: Vector3,
|
|
209
|
+
foot_targets: FootTargets,
|
|
210
|
+
q_init: Optional[Union[JointDict, Sequence[float]]],
|
|
211
|
+
iterations: Optional[int],
|
|
212
|
+
) -> Tuple[JointDict, bool]:
|
|
213
|
+
"""Shared QP solve: impose the base pose, set foot tasks, iterate and measure residual."""
|
|
214
|
+
n_iterations = _DEFAULT_ITERATIONS if iterations is None else iterations
|
|
215
|
+
seed = self.to_dict(q_init) if q_init is not None else None
|
|
216
|
+
|
|
217
|
+
completed = self._run_qp(body_xyz, body_rpy, foot_targets, seed, n_iterations)
|
|
218
|
+
if completed == 0:
|
|
219
|
+
logger.warning(
|
|
220
|
+
"Kromatics: seed rejected by the QP on its first iteration - retrying from "
|
|
221
|
+
"WAKE_UP_POSE"
|
|
222
|
+
)
|
|
223
|
+
self._run_qp(body_xyz, body_rpy, foot_targets, dict(WAKE_UP_POSE), n_iterations)
|
|
224
|
+
|
|
225
|
+
q = {name: float(self.robot.get_joint(name)) for name in JOINT_NAMES} # [rad]
|
|
226
|
+
max_error = 0.0 # [m]
|
|
227
|
+
for leg, target in foot_targets.items():
|
|
228
|
+
achieved = self.robot.get_T_world_frame(FOOT_FRAME_NAMES[leg])[:3, 3] # [m]
|
|
229
|
+
max_error = max(max_error, float(np.linalg.norm(achieved - np.asarray(target))))
|
|
230
|
+
self.last_solve_error_m = max_error
|
|
231
|
+
return q, max_error < _CONVERGENCE_TOL_M
|
|
232
|
+
|
|
233
|
+
def inverse_kinematics(
|
|
234
|
+
self,
|
|
235
|
+
foot_targets: FootTargets,
|
|
236
|
+
q_init: Optional[Union[JointDict, Sequence[float]]] = None,
|
|
237
|
+
use_warm_start: bool = True,
|
|
238
|
+
iterations: Optional[int] = None,
|
|
239
|
+
) -> Tuple[JointDict, bool]:
|
|
240
|
+
"""Compute the 12 joint angles placing each foot at its target (in chassis frame).
|
|
241
|
+
|
|
242
|
+
Args:
|
|
243
|
+
foot_targets: Mapping leg -> desired (x, y, z) in the chassis frame [m]. Legs absent
|
|
244
|
+
from the mapping keep their current target.
|
|
245
|
+
q_init: Optional joint configuration [rad] - re-syncs the solver's internal state to it before solving.
|
|
246
|
+
use_warm_start: If True (default) and ``q_init`` is None, continue from the
|
|
247
|
+
solver's current configuration. If False, reset to
|
|
248
|
+
:data:`~chromapi.kinematics.poses.WAKE_UP_POSE` first.
|
|
249
|
+
iterations: QP iterations for this call - defaults to :data:`_DEFAULT_ITERATIONS`.
|
|
250
|
+
|
|
251
|
+
Returns:
|
|
252
|
+
(q, converged): the 12 joint angles [rad].
|
|
253
|
+
|
|
254
|
+
"""
|
|
255
|
+
if q_init is None and not use_warm_start:
|
|
256
|
+
self._apply_q(dict(WAKE_UP_POSE))
|
|
257
|
+
return self._solve(_ZERO3, _ZERO3, foot_targets, q_init, iterations)
|
|
258
|
+
|
|
259
|
+
# -- stance generation -------------------------------------------------------------
|
|
260
|
+
|
|
261
|
+
def stance_targets(
|
|
262
|
+
self,
|
|
263
|
+
height: float,
|
|
264
|
+
x_reach: float = _STAND_REACH[0],
|
|
265
|
+
y_reach: float = _STAND_REACH[1],
|
|
266
|
+
) -> FootTargets:
|
|
267
|
+
"""Build a symmetric 4-foot footprint under the hips, in the chassis frame.
|
|
268
|
+
|
|
269
|
+
Each foot is placed ``height`` below its own hip, offset by ``x_reach``/``y_reach``
|
|
270
|
+
away from the chassis centerline (sign follows each hip, so this stays symmetric
|
|
271
|
+
regardless of which leg is physically front/back/left/right).
|
|
272
|
+
|
|
273
|
+
Args:
|
|
274
|
+
height: Vertical drop from hip to foot (positive = down) [m].
|
|
275
|
+
x_reach: Horizontal offset along chassis X (magnitude only) [m].
|
|
276
|
+
y_reach: Horizontal offset along chassis Y (magnitude only) [m].
|
|
277
|
+
|
|
278
|
+
"""
|
|
279
|
+
targets: FootTargets = {}
|
|
280
|
+
for leg in LEG_NAMES:
|
|
281
|
+
hip = self._hip_origin_chassis(leg) # [m]
|
|
282
|
+
sign_x = 1.0 if hip[0] >= 0 else -1.0
|
|
283
|
+
sign_y = 1.0 if hip[1] >= 0 else -1.0
|
|
284
|
+
targets[leg] = hip + np.array(
|
|
285
|
+
[sign_x * x_reach, sign_y * y_reach, -abs(height)]
|
|
286
|
+
)
|
|
287
|
+
return targets
|
|
288
|
+
|
|
289
|
+
# -- quasi-static body posing -------------------------------------------------------
|
|
290
|
+
|
|
291
|
+
def body_ik(
|
|
292
|
+
self,
|
|
293
|
+
body_xyz: Vector3,
|
|
294
|
+
body_rpy: Vector3,
|
|
295
|
+
foot_targets_world: Optional[FootTargets] = None,
|
|
296
|
+
reference_height: float = _STAND_REACH[2],
|
|
297
|
+
q_init: Optional[Union[JointDict, Sequence[float]]] = None,
|
|
298
|
+
iterations: Optional[int] = None,
|
|
299
|
+
) -> Tuple[JointDict, bool]:
|
|
300
|
+
"""Solve joint angles for a desired chassis pose with the feet planted on the ground.
|
|
301
|
+
|
|
302
|
+
Args:
|
|
303
|
+
body_xyz: Desired chassis origin position, in the world/ground frame [m].
|
|
304
|
+
body_rpy: Desired chassis orientation (roll, pitch, yaw), in the world frame [rad].
|
|
305
|
+
foot_targets_world: Foot positions to hold fixed, in the world frame [m].
|
|
306
|
+
reference_height: Stance height for the default footprint when ``foot_targets_world`` is not given [m].
|
|
307
|
+
q_init: Optional joint configuration (dict or length-12 array) [rad].
|
|
308
|
+
iterations: QP iterations for this call - defaults to :data:`_DEFAULT_ITERATIONS`.
|
|
309
|
+
|
|
310
|
+
Returns:
|
|
311
|
+
(q, converged).
|
|
312
|
+
|
|
313
|
+
"""
|
|
314
|
+
if foot_targets_world is None:
|
|
315
|
+
nominal = self.stance_targets(reference_height)
|
|
316
|
+
foot_targets_world = {leg: pos.copy() for leg, pos in nominal.items()}
|
|
317
|
+
|
|
318
|
+
return self._solve(
|
|
319
|
+
np.asarray(body_xyz, dtype=float),
|
|
320
|
+
np.asarray(body_rpy, dtype=float),
|
|
321
|
+
foot_targets_world,
|
|
322
|
+
q_init,
|
|
323
|
+
iterations,
|
|
324
|
+
)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Named joint configurations."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from typing import Dict
|
|
7
|
+
|
|
8
|
+
from chromapi.kinematics.topology import (
|
|
9
|
+
JOINT_NAMES,
|
|
10
|
+
JOINT_SUFFIXES,
|
|
11
|
+
LEG_NAMES,
|
|
12
|
+
joint_name,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
ZERO_POSE: Dict[str, float] = dict.fromkeys(JOINT_NAMES, 0.0) # [rad]
|
|
16
|
+
|
|
17
|
+
def _symmetric_pose(hip_yaw: float, hip_pitch: float, knee_pitch: float) -> Dict[str, float]:
|
|
18
|
+
"""Build a 12-joint pose from one (hip yaw, hip pitch, knee pitch) triple [rad]."""
|
|
19
|
+
pose: Dict[str, float] = {} # [rad]
|
|
20
|
+
for leg in LEG_NAMES:
|
|
21
|
+
for suffix, value in zip(JOINT_SUFFIXES, (hip_yaw, hip_pitch, knee_pitch)):
|
|
22
|
+
if suffix == "1" and leg in ("bl", "br"):
|
|
23
|
+
value = -value
|
|
24
|
+
pose[joint_name(leg, suffix)] = value
|
|
25
|
+
return pose
|
|
26
|
+
|
|
27
|
+
APPROACH_POSE: Dict[str, float] = _symmetric_pose(0.65, 1.2, -1.7) # [rad]
|
|
28
|
+
WAKE_UP_POSE: Dict[str, float] = _symmetric_pose(0.65, 0.65, -1.63) # [rad]
|
|
29
|
+
REST_POSE: Dict[str, float] = _symmetric_pose(0.65, 1.42, -1.72) # [rad]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Chromapi's topology - leg/joint naming."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from typing import Dict, Tuple, Union
|
|
7
|
+
|
|
8
|
+
#: Leg tags as named in the URDF/MJCF (``model/urdf/robot.urdf``, ``model/mjcf/robot.xml``).
|
|
9
|
+
LEG_NAMES: Tuple[str, str, str, str] = ("tl", "tr", "bl", "br")
|
|
10
|
+
|
|
11
|
+
#: Per-leg joint suffixes.
|
|
12
|
+
JOINT_SUFFIXES: Tuple[str, str, str] = ("1", "2", "3")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def joint_name(leg: str, suffix: Union[int, str]) -> str:
|
|
16
|
+
"""Build a joint name (e.g. ``joint_name("tl", 1) == "tl_1"``)."""
|
|
17
|
+
return f"{leg}_{suffix}"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
#: The 12 joint names.
|
|
21
|
+
JOINT_NAMES: Tuple[str, ...] = tuple(
|
|
22
|
+
joint_name(leg, s) for leg in LEG_NAMES for s in JOINT_SUFFIXES
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
#: Foot contact frame name per leg.
|
|
26
|
+
FOOT_FRAME_NAMES: Dict[str, str] = {leg: f"{leg}_foot" for leg in LEG_NAMES}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Shared array/dict type aliases for the kinematics layer."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from typing import Dict, Sequence, Union
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import numpy.typing as npt
|
|
10
|
+
|
|
11
|
+
#: A plain 3-vector (position [m]) - most commonly a foot position.
|
|
12
|
+
Vector3 = npt.NDArray[np.float64]
|
|
13
|
+
|
|
14
|
+
#: A 3-element joint-angle vector [rad], accepted as a plain sequence or a numpy array.
|
|
15
|
+
QVector = Union[Sequence[float], npt.NDArray[np.float64]]
|
|
16
|
+
|
|
17
|
+
#: The 12 joint angles [rad], keyed by joint name.
|
|
18
|
+
JointDict = Dict[str, float]
|
|
19
|
+
|
|
20
|
+
#: One foot position (in chassis frame) [m] per leg.
|
|
21
|
+
FootTargets = Dict[str, Vector3]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Locomotion layer for Chromapi - gait generation on top of :mod:`chromapi.kinematics`.
|
|
3
|
+
|
|
4
|
+
- :mod:`chromapi.locomotion.gait` - :class:`~chromapi.locomotion.gait.GaitParams`,
|
|
5
|
+
:class:`~chromapi.locomotion.gait.GaitEngine` and
|
|
6
|
+
:class:`~chromapi.locomotion.gait.WalkMove`
|
|
7
|
+
"""
|