Skip to content
Snippets Groups Projects
Commit ee73d4bc authored by Ale's avatar Ale
Browse files

working on implementation

parent a8e53ca4
No related branches found
No related tags found
1 merge request!32Draft: Reverse-merge casadi-walking into new WBC devel branch
...@@ -48,7 +48,8 @@ class Interpolator: ...@@ -48,7 +48,8 @@ class Interpolator:
self.beta = self.v1 self.beta = self.v1
self.gamma = self.q0 self.gamma = self.q0
elif self.type == 1: # Quadratic fixed velocity elif self.type == 1: # Quadratic fixed velocity
self.alpha = 2 * (self.q1 - self.q0 - self.v0 * self.dt) / self.dt**2 self.alpha = 2 * (self.q1 - self.q0 -
self.v0 * self.dt) / self.dt**2
self.beta = self.v0 self.beta = self.v0
self.gamma = self.q0 self.gamma = self.q0
elif self.type == 2: # Quadratic time variable elif self.type == 2: # Quadratic time variable
...@@ -104,7 +105,8 @@ class Interpolator: ...@@ -104,7 +105,8 @@ class Interpolator:
plt.scatter(y=self.q0[i], x=0.0, color="violet", marker="+") plt.scatter(y=self.q0[i], x=0.0, color="violet", marker="+")
plt.scatter(y=self.q1[i], x=self.dt, color="violet", marker="+") plt.scatter(y=self.q1[i], x=self.dt, color="violet", marker="+")
if self.type == 3 and self.q2 is not None: if self.type == 3 and self.q2 is not None:
plt.scatter(y=self.q2[i], x=2 * self.dt, color="violet", marker="+") plt.scatter(y=self.q2[i], x=2 * self.dt,
color="violet", marker="+")
plt.subplot(3, 2, (i * 2) + 2) plt.subplot(3, 2, (i * 2) + 2)
plt.title("Velocity interpolation") plt.title("Velocity interpolation")
...@@ -112,10 +114,12 @@ class Interpolator: ...@@ -112,10 +114,12 @@ class Interpolator:
plt.scatter(y=self.v0[i], x=0.0, color="violet", marker="+") plt.scatter(y=self.v0[i], x=0.0, color="violet", marker="+")
plt.scatter(y=self.v1[i], x=self.dt, color="violet", marker="+") plt.scatter(y=self.v1[i], x=self.dt, color="violet", marker="+")
if self.type == 3 and self.v2 is not None: if self.type == 3 and self.v2 is not None:
plt.scatter(y=self.v2[i], x=2 * self.dt, color="violet", marker="+") plt.scatter(y=self.v2[i], x=2 * self.dt,
color="violet", marker="+")
plt.show() plt.show()
class DummyDevice: class DummyDevice:
def __init__(self): def __init__(self):
self.imu = self.IMU() self.imu = self.IMU()
...@@ -171,9 +175,10 @@ class Controller: ...@@ -171,9 +175,10 @@ class Controller:
self.result.FF = self.params.Kff_main * np.ones(12) self.result.FF = self.params.Kff_main * np.ones(12)
self.target = Target(params) self.target = Target(params)
self.velocity_task = self.target.velocity_task self.velocity_task = self.target.velocity_task
self.mpc = WB_MPC_Wrapper.MPC_Wrapper(pd, params, self.velocity_task, self.gait) self.mpc = WB_MPC_Wrapper.MPC_Wrapper(
pd, params, self.velocity_task, self.gait)
self.mpc_solved = False self.mpc_solved = False
self.k_result = 0 self.k_result = 0
self.k_solve = 0 self.k_solve = 0
...@@ -183,8 +188,8 @@ class Controller: ...@@ -183,8 +188,8 @@ class Controller:
) )
try: try:
file = np.load("/tmp/init_guess.npy", allow_pickle=True).item() file = np.load("/tmp/init_guess.npy", allow_pickle=True).item()
self.xs_init = list(file["xs"]) self.guess = {'xs': list(file['xs']), 'us': list(file['us']),
self.us_init = list(file["us"]) 'acs': file['acs'], 'fs': file['fs']}
print("Initial guess loaded \n") print("Initial guess loaded \n")
except: except:
self.xs_init = None self.xs_init = None
...@@ -223,8 +228,7 @@ class Controller: ...@@ -223,8 +228,7 @@ class Controller:
m["x_m"], m["x_m"],
self.velocity_task.copy(), self.velocity_task.copy(),
self.gait, self.gait,
self.xs_init, self.guess
self.us_init,
) )
# if self.initialized: # if self.initialized:
# self.mpc.solve( # self.mpc.solve(
...@@ -279,13 +283,12 @@ class Controller: ...@@ -279,13 +283,12 @@ class Controller:
q, v = self.interpolator.interpolate(t) q, v = self.interpolator.interpolate(t)
else: else:
q, v = self.integrate_x(m) q, v = self.integrate_x(m)
q = q[7: ] q = q[7:]
v = v[6 :] v = v[6:]
# q = xs[1][7: self.pd.nq] # q = xs[1][7: self.pd.nq]
# v = xs[1][6 + self.pd.nq:] # v = xs[1][6 + self.pd.nq:]
self.result.q_des = q[:] self.result.q_des = q[:]
self.result.v_des = v[:] self.result.v_des = v[:]
...@@ -299,8 +302,11 @@ class Controller: ...@@ -299,8 +302,11 @@ class Controller:
) )
for i in range(3) for i in range(3)
] ]
self.xs_init = self.mpc_result.xs[1:] + [self.mpc_result.xs[-1]]
self.us_init = self.mpc_result.us[1:] + [self.mpc_result.us[-1]] self.guess["xs"] = self.mpc_result.xs[1:] + [self.mpc_result.xs[-1]]
self.guess["us"] = self.mpc_result.us[1:] + [self.mpc_result.us[-1]]
self.guess["acs"] = self.mpc_result.acs[1:] + [self.mpc_result.acs[-1]]
self.guess["us"] = self.mpc_result.fs[1:] + [self.mpc_result.fs[-1]]
t_send = time.time() t_send = time.time()
self.t_send = t_send - t_mpc self.t_send = t_send - t_mpc
...@@ -464,7 +470,8 @@ class Controller: ...@@ -464,7 +470,8 @@ class Controller:
self.q[:3] = self.estimator.get_q_estimate()[:3] self.q[:3] = self.estimator.get_q_estimate()[:3]
self.q[6:] = self.estimator.get_q_estimate()[7:] self.q[6:] = self.estimator.get_q_estimate()[7:]
self.q[3:6] = quaternionToRPY(self.estimator.get_q_estimate()[3:7]).ravel() self.q[3:6] = quaternionToRPY(
self.estimator.get_q_estimate()[3:7]).ravel()
self.v = self.estimator.get_v_reference() self.v = self.estimator.get_v_reference()
return oRh, hRb, oTh return oRh, hRb, oTh
...@@ -505,7 +512,7 @@ class Controller: ...@@ -505,7 +512,7 @@ class Controller:
feedforward torque feedforward torque
""" """
q0 = m["x_m"].copy()[: 19] q0 = m["x_m"].copy()[: 19]
v0 = m["x_m"].copy()[19 :] v0 = m["x_m"].copy()[19:]
tau = np.concatenate([np.zeros(6), self.result.tau_ff.copy()]) tau = np.concatenate([np.zeros(6), self.result.tau_ff.copy()])
a = pin.aba(self.pd.model, self.pd.rdata, q0, v0, tau) a = pin.aba(self.pd.model, self.pd.rdata, q0, v0, tau)
......
...@@ -55,7 +55,7 @@ class MPC_Wrapper: ...@@ -55,7 +55,7 @@ class MPC_Wrapper:
self.last_available_result = Result(pd) self.last_available_result = Result(pd)
self.new_result = Value("b", False) self.new_result = Value("b", False)
def solve(self, k, x0, tasks, gait, xs=None, us=None): def solve(self, k, x0, tasks, gait, guess={}):
""" """
Call either the asynchronous MPC or the synchronous MPC depending on the value Call either the asynchronous MPC or the synchronous MPC depending on the value
of multiprocessing during the creation of the wrapper of multiprocessing during the creation of the wrapper
...@@ -66,7 +66,7 @@ class MPC_Wrapper: ...@@ -66,7 +66,7 @@ class MPC_Wrapper:
if self.multiprocessing: if self.multiprocessing:
self.run_MPC_asynchronous(k, x0, tasks, gait, xs, us) self.run_MPC_asynchronous(k, x0, tasks, gait, xs, us)
else: else:
self.run_MPC_synchronous(x0, tasks, gait, xs, us) self.run_MPC_synchronous(x0, tasks, gait, guess)
def get_latest_result(self): def get_latest_result(self):
""" """
...@@ -91,11 +91,11 @@ class MPC_Wrapper: ...@@ -91,11 +91,11 @@ class MPC_Wrapper:
return self.last_available_result return self.last_available_result
def run_MPC_synchronous(self, x0, tasks, gait, xs, us): def run_MPC_synchronous(self, x0, tasks, gait, guess):
""" """
Run the MPC (synchronous version) Run the MPC (synchronous version)
""" """
self.ocp.solve(x0, tasks, gait, xs, us) self.ocp.solve(x0, tasks, gait, guess)
( (
self.last_available_result.xs, self.last_available_result.xs,
self.last_available_result.us, self.last_available_result.us,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment