Skip to content

Commit a0281cc

Browse files
committed
Updated remaining classes to simplify
1 parent 7dd41c6 commit a0281cc

9 files changed

+148
-196
lines changed

MPC_linear_tracking.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import numpy as np
1111
import matplotlib.pyplot as plt
12-
from mobotpy import models
13-
from mobotpy import integration
1412
from scipy import signal
1513
from numpy.linalg import matrix_power
1614
from numpy.linalg import inv

ackermann_kinematic.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import numpy as np
1111
import matplotlib.pyplot as plt
12-
from mobotpy import integration
13-
from mobotpy import models
12+
from mobotpy.integration import rk_four
13+
from mobotpy.models import Ackermann
1414

1515
# Set the simulation time [s] and the sample period [s]
1616
SIM_TIME = 15.0
@@ -42,12 +42,11 @@
4242
u[1, 0] = 0
4343

4444
# Let's now use the class Ackermann for plotting
45-
vehicle = models.Ackermann(ELL_W, ELL_T)
45+
vehicle = Ackermann(ELL_W, ELL_T)
4646

4747
# Run the simulation
4848
for k in range(1, N):
49-
x[:, k] = integration.rk_four(
50-
vehicle.f, x[:, k - 1], u[:, k - 1], T, ELL_W)
49+
x[:, k] = rk_four(vehicle.f, x[:, k - 1], u[:, k - 1], T)
5150
phi_L[k] = np.arctan(
5251
2 * ELL_W * np.tan(x[3, k]) / (2 * ELL_W - ELL_T * np.tan(x[3, k]))
5352
)
@@ -100,15 +99,15 @@
10099
plt.plot(x[0, :], x[1, :])
101100
plt.axis("equal")
102101
X_BL, Y_BL, X_BR, Y_BR, X_FL, Y_FL, X_FR, Y_FR, X_BD, Y_BD = vehicle.draw(
103-
x[0, 0], x[1, 0], x[2, 0], phi_L[0], phi_R[0], ELL_W, ELL_T
102+
x[0, 0], x[1, 0], x[2, 0], phi_L[0], phi_R[0]
104103
)
105104
plt.fill(X_BL, Y_BL, "k")
106105
plt.fill(X_BR, Y_BR, "k")
107106
plt.fill(X_FR, Y_FR, "k")
108107
plt.fill(X_FL, Y_FL, "k")
109108
plt.fill(X_BD, Y_BD, "C2", alpha=0.5, label="Start")
110109
X_BL, Y_BL, X_BR, Y_BR, X_FL, Y_FL, X_FR, Y_FR, X_BD, Y_BD = vehicle.draw(
111-
x[0, N - 1], x[1, N - 1], x[2, N - 1], phi_L[N - 1], phi_R[N - 1], ELL_W, ELL_T
110+
x[0, N - 1], x[1, N - 1], x[2, N - 1], phi_L[N - 1], phi_R[N - 1]
112111
)
113112
plt.fill(X_BL, Y_BL, "k")
114113
plt.fill(X_BR, Y_BR, "k")
@@ -134,8 +133,6 @@
134133
T,
135134
phi_L,
136135
phi_R,
137-
ELL_W,
138-
ELL_T,
139136
True,
140137
"../agv-book/gifs/ch3/ackermann_kinematic.gif",
141138
)

control_approx_linearization.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import numpy as np
1111
import matplotlib.pyplot as plt
12-
from mobotpy import models
13-
from mobotpy import integration
12+
from mobotpy.models import DiffDrive
13+
from mobotpy.integration import rk_four
1414
from scipy import signal
1515

1616
# Set the simulation time [s] and the sample period [s]
@@ -47,7 +47,7 @@
4747
ELL = 1.0
4848

4949
# Create a vehicle object of type DiffDrive
50-
vehicle = models.DiffDrive(ELL)
50+
vehicle = DiffDrive(ELL)
5151

5252
# %%
5353
# SIMULATE THE CLOSED-LOOP SYSTEM
@@ -81,10 +81,10 @@
8181

8282
# Compute the controls (v, omega) and convert to wheel speeds (v_L, v_R)
8383
u_unicycle = -K.gain_matrix @ (x[:, k - 1] - x_d[:, k - 1]) + u_d[:, k - 1]
84-
u[:, k] = vehicle.uni2diff(u_unicycle, ELL)
84+
u[:, k] = vehicle.uni2diff(u_unicycle)
8585

8686
# Simulate the differential drive vehicle motion
87-
x[:, k] = integration.rk_four(vehicle.f, x[:, k - 1], u[:, k - 1], T, ELL)
87+
x[:, k] = rk_four(vehicle.f, x[:, k - 1], u[:, k - 1], T)
8888

8989
# %%
9090
# MAKE PLOTS
@@ -133,13 +133,13 @@
133133
plt.plot(x_d[0, :], x_d[1, :], "C1--", label="Desired")
134134
plt.plot(x[0, :], x[1, :], "C0", label="Actual")
135135
plt.axis("equal")
136-
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(x[0, 0], x[1, 0], x[2, 0], ELL)
136+
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(x[0, 0], x[1, 0], x[2, 0])
137137
plt.fill(X_L, Y_L, "k")
138138
plt.fill(X_R, Y_R, "k")
139139
plt.fill(X_C, Y_C, "k")
140140
plt.fill(X_B, Y_B, "C2", alpha=0.5, label="Start")
141141
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(
142-
x[0, N - 1], x[1, N - 1], x[2, N - 1], ELL
142+
x[0, N - 1], x[1, N - 1], x[2, N - 1]
143143
)
144144
plt.fill(X_L, Y_L, "k")
145145
plt.fill(X_R, Y_R, "k")
@@ -160,7 +160,7 @@
160160

161161
# Create and save the animation
162162
ani = vehicle.animate_trajectory(
163-
x, x_d, T, ELL, True, "../agv-book/gifs/ch4/control_approx_linearization.gif"
163+
x, x_d, T, True, "../agv-book/gifs/ch4/control_approx_linearization.gif"
164164
)
165165

166166
# Show the movie to the screen

diffdrive_kinematic.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import numpy as np
1111
import matplotlib.pyplot as plt
12-
from mobotpy import models
12+
from mobotpy.models import DiffDrive
1313

1414
# Set the simulation time [s] and the sample period [s]
1515
SIM_TIME = 30.0
@@ -19,35 +19,38 @@
1919
t = np.arange(0.0, SIM_TIME, T)
2020
N = np.size(t)
2121

22+
# Set the track of the vehicle [m]
23+
ELL = 0.35
24+
2225
# %%
2326
# FUNCTION DEFINITIONS
2427

2528

26-
def rk_four(f, x, u, T, P):
29+
def rk_four(f, x, u, T):
2730
"""Fourth-order Runge-Kutta numerical integration."""
28-
k_1 = f(x, u, P)
29-
k_2 = f(x + T * k_1 / 2.0, u, P)
30-
k_3 = f(x + T * k_2 / 2.0, u, P)
31-
k_4 = f(x + T * k_3, u, P)
31+
k_1 = f(x, u)
32+
k_2 = f(x + T * k_1 / 2.0, u)
33+
k_3 = f(x + T * k_2 / 2.0, u)
34+
k_4 = f(x + T * k_3, u)
3235
x_new = x + T / 6.0 * (k_1 + 2.0 * k_2 + 2.0 * k_3 + k_4)
3336
return x_new
3437

3538

36-
def diffdrive_f(x, u, ell):
39+
def diffdrive_f(x, u):
3740
"""Differential drive kinematic vehicle model."""
3841
f = np.zeros(3)
3942
f[0] = 0.5 * (u[0] + u[1]) * np.cos(x[2])
4043
f[1] = 0.5 * (u[0] + u[1]) * np.sin(x[2])
41-
f[2] = 1.0 / ell * (u[1] - u[0])
44+
f[2] = 1.0 / ELL * (u[1] - u[0])
4245
return f
4346

4447

45-
def uni2diff(u, ell):
48+
def uni2diff(u):
4649
"""Convert speed and angular rate to wheel speeds."""
4750
v = u[0]
4851
omega = u[1]
49-
v_L = v - ell / 2 * omega
50-
v_R = v + ell / 2 * omega
52+
v_L = v - ELL / 2 * omega
53+
v_R = v + ELL / 2 * omega
5154
return np.array([v_L, v_R])
5255

5356

@@ -65,19 +68,16 @@ def openloop(t):
6568
x = np.zeros((3, N))
6669
u = np.zeros((2, N))
6770

68-
# Set the track of the vehicle [m]
69-
ELL = 0.35
70-
7171
# Set the initial pose [m, m, rad], velocities [m/s, m/s]
7272
x[0, 0] = 0.0
7373
x[1, 0] = 0.0
7474
x[2, 0] = np.pi / 2.0
75-
u[:, 0] = uni2diff(openloop(t[0]), ELL)
75+
u[:, 0] = uni2diff(openloop(t[0]))
7676

7777
# Run the simulation
7878
for k in range(1, N):
79-
x[:, k] = rk_four(diffdrive_f, x[:, k - 1], u[:, k - 1], T, ELL)
80-
u[:, k] = uni2diff(openloop(t[k]), ELL)
79+
x[:, k] = rk_four(diffdrive_f, x[:, k - 1], u[:, k - 1], T)
80+
u[:, k] = uni2diff(openloop(t[k]))
8181

8282
# %%
8383
# MAKE PLOTS
@@ -118,20 +118,19 @@ def openloop(t):
118118
plt.savefig("../agv-book/figs/ch3/diffdrive_kinematic_fig1.pdf")
119119

120120
# Let's now use the class DiffDrive for plotting
121-
vehicle = models.DiffDrive(ELL)
121+
vehicle = DiffDrive(ELL)
122122

123123
# Plot the position of the vehicle in the plane
124124
fig2 = plt.figure(2)
125125
plt.plot(x[0, :], x[1, :], "C0")
126126
plt.axis("equal")
127-
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(
128-
x[0, 0], x[1, 0], x[2, 0], ELL)
127+
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(x[0, 0], x[1, 0], x[2, 0])
129128
plt.fill(X_L, Y_L, "k")
130129
plt.fill(X_R, Y_R, "k")
131130
plt.fill(X_C, Y_C, "k")
132131
plt.fill(X_B, Y_B, "C2", alpha=0.5, label="Start")
133132
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(
134-
x[0, N - 1], x[1, N - 1], x[2, N - 1], ELL
133+
x[0, N - 1], x[1, N - 1], x[2, N - 1]
135134
)
136135
plt.fill(X_L, Y_L, "k")
137136
plt.fill(X_R, Y_R, "k")
@@ -151,8 +150,7 @@ def openloop(t):
151150
# MAKE AN ANIMATION
152151

153152
# Create and save the animation
154-
ani = vehicle.animate(
155-
x, T, ELL, True, "../agv-book/gifs/ch3/diffdrive_kinematic.gif")
153+
ani = vehicle.animate(x, T, True, "../agv-book/gifs/ch3/diffdrive_kinematic.gif")
156154

157155
# Show the movie to the screen
158156
plt.show()

dynamic_extension_tracking.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@
114114
u_unicycle[1] = w[1]
115115

116116
# Convert unicycle inputs to differential drive wheel speeds
117-
u[:, k] = vehicle.uni2diff(u_unicycle, ELL)
117+
u[:, k] = vehicle.uni2diff(u_unicycle)
118118

119119
# Simulate the vehicle motion
120-
x[:, k] = rk_four(vehicle.f, x[:, k - 1], u[:, k - 1], T, ELL)
120+
x[:, k] = rk_four(vehicle.f, x[:, k - 1], u[:, k - 1], T)
121121

122122
# Update the extended system states
123123
xi[0, k] = x[0, k]
@@ -172,13 +172,13 @@
172172
plt.plot(x_d[0, :], x_d[1, :], "C1--", label="Desired")
173173
plt.plot(x[0, :], x[1, :], "C0", label="Actual")
174174
plt.axis("equal")
175-
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(x[0, 0], x[1, 0], x[2, 0], ELL)
175+
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(x[0, 0], x[1, 0], x[2, 0])
176176
plt.fill(X_L, Y_L, "k")
177177
plt.fill(X_R, Y_R, "k")
178178
plt.fill(X_C, Y_C, "k")
179179
plt.fill(X_B, Y_B, "C2", alpha=0.5, label="Start")
180180
X_L, Y_L, X_R, Y_R, X_B, Y_B, X_C, Y_C = vehicle.draw(
181-
x[0, N - 1], x[1, N - 1], x[2, N - 1], ELL
181+
x[0, N - 1], x[1, N - 1], x[2, N - 1]
182182
)
183183
plt.fill(X_L, Y_L, "k")
184184
plt.fill(X_R, Y_R, "k")
@@ -199,7 +199,7 @@
199199

200200
# Create and save the animation
201201
ani = vehicle.animate_trajectory(
202-
x, x_d, T, ELL, True, "../agv-book/gifs/ch4/dynamic_extension_tracking.gif"
202+
x, x_d, T, True, "../agv-book/gifs/ch4/dynamic_extension_tracking.gif"
203203
)
204204

205205
# Show the movie to the screen

mobotpy/integration.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@
55
"""
66

77

8-
def rk_four(f, x, u, T, params):
8+
def rk_four(f, x, u, T):
99
"""
1010
Perform fourth-order Runge-Kutta numerical integration.
1111
1212
The function to integrate is f(x, u, params), where the state varables are
1313
collected in the variable x, we assume a constant input vector u over time
1414
interval T, and params is an array of the system's parameters.
1515
"""
16-
k_1 = f(x, u, params)
17-
k_2 = f(x + T * k_1 / 2.0, u, params)
18-
k_3 = f(x + T * k_2 / 2.0, u, params)
19-
k_4 = f(x + T * k_3, u, params)
16+
k_1 = f(x, u)
17+
k_2 = f(x + T * k_1 / 2.0, u)
18+
k_3 = f(x + T * k_2 / 2.0, u)
19+
k_4 = f(x + T * k_3, u)
2020
x_new = x + T / 6.0 * (k_1 + 2.0 * k_2 + 2.0 * k_3 + k_4)
2121
return x_new
2222

2323

24-
def euler_int(f, x, u, T, params):
24+
def euler_int(f, x, u, T):
2525
"""
2626
Perform Euler (trapezoidal) numerical integration.
2727
2828
The function to integrate is f(x, u, params), where the state varables are
2929
collected in the variable x, we assume a constant input vector u over time
3030
interval T, and params is an array of the system's parameters.
3131
"""
32-
x_new = x + T * f(x, u, params)
32+
x_new = x + T * f(x, u)
3333
return x_new

0 commit comments

Comments
 (0)