ex_2.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 pinocchio as pin
6 import plot_utils as plut
7 import romeo_conf as conf
8 from numpy import nan
9 from numpy.linalg import norm as norm
10 from tsid_biped import TsidBiped
11 
12 print("".center(conf.LINE_WIDTH, "#"))
13 print(" TSID - Biped Sin Tracking ".center(conf.LINE_WIDTH, "#"))
14 print("".center(conf.LINE_WIDTH, "#"), "\n")
15 
16 tsid = TsidBiped(conf, conf.viewer)
17 
18 N = conf.N_SIMULATION
19 com_pos = np.empty((3, N)) * nan
20 com_vel = np.empty((3, N)) * nan
21 com_acc = np.empty((3, N)) * nan
22 
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 # acc_des = acc_ref - Kp*pos_err - Kd*vel_err
27 
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
33 
34 sampleCom = tsid.trajCom.computeNext()
35 samplePosture = tsid.trajPosture.computeNext()
36 
37 t = 0.0
38 q, v = tsid.q, tsid.v
39 
40 for i in range(0, N):
41  time_start = time.time()
42 
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))
46 
47  tsid.comTask.setReference(sampleCom)
48  tsid.postureTask.setReference(samplePosture)
49 
50  HQPData = tsid.formulation.computeProblemData(t, q, v)
51  # if i == 0: HQPData.print_all()
52 
53  sol = tsid.solver.solve(HQPData)
54  if sol.status != 0:
55  print("QP problem could not be solved! Error code:", sol.status)
56  break
57 
58  tau = tsid.formulation.getActuatorForces(sol)
59  dv = tsid.formulation.getAccelerations(sol)
60 
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
68 
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)
73  print(
74  "\tnormal force %s: %.1f"
75  % (tsid.contactRF.name.ljust(20, "."), tsid.contactRF.getNormalForce(f))
76  )
77 
78  if tsid.formulation.checkContact(tsid.contactLF.name, sol):
79  f = tsid.formulation.getContactForce(tsid.contactLF.name, sol)
80  print(
81  "\tnormal force %s: %.1f"
82  % (tsid.contactLF.name.ljust(20, "."), tsid.contactLF.getNormalForce(f))
83  )
84 
85  print(
86  "\ttracking err %s: %.3f"
87  % (tsid.comTask.name.ljust(20, "."), norm(tsid.comTask.position_error, 2))
88  )
89  print("\t||v||: %.3f\t ||dv||: %.3f" % (norm(v, 2), norm(dv)))
90 
91  q, v = tsid.integrate_dv(q, v, dv, conf.dt)
92  t += conf.dt
93 
94  if i % conf.DISPLAY_N == 0:
95  tsid.display(q)
96 
97  time_spent = time.time() - time_start
98  if time_spent < conf.dt:
99  time.sleep(conf.dt - time_spent)
100 
101 # PLOT STUFF
102 time = np.arange(0.0, N * conf.dt, conf.dt)
103 
104 (f, ax) = plut.create_empty_figure(3, 1)
105 for i in range(3):
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]")
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_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]")
119  leg = ax[i].legend()
120  leg.get_frame().set_alpha(0.5)
121 
122 (f, ax) = plut.create_empty_figure(3, 1)
123 for i in range(3):
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]")
129  leg = ax[i].legend()
130  leg.get_frame().set_alpha(0.5)
131 
132 plt.show()
Vec3f center


tsid
Author(s): Andrea Del Prete, Justin Carpentier
autogenerated on Sun Jul 2 2023 02:21:51