3 import ex_4_conf
as conf
4 import matplotlib.pyplot
as plt
6 import plot_utils
as plut
9 from numpy.linalg
import norm
as norm
10 from tsid_biped
import TsidBiped
12 print(
"".
center(conf.LINE_WIDTH,
"#"))
13 print(
" Test Walking ".
center(conf.LINE_WIDTH,
"#"))
14 print(
"".
center(conf.LINE_WIDTH,
"#"),
"\n")
29 data = np.load(conf.DATA_FILE_TSID)
30 except FileNotFoundError
as e:
32 "To run this walking example, you need to first execute ex_4_plan_LIPM_romeo.py and ex_4_LIPM_to_TSID." 40 print(
"Using eiquadprog")
41 tsid_biped.solver = tsid.SolverHQuadProgFast(
"qp solver")
45 tsid_biped.solver = tsid.SolverProxQP(
"qp solver")
46 tsid_biped.solver.set_epsilon_absolute(1e-5)
47 tsid_biped.solver.set_maximum_iterations(int(1e6))
48 tsid_biped.solver.set_verbose(VERBOSE)
52 tsid_biped.solver = tsid.SolverOSQP(
"qp solver")
53 tsid_biped.solver.set_epsilon_absolute(1e-8)
54 tsid_biped.solver.set_maximum_iterations(int(1e6))
55 tsid_biped.solver.set_verbose(VERBOSE)
58 print(
"Please specify which solver to use.")
61 tsid_biped.solver.resize(
62 tsid_biped.formulation.nVar, tsid_biped.formulation.nEq, tsid_biped.formulation.nIn
65 N = data[
"com"].shape[1]
66 N_pre = int(conf.T_pre / conf.dt)
67 N_post = int(conf.T_post / conf.dt)
69 com_pos = np.empty((3, N + N_post)) * nan
70 com_vel = np.empty((3, N + N_post)) * nan
71 com_acc = np.empty((3, N + N_post)) * nan
72 x_LF = np.empty((3, N + N_post)) * nan
73 dx_LF = np.empty((3, N + N_post)) * nan
74 ddx_LF = np.empty((3, N + N_post)) * nan
75 ddx_LF_des = np.empty((3, N + N_post)) * nan
76 x_RF = np.empty((3, N + N_post)) * nan
77 dx_RF = np.empty((3, N + N_post)) * nan
78 ddx_RF = np.empty((3, N + N_post)) * nan
79 ddx_RF_des = np.empty((3, N + N_post)) * nan
80 f_RF = np.zeros((6, N + N_post))
81 f_LF = np.zeros((6, N + N_post))
82 cop_RF = np.zeros((2, N + N_post))
83 cop_LF = np.zeros((2, N + N_post))
84 tau = np.zeros((tsid_biped.robot.na, N + N_post))
85 q_log = np.zeros((tsid_biped.robot.nq, N + N_post))
86 v_log = np.zeros((tsid_biped.robot.nv, N + N_post))
88 contact_phase = data[
"contact_phase"]
89 com_pos_ref = np.asarray(data[
"com"])
90 com_vel_ref = np.asarray(data[
"dcom"])
91 com_acc_ref = np.asarray(data[
"ddcom"])
92 x_RF_ref = np.asarray(data[
"x_RF"])
93 dx_RF_ref = np.asarray(data[
"dx_RF"])
94 ddx_RF_ref = np.asarray(data[
"ddx_RF"])
95 x_LF_ref = np.asarray(data[
"x_LF"])
96 dx_LF_ref = np.asarray(data[
"dx_LF"])
97 ddx_LF_ref = np.asarray(data[
"ddx_LF"])
98 cop_ref = np.asarray(data[
"cop"])
100 np.empty((3, N + N_post)) * nan
103 x_rf = tsid_biped.get_placement_RF().translation
104 offset = x_rf - x_RF_ref[:, 0]
106 com_pos_ref[:, i] += offset + np.array([0.0, 0.0, 0.0])
107 x_RF_ref[:, i] += offset
108 x_LF_ref[:, i] += offset
111 q, v = tsid_biped.q, tsid_biped.v
117 input(
"Press enter to start")
118 for i
in range(-N_pre, N + N_post):
119 time_start = time.time()
122 print(
"Starting to walk (remove contact left foot)")
123 tsid_biped.remove_contact_LF()
124 elif i > 0
and i < N - 1:
125 if contact_phase[i] != contact_phase[i - 1]:
127 "Time %.3f Changing contact phase from %s to %s" 128 % (t, contact_phase[i - 1], contact_phase[i])
130 if contact_phase[i] ==
"left":
131 tsid_biped.add_contact_LF()
132 tsid_biped.remove_contact_RF()
134 tsid_biped.add_contact_RF()
135 tsid_biped.remove_contact_LF()
138 tsid_biped.set_com_ref(
139 com_pos_ref[:, 0], 0 * com_vel_ref[:, 0], 0 * com_acc_ref[:, 0]
142 tsid_biped.set_com_ref(com_pos_ref[:, i], com_vel_ref[:, i], com_acc_ref[:, i])
143 tsid_biped.set_LF_3d_ref(x_LF_ref[:, i], dx_LF_ref[:, i], ddx_LF_ref[:, i])
144 tsid_biped.set_RF_3d_ref(x_RF_ref[:, i], dx_RF_ref[:, i], ddx_RF_ref[:, i])
146 HQPData = tsid_biped.formulation.computeProblemData(t, q, v)
147 sol = tsid_biped.solver.solve(HQPData)
150 print(
"QP problem could not be solved! Error code:", sol.status)
152 if norm(v, 2) > 40.0:
153 print(
"Time %.3f Velocities are too high, stop everything!" % (t), norm(v))
159 tau[:, i] = tsid_biped.formulation.getActuatorForces(sol)
160 dv = tsid_biped.formulation.getAccelerations(sol)
163 com_pos[:, i] = tsid_biped.robot.com(tsid_biped.formulation.data())
164 com_vel[:, i] = tsid_biped.robot.com_vel(tsid_biped.formulation.data())
165 com_acc[:, i] = tsid_biped.comTask.getAcceleration(dv)
166 com_acc_des[:, i] = tsid_biped.comTask.getDesiredAcceleration
167 x_LF[:, i], dx_LF[:, i], ddx_LF[:, i] = tsid_biped.get_LF_3d_pos_vel_acc(dv)
168 if not tsid_biped.contact_LF_active:
169 ddx_LF_des[:, i] = tsid_biped.leftFootTask.getDesiredAcceleration[:3]
170 x_RF[:, i], dx_RF[:, i], ddx_RF[:, i] = tsid_biped.get_RF_3d_pos_vel_acc(dv)
171 if not tsid_biped.contact_RF_active:
172 ddx_RF_des[:, i] = tsid_biped.rightFootTask.getDesiredAcceleration[:3]
174 if tsid_biped.formulation.checkContact(tsid_biped.contactRF.name, sol):
175 T_RF = tsid_biped.contactRF.getForceGeneratorMatrix
176 f_RF[:, i] = T_RF.dot(
177 tsid_biped.formulation.getContactForce(tsid_biped.contactRF.name, sol)
179 if f_RF[2, i] > 1e-3:
180 cop_RF[0, i] = f_RF[4, i] / f_RF[2, i]
181 cop_RF[1, i] = -f_RF[3, i] / f_RF[2, i]
182 if tsid_biped.formulation.checkContact(tsid_biped.contactLF.name, sol):
183 T_LF = tsid_biped.contactLF.getForceGeneratorMatrix
184 f_LF[:, i] = T_LF.dot(
185 tsid_biped.formulation.getContactForce(tsid_biped.contactLF.name, sol)
187 if f_LF[2, i] > 1e-3:
188 cop_LF[0, i] = f_LF[4, i] / f_LF[2, i]
189 cop_LF[1, i] = -f_LF[3, i] / f_LF[2, i]
191 if i % conf.PRINT_N == 0:
192 print(
"Time %.3f" % (t))
194 tsid_biped.formulation.checkContact(tsid_biped.contactRF.name, sol)
198 "\tnormal force %s: %.1f" 199 % (tsid_biped.contactRF.name.ljust(20,
"."), f_RF[2, i])
203 tsid_biped.formulation.checkContact(tsid_biped.contactLF.name, sol)
207 "\tnormal force %s: %.1f" 208 % (tsid_biped.contactLF.name.ljust(20,
"."), f_LF[2, i])
212 "\ttracking err %s: %.3f" 214 tsid_biped.comTask.name.ljust(20,
"."),
215 norm(tsid_biped.comTask.position_error, 2),
218 print(
"\t||v||: %.3f\t ||dv||: %.3f" % (norm(v, 2), norm(dv)))
220 q, v = tsid_biped.integrate_dv(q, v, dv, conf.dt)
225 if i % conf.DISPLAY_N == 0:
226 tsid_biped.display(q)
228 time_spent = time.time() - time_start
229 if time_spent < conf.dt:
230 time.sleep(conf.dt - time_spent)
233 replay = input(
"Play video again? [Y]/n: ")
or "Y" 234 if replay.lower() ==
"y":
236 tsid_biped.display(q)
240 time = np.arange(0.0, (N + N_post) * conf.dt, conf.dt)
243 (f, ax) = plut.create_empty_figure(3, 1)
245 ax[i].plot(time, com_pos[i, :], label=
"CoM " + str(i))
246 ax[i].plot(time[:N], com_pos_ref[i, :],
"r:", label=
"CoM Ref " + str(i))
247 ax[i].set_xlabel(
"Time [s]")
248 ax[i].set_ylabel(
"CoM [m]")
250 leg.get_frame().set_alpha(0.5)
252 (f, ax) = plut.create_empty_figure(3, 1)
254 ax[i].plot(time, com_vel[i, :], label=
"CoM Vel " + str(i))
255 ax[i].plot(time[:N], com_vel_ref[i, :],
"r:", label=
"CoM Vel Ref " + str(i))
256 ax[i].set_xlabel(
"Time [s]")
257 ax[i].set_ylabel(
"CoM Vel [m/s]")
259 leg.get_frame().set_alpha(0.5)
261 (f, ax) = plut.create_empty_figure(3, 1)
263 ax[i].plot(time, com_acc[i, :], label=
"CoM Acc " + str(i))
264 ax[i].plot(time[:N], com_acc_ref[i, :],
"r:", label=
"CoM Acc Ref " + str(i))
265 ax[i].plot(time, com_acc_des[i, :],
"g--", label=
"CoM Acc Des " + str(i))
266 ax[i].set_xlabel(
"Time [s]")
267 ax[i].set_ylabel(
"CoM Acc [m/s^2]")
269 leg.get_frame().set_alpha(0.5)
272 (f, ax) = plut.create_empty_figure(2, 1)
274 ax[i].plot(time, cop_LF[i, :], label=
"CoP LF " + str(i))
275 ax[i].plot(time, cop_RF[i, :], label=
"CoP RF " + str(i))
280 [-conf.lxn, -conf.lxn],
282 label=
"CoP Lim " + str(i),
286 [conf.lxp, conf.lxp],
288 label=
"CoP Lim " + str(i),
293 [-conf.lyn, -conf.lyn],
295 label=
"CoP Lim " + str(i),
299 [conf.lyp, conf.lyp],
301 label=
"CoP Lim " + str(i),
303 ax[i].set_xlabel(
"Time [s]")
304 ax[i].set_ylabel(
"CoP [m]")
306 leg.get_frame().set_alpha(0.5)
322 plt.plot(time, x_RF[i, :], label=
"x RF " + str(i))
323 plt.plot(time[:N], x_RF_ref[i, :],
":", label=
"x RF ref " + str(i))
324 plt.plot(time, x_LF[i, :], label=
"x LF " + str(i))
325 plt.plot(time[:N], x_LF_ref[i, :],
":", label=
"x LF ref " + str(i))
348 for i
in range(tsid_biped.robot.na):
351 * (tau[i, :] - tsid_biped.tau_min[i])
352 / (tsid_biped.tau_max[i] - tsid_biped.tau_min[i])
356 if np.max(np.abs(tau_normalized)) > 0.5:
358 time, tau_normalized, alpha=0.5, label=tsid_biped.model.names[i + 2]
360 plt.plot([time[0], time[-1]], 2 * [-1.0],
":")
361 plt.plot([time[0], time[-1]], 2 * [1.0],
":")
362 plt.gca().set_xlabel(
"Time [s]")
363 plt.gca().set_ylabel(
"Normalized Torque")
365 leg.get_frame().set_alpha(0.5)
369 for i
in range(tsid_biped.robot.na):
372 * (v_log[6 + i, :] - tsid_biped.v_min[i])
373 / (tsid_biped.v_max[i] - tsid_biped.v_min[i])
377 if np.max(np.abs(v_normalized)) > 0.5:
378 plt.plot(time, v_normalized, alpha=0.5, label=tsid_biped.model.names[i + 2])
379 plt.plot([time[0], time[-1]], 2 * [-1.0],
":")
380 plt.plot([time[0], time[-1]], 2 * [1.0],
":")
381 plt.gca().set_xlabel(
"Time [s]")
382 plt.gca().set_ylabel(
"Normalized Joint Vel")