Skip to content

Commit 0b07425

Browse files
committed
Code clen up
1 parent c29d425 commit 0b07425

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

SLAM/EKFSLAM/ekf_slam.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
Extended Kalman Filter SLAM example
3+
34
author: Atsushi Sakai (@Atsushi_twi)
45
"""
56

@@ -35,19 +36,19 @@ def ekf_slam(xEst, PEst, u, z):
3536

3637
# Update
3738
for iz in range(len(z[:, 0])): # for each observation
38-
minid = search_correspond_landmark_id(xEst, PEst, z[iz, 0:2])
39+
min_id = search_correspond_landmark_id(xEst, PEst, z[iz, 0:2])
3940

4041
nLM = calc_n_lm(xEst)
41-
if minid == nLM:
42+
if min_id == nLM:
4243
print("New LM")
4344
# Extend state and covariance matrix
4445
xAug = np.vstack((xEst, calc_landmark_position(xEst, z[iz, :])))
4546
PAug = np.vstack((np.hstack((PEst, np.zeros((len(xEst), LM_SIZE)))),
4647
np.hstack((np.zeros((LM_SIZE, len(xEst))), initP))))
4748
xEst = xAug
4849
PEst = PAug
49-
lm = get_landmark_position_from_state(xEst, minid)
50-
y, S, H = calc_innovation(lm, xEst, PEst, z[iz, 0:2], minid)
50+
lm = get_landmark_position_from_state(xEst, min_id)
51+
y, S, H = calc_innovation(lm, xEst, PEst, z[iz, 0:2], min_id)
5152

5253
K = (PEst @ H.T) @ np.linalg.inv(S)
5354
xEst = xEst + (K @ y)
@@ -60,8 +61,8 @@ def ekf_slam(xEst, PEst, u, z):
6061

6162
def calc_input():
6263
v = 1.0 # [m/s]
63-
yawrate = 0.1 # [rad/s]
64-
u = np.array([[v, yawrate]]).T
64+
yaw_rate = 0.1 # [rad/s]
65+
u = np.array([[v, yaw_rate]]).T
6566
return u
6667

6768

@@ -79,8 +80,8 @@ def observation(xTrue, xd, u, RFID):
7980
angle = pi_2_pi(math.atan2(dy, dx) - xTrue[2, 0])
8081
if d <= MAX_RANGE:
8182
dn = d + np.random.randn() * Q_sim[0, 0] ** 0.5 # add noise
82-
anglen = angle + np.random.randn() * Q_sim[1, 1] ** 0.5 # add noise
83-
zi = np.array([dn, anglen, i])
83+
angle_n = angle + np.random.randn() * Q_sim[1, 1] ** 0.5 # add noise
84+
zi = np.array([dn, angle_n, i])
8485
z = np.vstack((z, zi))
8586

8687
# add noise to input
@@ -128,8 +129,6 @@ def calc_landmark_position(x, z):
128129

129130
zp[0, 0] = x[0, 0] + z[0] * math.cos(x[2, 0] + z[1])
130131
zp[1, 0] = x[1, 0] + z[0] * math.sin(x[2, 0] + z[1])
131-
# zp[0, 0] = x[0, 0] + z[0, 0] * math.cos(x[2, 0] + z[0, 1])
132-
# zp[1, 0] = x[1, 0] + z[0, 0] * math.sin(x[2, 0] + z[0, 1])
133132

134133
return zp
135134

@@ -147,25 +146,25 @@ def search_correspond_landmark_id(xAug, PAug, zi):
147146

148147
nLM = calc_n_lm(xAug)
149148

150-
mdist = []
149+
min_dist = []
151150

152151
for i in range(nLM):
153152
lm = get_landmark_position_from_state(xAug, i)
154153
y, S, H = calc_innovation(lm, xAug, PAug, zi, i)
155-
mdist.append(y.T @ np.linalg.inv(S) @ y)
154+
min_dist.append(y.T @ np.linalg.inv(S) @ y)
156155

157-
mdist.append(M_DIST_TH) # new landmark
156+
min_dist.append(M_DIST_TH) # new landmark
158157

159-
minid = mdist.index(min(mdist))
158+
min_id = min_dist.index(min(min_dist))
160159

161-
return minid
160+
return min_id
162161

163162

164163
def calc_innovation(lm, xEst, PEst, z, LMid):
165164
delta = lm - xEst[0:2]
166165
q = (delta.T @ delta)[0, 0]
167-
zangle = math.atan2(delta[1, 0], delta[0, 0]) - xEst[2, 0]
168-
zp = np.array([[math.sqrt(q), pi_2_pi(zangle)]])
166+
z_angle = math.atan2(delta[1, 0], delta[0, 0]) - xEst[2, 0]
167+
zp = np.array([[math.sqrt(q), pi_2_pi(z_angle)]])
169168
y = (z - zp).T
170169
y[1] = pi_2_pi(y[1])
171170
H = jacob_h(q, delta, xEst, LMid + 1)

0 commit comments

Comments
 (0)