|
4 | 4 | from bullet_robot import BulletRobot |
5 | 5 | import time |
6 | 6 | from utils import loadTalos |
7 | | -from simple_mpc import RobotModelHandler, RobotDataHandler, KinodynamicsOCP, MPC |
| 7 | +from simple_mpc import RobotModelHandler, RobotDataHandler, Interpolator, KinodynamicsOCP, MPC, KinodynamicsID, KinodynamicsIDSettings |
8 | 8 |
|
9 | 9 | # RobotWrapper |
10 | 10 | URDF_SUBPATH = "/talos_data/robots/talos_reduced.urdf" |
|
73 | 73 | w_centder_ang = np.ones(3) * 0.1 |
74 | 74 | w_centder = np.diag(np.concatenate((w_centder_lin, w_centder_ang))) |
75 | 75 |
|
| 76 | +dt_mpc = 0.01 |
| 77 | + |
76 | 78 | problem_conf = dict( |
77 | | - timestep=0.01, |
| 79 | + timestep=dt_mpc, |
78 | 80 | w_x=w_x, |
79 | 81 | w_u=w_u, |
80 | 82 | w_cent=w_cent, |
|
136 | 138 |
|
137 | 139 | mpc.generateCycleHorizon(contact_phases) |
138 | 140 |
|
| 141 | +""" Interpolation """ |
| 142 | +N_simu = 10 # Number of substep the simulation does between two MPC computation |
| 143 | +dt_simu = dt_mpc/N_simu |
| 144 | +interpolator = Interpolator(model_handler.getModel()) |
| 145 | + |
| 146 | +""" Inverse Dynamics """ |
| 147 | +kino_ID_settings = KinodynamicsIDSettings() |
| 148 | +kino_ID_settings.kp_base = 7. |
| 149 | +kino_ID_settings.kp_posture = 10. |
| 150 | +kino_ID_settings.kp_contact = 10. |
| 151 | +kino_ID_settings.w_base = 100. |
| 152 | +kino_ID_settings.w_posture = 1. |
| 153 | +kino_ID_settings.w_contact_force = 1. |
| 154 | +kino_ID_settings.w_contact_motion = 1. |
| 155 | + |
| 156 | +kino_ID = KinodynamicsID(model_handler, dt_simu, kino_ID_settings) |
| 157 | + |
139 | 158 | """ Initialize simulation""" |
| 159 | +N_simu = 10 # Number of substep the simulation does between two MPC computation |
140 | 160 | device = BulletRobot( |
141 | 161 | model_handler.getModel().names, |
142 | 162 | erd.getModelPath(URDF_SUBPATH), |
143 | 163 | URDF_SUBPATH, |
144 | | - 1e-3, |
| 164 | + dt_simu, |
145 | 165 | model_handler.getModel(), |
146 | 166 | model_handler.getReferenceState()[:3], |
147 | 167 | ) |
|
163 | 183 | v = np.zeros(6) |
164 | 184 | v[0] = 0.2 |
165 | 185 | mpc.velocity_base = v |
166 | | -for t in range(600): |
167 | | - # print("Time " + str(t)) |
168 | | - if t == 400: |
| 186 | +for step in range(600): |
| 187 | + # print("Time " + str(step)) |
| 188 | + if step == 400: |
169 | 189 | print("SWITCH TO STAND") |
170 | 190 | mpc.switchToStand() |
171 | 191 |
|
|
192 | 212 | forces1 = mpc.us[1][: nk * force_size] |
193 | 213 | contact_states = mpc.ocp_handler.getContactState(0) |
194 | 214 |
|
| 215 | + forces = [forces0, forces1] |
| 216 | + ddqs = [a0, a1] |
| 217 | + xss = [mpc.xs[0], mpc.xs[1]] |
| 218 | + uss = [mpc.us[0], mpc.us[1]] |
| 219 | + |
195 | 220 | device.moveMarkers( |
196 | 221 | mpc.getReferencePose(0, "left_sole_link").translation, |
197 | 222 | mpc.getReferencePose(0, "right_sole_link").translation, |
198 | 223 | ) |
199 | 224 |
|
200 | | - for j in range(10): |
| 225 | + for sub_step in range(N_simu): |
| 226 | + t = step * dt_mpc + sub_step * dt_simu |
| 227 | + |
| 228 | + delay = sub_step / float(N_simu) * dt_mpc |
| 229 | + xs_interp = interpolator.interpolateLinear(delay, dt_mpc, xss) |
| 230 | + acc_interp = interpolator.interpolateLinear(delay, dt_mpc, ddqs) |
| 231 | + force_interp = interpolator.interpolateLinear(delay, dt_mpc, forces).reshape((2,6)) |
| 232 | + |
| 233 | + q_interp = xs_interp[:mpc.getModelHandler().getModel().nq] |
| 234 | + v_interp = xs_interp[mpc.getModelHandler().getModel().nq:] |
| 235 | + |
201 | 236 | q_meas, v_meas = device.measureState() |
202 | 237 | x_measured = np.concatenate([q_meas, v_meas]) |
203 | 238 |
|
204 | | - state_diff = model_handler.difference(x_measured, mpc.xs[0]) |
205 | 239 | mpc.getDataHandler().updateInternalData(x_measured, True) |
206 | 240 |
|
207 | | - a_interp = (10 - j) / 10 * a0 + j / 10 * a1 |
208 | | - f_interp = (10 - j) / 10 * forces0 + j / 10 * forces1 |
209 | | - |
210 | | - # TODO: use TSID to compute inverse dynamics and get tau_cmd |
| 241 | + # TODO: take 6D forces into account |
| 242 | + kino_ID.setTarget(q_interp, v_interp, acc_interp, contact_states, force_interp) |
| 243 | + tau_cmd = kino_ID.solve(t, q_meas, v_meas) |
211 | 244 |
|
212 | | - #device.execute(tau_cmd) |
| 245 | + device.execute(tau_cmd) |
0 commit comments