-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPF_range.py
346 lines (257 loc) · 9.22 KB
/
PF_range.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
Example PF_range.py
Author: Joshua A. Marshall <[email protected]>
GitHub: https://github.com/botprof/agv-examples
"""
# %%
# SIMULATION SETUP
import numpy as np
import matplotlib.pyplot as plt
from mobotpy.integration import rk_four
from mobotpy.models import DiffDrive
from scipy.stats import chi2
# Set the simulation time [s] and the sample period [s]
SIM_TIME = 30.0
T = 0.5
# Create an array of time values [s]
t = np.arange(0.0, SIM_TIME, T)
N = np.size(t)
# %%
# VEHICLE SETUP
# Set the track length of the vehicle [m]
ELL = 1.0
# Create a vehicle object of type DiffDrive
vehicle = DiffDrive(ELL)
# Uncertainty in wheel speed measurements [m/s]
SIGMA_SPEED = 0.01
# Set the range [m] sensor uncertainty
SIGMA_RANGE = 0.3
# %%
# DEAD RECKONING EXAMPLE
# Set the number of particles to use
M = 1000
# Create an array of particles for each time index
x_pf = np.zeros((3, M, N))
# Set the covariance matrices
Q = np.diag([SIGMA_SPEED**2, SIGMA_SPEED**2])
# Initialize the vehicle's true state
x = np.zeros((3, N))
# Initialize a estimated pose estimate
x_hat = np.zeros((3, N))
# Initialize a covariance matrix
P_hat = np.zeros((3, 3, N))
# Set the initial process covariance
P_hat[:, :, 0] = np.diag(np.square([0.1, 0.1, 0.01]))
# Initialize the first particles on the basis of the initial uncertainty
for i in range(1, M):
x_pf[:, i, 0] = x_hat[:, 0] + np.sqrt(P_hat[:, :, 0]) @ np.random.standard_normal(3)
# Initialize the first particles on a uniform distribution over the space
# for i in range(1, M):
# x_pf[:, i, 0] = 100 * np.random.uniform(-1, 1, 3)
for i in range(1, N):
# Compute some inputs (i.e., drive around)
v = np.array([3.95, 4.05])
# Run the vehicle motion model
x[:, i] = rk_four(vehicle.f, x[:, i - 1], v, T)
# Propagate each particle through the motion model
for j in range(0, M):
# Model the proprioceptive sensors (i.e., speed and turning rate)
v_m = v + np.sqrt(Q) @ np.random.standard_normal(2)
# Propagate each particle
x_pf[:, j, i] = rk_four(vehicle.f, x_pf[:, j, i - 1], v_m, T)
# Plot the results of the dead reckoning example
plt.figure(1)
plt.plot(x_pf[0, :, 0], x_pf[1, :, 0], ".", label="Particles", alpha=0.2)
for k in range(1, N, 1):
plt.plot(x_pf[0, :, k], x_pf[1, :, k], ".", alpha=0.2)
plt.plot(x[0, :], x[1, :], "C0", label="Actual path")
plt.axis("equal")
plt.xlabel("$x$ [m]")
plt.ylabel("$y$ [m]")
plt.legend()
plt.show()
# %%
# CREATE A MAP OF FEATURES
# Set the number of features in the map
m = 50
# Set the size [m] of a square map
D_MAP = 100
# Create a map of randomly placed feature locations
f_map = np.zeros((2, m))
for i in range(0, m):
f_map[:, i] = D_MAP * (2.0 * np.random.rand(2) - 1.0)
plt.figure(2)
plt.plot(f_map[0, :], f_map[1, :], "C2*", label="Feature")
plt.axis("equal")
plt.xlabel("$x$ [m]")
plt.ylabel("$y$ [m]")
plt.legend()
plt.show()
# %%
# FUNCTION TO MODEL RANGE TO FEATURES
def range_sensor(x, f_map, R, R_MAX, R_MIN):
"""Function to model the range sensor."""
# Find the indices of features that are within range (R_MIN, R_MAX)
a = np.array([])
for i in range(0, m):
r = np.sqrt((f_map[0, i] - x[0]) ** 2 + (f_map[1, i] - x[1]) ** 2)
if np.all([r < R_MAX, r > R_MIN]):
a = np.append(a, i)
# Simulate for each time
if np.shape(a)[0] > 0:
# Assign the size of the output
k = np.shape(a)[0]
y = np.zeros(k)
# Compute the range and bearing to all features (plus sensor noise)
for i in range(0, k):
# Range measurement [m]
y[i] = (
np.sqrt(
(f_map[0, int(a[i])] - x[0]) ** 2
+ (f_map[1, int(a[i])] - x[1]) ** 2
)
+ np.sqrt(R[0, 0]) * np.random.standard_normal()
)
else:
# No features were found within the sensing range
y = np.array([])
# Return the measurements and indices map
return y, a
# %%
# PARTICLE FILTER FUNCTIONS
def pf_resample(x_pf, x_likelihood):
"""Function to resample particles."""
# Initialize a set of output particles
x_pf_resampled = np.zeros((3, M))
# Do the resampling (one way)
indices = np.searchsorted(np.cumsum(x_likelihood), np.random.random_sample(M))
for j in range(0, M):
x_pf_resampled[:, j] = x_pf[:, indices[j]]
# Return the resampled particles
return x_pf_resampled
def diffdrive_pf(x_pf, v, y, a, f_map, Q, R, T):
"""Particle filter for differential drive vehicle function."""
# Find the number of observed features
m_k = a.shape[0]
# Initialize the output
x_pf_new = np.zeros((3, M))
# Propagate the particles through the vehicle model (i.e., a priori step)
for j in range(0, M):
# Model the wheel speed measurements
v_m = v + np.sqrt(Q) @ np.random.standard_normal(2)
# Propagate each particle
x_pf_new[:, j] = rk_four(vehicle.f, x_pf[:, j], v_m, T)
# Set likelihoods all equal to start the a posteriori step
x_likelihood = 1.0 / M * np.ones(M)
# Compute the relative likelihood
if m_k > 1:
# Set up some arrays
y_hat = np.zeros((m_k, M))
y_dif = np.zeros(m_k)
# Compute some needed matrices
R_inv = np.linalg.inv(np.kron(np.identity(m_k), R))
R_det = np.linalg.det(np.kron(np.identity(m_k), R))
for j in range(0, M):
# For each visible beacon find the expected measurement
for i in range(0, m_k):
y_hat[i, j] = np.sqrt(
(f_map[0, int(a[i])] - x_pf_new[0, j]) ** 2
+ (f_map[1, int(a[i])] - x_pf_new[1, j]) ** 2
)
# Compute the relative likelihoods
y_dif = y - y_hat[:, j]
x_likelihood[j] = (
1.0
/ ((2.0 * np.pi) ** (k / 2) * np.sqrt(R_det))
* np.exp(-0.5 * y_dif.T @ R_inv @ y_dif)
)
# Normalize the likelihoods
x_likelihood /= np.sum(x_likelihood)
# Generate a set of a posteriori particles by re-sampling on the basis of the likelihoods
x_pf_new = pf_resample(x_pf_new, x_likelihood)
return x_pf_new
# %%
# RUN THE PARTICLE FILTER SIMULATION
# Initialize some arrays
x_pf = np.zeros((3, M, N))
x_hat = np.zeros((3, N))
P_hat = np.zeros((3, 3, N))
# Initialize the initial guess to a location different from the actual location
x_hat[:, 0] = x[:, 0] + np.array([0, 5.0, 0.1])
# Set some initial conditions
P_hat[:, :, 0] = np.diag(np.square([5.0, 5.0, 0.1]))
# Set the covariance matrices
Q = np.diag([SIGMA_SPEED**2, SIGMA_SPEED**2])
# Set sensor range
R_MAX = 25.0
R_MIN = 1.0
# Set the range and bearing covariance
R = np.diag([SIGMA_RANGE**2])
# Initialize the first particles on the basis of the initial uncertainty
for i in range(1, M):
x_pf[:, i, 0] = x_hat[:, 0] + np.sqrt(P_hat[:, :, 0]) @ np.random.standard_normal(3)
# Initialize the first particles on the basis of the initial uncertainty
# for i in range(1, M):
# x_pf[:, i, 0] = 100 * np.random.uniform(-1, 1, 3)
# Simulate for each time
for i in range(1, N):
# Compute some inputs (i.e., drive around)
v = np.array([3.95, 4.05])
# Run the vehicle motion model
x[:, i] = rk_four(vehicle.f, x[:, i - 1], v, T)
# Run the range and bearing sensor model
y_m, a = range_sensor(x[:, i], f_map, R, R_MAX, R_MIN)
# Run the particle filter
x_pf[:, :, i] = diffdrive_pf(x_pf[:, :, i - 1], v, y_m, a, f_map, Q, R, T)
# %%
# PLOT THE SIMULATION OUTPUTS
# Change some plot settings (optional)
plt.rc("text", usetex=True)
plt.rc("text.latex", preamble=r"\usepackage{cmbright,amsmath,bm}")
plt.rc("savefig", format="pdf")
plt.rc("savefig", bbox="tight")
# Plot the results of the particle filter simulation
plt.figure(3)
plt.plot(x_pf[0, :, 0], x_pf[1, :, 0], ".", label="Particles", alpha=0.2)
for k in range(1, N, 1):
plt.plot(x_pf[0, :, k], x_pf[1, :, k], ".", alpha=0.2)
plt.plot(x[0, :], x[1, :], "C0", label="Actual path")
plt.plot(f_map[0, :], f_map[1, :], "C2*", label="Feature")
plt.axis("equal")
plt.xlabel("$x$ [m]")
plt.ylabel("$y$ [m]")
plt.legend()
plt.show()
# Compute the mean errors and estimated covariance bounds
for i in range(0, N):
x_hat[0, i] = np.mean(x_pf[0, :, i])
x_hat[1, i] = np.mean(x_pf[1, :, i])
x_hat[2, i] = np.mean(x_pf[2, :, i])
for i in range(0, N):
P_hat[:, :, i] = np.cov(x_pf[:, :, i])
# Find the scaling factors for plotting covariance bounds
ALPHA = 0.01
s1 = chi2.isf(ALPHA, 1)
s2 = chi2.isf(ALPHA, 2)
fig5 = plt.figure(4)
sigma = np.zeros((3, N))
ax1 = plt.subplot(311)
sigma[0, :] = np.sqrt(s1 * P_hat[0, 0, :])
plt.fill_between(t, -sigma[0, :], sigma[0, :], color="C1", alpha=0.2)
plt.plot(t, x[0, :] - x_hat[0, :], "C0")
plt.ylabel(r"$e_1$ [m]")
plt.setp(ax1, xticklabels=[])
ax2 = plt.subplot(312)
sigma[1, :] = np.sqrt(s1 * P_hat[1, 1, :])
plt.fill_between(t, -sigma[1, :], sigma[1, :], color="C1", alpha=0.2)
plt.plot(t, x[1, :] - x_hat[1, :], "C0")
plt.ylabel(r"$e_2$ [m]")
plt.setp(ax2, xticklabels=[])
ax3 = plt.subplot(313)
sigma[2, :] = np.sqrt(s1 * P_hat[2, 2, :])
plt.fill_between(t, -sigma[2, :], sigma[2, :], color="C1", alpha=0.2)
plt.plot(t, x[2, :] - x_hat[2, :], "C0")
plt.ylabel(r"$e_3$ [rad]")
plt.xlabel(r"$t$ [s]")
plt.show()
# %%