|
2 | 2 | import pinocchio as pin |
3 | 3 | import example_robot_data as erd |
4 | 4 | from bullet_robot import BulletRobot |
5 | | -from simple_mpc import RobotModelHandler, RobotDataHandler, CentroidalOCP, MPC |
| 5 | +from simple_mpc import RobotModelHandler, RobotDataHandler, CentroidalOCP, MPC, CentroidalID, CentroidalIDSettings, Interpolator |
6 | 6 | from utils import loadTalos |
7 | 7 | import time |
8 | 8 |
|
|
15 | 15 |
|
16 | 16 | # Create Model and Data handler |
17 | 17 | model_handler = RobotModelHandler(rmodel, reference_configuration_name, base_joint_name) |
18 | | -model_handler.addQuadFoot("left_sole_link", base_joint_name) |
19 | | -model_handler.addQuadFoot("right_sole_link", base_joint_name) |
| 18 | +foot_points = np.array([ |
| 19 | + [0.1, 0.075, 0], |
| 20 | + [-0.1, 0.075, 0], |
| 21 | + [-0.1, -0.075, 0], |
| 22 | + [0.1, -0.075, 0], |
| 23 | +]) |
| 24 | +model_handler.addQuadFoot("left_sole_link", base_joint_name, foot_points) |
| 25 | +model_handler.addQuadFoot("right_sole_link", base_joint_name, foot_points) |
20 | 26 | model_handler.setFootReferencePlacement("left_sole_link", pin.XYZQUATToSE3(np.array([ 0.0, 0.1, 0.0, 0,0,0,1]))) |
21 | 27 | model_handler.setFootReferencePlacement("right_sole_link", pin.XYZQUATToSE3(np.array([ 0.0,-0.1, 0.0, 0,0,0,1]))) |
22 | 28 |
|
|
44 | 50 | w_angular_mom = np.diag(np.array([0.1, 0.1, 1000])) |
45 | 51 | w_angular_acc = 0.01 * np.eye(3) |
46 | 52 |
|
| 53 | +dt_mpc = 0.01 |
| 54 | + |
47 | 55 | problem_conf = dict( |
48 | | - timestep=0.01, |
| 56 | + timestep=dt_mpc, |
49 | 57 | w_u=w_u, |
50 | 58 | w_com=w_com, |
51 | 59 | w_linear_mom=w_linear_mom, |
|
101 | 109 |
|
102 | 110 | mpc.generateCycleHorizon(contact_phases) |
103 | 111 |
|
| 112 | +""" Interpolation """ |
| 113 | +N_simu = 10 # Number of substep the simulation does between two MPC computation |
| 114 | +dt_simu = dt_mpc/N_simu |
| 115 | +interpolator = Interpolator(model_handler.getModel()) |
| 116 | + |
| 117 | +""" Inverse Dynamics """ |
| 118 | +centroidal_ID_settings = CentroidalIDSettings() |
| 119 | +centroidal_ID_settings.kp_feet_tracking = 10. |
| 120 | +centroidal_ID_settings.kp_com = 10. |
| 121 | +centroidal_ID_settings.kp_posture = 1. |
| 122 | +centroidal_ID_settings.kp_contact = 10. |
| 123 | +centroidal_ID_settings.w_feet_tracking = 10 |
| 124 | +centroidal_ID_settings.w_com = 10 |
| 125 | +centroidal_ID_settings.w_posture = 0.1 |
| 126 | +centroidal_ID_settings.w_contact_force = 1. |
| 127 | +centroidal_ID_settings.w_contact_motion = 1. |
| 128 | + |
| 129 | +centroidal_ID = CentroidalID(model_handler, dt_simu, centroidal_ID_settings) |
| 130 | + |
104 | 131 | """ Initialize simulation""" |
105 | 132 | device = BulletRobot( |
106 | 133 | model_handler.getModel().names, |
|
132 | 159 | v = np.zeros(6) |
133 | 160 | v[0] = 0.2 |
134 | 161 | mpc.velocity_base = v |
135 | | -for t in range(600): |
136 | | - # print("Time " + str(t)) |
137 | | - if t == 400: |
| 162 | +for step in range(600): |
| 163 | + # print("Time " + str(step)) |
| 164 | + if step == 400: |
138 | 165 | print("SWITCH TO STAND") |
139 | 166 | mpc.switchToStand() |
140 | 167 |
|
|
157 | 184 | ) |
158 | 185 |
|
159 | 186 | contact_states = mpc.ocp_handler.getContactState(0) |
160 | | - foot_ref = [mpc.getReferencePose(0, name) for name in model_handler.getFeetFrameNames()] |
161 | | - foot_ref_next = [mpc.getReferencePose(1, name) for name in model_handler.getFeetFrameNames()] |
162 | | - dH = mpc.getStateDerivative(0)[3:9] |
| 187 | + feet_ref = [mpc.getReferencePose(0, name) for name in model_handler.getFeetFrameNames()] |
| 188 | + feet_ref_next = [mpc.getReferencePose(1, name) for name in model_handler.getFeetFrameNames()] |
| 189 | + |
| 190 | + pos_com = mpc.xs[0][:3] |
| 191 | + pos_com_next = mpc.xs[1][:3] |
163 | 192 |
|
164 | | - for j in range(10): |
165 | | - time.sleep(0.001) |
| 193 | + forces = mpc.us[0][: nk * force_size] |
| 194 | + forces_next = mpc.us[0][: nk * force_size] |
| 195 | + |
| 196 | + for sub_step in range(N_simu): |
166 | 197 | q_meas, v_meas = device.measureState() |
167 | 198 | x_measured = np.concatenate([q_meas, v_meas]) |
168 | 199 |
|
169 | | - mpc.getDataHandler().updateInternalData(x_measured, True) |
170 | | - x_centroidal = mpc.getDataHandler().getCentroidalState() |
171 | | - state_diff = mpc.xs[0] - x_centroidal |
172 | | - |
173 | | - forces = ( |
174 | | - mpc.us[0][: nk * force_size] |
175 | | - - 1 * mpc.solver.results.controlFeedbacks()[0] @ state_diff |
176 | | - ) |
| 200 | + # Interpolate solution |
| 201 | + pos_com_interp = interpolator.interpolateLinear(sub_step, N_simu, [pos_com, pos_com_next]) |
| 202 | + v_com = (pos_com_next - pos_com) / dt_simu |
177 | 203 |
|
| 204 | + feet_ref_interp = [pin.SE3.Interpolate(foot_ref, foot_ref_next, (1.0 * sub_step)/N_simu) for foot_ref, foot_ref_next in zip(feet_ref, feet_ref_next)] |
| 205 | + feet_velocity = [ pin.log6(foot_ref.actInv(foot_ref_next)) / dt_simu for foot_ref, foot_ref_next in zip(feet_ref, feet_ref_next)] |
178 | 206 |
|
179 | | - # TODO: use TSID to compute inverse dynamics and get tau_cmd |
| 207 | + forces_interp = interpolator.interpolateLinear(sub_step, N_simu, [forces, forces_next]) |
| 208 | + forces_interp = forces_interp.reshape(2,6) |
| 209 | + forces_interp = [forces_interp[i, :] for i in range(2)] |
180 | 210 |
|
| 211 | + tau_cmd = centroidal_ID.setTarget(pos_com_interp, v_com, feet_ref_interp, feet_velocity, contact_states, forces_interp) |
181 | 212 |
|
182 | | - #device.execute(tau_cmd) |
| 213 | + device.execute(tau_cmd) |
0 commit comments