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 N = conf.nb_steps * int(
40  round(conf.T_step / conf.dt_mpc)
41 ) # number of time steps for traj-opt
42 N_ctrl = int((N * conf.dt_mpc) / dt_ctrl) # number of time steps for TSID
43 foot_steps_RF = foot_steps[::2, :] # assume first foot step corresponds to right foot
44 x_RF, dx_RF, ddx_RF = compute_foot_traj(
45  foot_steps_RF, N_ctrl, dt_ctrl, conf.T_step, conf.step_height, "stance"
46 )
47 foot_steps_LF = foot_steps[1::2, :]
48 x_LF, dx_LF, ddx_LF = compute_foot_traj(
49  foot_steps_LF, N_ctrl, dt_ctrl, conf.T_step, conf.step_height, "swing"
50 )
51 
52 # SAVE COMPUTED TRAJECTORIES IN NPY FILE FOR TSID
53 np.savez(
54  conf.DATA_FILE_TSID,
55  com=com,
56  dcom=dcom,
57  ddcom=ddcom,
58  x_RF=x_RF,
59  dx_RF=dx_RF,
60  ddx_RF=ddx_RF,
61  x_LF=x_LF,
62  dx_LF=dx_LF,
63  ddx_LF=ddx_LF,
64  contact_phase=contact_phase,
65  cop=cop,
66 )
67 
68 # PLOT STUFF
69 time_ctrl = np.arange(0, round(N_ctrl * dt_ctrl, 2), dt_ctrl)
70 
71 for i in range(3):
72  plt.figure()
73  plt.plot(time_ctrl, x_RF[i, :-1], label="x RF " + str(i))
74  plt.plot(time_ctrl, x_LF[i, :-1], label="x LF " + str(i))
75  plt.legend()
76 
77 # for i in range(2):
78 # plt.figure()
79 # plt.plot(time_ctrl, dx_RF[i,:-1], label='dx RF '+str(i))
80 # plt.plot(time_ctrl, dx_LF[i,:-1], label='dx LF '+str(i))
81 # plt.legend()
82 #
83 # for i in range(2):
84 # plt.figure()
85 # plt.plot(time_ctrl, ddx_RF[i,:-1], label='ddx RF '+str(i))
86 # plt.plot(time_ctrl, ddx_LF[i,:-1], label='ddx LF '+str(i))
87 # plt.legend()
88 
89 time = np.arange(0, round(N * conf.dt_mpc, 2), conf.dt_mpc)
90 for i in range(2):
91  plt.figure()
92  plt.plot(time_ctrl, cop[i, :-1], label="CoP")
93  # plt.plot(time_ctrl, foot_steps_ctrl[i,:-1], label='Foot step')
94  plt.plot(time_ctrl, com[i, :-1], "g", label="CoM")
95  if i == 0:
96  plt.plot(time, com_state_x[:-1, 0], ":", label="CoM TO")
97  else:
98  plt.plot(time, com_state_y[:-1, 0], ":", label="CoM TO")
99  plt.legend()
100 
101 # for i in range(2):
102 # plt.figure()
103 # plt.plot(time_ctrl, dcom[i,:-1], label='CoM vel')
104 # vel_fd = (com[i,1:] - com[i,:-1]) / dt_ctrl
105 # plt.plot(time_ctrl, vel_fd, ':', label='CoM vel fin-diff')
106 # plt.legend()
107 #
108 # for i in range(2):
109 # plt.figure()
110 # plt.plot(time_ctrl, ddcom[i,:-1], label='CoM acc')
111 # acc_fd = (dcom[i,1:] - dcom[i,:-1]) / dt_ctrl
112 # plt.plot(time_ctrl, acc_fd, ':', label='CoM acc fin-diff')
113 # plt.legend()
114 
115 foot_length = conf.lxn + conf.lxp # foot size in the x-direction
116 foot_width = conf.lyn + conf.lyp # foot size in the y-direciton
117 plot_utils.plot_xy(
118  time_ctrl,
119  N_ctrl,
120  foot_length,
121  foot_width,
122  foot_steps_ctrl.T,
123  cop[0, :],
124  cop[1, :],
125  com[0, :].reshape((N_ctrl + 1, 1)),
126  com[1, :].reshape((N_ctrl + 1, 1)),
127 )
128 plt.plot(
129  com_state_x[:, 0],
130  com_state_y[:, 0],
131  "r* ",
132  markersize=15,
133 )
134 plt.gca().set_xlim([-0.2, 0.4])
135 plt.gca().set_ylim([-0.3, 0.3])


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