Skip to content

Commit 07f0781

Browse files
committed
2 parents 8fb4b8a + 0cca27d commit 07f0781

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

Localization/particle_filter/particle_filter.py

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
77
"""
88

9-
import numpy as np
109
import math
10+
1111
import matplotlib.pyplot as plt
12+
import numpy as np
1213

1314
# Estimation parameter of PF
14-
Q = np.diag([0.1])**2 # range error
15-
R = np.diag([1.0, np.deg2rad(40.0)])**2 # input error
15+
Q = np.diag([0.2]) ** 2 # range error
16+
R = np.diag([2.0, np.deg2rad(40.0)]) ** 2 # input error
1617

1718
# Simulation parameter
18-
Qsim = np.diag([0.2])**2
19-
Rsim = np.diag([1.0, np.deg2rad(30.0)])**2
19+
Q_sim = np.diag([0.2]) ** 2
20+
R_sim = np.diag([1.0, np.deg2rad(30.0)]) ** 2
2021

2122
DT = 0.1 # time tick [s]
2223
SIM_TIME = 50.0 # simulation time [s]
@@ -31,31 +32,30 @@
3132

3233
def calc_input():
3334
v = 1.0 # [m/s]
34-
yawrate = 0.1 # [rad/s]
35-
u = np.array([[v, yawrate]]).T
35+
yaw_rate = 0.1 # [rad/s]
36+
u = np.array([[v, yaw_rate]]).T
3637
return u
3738

3839

39-
def observation(xTrue, xd, u, RFID):
40-
40+
def observation(xTrue, xd, u, RF_ID):
4141
xTrue = motion_model(xTrue, u)
4242

4343
# add noise to gps x-y
4444
z = np.zeros((0, 3))
4545

46-
for i in range(len(RFID[:, 0])):
46+
for i in range(len(RF_ID[:, 0])):
4747

48-
dx = xTrue[0, 0] - RFID[i, 0]
49-
dy = xTrue[1, 0] - RFID[i, 1]
50-
d = math.sqrt(dx**2 + dy**2)
48+
dx = xTrue[0, 0] - RF_ID[i, 0]
49+
dy = xTrue[1, 0] - RF_ID[i, 1]
50+
d = math.sqrt(dx ** 2 + dy ** 2)
5151
if d <= MAX_RANGE:
52-
dn = d + np.random.randn() * Qsim[0, 0] # add noise
53-
zi = np.array([[dn, RFID[i, 0], RFID[i, 1]]])
52+
dn = d + np.random.randn() * Q_sim[0, 0] ** 0.5 # add noise
53+
zi = np.array([[dn, RF_ID[i, 0], RF_ID[i, 1]]])
5454
z = np.vstack((z, zi))
5555

5656
# add noise to input
57-
ud1 = u[0, 0] + np.random.randn() * Rsim[0, 0]
58-
ud2 = u[1, 0] + np.random.randn() * Rsim[1, 1]
57+
ud1 = u[0, 0] + np.random.randn() * R_sim[0, 0] ** 0.5
58+
ud2 = u[1, 0] + np.random.randn() * R_sim[1, 1] ** 0.5
5959
ud = np.array([[ud1, ud2]]).T
6060

6161
xd = motion_model(xd, ud)
@@ -64,7 +64,6 @@ def observation(xTrue, xd, u, RFID):
6464

6565

6666
def motion_model(x, u):
67-
6867
F = np.array([[1.0, 0, 0, 0],
6968
[0, 1.0, 0, 0],
7069
[0, 0, 1.0, 0],
@@ -93,30 +92,32 @@ def calc_covariance(xEst, px, pw):
9392
for i in range(px.shape[1]):
9493
dx = (px[:, i] - xEst)[0:3]
9594
cov += pw[0, i] * dx.dot(dx.T)
95+
cov /= NP
9696

9797
return cov
9898

9999

100-
def pf_localization(px, pw, xEst, PEst, z, u):
100+
def pf_localization(px, pw, z, u):
101101
"""
102102
Localization with Particle filter
103103
"""
104104

105105
for ip in range(NP):
106106
x = np.array([px[:, ip]]).T
107107
w = pw[0, ip]
108+
108109
# Predict with random input sampling
109-
ud1 = u[0, 0] + np.random.randn() * Rsim[0, 0]
110-
ud2 = u[1, 0] + np.random.randn() * Rsim[1, 1]
110+
ud1 = u[0, 0] + np.random.randn() * R[0, 0] ** 0.5
111+
ud2 = u[1, 0] + np.random.randn() * R[1, 1] ** 0.5
111112
ud = np.array([[ud1, ud2]]).T
112113
x = motion_model(x, ud)
113114

114115
# Calc Importance Weight
115116
for i in range(len(z[:, 0])):
116117
dx = x[0, 0] - z[i, 1]
117118
dy = x[1, 0] - z[i, 2]
118-
prez = math.sqrt(dx**2 + dy**2)
119-
dz = prez - z[i, 0]
119+
pre_z = math.sqrt(dx ** 2 + dy ** 2)
120+
dz = pre_z - z[i, 0]
120121
w = w * gauss_likelihood(dz, math.sqrt(Q[0, 0]))
121122

122123
px[:, ip] = x[:, 0]
@@ -127,66 +128,66 @@ def pf_localization(px, pw, xEst, PEst, z, u):
127128
xEst = px.dot(pw.T)
128129
PEst = calc_covariance(xEst, px, pw)
129130

130-
px, pw = resampling(px, pw)
131+
px, pw = re_sampling(px, pw)
131132

132133
return xEst, PEst, px, pw
133134

134135

135-
def resampling(px, pw):
136+
def re_sampling(px, pw):
136137
"""
137138
low variance re-sampling
138139
"""
139140

140-
Neff = 1.0 / (pw.dot(pw.T))[0, 0] # Effective particle number
141-
if Neff < NTh:
142-
wcum = np.cumsum(pw)
141+
N_eff = 1.0 / (pw.dot(pw.T))[0, 0] # Effective particle number
142+
if N_eff < NTh:
143+
w_cum = np.cumsum(pw)
143144
base = np.cumsum(pw * 0.0 + 1 / NP) - 1 / NP
144-
resampleid = base + np.random.rand(base.shape[0]) / NP
145+
re_sample_id = base + np.random.rand(base.shape[0]) / NP
145146

146-
inds = []
147+
indexes = []
147148
ind = 0
148149
for ip in range(NP):
149-
while resampleid[ip] > wcum[ind]:
150+
while re_sample_id[ip] > w_cum[ind]:
150151
ind += 1
151-
inds.append(ind)
152+
indexes.append(ind)
152153

153-
px = px[:, inds]
154+
px = px[:, indexes]
154155
pw = np.zeros((1, NP)) + 1.0 / NP # init weight
155156

156157
return px, pw
157158

158159

159160
def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
160161
Pxy = PEst[0:2, 0:2]
161-
eigval, eigvec = np.linalg.eig(Pxy)
162+
eig_val, eig_vec = np.linalg.eig(Pxy)
162163

163-
if eigval[0] >= eigval[1]:
164-
bigind = 0
165-
smallind = 1
164+
if eig_val[0] >= eig_val[1]:
165+
big_ind = 0
166+
small_ind = 1
166167
else:
167-
bigind = 1
168-
smallind = 0
168+
big_ind = 1
169+
small_ind = 0
169170

170171
t = np.arange(0, 2 * math.pi + 0.1, 0.1)
171172

172-
# eigval[bigind] or eiqval[smallind] were occassionally negative numbers extremely
173+
# eig_val[big_ind] or eiq_val[small_ind] were occasionally negative numbers extremely
173174
# close to 0 (~10^-20), catch these cases and set the respective variable to 0
174175
try:
175-
a = math.sqrt(eigval[bigind])
176+
a = math.sqrt(eig_val[big_ind])
176177
except ValueError:
177178
a = 0
178179

179180
try:
180-
b = math.sqrt(eigval[smallind])
181+
b = math.sqrt(eig_val[small_ind])
181182
except ValueError:
182183
b = 0
183184

184185
x = [a * math.cos(it) for it in t]
185186
y = [b * math.sin(it) for it in t]
186-
angle = math.atan2(eigvec[bigind, 1], eigvec[bigind, 0])
187-
R = np.array([[math.cos(angle), math.sin(angle)],
188-
[-math.sin(angle), math.cos(angle)]])
189-
fx = R.dot(np.array([[x, y]]))
187+
angle = math.atan2(eig_vec[big_ind, 1], eig_vec[big_ind, 0])
188+
Rot = np.array([[math.cos(angle), -math.sin(angle)],
189+
[math.sin(angle), math.cos(angle)]])
190+
fx = Rot.dot(np.array([[x, y]]))
190191
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
191192
py = np.array(fx[1, :] + xEst[1, 0]).flatten()
192193
plt.plot(px, py, "--r")
@@ -197,16 +198,15 @@ def main():
197198

198199
time = 0.0
199200

200-
# RFID positions [x, y]
201-
RFID = np.array([[10.0, 0.0],
202-
[10.0, 10.0],
203-
[0.0, 15.0],
204-
[-5.0, 20.0]])
201+
# RF_ID positions [x, y]
202+
RFi_ID = np.array([[10.0, 0.0],
203+
[10.0, 10.0],
204+
[0.0, 15.0],
205+
[-5.0, 20.0]])
205206

206207
# State Vector [x y yaw v]'
207208
xEst = np.zeros((4, 1))
208209
xTrue = np.zeros((4, 1))
209-
PEst = np.eye(4)
210210

211211
px = np.zeros((4, NP)) # Particle store
212212
pw = np.zeros((1, NP)) + 1.0 / NP # Particle weight
@@ -221,9 +221,9 @@ def main():
221221
time += DT
222222
u = calc_input()
223223

224-
xTrue, z, xDR, ud = observation(xTrue, xDR, u, RFID)
224+
xTrue, z, xDR, ud = observation(xTrue, xDR, u, RFi_ID)
225225

226-
xEst, PEst, px, pw = pf_localization(px, pw, xEst, PEst, z, ud)
226+
xEst, PEst, px, pw = pf_localization(px, pw, z, ud)
227227

228228
# store data history
229229
hxEst = np.hstack((hxEst, xEst))
@@ -235,7 +235,7 @@ def main():
235235

236236
for i in range(len(z[:, 0])):
237237
plt.plot([xTrue[0, 0], z[i, 1]], [xTrue[1, 0], z[i, 2]], "-k")
238-
plt.plot(RFID[:, 0], RFID[:, 1], "*k")
238+
plt.plot(RFi_ID[:, 0], RFi_ID[:, 1], "*k")
239239
plt.plot(px[0, :], px[1, :], ".r")
240240
plt.plot(np.array(hxTrue[0, :]).flatten(),
241241
np.array(hxTrue[1, :]).flatten(), "-b")

PathPlanning/DynamicWindowApproach/dynamic_window_approach.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ def calc_to_goal_cost(traj, goal, config):
155155
dx = goal[0] - traj[-1, 0]
156156
dy = goal[1] - traj[-1, 1]
157157
error_angle = math.atan2(dy, dx)
158-
cost = abs(error_angle - traj[-1, 2])
158+
cost_angle = error_angle - traj[-1, 2]
159+
cost = abs(math.atan2(math.sin(cost_angle), math.cos(cost_angle)))
159160

160161
return cost
161162

users_comments.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ URL: https://2019.robocup.org/downloads/program/HughesEtAl2019.pdf
401401
6. Hughes, Josie, Masaru Shimizu, and Arnoud Visser. "A review of robot rescue simulation platforms for robotics education." (2019).
402402
URL: https://www.semanticscholar.org/paper/A-Review-of-Robot-Rescue-Simulation-Platforms-for-Hughes-Shimizu/318a4bcb97a44661422ae1430d950efc408097da
403403

404+
7. Ghosh, Ritwika, et al. "CyPhyHouse: A Programming, Simulation, and Deployment Toolchain for Heterogeneous Distributed Coordination." arXiv preprint arXiv:1910.01557 (2019).
405+
URL: https://arxiv.org/abs/1910.01557
406+
404407
# Others
405408

406409
- Autonomous Vehicle Readings https://richardkelley.io/readings

0 commit comments

Comments
 (0)