ex_1.py
Go to the documentation of this file.
1 import time
2 
3 import matplotlib.pyplot as plt
4 import numpy as np
5 import plot_utils as plut
6 import romeo_conf as conf
7 from numpy import nan
8 from numpy.linalg import norm as norm
9 from tsid_biped import TsidBiped
10 
11 print("".center(conf.LINE_WIDTH, "#"))
12 print(" Test Task Space Inverse Dynamics - Biped ".center(conf.LINE_WIDTH, "#"))
13 print("".center(conf.LINE_WIDTH, "#"), "\n")
14 
15 tsid = TsidBiped(conf, conf.viewer)
16 
17 N = conf.N_SIMULATION
18 com_pos = np.empty((3, N)) * nan
19 com_vel = np.empty((3, N)) * nan
20 com_acc = np.empty((3, N)) * nan
21 
22 com_pos_ref = np.empty((3, N)) * nan
23 com_vel_ref = np.empty((3, N)) * nan
24 com_acc_ref = np.empty((3, N)) * nan
25 com_acc_des = np.empty((3, N)) * nan # acc_des = acc_ref - Kp*pos_err - Kd*vel_err
26 
27 t = 0.0
28 q, v = tsid.q, tsid.v
29 
30 for i in range(0, N):
31  time_start = time.time()
32 
33  sampleCom = tsid.trajCom.computeNext()
34  tsid.comTask.setReference(sampleCom)
35  samplePosture = tsid.trajPosture.computeNext()
36  tsid.postureTask.setReference(samplePosture)
37 
38  HQPData = tsid.formulation.computeProblemData(t, q, v)
39  # if i == 0: HQPData.print_all()
40 
41  sol = tsid.solver.solve(HQPData)
42  if sol.status != 0:
43  print("QP problem could not be solved! Error code:", sol.status)
44  break
45 
46  tau = tsid.formulation.getActuatorForces(sol)
47  dv = tsid.formulation.getAccelerations(sol)
48 
49  com_pos[:, i] = tsid.robot.com(tsid.formulation.data())
50  com_vel[:, i] = tsid.robot.com_vel(tsid.formulation.data())
51  com_acc[:, i] = tsid.comTask.getAcceleration(dv)
52  com_pos_ref[:, i] = sampleCom.pos()
53  com_vel_ref[:, i] = sampleCom.vel()
54  com_acc_ref[:, i] = sampleCom.acc()
55  com_acc_des[:, i] = tsid.comTask.getDesiredAcceleration
56 
57  if i % conf.PRINT_N == 0:
58  print(f"Time {t:.3f}")
59  if tsid.formulation.checkContact(tsid.contactRF.name, sol):
60  f = tsid.formulation.getContactForce(tsid.contactRF.name, sol)
61  print(
62  "\tnormal force {}: {:.1f}".format(
63  tsid.contactRF.name.ljust(20, "."), tsid.contactRF.getNormalForce(f)
64  )
65  )
66 
67  if tsid.formulation.checkContact(tsid.contactLF.name, sol):
68  f = tsid.formulation.getContactForce(tsid.contactLF.name, sol)
69  print(
70  "\tnormal force {}: {:.1f}".format(
71  tsid.contactLF.name.ljust(20, "."), tsid.contactLF.getNormalForce(f)
72  )
73  )
74 
75  print(
76  "\ttracking err {}: {:.3f}".format(
77  tsid.comTask.name.ljust(20, "."), norm(tsid.comTask.position_error, 2)
78  )
79  )
80  print(f"\t||v||: {norm(v, 2):.3f}\t ||dv||: {norm(dv):.3f}")
81 
82  q, v = tsid.integrate_dv(q, v, dv, conf.dt)
83  t += conf.dt
84 
85  if i % conf.DISPLAY_N == 0:
86  tsid.display(q)
87 
88  time_spent = time.time() - time_start
89  if time_spent < conf.dt:
90  time.sleep(conf.dt - time_spent)
91 
92 # PLOT STUFF
93 time = np.arange(0.0, N * conf.dt, conf.dt)
94 
95 (f, ax) = plut.create_empty_figure(3, 1)
96 for i in range(3):
97  ax[i].plot(time, com_pos[i, :], label="CoM " + str(i))
98  ax[i].plot(time, com_pos_ref[i, :], "r:", label="CoM Ref " + str(i))
99  ax[i].set_xlabel("Time [s]")
100  ax[i].set_ylabel("CoM [m]")
101  leg = ax[i].legend()
102  leg.get_frame().set_alpha(0.5)
103 
104 (f, ax) = plut.create_empty_figure(3, 1)
105 for i in range(3):
106  ax[i].plot(time, com_vel[i, :], label="CoM Vel " + str(i))
107  ax[i].plot(time, com_vel_ref[i, :], "r:", label="CoM Vel Ref " + str(i))
108  ax[i].set_xlabel("Time [s]")
109  ax[i].set_ylabel("CoM Vel [m/s]")
110  leg = ax[i].legend()
111  leg.get_frame().set_alpha(0.5)
112 
113 (f, ax) = plut.create_empty_figure(3, 1)
114 for i in range(3):
115  ax[i].plot(time, com_acc[i, :], label="CoM Acc " + str(i))
116  ax[i].plot(time, com_acc_ref[i, :], "r:", label="CoM Acc Ref " + str(i))
117  ax[i].plot(time, com_acc_des[i, :], "g--", label="CoM Acc Des " + str(i))
118  ax[i].set_xlabel("Time [s]")
119  ax[i].set_ylabel("CoM Acc [m/s^2]")
120  leg = ax[i].legend()
121  leg.get_frame().set_alpha(0.5)
122 
123 plt.show()
plot
center
Vec3f center
tsid_biped.TsidBiped
Definition: tsid_biped.py:11


tsid
Author(s): Andrea Del Prete, Justin Carpentier
autogenerated on Sat May 3 2025 02:48:16