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,284 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Balance and chassis posing module for Chromapi - stability margin, attitude PD and a free-base QP."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Dict, Optional, Sequence, Tuple, Union
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
import numpy.typing as npt
|
|
13
|
+
import placo
|
|
14
|
+
from scipy.spatial import ConvexHull
|
|
15
|
+
from scipy.spatial.transform import Rotation as SciRotation
|
|
16
|
+
|
|
17
|
+
from chromapi.chromapi import DEFAULT_URDF_PATH, MotorCommand, Move, RobotState
|
|
18
|
+
from chromapi.kinematics.kromatics import Kromatics
|
|
19
|
+
from chromapi.kinematics.poses import WAKE_UP_POSE
|
|
20
|
+
from chromapi.kinematics.topology import FOOT_FRAME_NAMES, JOINT_NAMES, LEG_NAMES
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
_CHASSIS_FRAME = "chassis_assembly"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def stability_margin(feet_xy: Sequence[Sequence[float]], com_xy: Sequence[float]) -> float:
|
|
28
|
+
"""Signed distance [m] from the CoM to the support polygon edge (> 0 inside).
|
|
29
|
+
|
|
30
|
+
With fewer than 3 feet the polygon degenerates: 2 feet give minus the distance to the
|
|
31
|
+
segment between them, 1 foot minus the distance to that foot.
|
|
32
|
+
"""
|
|
33
|
+
pts = np.asarray(feet_xy, dtype=float)
|
|
34
|
+
c = np.asarray(com_xy, dtype=float)
|
|
35
|
+
if len(pts) == 0:
|
|
36
|
+
return float("-inf")
|
|
37
|
+
if len(pts) < 3:
|
|
38
|
+
a, b = pts[0], pts[-1]
|
|
39
|
+
ab = b - a
|
|
40
|
+
t = 0.0 if not ab.any() else float(np.clip((c - a) @ ab / (ab @ ab), 0.0, 1.0))
|
|
41
|
+
return -float(np.linalg.norm(c - (a + t * ab)))
|
|
42
|
+
poly = pts[ConvexHull(pts).vertices] # counter-clockwise in 2D
|
|
43
|
+
edges = np.roll(poly, -1, axis=0) - poly
|
|
44
|
+
normals = np.stack([-edges[:, 1], edges[:, 0]], axis=1) # inward for CCW
|
|
45
|
+
return float(np.min(np.sum(normals * (c - poly), axis=1) / np.linalg.norm(edges, axis=1)))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _measured_q(state: RobotState) -> Dict[str, float]:
|
|
49
|
+
return {name: state.joint_positions.get(name, WAKE_UP_POSE[name]) for name in JOINT_NAMES}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _stance_legs(state: RobotState) -> Sequence[str]:
|
|
53
|
+
# ponytail: no contact sensing on hardware yet -> all feet assumed planted.
|
|
54
|
+
if not state.foot_contacts:
|
|
55
|
+
return LEG_NAMES
|
|
56
|
+
return [leg for leg in LEG_NAMES if state.foot_contacts.get(leg)]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass
|
|
60
|
+
class BalanceGains:
|
|
61
|
+
"""Attitude PD and QP weights.
|
|
62
|
+
|
|
63
|
+
The PD output is the angular-velocity target of the orientation task.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
kp: float = 4.0 # [s⁻¹]
|
|
67
|
+
kd: float = 0.1 # [-]
|
|
68
|
+
max_tilt: float = 0.3 # [rad] bound on the commanded correction
|
|
69
|
+
w_feet: float = 1e3
|
|
70
|
+
w_attitude: float = 10.0
|
|
71
|
+
w_com: float = 10.0
|
|
72
|
+
w_posture: float = 1e-3
|
|
73
|
+
iterations: int = 8
|
|
74
|
+
max_joint_speed: float = 2.0 # [rad.s⁻¹]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class AttitudeCorrector:
|
|
78
|
+
"""Attitude PD: measured chassis tilt (IMU) -> chassis tilt to command in the feet frame."""
|
|
79
|
+
|
|
80
|
+
def __init__(self, kinematics: Kromatics, gains: Optional[BalanceGains] = None) -> None:
|
|
81
|
+
"""Build the robot model for the IMU frame and chassis frame."""
|
|
82
|
+
self.gains = gains or BalanceGains()
|
|
83
|
+
self._R_chassis_imu = kinematics.robot.get_T_a_b(_CHASSIS_FRAME, "imu")[:3, :3].copy()
|
|
84
|
+
self.roll_pitch = (0.0, 0.0) # [rad] measured, chassis frame
|
|
85
|
+
self.gyro = np.zeros(3) # [rad.s⁻¹] chassis frame
|
|
86
|
+
self.rpy_cmd = np.zeros(2) # [rad] commanded chassis (roll, pitch) in the feet frame
|
|
87
|
+
self.correction = np.zeros(2) # [rad] the PD's integrated part of rpy_cmd (a slope, a push)
|
|
88
|
+
|
|
89
|
+
def measure(self, state: RobotState) -> None:
|
|
90
|
+
"""Chassis roll/pitch and gyro from the IMU."""
|
|
91
|
+
w, x, y, z = state.imu_quat_wxyz
|
|
92
|
+
R_world_chassis = SciRotation.from_quat([x, y, z, w]).as_matrix() @ self._R_chassis_imu.T
|
|
93
|
+
_, pitch, roll = SciRotation.from_matrix(R_world_chassis).as_euler("ZYX")
|
|
94
|
+
self.roll_pitch = (float(roll), float(pitch))
|
|
95
|
+
self.gyro = self._R_chassis_imu @ np.asarray(state.imu_gyro)
|
|
96
|
+
|
|
97
|
+
def correct(self, dt: float, target: npt.ArrayLike = (0.0, 0.0)) -> npt.NDArray[np.float64]:
|
|
98
|
+
"""``rpy_cmd`` for the measured tilt ``target`` (roll, pitch) [rad], after ``dt`` [s].
|
|
99
|
+
|
|
100
|
+
The target goes straight through, and the PD integrates only
|
|
101
|
+
what is left: the measured error to the target, i.e. a slope or a push.
|
|
102
|
+
"""
|
|
103
|
+
g = self.gains
|
|
104
|
+
target = np.asarray(target, dtype=float)
|
|
105
|
+
w_star = -g.kp * (np.asarray(self.roll_pitch) - target) - g.kd * self.gyro[:2]
|
|
106
|
+
self.correction = np.clip(self.correction + w_star * dt, -g.max_tilt, g.max_tilt)
|
|
107
|
+
self.rpy_cmd = target + self.correction
|
|
108
|
+
return self.rpy_cmd
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class BalanceMove(Move):
|
|
112
|
+
"""Stand still, feet planted where they were at start; the chassis level, or posed by :meth:`set_body_pose`."""
|
|
113
|
+
|
|
114
|
+
def __init__(
|
|
115
|
+
self,
|
|
116
|
+
urdf_path: Union[str, Path] = DEFAULT_URDF_PATH,
|
|
117
|
+
dt: float = 0.02,
|
|
118
|
+
gains: Optional[BalanceGains] = None,
|
|
119
|
+
enabled: bool = True,
|
|
120
|
+
) -> None:
|
|
121
|
+
"""Build two private robot models: one for the QP, one for estimation.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
urdf_path: Robot URDF.
|
|
125
|
+
dt: Control period [s] - the QP integration step.
|
|
126
|
+
gains: PD gains and task weights.
|
|
127
|
+
enabled: If False, only estimate (margin, tilt) and hold the start pose: the
|
|
128
|
+
baseline of the push test.
|
|
129
|
+
|
|
130
|
+
"""
|
|
131
|
+
self.gains = gains or BalanceGains()
|
|
132
|
+
self.enabled = enabled
|
|
133
|
+
# Own models (10 ms each): no shared placo state with the gait or go_to_pose.
|
|
134
|
+
self._est = Kromatics(urdf_path, dt=dt)
|
|
135
|
+
self._kin = Kromatics(urdf_path, dt=dt)
|
|
136
|
+
robot = self._kin.robot
|
|
137
|
+
|
|
138
|
+
solver = placo.KinematicsSolver(robot) # Free the base!!
|
|
139
|
+
solver.enable_joint_limits(True)
|
|
140
|
+
solver.dt = dt
|
|
141
|
+
self._solver = solver
|
|
142
|
+
self._feet = {}
|
|
143
|
+
for leg in LEG_NAMES:
|
|
144
|
+
task = solver.add_position_task(FOOT_FRAME_NAMES[leg], np.zeros(3))
|
|
145
|
+
task.configure(f"foot_{leg}", "soft", self.gains.w_feet)
|
|
146
|
+
self._feet[leg] = task
|
|
147
|
+
self._attitude = solver.add_orientation_task(_CHASSIS_FRAME, np.eye(3))
|
|
148
|
+
self._attitude.configure("attitude", "soft", self.gains.w_attitude)
|
|
149
|
+
self._com = solver.add_com_task(np.zeros(3))
|
|
150
|
+
self._com.configure("com", "soft", self.gains.w_com)
|
|
151
|
+
posture = solver.add_joints_task()
|
|
152
|
+
posture.set_joints(dict(WAKE_UP_POSE))
|
|
153
|
+
posture.configure("posture", "soft", self.gains.w_posture)
|
|
154
|
+
self._posture = posture
|
|
155
|
+
|
|
156
|
+
self.attitude = AttitudeCorrector(self._est, self.gains)
|
|
157
|
+
|
|
158
|
+
self._started = False
|
|
159
|
+
self.margin = 0.0 # [m]
|
|
160
|
+
self.com0 = np.zeros(3) # [m] CoM at start, feet frame
|
|
161
|
+
self._body_xyz = np.zeros(3) # [m] CoM offset from com0, feet frame
|
|
162
|
+
self._body_rp = np.zeros(2) # [rad] chassis (roll, pitch) target, measured from level
|
|
163
|
+
self._body_yaw = 0.0 # [rad] chassis yaw over the feet, open loop (the IMU yaw drifts)
|
|
164
|
+
self._lifted: Tuple[Optional[str], Dict[str, float]] = (None, {})
|
|
165
|
+
self._masked: Optional[str] = None # leg whose joints the QP currently leaves alone
|
|
166
|
+
|
|
167
|
+
@property
|
|
168
|
+
def roll_pitch(self) -> Tuple[float, float]:
|
|
169
|
+
"""Measured chassis (roll, pitch) [rad]."""
|
|
170
|
+
return self.attitude.roll_pitch
|
|
171
|
+
|
|
172
|
+
@property
|
|
173
|
+
def rpy_cmd(self) -> npt.NDArray[np.float64]:
|
|
174
|
+
"""Commanded chassis (roll, pitch) in the feet frame [rad]."""
|
|
175
|
+
return self.attitude.rpy_cmd
|
|
176
|
+
|
|
177
|
+
@property
|
|
178
|
+
def feet(self) -> Dict[str, npt.NDArray[np.float64]]:
|
|
179
|
+
"""Foot positions held, feet frame (= chassis frame at start) [m]."""
|
|
180
|
+
return {leg: task.target_world.copy() for leg, task in self._feet.items()}
|
|
181
|
+
|
|
182
|
+
def set_body_pose(self, xyz: npt.ArrayLike = (0.0, 0.0, 0.0), rpy: npt.ArrayLike = (0.0, 0.0)) -> None:
|
|
183
|
+
"""Pose the chassis over the planted feet; zero is the start pose, level.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
xyz: CoM offset from its start position, in the feet frame (+X forward, +Y left, +Z up) [m].
|
|
187
|
+
rpy: Chassis (roll, pitch) [rad], measured by the IMU. An optional third value turns the
|
|
188
|
+
chassis over the feet (yaw, +Z, to the left), without feedback.
|
|
189
|
+
|
|
190
|
+
"""
|
|
191
|
+
rpy = np.asarray(rpy, dtype=float)
|
|
192
|
+
self._body_xyz = np.array(xyz, dtype=float)
|
|
193
|
+
self._body_rp = rpy[:2].copy()
|
|
194
|
+
self._body_yaw = float(rpy[2]) if len(rpy) > 2 else 0.0
|
|
195
|
+
|
|
196
|
+
def lift(self, leg: Optional[str], q_leg: Optional[Dict[str, float]] = None) -> None:
|
|
197
|
+
"""Let ``leg`` off the ground, its 3 joints at ``q_leg`` [rad], within the joint limits; None: all feet down again."""
|
|
198
|
+
limits = {joint: self._kin.joint_limits(joint) for joint in (q_leg or {})}
|
|
199
|
+
self._lifted = (leg, {joint: min(max(q, limits[joint][0]), limits[joint][1]) for joint, q in (q_leg or {}).items()})
|
|
200
|
+
|
|
201
|
+
def on_start(self) -> None:
|
|
202
|
+
"""Re-anchor the feet on the next tick, start pose, all feet down."""
|
|
203
|
+
self._started = False
|
|
204
|
+
self.set_body_pose()
|
|
205
|
+
self.lift(None)
|
|
206
|
+
|
|
207
|
+
def _anchor(self, q: Dict[str, float]) -> None:
|
|
208
|
+
"""QP world frame = chassis frame at start; feet held where they are."""
|
|
209
|
+
robot = self._kin.robot
|
|
210
|
+
for name, value in q.items():
|
|
211
|
+
robot.set_joint(name, value)
|
|
212
|
+
robot.set_T_world_fbase(np.eye(4))
|
|
213
|
+
robot.update_kinematics()
|
|
214
|
+
for leg in LEG_NAMES:
|
|
215
|
+
self._feet[leg].target_world = robot.get_T_world_frame(FOOT_FRAME_NAMES[leg])[:3, 3].copy()
|
|
216
|
+
self.com0 = robot.com_world().copy()
|
|
217
|
+
# Hold the start posture, not WAKE_UP_POSE: after a walk the robot does not stand exactly there.
|
|
218
|
+
self._posture.set_joints(dict(q))
|
|
219
|
+
self.attitude.rpy_cmd[:] = 0.0
|
|
220
|
+
self.attitude.correction[:] = 0.0
|
|
221
|
+
self._started = True
|
|
222
|
+
|
|
223
|
+
def _estimate(self, state: RobotState) -> float:
|
|
224
|
+
"""Stability margin [m] from the measured joints and IMU tilt."""
|
|
225
|
+
robot = self._est.robot
|
|
226
|
+
for name, value in _measured_q(state).items():
|
|
227
|
+
robot.set_joint(name, value)
|
|
228
|
+
robot.set_T_world_fbase(np.eye(4))
|
|
229
|
+
robot.update_kinematics()
|
|
230
|
+
roll, pitch = self.roll_pitch
|
|
231
|
+
R = SciRotation.from_euler("ZYX", [0.0, pitch, roll]).as_matrix() # chassis -> level
|
|
232
|
+
feet = [R @ robot.get_T_world_frame(FOOT_FRAME_NAMES[leg])[:3, 3] for leg in _stance_legs(state)]
|
|
233
|
+
com = R @ robot.com_world()
|
|
234
|
+
return stability_margin([f[:2] for f in feet], com[:2])
|
|
235
|
+
|
|
236
|
+
def step(self, state: RobotState, command: MotorCommand, dt: float) -> None:
|
|
237
|
+
"""Estimate, run the attitude PD, solve the free-base QP."""
|
|
238
|
+
self.attitude.measure(state)
|
|
239
|
+
self.margin = self._estimate(state)
|
|
240
|
+
if not self._started:
|
|
241
|
+
# The targets the servos hold, not the measured joints: those sag under the load.
|
|
242
|
+
start = {**_measured_q(state), **{j: command.target_angles[j] for j in JOINT_NAMES if j in command.target_angles}}
|
|
243
|
+
self._anchor(start)
|
|
244
|
+
command.target_angles.update(start)
|
|
245
|
+
if not self.enabled:
|
|
246
|
+
return
|
|
247
|
+
|
|
248
|
+
g = self.gains
|
|
249
|
+
rpy_cmd = self.attitude.correct(dt, self._body_rp)
|
|
250
|
+
self._attitude.R_world_frame = SciRotation.from_euler(
|
|
251
|
+
"ZYX", [self._body_yaw, rpy_cmd[1], rpy_cmd[0]]
|
|
252
|
+
).as_matrix()
|
|
253
|
+
# ponytail: CoM target in the feet frame, = level frame only on flat ground.
|
|
254
|
+
centre = np.mean([task.target_world for task in self._feet.values()], axis=0)
|
|
255
|
+
self._com.target_world = np.array([centre[0], centre[1], self.com0[2]]) + self._body_xyz
|
|
256
|
+
|
|
257
|
+
robot = self._kin.robot
|
|
258
|
+
lifted, q_lifted = self._lifted
|
|
259
|
+
if lifted != self._masked:
|
|
260
|
+
for leg in (self._masked, lifted):
|
|
261
|
+
if leg is not None:
|
|
262
|
+
down = leg != lifted
|
|
263
|
+
self._feet[leg].configure(f"foot_{leg}", "soft", g.w_feet if down else 0.0)
|
|
264
|
+
for joint in (f"{leg}_1", f"{leg}_2", f"{leg}_3"):
|
|
265
|
+
(self._solver.unmask_dof if down else self._solver.mask_dof)(joint)
|
|
266
|
+
self._masked = lifted
|
|
267
|
+
for joint, value in q_lifted.items():
|
|
268
|
+
robot.set_joint(joint, value)
|
|
269
|
+
for _ in range(g.iterations):
|
|
270
|
+
try:
|
|
271
|
+
self._solver.solve(True)
|
|
272
|
+
except RuntimeError as exc:
|
|
273
|
+
logger.warning("BalanceMove: QP solve stopped early (%s)", exc)
|
|
274
|
+
break
|
|
275
|
+
robot.update_kinematics()
|
|
276
|
+
free = [name for name in JOINT_NAMES if name not in q_lifted]
|
|
277
|
+
sent = np.array([command.target_angles.get(name, float(robot.get_joint(name))) for name in free])
|
|
278
|
+
delta = np.array([robot.get_joint(name) for name in free]) - sent # [rad]
|
|
279
|
+
scale = min(1.0, g.max_joint_speed * dt / max(float(np.abs(delta).max()), 1e-12))
|
|
280
|
+
for name, target in zip(free, sent + scale * delta):
|
|
281
|
+
command.target_angles[name] = float(target)
|
|
282
|
+
robot.set_joint(name, float(target))
|
|
283
|
+
command.target_angles.update(q_lifted)
|
|
284
|
+
robot.update_kinematics()
|