Skip to content

Commit 79c2cf6

Browse files
committed
adding wheels kinematics
1 parent 49e76cc commit 79c2cf6

File tree

4 files changed

+92
-17
lines changed

4 files changed

+92
-17
lines changed

proj/model/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Config:
5252

5353
USE_FAST = True # if true use cumba's methods
5454
SPAWN_TYPE = "trajectory"
55-
LIVE_PLOT = False
55+
LIVE_PLOT = True
5656

5757
mouse_type = "realistic"
5858
model_type = "cart"
@@ -76,7 +76,7 @@ class Config:
7676

7777
# ------------------------------ Planning params ----------------------------- #
7878
planning = dict( # params used to compute goal states to be used for control
79-
prediction_length=20,
79+
prediction_length=80,
8080
n_ahead=5, # start prediction states from N steps ahead
8181
)
8282

proj/model/model.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class Model(Config):
5555

5656
_control = namedtuple("control", "tau_r, tau_l")
5757
_state = namedtuple("state", "x, y, theta, v, omega")
58-
_dxdt = namedtuple("dxdt", "x_dot, y_dt, theta_dot, v_dot, omega_dot")
58+
_dxdt = namedtuple("dxdt", "x_dot, y_dot, theta_dot, v_dot, omega_dot")
59+
_wheel_state = namedtuple("wheel_state", "nudot_right, nudot_left")
5960

6061
def __init__(self, startup=True):
6162
Config.__init__(self)
@@ -71,6 +72,8 @@ def __init__(self, startup=True):
7172
self.calc_dqdt = fast_dqdt
7273
self.calc_model_jacobian_state = fast_model_jacobian_state
7374
self.calc_model_jacobian_input = fast_model_jacobian_input
75+
76+
self.get_wheels_dynamics()
7477
self.reset()
7578

7679
def reset(self):
@@ -85,10 +88,12 @@ def reset(self):
8588
r=[],
8689
gamma=[],
8790
trajectory_idx=[],
91+
nudot_left=[], # acceleration of left wheel
92+
nudot_right=[], # acceleration of right wheel
8893
)
8994

9095
def _append_history(self):
91-
for ntuple in [self.curr_x, self.curr_control]:
96+
for ntuple in [self.curr_x, self.curr_control, self.curr_wheel_state]:
9297
for k, v in ntuple._asdict().items():
9398
self.history[k].append(v)
9499

@@ -251,6 +256,43 @@ def get_jacobians(self):
251256
args, self.model_jacobian_input, modules="numpy"
252257
)
253258

259+
def get_wheels_dynamics(self):
260+
(
261+
x,
262+
y,
263+
theta,
264+
L,
265+
R,
266+
m,
267+
m_w,
268+
d,
269+
tau_l,
270+
tau_r,
271+
v,
272+
omega,
273+
) = self.variables.values()
274+
275+
x_dot, y_dot, theta_dot = symbols("xdot, ydot, thetadot")
276+
nu_l_dot, nu_r_dot = symbols("nudot_L, nudot_R")
277+
278+
vels = Matrix([x_dot, y_dot, theta_dot])
279+
K = Matrix(
280+
[
281+
[R / 2 * cos(theta), R / 2 * cos(theta)],
282+
[R / 2 * sin(theta), R / 2 * sin(theta)],
283+
[R / (2 * L), R / (2 * L)],
284+
]
285+
)
286+
287+
# In the model you can use the wheels accelerations
288+
# to get the x,y,theta velocity.
289+
# Here we do the inverse, given x,y,theta velocities
290+
# we get the wheel's accelerations
291+
292+
nu = K.pinv() * vels
293+
args = [L, R, theta, x_dot, y_dot, theta_dot]
294+
self.calc_wheels_accels = lambdify(args, nu, modules="numpy")
295+
254296
def step(self, u):
255297
u = self._control(*np.array(u))
256298
self.curr_x = self._state(*self.curr_x)
@@ -268,6 +310,17 @@ def step(self, u):
268310
next_x = np.array(self.curr_x) + dxdt * self.dt
269311
self.curr_x = self._state(*next_x)
270312

313+
# Compute wheel accelerations
314+
w = self.calc_wheels_accels(
315+
variables["L"],
316+
variables["R"],
317+
variables["theta"],
318+
self.curr_dxdt.x_dot,
319+
self.curr_dxdt.y_dot,
320+
self.curr_dxdt.theta_dot,
321+
)
322+
self.curr_wheel_state = self._wheel_state(*w.ravel())
323+
271324
# Update history
272325
self.curr_control = u
273326
self._append_history()

proj/plotting/results.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,21 @@ def _plot_cost(cost_history, ax=None):
135135

136136

137137
def _plot_integrals(history, dt, tax=None, aax=None):
138-
R, L = np.cumsum(history["tau_r"]), np.cumsum(history["tau_l"])
138+
R, L = history["nudot_right"], history["nudot_left"]
139139

140140
plot_line_outlined(
141141
tax,
142142
R,
143143
color=desaturate_color(colors["tau_r"]),
144-
label="R_wheel_speed",
144+
label="R_wheel_accel",
145145
lw=2,
146146
solid_capstyle="round",
147147
)
148148
plot_line_outlined(
149149
tax,
150150
L,
151151
color=desaturate_color(colors["tau_l"]),
152-
label="L_wheel_speed",
152+
label="L_wheel_accel",
153153
lw=2,
154154
solid_capstyle="round",
155155
)

workspace.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
# %%
2-
from proj.utils.misc import load_results_from_folder
2+
from proj import Model
3+
from sympy import Matrix, symbols, sin, cos
34

4-
fld = "/Users/federicoclaudi/Dropbox (UCL - SWC)/Rotation_vte/Locomotion/control/tracking_200923_141709_1491_good_example/results"
5+
model = Model()
56

67
# %%
7-
config, trajectory, history, cost_history = load_results_from_folder(fld)
8+
(
9+
x,
10+
y,
11+
theta,
12+
L,
13+
R,
14+
m,
15+
m_w,
16+
d,
17+
tau_l,
18+
tau_r,
19+
v,
20+
omega,
21+
) = model.variables.values()
822

923
# %%
10-
import matplotlib.pyplot as plt
24+
x_dot, y_dot, theta_dot = symbols("xdot, ydot, thetadot")
1125

12-
plt.plot(cost_history["x"])
26+
nu_l_dot, nu_r_dot = symbols("nudot_L, nudot_R")
1327

14-
# %%
15-
plt.plot(history["tau_r"])
16-
plt.plot(history["tau_l"])
1728

18-
# %%
19-
plt.plot(history["v"])
29+
vels = Matrix([x_dot, y_dot, theta_dot])
30+
31+
K = Matrix(
32+
[
33+
[R / 2 * cos(theta), R / 2 * cos(theta)],
34+
[R / 2 * sin(theta), R / 2 * sin(theta)],
35+
[R / (2 * L), R / (2 * L)],
36+
]
37+
)
38+
39+
nu = Matrix([nu_l_dot, nu_r_dot])
2040

41+
nu = K.pinv() * vels
42+
nu
2143
# %%

0 commit comments

Comments
 (0)