Skip to content

Commit 16c16b6

Browse files
authored
clean up fast slam codes (AtsushiSakai#1012)
* clean up fast slam codes * clean up fast slam codes * clean up fast slam codes
1 parent c9ae446 commit 16c16b6

File tree

2 files changed

+104
-106
lines changed

2 files changed

+104
-106
lines changed

SLAM/FastSLAM1/fast_slam1.py

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
R = np.diag([1.0, np.deg2rad(20.0)]) ** 2
1818

1919
# Simulation parameter
20-
Q_sim = np.diag([0.3, np.deg2rad(2.0)]) ** 2
21-
R_sim = np.diag([0.5, np.deg2rad(10.0)]) ** 2
20+
Q_SIM = np.diag([0.3, np.deg2rad(2.0)]) ** 2
21+
R_SIM = np.diag([0.5, np.deg2rad(10.0)]) ** 2
2222
OFFSET_YAW_RATE_NOISE = 0.01
2323

2424
DT = 0.1 # time tick [s]
@@ -72,19 +72,18 @@ def normalize_weight(particles):
7272

7373

7474
def calc_final_state(particles):
75-
xEst = np.zeros((STATE_SIZE, 1))
75+
x_est = np.zeros((STATE_SIZE, 1))
7676

7777
particles = normalize_weight(particles)
7878

7979
for i in range(N_PARTICLE):
80-
xEst[0, 0] += particles[i].w * particles[i].x
81-
xEst[1, 0] += particles[i].w * particles[i].y
82-
xEst[2, 0] += particles[i].w * particles[i].yaw
80+
x_est[0, 0] += particles[i].w * particles[i].x
81+
x_est[1, 0] += particles[i].w * particles[i].y
82+
x_est[2, 0] += particles[i].w * particles[i].yaw
8383

84-
xEst[2, 0] = pi_2_pi(xEst[2, 0])
85-
# print(xEst)
84+
x_est[2, 0] = pi_2_pi(x_est[2, 0])
8685

87-
return xEst
86+
return x_est
8887

8988

9089
def predict_particles(particles, u):
@@ -235,28 +234,27 @@ def resampling(particles):
235234
pw = np.array(pw)
236235

237236
n_eff = 1.0 / (pw @ pw.T) # Effective particle number
238-
# print(n_eff)
239237

240238
if n_eff < NTH: # resampling
241239
w_cum = np.cumsum(pw)
242240
base = np.cumsum(pw * 0.0 + 1 / N_PARTICLE) - 1 / N_PARTICLE
243241
resample_id = base + np.random.rand(base.shape[0]) / N_PARTICLE
244242

245-
inds = []
246-
ind = 0
243+
indexes = []
244+
index = 0
247245
for ip in range(N_PARTICLE):
248-
while (ind < w_cum.shape[0] - 1) \
249-
and (resample_id[ip] > w_cum[ind]):
250-
ind += 1
251-
inds.append(ind)
246+
while (index < w_cum.shape[0] - 1) \
247+
and (resample_id[ip] > w_cum[index]):
248+
index += 1
249+
indexes.append(index)
252250

253251
tmp_particles = particles[:]
254-
for i in range(len(inds)):
255-
particles[i].x = tmp_particles[inds[i]].x
256-
particles[i].y = tmp_particles[inds[i]].y
257-
particles[i].yaw = tmp_particles[inds[i]].yaw
258-
particles[i].lm = tmp_particles[inds[i]].lm[:, :]
259-
particles[i].lmP = tmp_particles[inds[i]].lmP[:, :]
252+
for i in range(len(indexes)):
253+
particles[i].x = tmp_particles[indexes[i]].x
254+
particles[i].y = tmp_particles[indexes[i]].y
255+
particles[i].yaw = tmp_particles[indexes[i]].yaw
256+
particles[i].lm = tmp_particles[indexes[i]].lm[:, :]
257+
particles[i].lmP = tmp_particles[indexes[i]].lmP[:, :]
260258
particles[i].w = 1.0 / N_PARTICLE
261259

262260
return particles
@@ -275,34 +273,34 @@ def calc_input(time):
275273
return u
276274

277275

278-
def observation(xTrue, xd, u, rfid):
276+
def observation(x_true, xd, u, rfid):
279277
# calc true state
280-
xTrue = motion_model(xTrue, u)
278+
x_true = motion_model(x_true, u)
281279

282280
# add noise to range observation
283281
z = np.zeros((3, 0))
284282
for i in range(len(rfid[:, 0])):
285283

286-
dx = rfid[i, 0] - xTrue[0, 0]
287-
dy = rfid[i, 1] - xTrue[1, 0]
284+
dx = rfid[i, 0] - x_true[0, 0]
285+
dy = rfid[i, 1] - x_true[1, 0]
288286
d = math.hypot(dx, dy)
289-
angle = pi_2_pi(math.atan2(dy, dx) - xTrue[2, 0])
287+
angle = pi_2_pi(math.atan2(dy, dx) - x_true[2, 0])
290288
if d <= MAX_RANGE:
291-
dn = d + np.random.randn() * Q_sim[0, 0] ** 0.5 # add noise
292-
angle_with_noize = angle + np.random.randn() * Q_sim[
289+
dn = d + np.random.randn() * Q_SIM[0, 0] ** 0.5 # add noise
290+
angle_with_noize = angle + np.random.randn() * Q_SIM[
293291
1, 1] ** 0.5 # add noise
294292
zi = np.array([dn, pi_2_pi(angle_with_noize), i]).reshape(3, 1)
295293
z = np.hstack((z, zi))
296294

297295
# add noise to input
298-
ud1 = u[0, 0] + np.random.randn() * R_sim[0, 0] ** 0.5
299-
ud2 = u[1, 0] + np.random.randn() * R_sim[
296+
ud1 = u[0, 0] + np.random.randn() * R_SIM[0, 0] ** 0.5
297+
ud2 = u[1, 0] + np.random.randn() * R_SIM[
300298
1, 1] ** 0.5 + OFFSET_YAW_RATE_NOISE
301299
ud = np.array([ud1, ud2]).reshape(2, 1)
302300

303301
xd = motion_model(xd, ud)
304302

305-
return xTrue, z, xd, ud
303+
return x_true, z, xd, ud
306304

307305

308306
def motion_model(x, u):
@@ -331,7 +329,7 @@ def main():
331329
time = 0.0
332330

333331
# RFID positions [x, y]
334-
RFID = np.array([[10.0, -2.0],
332+
rfid = np.array([[10.0, -2.0],
335333
[15.0, 10.0],
336334
[15.0, 15.0],
337335
[10.0, 20.0],
@@ -340,53 +338,53 @@ def main():
340338
[-5.0, 5.0],
341339
[-10.0, 15.0]
342340
])
343-
n_landmark = RFID.shape[0]
341+
n_landmark = rfid.shape[0]
344342

345343
# State Vector [x y yaw v]'
346-
xEst = np.zeros((STATE_SIZE, 1)) # SLAM estimation
347-
xTrue = np.zeros((STATE_SIZE, 1)) # True state
348-
xDR = np.zeros((STATE_SIZE, 1)) # Dead reckoning
344+
x_est = np.zeros((STATE_SIZE, 1)) # SLAM estimation
345+
x_true = np.zeros((STATE_SIZE, 1)) # True state
346+
x_dr = np.zeros((STATE_SIZE, 1)) # Dead reckoning
349347

350348
# history
351-
hxEst = xEst
352-
hxTrue = xTrue
353-
hxDR = xTrue
349+
hist_x_est = x_est
350+
hist_x_true = x_true
351+
hist_x_dr = x_dr
354352

355353
particles = [Particle(n_landmark) for _ in range(N_PARTICLE)]
356354

357355
while SIM_TIME >= time:
358356
time += DT
359357
u = calc_input(time)
360358

361-
xTrue, z, xDR, ud = observation(xTrue, xDR, u, RFID)
359+
x_true, z, x_dr, ud = observation(x_true, x_dr, u, rfid)
362360

363361
particles = fast_slam1(particles, ud, z)
364362

365-
xEst = calc_final_state(particles)
363+
x_est = calc_final_state(particles)
366364

367-
x_state = xEst[0: STATE_SIZE]
365+
x_state = x_est[0: STATE_SIZE]
368366

369367
# store data history
370-
hxEst = np.hstack((hxEst, x_state))
371-
hxDR = np.hstack((hxDR, xDR))
372-
hxTrue = np.hstack((hxTrue, xTrue))
368+
hist_x_est = np.hstack((hist_x_est, x_state))
369+
hist_x_dr = np.hstack((hist_x_dr, x_dr))
370+
hist_x_true = np.hstack((hist_x_true, x_true))
373371

374372
if show_animation: # pragma: no cover
375373
plt.cla()
376374
# for stopping simulation with the esc key.
377375
plt.gcf().canvas.mpl_connect(
378376
'key_release_event', lambda event:
379377
[exit(0) if event.key == 'escape' else None])
380-
plt.plot(RFID[:, 0], RFID[:, 1], "*k")
378+
plt.plot(rfid[:, 0], rfid[:, 1], "*k")
381379

382380
for i in range(N_PARTICLE):
383381
plt.plot(particles[i].x, particles[i].y, ".r")
384382
plt.plot(particles[i].lm[:, 0], particles[i].lm[:, 1], "xb")
385383

386-
plt.plot(hxTrue[0, :], hxTrue[1, :], "-b")
387-
plt.plot(hxDR[0, :], hxDR[1, :], "-k")
388-
plt.plot(hxEst[0, :], hxEst[1, :], "-r")
389-
plt.plot(xEst[0], xEst[1], "xk")
384+
plt.plot(hist_x_true[0, :], hist_x_true[1, :], "-b")
385+
plt.plot(hist_x_dr[0, :], hist_x_dr[1, :], "-k")
386+
plt.plot(hist_x_est[0, :], hist_x_est[1, :], "-r")
387+
plt.plot(x_est[0], x_est[1], "xk")
390388
plt.axis("equal")
391389
plt.grid(True)
392390
plt.pause(0.001)

0 commit comments

Comments
 (0)