Skip to content

Commit 7f852fd

Browse files
authored
Merge pull request AtsushiSakai#131 from kyberszittya/master
Changing np.matrix to np.array, changing .dot to @ (Issue AtsushiSakai#115)
2 parents 916b438 + 320e187 commit 7f852fd

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

SLAM/EKFSLAM/ekf_slam.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def ekf_slam(xEst, PEst, u, z):
5050
lm = get_LM_Pos_from_state(xEst, minid)
5151
y, S, H = calc_innovation(lm, xEst, PEst, z[iz, 0:2], minid)
5252

53-
K = PEst.dot(H.T).dot(np.linalg.inv(S))
54-
xEst = xEst + K.dot(y)
55-
PEst = (np.eye(len(xEst)) - K.dot(H)).dot(PEst)
53+
K = (PEst @ H.T) @ np.linalg.inv(S)
54+
xEst = xEst + (K @ y)
55+
PEst = (np.eye(len(xEst)) - (K @ H)) @ PEst
5656

5757
xEst[2] = pi_2_pi(xEst[2])
5858

@@ -104,7 +104,7 @@ def motion_model(x, u):
104104
[DT * math.sin(x[2, 0]), 0],
105105
[0.0, DT]])
106106

107-
x = F.dot(x) + B .dot(u)
107+
x = (F @ x) + (B @ u)
108108
return x
109109

110110

@@ -157,7 +157,7 @@ def search_correspond_LM_ID(xAug, PAug, zi):
157157
for i in range(nLM):
158158
lm = get_LM_Pos_from_state(xAug, i)
159159
y, S, H = calc_innovation(lm, xAug, PAug, zi, i)
160-
mdist.append(y.T.dot(np.linalg.inv(S)).dot(y))
160+
mdist.append(y.T @ np.linalg.inv(S) @ y)
161161

162162
mdist.append(M_DIST_TH) # new landmark
163163

@@ -168,14 +168,14 @@ def search_correspond_LM_ID(xAug, PAug, zi):
168168

169169
def calc_innovation(lm, xEst, PEst, z, LMid):
170170
delta = lm - xEst[0:2]
171-
q = (delta.T.dot(delta))[0, 0]
171+
q = (delta.T @ delta)[0, 0]
172172
#zangle = math.atan2(delta[1], delta[0]) - xEst[2]
173173
zangle = math.atan2(delta[1,0], delta[0,0]) - xEst[2]
174174
zp = np.array([[math.sqrt(q), pi_2_pi(zangle)]])
175175
y = (z - zp).T
176176
y[1] = pi_2_pi(y[1])
177177
H = jacobH(q, delta, xEst, LMid + 1)
178-
S = H.dot(PEst).dot(H.T) + Cx[0:2, 0:2]
178+
S = H @ PEst @ H.T + Cx[0:2, 0:2]
179179

180180
return y, S, H
181181

@@ -193,7 +193,7 @@ def jacobH(q, delta, x, i):
193193

194194
F = np.vstack((F1, F2))
195195

196-
H = G.dot(F)
196+
H = G @ F
197197

198198
return H
199199

SLAM/GraphBasedSLAM/graph_based_slam.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def cal_observation_sigma(d):
6464

6565
def calc_rotational_matrix(angle):
6666

67-
Rt = np.matrix([[math.cos(angle), -math.sin(angle), 0],
67+
Rt = np.array([[math.cos(angle), -math.sin(angle), 0],
6868
[math.sin(angle), math.cos(angle), 0],
6969
[0, 0, 1.0]])
7070
return Rt
@@ -92,7 +92,7 @@ def calc_edge(x1, y1, yaw1, x2, y2, yaw2, d1,
9292
sig1 = cal_observation_sigma(d1)
9393
sig2 = cal_observation_sigma(d2)
9494

95-
edge.omega = np.linalg.inv(Rt1 * sig1 * Rt1.T + Rt2 * sig2 * Rt2.T)
95+
edge.omega = np.linalg.inv(Rt1 @ sig1 @ Rt1.T + Rt2 @ sig2 @ Rt2.T)
9696

9797
edge.d1, edge.d2 = d1, d2
9898
edge.yaw1, edge.yaw2 = yaw1, yaw2
@@ -127,20 +127,20 @@ def calc_edges(xlist, zlist):
127127
angle1, phi1, d2, angle2, phi2, t1, t2)
128128

129129
edges.append(edge)
130-
cost += (edge.e.T * edge.omega * edge.e)[0, 0]
130+
cost += (edge.e.T @ (edge.omega) @ edge.e)[0, 0]
131131

132132
print("cost:", cost, ",nedge:", len(edges))
133133
return edges
134134

135135

136136
def calc_jacobian(edge):
137137
t1 = edge.yaw1 + edge.angle1
138-
A = np.matrix([[-1.0, 0, edge.d1 * math.sin(t1)],
138+
A = np.array([[-1.0, 0, edge.d1 * math.sin(t1)],
139139
[0, -1.0, -edge.d1 * math.cos(t1)],
140140
[0, 0, -1.0]])
141141

142142
t2 = edge.yaw2 + edge.angle2
143-
B = np.matrix([[1.0, 0, -edge.d2 * math.sin(t2)],
143+
B = np.array([[1.0, 0, -edge.d2 * math.sin(t2)],
144144
[0, 1.0, edge.d2 * math.cos(t2)],
145145
[0, 0, 1.0]])
146146

@@ -154,13 +154,13 @@ def fill_H_and_b(H, b, edge):
154154
id1 = edge.id1 * STATE_SIZE
155155
id2 = edge.id2 * STATE_SIZE
156156

157-
H[id1:id1 + STATE_SIZE, id1:id1 + STATE_SIZE] += A.T * edge.omega * A
158-
H[id1:id1 + STATE_SIZE, id2:id2 + STATE_SIZE] += A.T * edge.omega * B
159-
H[id2:id2 + STATE_SIZE, id1:id1 + STATE_SIZE] += B.T * edge.omega * A
160-
H[id2:id2 + STATE_SIZE, id2:id2 + STATE_SIZE] += B.T * edge.omega * B
157+
H[id1:id1 + STATE_SIZE, id1:id1 + STATE_SIZE] += A.T @ edge.omega @ A
158+
H[id1:id1 + STATE_SIZE, id2:id2 + STATE_SIZE] += A.T @ edge.omega @ B
159+
H[id2:id2 + STATE_SIZE, id1:id1 + STATE_SIZE] += B.T @ edge.omega @ A
160+
H[id2:id2 + STATE_SIZE, id2:id2 + STATE_SIZE] += B.T @ edge.omega @ B
161161

162-
b[id1:id1 + STATE_SIZE, 0] += (A.T * edge.omega * edge.e)
163-
b[id2:id2 + STATE_SIZE, 0] += (B.T * edge.omega * edge.e)
162+
b[id1:id1 + STATE_SIZE] += (A.T @ edge.omega @ edge.e)
163+
b[id2:id2 + STATE_SIZE] += (B.T @ edge.omega @ edge.e)
164164

165165
return H, b
166166

@@ -178,21 +178,21 @@ def graph_based_slam(x_init, hz):
178178
for itr in range(MAX_ITR):
179179
edges = calc_edges(x_opt, zlist)
180180

181-
H = np.matrix(np.zeros((n, n)))
182-
b = np.matrix(np.zeros((n, 1)))
181+
H = np.zeros((n, n))
182+
b = np.zeros((n, 1))
183183

184184
for edge in edges:
185185
H, b = fill_H_and_b(H, b, edge)
186186

187187
# to fix origin
188188
H[0:STATE_SIZE, 0:STATE_SIZE] += np.identity(STATE_SIZE)
189189

190-
dx = - np.linalg.inv(H).dot(b)
190+
dx = - np.linalg.inv(H) @ b
191191

192192
for i in range(nt):
193-
x_opt[0:3, i] += dx[i * 3:i * 3 + 3, 0]
193+
x_opt[0:3, i] += dx[i * 3:i * 3 + 3,0]
194194

195-
diff = dx.T.dot(dx)
195+
diff = dx.T @ dx
196196
print("iteration: %d, diff: %f" % (itr + 1, diff))
197197
if diff < 1.0e-5:
198198
break
@@ -203,7 +203,7 @@ def graph_based_slam(x_init, hz):
203203
def calc_input():
204204
v = 1.0 # [m/s]
205205
yawrate = 0.1 # [rad/s]
206-
u = np.matrix([v, yawrate]).T
206+
u = np.array([[v, yawrate]]).T
207207
return u
208208

209209

@@ -212,7 +212,7 @@ def observation(xTrue, xd, u, RFID):
212212
xTrue = motion_model(xTrue, u)
213213

214214
# add noise to gps x-y
215-
z = np.matrix(np.zeros((0, 4)))
215+
z = np.zeros((0, 4))
216216

217217
for i in range(len(RFID[:, 0])):
218218

@@ -224,13 +224,13 @@ def observation(xTrue, xd, u, RFID):
224224
if d <= MAX_RANGE:
225225
dn = d + np.random.randn() * Qsim[0, 0] # add noise
226226
anglen = angle + np.random.randn() * Qsim[1, 1] # add noise
227-
zi = np.matrix([dn, anglen, phi, i])
227+
zi = np.array([dn, anglen, phi, i])
228228
z = np.vstack((z, zi))
229229

230230
# add noise to input
231231
ud1 = u[0, 0] + np.random.randn() * Rsim[0, 0]
232232
ud2 = u[1, 0] + np.random.randn() * Rsim[1, 1]
233-
ud = np.matrix([ud1, ud2]).T
233+
ud = np.array([[ud1, ud2]]).T
234234

235235
xd = motion_model(xd, ud)
236236

@@ -239,15 +239,15 @@ def observation(xTrue, xd, u, RFID):
239239

240240
def motion_model(x, u):
241241

242-
F = np.matrix([[1.0, 0, 0],
242+
F = np.array([[1.0, 0, 0],
243243
[0, 1.0, 0],
244244
[0, 0, 1.0]])
245245

246-
B = np.matrix([[DT * math.cos(x[2, 0]), 0],
246+
B = np.array([[DT * math.cos(x[2, 0]), 0],
247247
[DT * math.sin(x[2, 0]), 0],
248248
[0.0, DT]])
249249

250-
x = F * x + B * u
250+
x = F @ x + B @ u
251251

252252
return x
253253

@@ -270,8 +270,8 @@ def main():
270270
])
271271

272272
# State Vector [x y yaw v]'
273-
xTrue = np.matrix(np.zeros((STATE_SIZE, 1)))
274-
xDR = np.matrix(np.zeros((STATE_SIZE, 1))) # Dead reckoning
273+
xTrue = np.zeros((STATE_SIZE, 1))
274+
xDR = np.zeros((STATE_SIZE, 1)) # Dead reckoning
275275

276276
# history
277277
hxTrue = xTrue
@@ -299,17 +299,17 @@ def main():
299299

300300
plt.plot(RFID[:, 0], RFID[:, 1], "*k")
301301

302-
plt.plot(np.array(hxTrue[0, :]).flatten(),
303-
np.array(hxTrue[1, :]).flatten(), "-b")
304-
plt.plot(np.array(hxDR[0, :]).flatten(),
305-
np.array(hxDR[1, :]).flatten(), "-k")
306-
plt.plot(np.array(x_opt[0, :]).flatten(),
307-
np.array(x_opt[1, :]).flatten(), "-r")
302+
plt.plot(hxTrue[0, :].flatten(),
303+
hxTrue[1, :].flatten(), "-b")
304+
plt.plot(hxDR[0, :].flatten(),
305+
hxDR[1, :].flatten(), "-k")
306+
plt.plot(x_opt[0, :].flatten(),
307+
x_opt[1, :].flatten(), "-r")
308308
plt.axis("equal")
309309
plt.grid(True)
310310
plt.title("Time" + str(time)[0:5])
311311
plt.pause(1.0)
312312

313313

314314
if __name__ == '__main__':
315-
main()
315+
main()

SLAM/iterative_closest_point/iterative_closest_point.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def ICP_matching(ppoints, cpoints):
4545
Rt, Tt = SVD_motion_estimation(ppoints[:, inds], cpoints)
4646

4747
# update current points
48-
cpoints = (Rt.dot(cpoints)) + Tt[:,np.newaxis]
48+
cpoints = (Rt @ cpoints) + Tt[:,np.newaxis]
4949

5050
H = update_homogenerous_matrix(H, Rt, Tt)
5151

@@ -111,18 +111,17 @@ def nearest_neighbor_assosiation(ppoints, cpoints):
111111

112112
def SVD_motion_estimation(ppoints, cpoints):
113113

114-
pm = np.asarray(np.mean(ppoints, axis=1))
115-
cm = np.asarray(np.mean(cpoints, axis=1))
116-
print(cm)
114+
pm = np.mean(ppoints, axis=1)
115+
cm = np.mean(cpoints, axis=1)
117116

118-
pshift = np.array(ppoints - pm[:,np.newaxis])
119-
cshift = np.array(cpoints - cm[:,np.newaxis])
117+
pshift = ppoints - pm[:,np.newaxis]
118+
cshift = cpoints - cm[:,np.newaxis]
120119

121-
W = cshift.dot(pshift.T)
120+
W = cshift @ pshift.T
122121
u, s, vh = np.linalg.svd(W)
123122

124-
R = (u.dot(vh)).T
125-
t = pm - R.dot(cm)
123+
R = (u @ vh).T
124+
t = pm - (R @ cm)
126125

127126
return R, t
128127

@@ -150,7 +149,6 @@ def main():
150149
cy = [math.sin(motion[2]) * x + math.cos(motion[2]) * y + motion[1]
151150
for (x, y) in zip(px, py)]
152151
cpoints = np.vstack((cx, cy))
153-
print(cpoints)
154152

155153
R, T = ICP_matching(ppoints, cpoints)
156154

0 commit comments

Comments
 (0)