ex_4_LIPM_to_TSID.py
Go to the documentation of this file.
1 import ex_4_conf as conf
2 import LMPC_walking.second_order.plot_utils as plot_utils
3 import matplotlib.pyplot as plt
4 import numpy as np
5 from LMPC_walking.second_order.LIPM_to_whole_body import (
6  compute_foot_traj,
7  interpolate_lipm_traj,
8 )
9 
10 # import ex_4_long_conf as conf
11 
12 
13 # READ COM-COP TRAJECTORIES COMPUTED WITH LIPM MODEL
14 data = np.load(conf.DATA_FILE_LIPM)
15 com_state_x = data["com_state_x"]
16 com_state_y = data["com_state_y"]
17 cop_ref = data["cop_ref"]
18 cop_x = data["cop_x"]
19 cop_y = data["cop_y"]
20 foot_steps = data["foot_steps"]
21 
22 # INTERPOLATE WITH TIME STEP OF CONTROLLER (TSID)
23 dt_ctrl = conf.dt # time step used by TSID
24 com, dcom, ddcom, cop, contact_phase, foot_steps_ctrl = interpolate_lipm_traj(
25  conf.T_step,
26  conf.nb_steps,
27  conf.dt_mpc,
28  dt_ctrl,
29  conf.h,
30  conf.g,
31  com_state_x,
32  com_state_y,
33  cop_ref,
34  cop_x,
35  cop_y,
36 )
37 
38 # COMPUTE TRAJECTORIES FOR FEET
39 # number of time steps for traj-opt
40 N = conf.nb_steps * round(conf.T_step / conf.dt_mpc)
41 N_ctrl = int((N * conf.dt_mpc) / dt_ctrl) # number of time steps for TSID
42 foot_steps_RF = foot_steps[::2, :] # assume first foot step corresponds to right foot
43 x_RF, dx_RF, ddx_RF = compute_foot_traj(
44  foot_steps_RF, N_ctrl, dt_ctrl, conf.T_step, conf.step_height, "stance"
45 )
46 foot_steps_LF = foot_steps[1::2, :]
47 x_LF, dx_LF, ddx_LF = compute_foot_traj(
48  foot_steps_LF, N_ctrl, dt_ctrl, conf.T_step, conf.step_height, "swing"
49 )
50 
51 # SAVE COMPUTED TRAJECTORIES IN NPY FILE FOR TSID
52 np.savez(
53  conf.DATA_FILE_TSID,
54  com=com,
55  dcom=dcom,
56  ddcom=ddcom,
57  x_RF=x_RF,
58  dx_RF=dx_RF,
59  ddx_RF=ddx_RF,
60  x_LF=x_LF,
61  dx_LF=dx_LF,
62  ddx_LF=ddx_LF,
63  contact_phase=contact_phase,
64  cop=cop,
65 )
66 
67 # PLOT STUFF
68 time_ctrl = np.arange(0, round(N_ctrl * dt_ctrl, 2), dt_ctrl)
69 
70 for i in range(3):
71  plt.figure()
72  plt.plot(time_ctrl, x_RF[i, :-1], label="x RF " + str(i))
73  plt.plot(time_ctrl, x_LF[i, :-1], label="x LF " + str(i))
74  plt.legend()
75 
76 # for i in range(2):
77 # plt.figure()
78 # plt.plot(time_ctrl, dx_RF[i,:-1], label='dx RF '+str(i))
79 # plt.plot(time_ctrl, dx_LF[i,:-1], label='dx LF '+str(i))
80 # plt.legend()
81 #
82 # for i in range(2):
83 # plt.figure()
84 # plt.plot(time_ctrl, ddx_RF[i,:-1], label='ddx RF '+str(i))
85 # plt.plot(time_ctrl, ddx_LF[i,:-1], label='ddx LF '+str(i))
86 # plt.legend()
87 
88 time = np.arange(0, round(N * conf.dt_mpc, 2), conf.dt_mpc)
89 for i in range(2):
90  plt.figure()
91  plt.plot(time_ctrl, cop[i, :-1], label="CoP")
92  # plt.plot(time_ctrl, foot_steps_ctrl[i,:-1], label='Foot step')
93  plt.plot(time_ctrl, com[i, :-1], "g", label="CoM")
94  if i == 0:
95  plt.plot(time, com_state_x[:-1, 0], ":", label="CoM TO")
96  else:
97  plt.plot(time, com_state_y[:-1, 0], ":", label="CoM TO")
98  plt.legend()
99 
100 # for i in range(2):
101 # plt.figure()
102 # plt.plot(time_ctrl, dcom[i,:-1], label='CoM vel')
103 # vel_fd = (com[i,1:] - com[i,:-1]) / dt_ctrl
104 # plt.plot(time_ctrl, vel_fd, ':', label='CoM vel fin-diff')
105 # plt.legend()
106 #
107 # for i in range(2):
108 # plt.figure()
109 # plt.plot(time_ctrl, ddcom[i,:-1], label='CoM acc')
110 # acc_fd = (dcom[i,1:] - dcom[i,:-1]) / dt_ctrl
111 # plt.plot(time_ctrl, acc_fd, ':', label='CoM acc fin-diff')
112 # plt.legend()
113 
114 foot_length = conf.lxn + conf.lxp # foot size in the x-direction
115 foot_width = conf.lyn + conf.lyp # foot size in the y-direciton
116 plot_utils.plot_xy(
117  time_ctrl,
118  N_ctrl,
119  foot_length,
120  foot_width,
121  foot_steps_ctrl.T,
122  cop[0, :],
123  cop[1, :],
124  com[0, :].reshape((N_ctrl + 1, 1)),
125  com[1, :].reshape((N_ctrl + 1, 1)),
126 )
127 plt.plot(
128  com_state_x[:, 0],
129  com_state_y[:, 0],
130  "r* ",
131  markersize=15,
132 )
133 plt.gca().set_xlim([-0.2, 0.4])
134 plt.gca().set_ylim([-0.3, 0.3])


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