Skip to content

Commit 320e187

Browse files
committed
Graph SLAM -> np.array finished
1 parent e1af88b commit 320e187

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ 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))
114+
pm = np.mean(ppoints, axis=1)
115+
cm = np.mean(cpoints, axis=1)
116116

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

120120
W = cshift @ pshift.T
121121
u, s, vh = np.linalg.svd(W)

0 commit comments

Comments
 (0)