Skip to content

Commit 6ad39fb

Browse files
committed
add animation for FastSLAM1.0
1 parent 1b1cc1d commit 6ad39fb

2 files changed

Lines changed: 33 additions & 21 deletions

File tree

SLAM/FastSLAM1/animation.gif

6.47 MB
Loading

SLAM/FastSLAM1/fast_slam1.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
3-
Fast SLAM example
3+
FastSLAM 1.0 example
44
55
author: Atsushi Sakai (@Atsushi_twi)
66
@@ -11,13 +11,14 @@
1111
import matplotlib.pyplot as plt
1212

1313

14-
# EKF state covariance
15-
Q = np.diag([1.0, math.radians(4.0)])**2
16-
R = np.diag([0.5, math.radians(15.0)])**2
14+
# Fast SLAM covariance
15+
Q = np.diag([3.0, math.radians(10.0)])**2
16+
R = np.diag([1.0, math.radians(20.0)])**2
1717

1818
# Simulation parameter
1919
Qsim = np.diag([0.3, math.radians(2.0)])**2
2020
Rsim = np.diag([0.5, math.radians(10.0)])**2
21+
OFFSET_YAWRATE_NOISE = 0.01
2122

2223
DT = 0.1 # time tick [s]
2324
SIM_TIME = 50.0 # simulation time [s]
@@ -26,7 +27,7 @@
2627
STATE_SIZE = 3 # State size [x,y,yaw]
2728
LM_SIZE = 2 # LM srate size [x,y]
2829
N_PARTICLE = 100 # number of particle
29-
NTH = N_PARTICLE / 2.0 # Number of particle for re-sampling
30+
NTH = N_PARTICLE / 1.5 # Number of particle for re-sampling
3031

3132
show_animation = True
3233

@@ -38,21 +39,21 @@ def __init__(self, N_LM):
3839
self.x = 0.0
3940
self.y = 0.0
4041
self.yaw = 0.0
41-
self.lm = np.matrix(np.zeros((N_LM, 2)))
42-
self.lmP = np.matrix(np.zeros((N_LM * 2, 2)))
42+
# landmark x-y positions
43+
self.lm = np.matrix(np.zeros((N_LM, LM_SIZE)))
44+
# landmark position covariance
45+
self.lmP = np.matrix(np.zeros((N_LM * LM_SIZE, LM_SIZE)))
4346

4447

45-
def fast_slam(particles, PEst, u, z):
48+
def fast_slam(particles, u, z):
4649

4750
particles = predict_particles(particles, u)
4851

4952
particles = update_with_observation(particles, z)
5053

5154
particles = resampling(particles)
5255

53-
xEst = calc_final_state(particles)
54-
55-
return xEst, PEst, particles
56+
return particles
5657

5758

5859
def normalize_weight(particles):
@@ -193,6 +194,7 @@ def compute_weight(particle, z, Q):
193194
try:
194195
invS = np.linalg.inv(S)
195196
except np.linalg.linalg.LinAlgError:
197+
print("singuler")
196198
return 1.0
197199

198200
num = math.exp(-0.5 * dx.T * invS * dx)
@@ -216,7 +218,7 @@ def update_with_observation(particles, z):
216218
# known landmark
217219
else:
218220
w = compute_weight(particles[ip], z[iz, :], Q)
219-
particles[ip].w = particles[ip].w * w
221+
particles[ip].w *= w
220222
particles[ip] = update_landmark(particles[ip], z[iz, :], Q)
221223

222224
return particles
@@ -239,7 +241,6 @@ def resampling(particles):
239241
# print(Neff)
240242

241243
if Neff < NTH: # resampling
242-
# print("resampling")
243244
wcum = np.cumsum(pw)
244245
base = np.cumsum(pw * 0.0 + 1 / N_PARTICLE) - 1 / N_PARTICLE
245246
resampleid = base + np.random.rand(base.shape[1]) / N_PARTICLE
@@ -263,10 +264,17 @@ def resampling(particles):
263264
return particles
264265

265266

266-
def calc_input():
267-
v = 1.0 # [m/s]
268-
yawrate = 0.1 # [rad/s]
267+
def calc_input(time):
268+
269+
if time <= 3.0:
270+
v = 0.0
271+
yawrate = 0.0
272+
else:
273+
v = 1.0 # [m/s]
274+
yawrate = 0.1 # [rad/s]
275+
269276
u = np.matrix([v, yawrate]).T
277+
270278
return u
271279

272280

@@ -291,7 +299,7 @@ def observation(xTrue, xd, u, RFID):
291299

292300
# add noise to input
293301
ud1 = u[0, 0] + np.random.randn() * Rsim[0, 0]
294-
ud2 = u[1, 0] + np.random.randn() * Rsim[1, 1] + 0.01
302+
ud2 = u[1, 0] + np.random.randn() * Rsim[1, 1] + OFFSET_YAWRATE_NOISE
295303
ud = np.matrix([ud1, ud2]).T
296304

297305
xd = motion_model(xd, ud)
@@ -334,16 +342,18 @@ def main():
334342
# RFID positions [x, y]
335343
RFID = np.array([[10.0, -2.0],
336344
[15.0, 10.0],
345+
[15.0, 15.0],
346+
[10.0, 20.0],
337347
[3.0, 15.0],
338348
[-5.0, 20.0],
339-
[-5.0, 5.0]
349+
[-5.0, 5.0],
350+
[-10.0, 15.0]
340351
])
341352
N_LM = RFID.shape[0]
342353

343354
# State Vector [x y yaw v]'
344355
xEst = np.matrix(np.zeros((STATE_SIZE, 1)))
345356
xTrue = np.matrix(np.zeros((STATE_SIZE, 1)))
346-
PEst = np.eye(STATE_SIZE)
347357

348358
xDR = np.matrix(np.zeros((STATE_SIZE, 1))) # Dead reckoning
349359

@@ -356,11 +366,13 @@ def main():
356366

357367
while SIM_TIME >= time:
358368
time += DT
359-
u = calc_input()
369+
u = calc_input(time)
360370

361371
xTrue, z, xDR, ud = observation(xTrue, xDR, u, RFID)
362372

363-
xEst, PEst, particles = fast_slam(particles, PEst, ud, z)
373+
particles = fast_slam(particles, ud, z)
374+
375+
xEst = calc_final_state(particles)
364376

365377
x_state = xEst[0: STATE_SIZE]
366378

0 commit comments

Comments
 (0)