Skip to content

Commit 425472b

Browse files
author
earlaud
committed
Update kinodynamics examples
1 parent f9e7997 commit 425472b

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

examples/go2_kinodynamics.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
fref = np.zeros(force_size)
3535
fref[2] = -model_handler.getMass() / nk * gravity[2]
3636
u0 = np.concatenate((fref, fref, fref, fref, np.zeros(model_handler.getModel().nv - 6)))
37-
dt = 0.01
37+
dt_mpc = 0.01
3838

3939
w_basepos = [0, 0, 100, 10, 10, 0]
4040
w_legpos = [1, 1, 1]
@@ -63,7 +63,7 @@
6363
w_centder = np.diag(np.concatenate((w_centder_lin, w_centder_ang)))
6464

6565
problem_conf = dict(
66-
timestep=dt,
66+
timestep=dt_mpc,
6767
w_x=w_x,
6868
w_u=w_u,
6969
w_cent=w_cent,
@@ -97,7 +97,7 @@
9797
swing_apex=0.15,
9898
T_fly=T_ss,
9999
T_contact=T_ds,
100-
timestep=dt,
100+
timestep=dt_mpc,
101101
)
102102

103103
mpc = MPC(mpc_conf, dynproblem)
@@ -134,6 +134,8 @@
134134
mpc.generateCycleHorizon(contact_phases)
135135

136136
""" Interpolation """
137+
N_simu = 10 # Number of substep the simulation does between two MPC computation
138+
dt_simu = dt_mpc/N_simu
137139
interpolator = Interpolator(model_handler.getModel())
138140

139141
""" Inverse Dynamics """
@@ -146,15 +148,15 @@
146148
kino_ID_settings.w_contact_force = 1.
147149
kino_ID_settings.w_contact_motion = 1.
148150

149-
kino_ID = KinodynamicsID(model_handler, dt, kino_ID_settings)
151+
kino_ID = KinodynamicsID(model_handler, dt_simu, kino_ID_settings)
150152

151153

152154
""" Initialize simulation"""
153155
device = BulletRobot(
154156
model_handler.getModel().names,
155157
erd.getModelPath(URDF_SUBPATH),
156158
URDF_SUBPATH,
157-
1e-3,
159+
dt_simu,
158160
model_handler.getModel(),
159161
model_handler.getReferenceState()[:3],
160162
)
@@ -191,7 +193,6 @@
191193
solve_time = []
192194
L_measured = []
193195

194-
N_simu = 10
195196
v = np.zeros(6)
196197
v[0] = 0.2
197198
mpc.velocity_base = v
@@ -249,12 +250,12 @@
249250
)
250251

251252
for sub_step in range(N_simu):
252-
t = (step * N_simu + sub_step) * dt
253+
t = step * dt_mpc + sub_step * dt_simu
253254

254-
delay = sub_step / float(N_simu) * dt
255-
xs_interp = interpolator.interpolateLinear(delay, dt, xss)
256-
acc_interp = interpolator.interpolateLinear(delay, dt, ddqs)
257-
force_interp = interpolator.interpolateLinear(delay, dt, forces).reshape((4,3))
255+
delay = sub_step / float(N_simu) * dt_mpc
256+
xs_interp = interpolator.interpolateLinear(delay, dt_mpc, xss)
257+
acc_interp = interpolator.interpolateLinear(delay, dt_mpc, ddqs)
258+
force_interp = interpolator.interpolateLinear(delay, dt_mpc, forces).reshape((4,3))
258259

259260
q_interp = xs_interp[:mpc.getModelHandler().getModel().nq]
260261
v_interp = xs_interp[mpc.getModelHandler().getModel().nq:]

examples/talos_kinodynamics.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from bullet_robot import BulletRobot
55
import time
66
from utils import loadTalos
7-
from simple_mpc import RobotModelHandler, RobotDataHandler, KinodynamicsOCP, MPC
7+
from simple_mpc import RobotModelHandler, RobotDataHandler, Interpolator, KinodynamicsOCP, MPC, KinodynamicsID, KinodynamicsIDSettings
88

99
# RobotWrapper
1010
URDF_SUBPATH = "/talos_data/robots/talos_reduced.urdf"
@@ -73,8 +73,10 @@
7373
w_centder_ang = np.ones(3) * 0.1
7474
w_centder = np.diag(np.concatenate((w_centder_lin, w_centder_ang)))
7575

76+
dt_mpc = 0.01
77+
7678
problem_conf = dict(
77-
timestep=0.01,
79+
timestep=dt_mpc,
7880
w_x=w_x,
7981
w_u=w_u,
8082
w_cent=w_cent,
@@ -136,12 +138,30 @@
136138

137139
mpc.generateCycleHorizon(contact_phases)
138140

141+
""" Interpolation """
142+
N_simu = 10 # Number of substep the simulation does between two MPC computation
143+
dt_simu = dt_mpc/N_simu
144+
interpolator = Interpolator(model_handler.getModel())
145+
146+
""" Inverse Dynamics """
147+
kino_ID_settings = KinodynamicsIDSettings()
148+
kino_ID_settings.kp_base = 7.
149+
kino_ID_settings.kp_posture = 10.
150+
kino_ID_settings.kp_contact = 10.
151+
kino_ID_settings.w_base = 100.
152+
kino_ID_settings.w_posture = 1.
153+
kino_ID_settings.w_contact_force = 1.
154+
kino_ID_settings.w_contact_motion = 1.
155+
156+
kino_ID = KinodynamicsID(model_handler, dt_simu, kino_ID_settings)
157+
139158
""" Initialize simulation"""
159+
N_simu = 10 # Number of substep the simulation does between two MPC computation
140160
device = BulletRobot(
141161
model_handler.getModel().names,
142162
erd.getModelPath(URDF_SUBPATH),
143163
URDF_SUBPATH,
144-
1e-3,
164+
dt_simu,
145165
model_handler.getModel(),
146166
model_handler.getReferenceState()[:3],
147167
)
@@ -163,9 +183,9 @@
163183
v = np.zeros(6)
164184
v[0] = 0.2
165185
mpc.velocity_base = v
166-
for t in range(600):
167-
# print("Time " + str(t))
168-
if t == 400:
186+
for step in range(600):
187+
# print("Time " + str(step))
188+
if step == 400:
169189
print("SWITCH TO STAND")
170190
mpc.switchToStand()
171191

@@ -192,21 +212,34 @@
192212
forces1 = mpc.us[1][: nk * force_size]
193213
contact_states = mpc.ocp_handler.getContactState(0)
194214

215+
forces = [forces0, forces1]
216+
ddqs = [a0, a1]
217+
xss = [mpc.xs[0], mpc.xs[1]]
218+
uss = [mpc.us[0], mpc.us[1]]
219+
195220
device.moveMarkers(
196221
mpc.getReferencePose(0, "left_sole_link").translation,
197222
mpc.getReferencePose(0, "right_sole_link").translation,
198223
)
199224

200-
for j in range(10):
225+
for sub_step in range(N_simu):
226+
t = step * dt_mpc + sub_step * dt_simu
227+
228+
delay = sub_step / float(N_simu) * dt_mpc
229+
xs_interp = interpolator.interpolateLinear(delay, dt_mpc, xss)
230+
acc_interp = interpolator.interpolateLinear(delay, dt_mpc, ddqs)
231+
force_interp = interpolator.interpolateLinear(delay, dt_mpc, forces).reshape((2,6))
232+
233+
q_interp = xs_interp[:mpc.getModelHandler().getModel().nq]
234+
v_interp = xs_interp[mpc.getModelHandler().getModel().nq:]
235+
201236
q_meas, v_meas = device.measureState()
202237
x_measured = np.concatenate([q_meas, v_meas])
203238

204-
state_diff = model_handler.difference(x_measured, mpc.xs[0])
205239
mpc.getDataHandler().updateInternalData(x_measured, True)
206240

207-
a_interp = (10 - j) / 10 * a0 + j / 10 * a1
208-
f_interp = (10 - j) / 10 * forces0 + j / 10 * forces1
209-
210-
# TODO: use TSID to compute inverse dynamics and get tau_cmd
241+
# TODO: take 6D forces into account
242+
kino_ID.setTarget(q_interp, v_interp, acc_interp, contact_states, force_interp)
243+
tau_cmd = kino_ID.solve(t, q_meas, v_meas)
211244

212-
#device.execute(tau_cmd)
245+
device.execute(tau_cmd)

0 commit comments

Comments
 (0)