-
Notifications
You must be signed in to change notification settings - Fork 10
/
quadruped_spot.py
111 lines (87 loc) · 3.37 KB
/
quadruped_spot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from pathlib import Path
import mujoco
import mujoco.viewer
import numpy as np
from loop_rate_limiters import RateLimiter
import mink
_HERE = Path(__file__).parent
_XML = _HERE / "boston_dynamics_spot" / "scene.xml"
if __name__ == "__main__":
model = mujoco.MjModel.from_xml_path(_XML.as_posix())
data = mujoco.MjData(model)
## =================== ##
## Setup IK.
## =================== ##
configuration = mink.Configuration(model)
feet = ["FL", "FR", "HR", "HL"]
base_task = mink.FrameTask(
frame_name="body",
frame_type="body",
position_cost=1.0,
orientation_cost=1.0,
)
posture_task = mink.PostureTask(model, cost=1e-5)
feet_tasks = []
for foot in feet:
task = mink.FrameTask(
frame_name=foot,
frame_type="geom",
position_cost=1.0,
orientation_cost=0.0,
)
feet_tasks.append(task)
eef_task = mink.FrameTask(
frame_name="EE",
frame_type="site",
position_cost=1.0,
orientation_cost=1.0,
)
tasks = [base_task, posture_task, *feet_tasks, eef_task]
## =================== ##
base_mid = model.body("body_target").mocapid[0]
feet_mid = [model.body(f"{foot}_target").mocapid[0] for foot in feet]
eef_mid = model.body("EE_target").mocapid[0]
# IK settings.
solver = "quadprog"
pos_threshold = 1e-4
ori_threshold = 1e-4
max_iters = 20
with mujoco.viewer.launch_passive(
model=model, data=data, show_left_ui=False, show_right_ui=False
) as viewer:
mujoco.mjv_defaultFreeCamera(model, viewer.cam)
mujoco.mj_resetDataKeyframe(model, data, model.key("home").id)
configuration.update(data.qpos)
mujoco.mj_forward(model, data)
posture_task.set_target_from_configuration(configuration)
for foot in feet:
mink.move_mocap_to_frame(model, data, f"{foot}_target", foot, "geom")
mink.move_mocap_to_frame(model, data, "body_target", "body", "body")
mink.move_mocap_to_frame(model, data, "EE_target", "EE", "site")
rate = RateLimiter(frequency=500.0, warn=False)
while viewer.is_running():
base_task.set_target(mink.SE3.from_mocap_id(data, base_mid))
for i, task in enumerate(feet_tasks):
task.set_target(mink.SE3.from_mocap_id(data, feet_mid[i]))
eef_task.set_target(mink.SE3.from_mocap_id(data, eef_mid))
# Compute velocity and integrate into the next configuration.
for i in range(max_iters):
vel = mink.solve_ik(configuration, tasks, rate.dt, solver, 1e-3)
configuration.integrate_inplace(vel, rate.dt)
pos_achieved = True
ori_achieved = True
for task in [
eef_task,
base_task,
*feet_tasks,
]:
err = eef_task.compute_error(configuration)
pos_achieved &= bool(np.linalg.norm(err[:3]) <= pos_threshold)
ori_achieved &= bool(np.linalg.norm(err[3:]) <= ori_threshold)
if pos_achieved and ori_achieved:
break
data.ctrl = configuration.q[7:]
mujoco.mj_step(model, data)
# Visualize at fixed FPS.
viewer.sync()
rate.sleep()