-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlipm_walking_controller.py
335 lines (288 loc) · 11 KB
/
lipm_walking_controller.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# SPDX-License-Identifier: Apache-2.0
# Copyright 2022 Stéphane Caron
"""Model predictive control part of the LIPM walking controller.
See also: https://github.com/stephane-caron/lipm_walking_controller/
"""
import argparse
from dataclasses import dataclass
import numpy as np
try:
from loop_rate_limiters import RateLimiter
except ImportError:
raise ImportError(
"This example requires an extra dependency. "
"You can install it by `pip install qpmpc[extras]`"
)
from qpmpc import MPCProblem, solve_mpc
from qpmpc.exceptions import ProblemDefinitionError
from qpmpc.live_plots.live_plot import LivePlot
MAX_ZMP_DIST = 100.0 # [m]
@dataclass
class Parameters:
"""Parameters of the step and humanoid.
These parameters are taken from the lipm_walking_controller.
"""
com_height: float = 0.84 # [m]
dsp_duration: float = 0.1 # [s]
foot_size: float = 0.065 # [m]
gravity: float = 9.81 # [m] / [s]²
init_support_foot_pos: float = 0.09 # [m]
nb_timesteps: int = 16
sampling_period: float = 0.1 # [s]
ssp_duration: float = 0.7 # [s]
strides = [-0.18, 0.18] # [m]
@property
def omega(self) -> float:
return np.sqrt(self.gravity / self.com_height)
@property
def dcm_from_state(self) -> np.ndarray:
return np.array([1.0, 1.0 / self.omega, 0.0])
@property
def zmp_from_state(self) -> np.ndarray:
return np.array([1.0, 0.0, -1.0 / self.omega**2])
def build_mpc_problem(params: Parameters):
"""Build the model predictive control problem.
Args:
params: Problem parameters.
For details on this problem and how open-loop model predictive control was
used in the LIPM walking controller, see "Stair Climbing Stabilization of
the HRP-4 Humanoid Robot using Whole-body Admittance Control" (Caron et
al., 2019).
"""
T = params.sampling_period
state_matrix = np.array(
[
[1.0, T, T**2 / 2.0],
[0.0, 1.0, T],
[0.0, 0.0, 1.0],
]
)
input_matrix = np.array(
[
T**3 / 6.0,
T**2 / 2.0,
T,
]
).reshape((3, 1))
ineq_matrix = np.array([+params.zmp_from_state, -params.zmp_from_state])
return MPCProblem(
transition_state_matrix=state_matrix,
transition_input_matrix=input_matrix,
ineq_state_matrix=ineq_matrix,
ineq_input_matrix=None,
ineq_vector=None,
initial_state=None,
goal_state=None,
nb_timesteps=params.nb_timesteps,
terminal_cost_weight=1.0,
stage_state_cost_weight=None,
stage_input_cost_weight=1e-3,
)
class PhaseStepper:
def __init__(self, params):
nb_dsp_steps = int(round(params.dsp_duration / T))
nb_ssp_steps = int(round(params.ssp_duration / T))
if 2 * (nb_dsp_steps + nb_ssp_steps) < params.nb_timesteps:
raise ProblemDefinitionError(
"there are more than two steps in the receding horizon"
)
# Skip edge cases of separate initial and final DSP durations (1/2):
# here we set the initial index in the middle of the first SSP phase in
# the receding horizon, thus creating a short first SSP phase.
initial_index = 5
self.index = initial_index
self.nb_dsp_steps = nb_dsp_steps
self.nb_ssp_steps = nb_ssp_steps
self.params = params
self.stride_index = 0
def advance(self):
self.index += 1
if self.index >= self.nb_dsp_steps + self.nb_ssp_steps:
self.index = 0
def advance_stride(self):
self.stride_index = (self.stride_index + 1) % len(self.params.strides)
def get_nb_steps(self):
offset = self.index
nb_init_dsp_steps = max(0, self.nb_dsp_steps - offset)
offset = max(0, offset - self.nb_dsp_steps)
nb_init_ssp_steps = max(0, self.nb_ssp_steps - offset)
offset = max(0, offset - self.nb_ssp_steps)
remaining = (
self.params.nb_timesteps - nb_init_dsp_steps - nb_init_ssp_steps
)
nb_next_dsp_steps = min(self.nb_dsp_steps, remaining)
remaining = max(0, remaining - self.nb_dsp_steps)
nb_next_ssp_steps = min(self.nb_ssp_steps, remaining)
remaining = max(0, remaining - self.nb_ssp_steps)
nb_last_dsp_steps = min(self.nb_dsp_steps, remaining)
remaining = max(0, remaining - self.nb_dsp_steps)
nb_last_ssp_steps = min(self.nb_ssp_steps, remaining)
remaining = max(0, remaining - self.nb_ssp_steps)
if remaining > 0:
raise ProblemDefinitionError(
"there are more than two steps in the receding horizon"
)
return (
nb_init_dsp_steps,
nb_init_ssp_steps,
nb_next_dsp_steps,
nb_next_ssp_steps,
nb_last_dsp_steps,
nb_last_ssp_steps,
)
def get_next_foot_pos(self, foot_pos: float) -> float:
return foot_pos + self.params.strides[self.stride_index]
def get_last_foot_pos(self, foot_pos: float) -> float:
upcoming_stride = (self.stride_index + 1) % len(self.params.strides)
return (
self.get_next_foot_pos(foot_pos)
+ self.params.strides[upcoming_stride]
)
def update_goal_and_constraints(
mpc_problem: MPCProblem,
phase: PhaseStepper,
cur_foot_pos: float,
):
(
nb_init_dsp_steps,
nb_init_ssp_steps,
nb_next_dsp_steps,
nb_next_ssp_steps,
nb_last_dsp_steps,
nb_last_ssp_steps,
) = phase.get_nb_steps()
next_foot_pos = phase.get_next_foot_pos(cur_foot_pos)
last_foot_pos = phase.get_last_foot_pos(cur_foot_pos)
cur_max = cur_foot_pos + 0.5 * params.foot_size
cur_min = cur_foot_pos - 0.5 * params.foot_size
next_max = next_foot_pos + 0.5 * params.foot_size
next_min = next_foot_pos - 0.5 * params.foot_size
last_max = last_foot_pos + 0.5 * params.foot_size
last_min = last_foot_pos - 0.5 * params.foot_size
mpc_problem.ineq_vector = (
[np.array([+MAX_ZMP_DIST, +MAX_ZMP_DIST])] * nb_init_dsp_steps
+ [np.array([+cur_max, -cur_min])] * nb_init_ssp_steps
+ [np.array([+MAX_ZMP_DIST, +MAX_ZMP_DIST])] * nb_next_dsp_steps
+ [np.array([+next_max, -next_min])] * nb_next_ssp_steps
+ [np.array([+MAX_ZMP_DIST, +MAX_ZMP_DIST])] * nb_last_dsp_steps
+ [np.array([+last_max, -last_min])] * nb_last_ssp_steps
)
goal_pos = last_foot_pos if nb_last_dsp_steps > 0 else next_foot_pos
mpc_problem.update_goal_state(np.array([goal_pos, 0.0, 0.0]))
def integrate(state: np.ndarray, jerk: float, dt: float) -> np.ndarray:
"""Integrate state (pos, vel, accel) with constant jerk.
Args:
state: Initial state.
jerk: Constant jerk to integrate.
dt: Duration to integrate for, in seconds.
Returns:
State after integration.
"""
p_0, v_0, a_0 = state
return np.array(
[
p_0 + dt * (v_0 + dt * (a_0 / 2 + dt * jerk / 6)),
v_0 + dt * (a_0 + dt * (jerk / 2)),
a_0 + dt * jerk,
]
).flatten()
def plot_plan(t, live_plot, params, mpc_problem, plan) -> None:
"""Plot plan resulting from the MPC problem.
Args:
params: Parameters of the problem.
mpc_problem: Model predictive control problem.
plan: Solution to the MPC problem.
state: Additional state to plot.
"""
horizon_duration = params.sampling_period * params.nb_timesteps
trange = np.linspace(t, t + horizon_duration, params.nb_timesteps + 1)
X = plan.states
zmp = X.dot(params.zmp_from_state)
pos = X[:, 0]
zmp_min = [
x[0] if abs(x[0]) < MAX_ZMP_DIST else None
for x in mpc_problem.ineq_vector
]
zmp_max = [
-x[1] if abs(x[1]) < MAX_ZMP_DIST else None
for x in mpc_problem.ineq_vector
]
zmp_min.append(zmp_min[-1])
zmp_max.append(zmp_max[-1])
live_plot.update_line("pos", trange, pos)
live_plot.update_line("zmp", trange, zmp)
live_plot.update_line("zmp_min", trange, zmp_min)
live_plot.update_line("zmp_max", trange, zmp_max)
def parse_command_line_arguments() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--slowdown",
help="Slow time down by a multiplicative factor",
type=float,
default=1.0,
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_command_line_arguments()
params = Parameters()
mpc_problem = build_mpc_problem(params)
T = params.sampling_period
substeps: int = 15 # number of integration substeps
dt = T / substeps
horizon_duration = params.sampling_period * params.nb_timesteps
live_plot = LivePlot(
xlim=(0, horizon_duration + T),
ylim=tuple(params.strides),
)
live_plot.add_line("pos", "b-")
live_plot.add_line("cur_pos", "bo", lw=2)
live_plot.add_line("cur_dcm", "go", lw=2)
live_plot.add_line("cur_zmp", "ro", lw=2)
live_plot.add_line("goal_pos", "ko", lw=2)
live_plot.add_line("zmp", "r-")
live_plot.add_line("zmp_min", "g:")
live_plot.add_line("zmp_max", "b:")
rate = RateLimiter(frequency=1.0 / (args.slowdown * dt), warn=False)
t = 0.0
phase = PhaseStepper(params)
support_foot_pos = params.init_support_foot_pos
# Skip edge cases of separate initial and final DSP durations (2/2): here
# we set the initial ZMP at the center of the initial foothold, and the
# initial DCM halfway. See the LIPM walking controller and its
# configuration for details on initial/final DSP phases.
init_accel = -params.omega**2 * support_foot_pos
init_vel = 0.5 * params.omega * support_foot_pos
state = np.array([0.0, init_vel, init_accel])
for _ in range(300):
mpc_problem.update_initial_state(state)
update_goal_and_constraints(mpc_problem, phase, support_foot_pos)
plan = solve_mpc(mpc_problem, solver="proxqp")
plot_plan(t, live_plot, params, mpc_problem, plan)
for step in range(substeps):
state = integrate(state, plan.inputs[0], dt)
t2 = t + step * dt
if t2 >= T:
t3 = t2 - T
live_plot.axis.set_xlim(t3, t3 + horizon_duration + T)
cur_pos = state[0]
cur_dcm = params.dcm_from_state.dot(state)
cur_zmp = params.zmp_from_state.dot(state)
live_plot.update_line("cur_pos", [t2], [cur_pos])
live_plot.update_line("cur_dcm", [t2], [cur_dcm])
live_plot.update_line("cur_zmp", [t2], [cur_zmp])
live_plot.update_line(
"goal_pos",
[t2 + horizon_duration],
[mpc_problem.goal_state[0]],
)
live_plot.update()
rate.sleep()
phase.advance()
if phase.index == 0:
support_foot_pos = phase.get_next_foot_pos(support_foot_pos)
phase.advance_stride()
t += T