3 import matplotlib.pyplot
as plt
5 import pinocchio
as pin
6 import plot_utils
as plut
7 import romeo_conf
as conf
9 from numpy.linalg
import norm
as norm
10 from tsid_biped
import TsidBiped
12 print(
"".
center(conf.LINE_WIDTH,
"#"))
13 print(
" TSID - Biped Sin Tracking ".
center(conf.LINE_WIDTH,
"#"))
14 print(
"".
center(conf.LINE_WIDTH,
"#"),
"\n")
19 com_pos = np.empty((3, N)) * nan
20 com_vel = np.empty((3, N)) * nan
21 com_acc = np.empty((3, N)) * nan
23 com_pos_ref = np.empty((3, N)) * nan
24 com_vel_ref = np.empty((3, N)) * nan
25 com_acc_ref = np.empty((3, N)) * nan
26 com_acc_des = np.empty((3, N)) * nan
28 offset = tsid.robot.com(tsid.formulation.data())
29 amp = np.array([0.0, 0.05, 0.0])
30 two_pi_f = 2 * np.pi * np.array([0.0, 0.5, 0.0])
31 two_pi_f_amp = two_pi_f * amp
32 two_pi_f_squared_amp = two_pi_f * two_pi_f_amp
34 sampleCom = tsid.trajCom.computeNext()
35 samplePosture = tsid.trajPosture.computeNext()
41 time_start = time.time()
43 sampleCom.pos(offset + amp * np.sin(two_pi_f * t))
44 sampleCom.vel(two_pi_f_amp * np.cos(two_pi_f * t))
45 sampleCom.acc(-two_pi_f_squared_amp * np.sin(two_pi_f * t))
47 tsid.comTask.setReference(sampleCom)
48 tsid.postureTask.setReference(samplePosture)
50 HQPData = tsid.formulation.computeProblemData(t, q, v)
53 sol = tsid.solver.solve(HQPData)
55 print(
"QP problem could not be solved! Error code:", sol.status)
58 tau = tsid.formulation.getActuatorForces(sol)
59 dv = tsid.formulation.getAccelerations(sol)
61 com_pos[:, i] = tsid.robot.com(tsid.formulation.data())
62 com_vel[:, i] = tsid.robot.com_vel(tsid.formulation.data())
63 com_acc[:, i] = tsid.comTask.getAcceleration(dv)
64 com_pos_ref[:, i] = sampleCom.pos()
65 com_vel_ref[:, i] = sampleCom.vel()
66 com_acc_ref[:, i] = sampleCom.acc()
67 com_acc_des[:, i] = tsid.comTask.getDesiredAcceleration
69 if i % conf.PRINT_N == 0:
70 print(
"Time %.3f" % (t))
71 if tsid.formulation.checkContact(tsid.contactRF.name, sol):
72 f = tsid.formulation.getContactForce(tsid.contactRF.name, sol)
74 "\tnormal force %s: %.1f" 75 % (tsid.contactRF.name.ljust(20,
"."), tsid.contactRF.getNormalForce(f))
78 if tsid.formulation.checkContact(tsid.contactLF.name, sol):
79 f = tsid.formulation.getContactForce(tsid.contactLF.name, sol)
81 "\tnormal force %s: %.1f" 82 % (tsid.contactLF.name.ljust(20,
"."), tsid.contactLF.getNormalForce(f))
86 "\ttracking err %s: %.3f" 87 % (tsid.comTask.name.ljust(20,
"."), norm(tsid.comTask.position_error, 2))
89 print(
"\t||v||: %.3f\t ||dv||: %.3f" % (norm(v, 2), norm(dv)))
91 q, v = tsid.integrate_dv(q, v, dv, conf.dt)
94 if i % conf.DISPLAY_N == 0:
97 time_spent = time.time() - time_start
98 if time_spent < conf.dt:
99 time.sleep(conf.dt - time_spent)
102 time = np.arange(0.0, N * conf.dt, conf.dt)
104 (f, ax) = plut.create_empty_figure(3, 1)
106 ax[i].plot(time, com_pos[i, :], label=
"CoM " + str(i))
107 ax[i].plot(time, com_pos_ref[i, :],
"r:", label=
"CoM Ref " + str(i))
108 ax[i].set_xlabel(
"Time [s]")
109 ax[i].set_ylabel(
"CoM [m]")
111 leg.get_frame().set_alpha(0.5)
113 (f, ax) = plut.create_empty_figure(3, 1)
115 ax[i].plot(time, com_vel[i, :], label=
"CoM Vel " + str(i))
116 ax[i].plot(time, com_vel_ref[i, :],
"r:", label=
"CoM Vel Ref " + str(i))
117 ax[i].set_xlabel(
"Time [s]")
118 ax[i].set_ylabel(
"CoM Vel [m/s]")
120 leg.get_frame().set_alpha(0.5)
122 (f, ax) = plut.create_empty_figure(3, 1)
124 ax[i].plot(time, com_acc[i, :], label=
"CoM Acc " + str(i))
125 ax[i].plot(time, com_acc_ref[i, :],
"r:", label=
"CoM Acc Ref " + str(i))
126 ax[i].plot(time, com_acc_des[i, :],
"g--", label=
"CoM Acc Des " + str(i))
127 ax[i].set_xlabel(
"Time [s]")
128 ax[i].set_ylabel(
"CoM Acc [m/s^2]")
130 leg.get_frame().set_alpha(0.5)