forked from annesa11/Vocowa-SLAM---Winter-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EKF3.py
431 lines (338 loc) · 11 KB
/
EKF3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
import math
from random import randrange
style.use('ggplot')
def toFrame(F,p,nargout=1):
# F is basically the reference frame,p is the point in global frame
# F=[F_x;F_y;F_alpha]
np.matrix(F)
np.matrix(p)
t=F[0:2]
a=F[2][0]
R=[[math.cos(a),-math.sin(a)],[math.sin(a),math.cos(a)]]
np.matrix(R)
k=np.matrix(np.subtract(p,t))
jf=np.transpose(R)*k
pf=jf.tolist()
if nargout>1:
px=p[0][0]
py=p[1][0]
x=t[0][0]
y=t[1][0]
Pf_f=[[-math.cos(a),-math.sin(a),math.cos(a)*(py-y)-math.sin(a)*(px-x)],[math.sin(a),-math.cos(a),-math.cos(a)*(px-x)-math.sin(a)*(py-y)]]
Pf_p=np.transpose(R)
Pf_p=Pf_p.tolist()
return([pf,Pf_f,Pf_p])
else:
return([pf])
def fromFrame(F,pf,nargout=1):
# F is basically the reference frame, pf is the point int the reference frame F_alpha
# We have to basically pf into global frame
# # F=[F_x;F_y;F_alpha]
np.matrix(F)
p1=np.matrix(pf) #Basically this part involves conversion of pf to matrix form multiplication
t=F[0:2]
a=F[2][0]
R=[[math.cos(a),-math.sin(a)],[math.sin(a),math.cos(a)]]
np.matrix(R)
pw=np.add(R*p1,t)
pw=pw.tolist()
if nargout>1:
px=pf[0][0]
py=pf[1][0]
Pw_f=[[1,0,-py*math.cos(a)-px*math.sin(a)],[0,1,px*math.cos(a)-py*math.sin(a)]]
Pw_pf=R;
return([pw,Pw_f,Pw_pf])
else:
return([pw])
def scan(p,nargout=1):
# This function basically performs a range and bearing measurement of a 2D point.
# p=[p_x;p_y] points in sensor frame
px=p[0][0]
py=p[1][0]
j=px**2+py**2
d=math.sqrt(j)
a=math.atan2(py,px)
y=[[d],[a]]
if nargout>1:
Y_p=[[(px/d),(py/d)],[-(py/j),(px/j)]]
return([y,Y_p])
else:
return([y])
def invscan(y,nargout=1):
# This function is basically used to backproject a range and bearring measurement into a 2-d point
# y=[range,bearing]
d=y[0][0]
a=y[1][0]
px=d*math.cos(a)
py=d*math.sin(a)
p=[[px],[py]]
if nargout>1:
P_y=[[math.cos(a),-py],[math.sin(a),px]]
return([p,P_y])
else:
return([p])
def move(r,u,n,nargout=1):
#r=[x;y;alpha]->This is the robot pose
#u=[dx;dalpha]->This is the control input
#n=[nx;nalpha]->This is the perturbation input
#ro->updated robot pose
# Basically the control inputs are provided with respect to the robot frame which is then being converted to World Frame.
a=r[2][0]
dx=u[0][0]+n[0][0]
da=u[1][0]+n[1][0]
ao=a+da
if ao>(math.pi):
ao=ao-2*(math.pi)
if ao<-(math.pi):
ao=ao+2*(math.pi)
dp=[[dx],[0]]
if nargout==1:
to=fromFrame(r,dp) # to is a list so ans is obtained using to[0]
ro=[to[0][0],to[0][1],[ao]]
return([ro])
else:
po=fromFrame(r,dp,3)
Ro_r=[po[1][0],po[1][1],[0,0,1]] # this is Jacobian wrt r
Ro_n=[[po[2][0][0],0],[po[2][1][0],0],[0,1]] # This is Jacobian wrt n
ro=[po[0][0],po[0][1],[ao]] # This is the point which contains the updated robot pose
return([ro,Ro_r,Ro_n])
def observe(r,p,nargout=1):
# This function converts the point p into global frame and then it uses to obtain Range and Bearing
# r=[ro_x,ro_y,ro_alpha]-> Robot pose
# p=[p_x,p_y]-> point in global frame
# y=[range,bearing]
if nargout==1:
y0=toFrame(r,p)
y=scan(y0[0])
return(y)
else:
J=toFrame(r,p,3)
Pr_r=np.matrix(J[1])# Converted to matrix form for the purpose of multiplication
Pr_p=np.matrix(J[2]) # Converted to matrix for the purpose of multiplication
K=scan(J[0],2)
Y_pr=np.matrix(K[1]) #Converted to matrix form for the purpose of multiplication
Y_r=Y_pr*Pr_r # Jacobian wrt r
Y_p=Y_pr*Pr_p # Jacobian wrt p
return([K[0],Y_r,Y_p])
def invobserve(r,y,nargout=1):
# Backprojects a range and bearing measurement and transport to Map Frame
# y=[range;bearing]
# r=[ro_x,ro_y,ro_alpha]-> Robot Pose
if nargout==1:
kim=invscan(y)
p=fromFrame(r,kim[0])
return(p)
else:
kim=invscan(y,2)
p_r=kim[0]
Pr_y=np.matrix(kim[1])
p0=fromFrame(r,p_r,3)
p=p0[0]
P_r=p0[1]
P_pr=np.matrix(p0[2])
P_y=P_pr*Pr_y
P_y=P_y.tolist()
return([p,P_r,P_y])
def convert_2_ellipse(x,P,n=3,NP=16):
alpha=[(2*math.pi/NP)*i for i in range(NP+1)]
circle=[[math.cos(i),math.sin(i)] for i in alpha]
R, D1, V = np.linalg.svd(P, full_matrices=True)
d1=[math.sqrt(i) for i in D1]
d=np.matrix([[d1[0],0],[0,d1[1]]])
ellipse=np.zeros((2,NP+1))
X=np.zeros(NP+1)
Y=np.zeros(NP+1)
R=np.matrix(R)
for i in range(NP+1):
circlealpha=np.matrix(np.reshape(circle[i],(2,1)))
ellip=n*R*d*circlealpha;
X[i]=ellip[0]+x[0][0]
Y[i]=ellip[1]+x[1][0]
return(X,Y)
#W=cloister(-4,4,-4,4,7) # Set of external landmarks of the form 2*N
W=[[2,2,2,2,2,2.5,3,3,3,3.5,4,4,4,3.5,3,2.5,1,5,5,1,4.5,2.5],[2,2.5,3,3.5,4,4,4,3.5,3,3,3,2.5,2,2,2,2,1,1,5,5,3.5,1.5]]
k=np.shape(W)
N=k[1] # Total No of landmarks
Walpha=np.matrix(W)
Xscatter=Walpha[0]
Yscatter=Walpha[1]
plt.figure(figsize=(6,6))
plt.xlim(0,6)
plt.ylim(0,6)
plt.scatter(Xscatter,Yscatter,marker='*',color='red')
R=[[3],[1],[0]] # Robot pose
U=[[0.1],[0.05]] # Control parameter
Y=np.zeros((2,N)) # Set of landmarks measurements
# Estimator
x=np.zeros(((3+2*N),1)) #State Vectors Mean
P=np.zeros((3+2*N,3+2*N)) # Covariance Matrix 3+2*N by 3+2*N type of array
q=[[0.01],[0.02]] # System noise ampli
Q=[[0.01**2,0],[0,0.02**2]] # System noise covariance Matrix
s=[[0.1],[math.pi/180]] # Measurement noise ampli
S=[[0.1**2,0],[0,(math.pi/180)**2]] # Measurement noise covariance Matrix
mapspace=np.zeros((1,3+2*N),dtype=bool)
landmarks=np.zeros((2,N))
r=np.where(mapspace==False)
r=r[1][0:3]
mapspace[0][r]=True
for i in range(3):
x[i]=R[i]
for j in range(3):
P[i][j]=0
Rshape=np.array([[0.2,-0.1,-0.1,0.2],[0,0.1,-0.1,0]])
Xgraph=np.zeros(4)
Ygraph=np.zeros(4)
for i in range(4):
k=np.reshape(Rshape[:,i],(2,1))
t=fromFrame(R,k)
Xgraph[i]=t[0][0][0]
Ygraph[i]=t[0][1][0]
plt.plot(Xgraph,Ygraph,'-y')
plt.pause(0.2)
plt.plot(Xgraph,Ygraph,'-b')
plt.pause(0.2)
#plt.show()
#plt.close('all')
plt.clf()
for t in range(125):
q0=np.random.randn(2,1)
n=np.multiply(q,q0) # Perturbation Vector
zeros=[[0],[0]]
R=move(R,U,zeros) # We will perturb the estimator instead of the stimulator
R=R[0] # THE ANS IS RETURN IN THE FORM OF LIST
for i in range(N):
s0=np.random.randn(2,1)
v=np.multiply(s,s0) #measurement noise
W1=[row[i] for row in W]
W1=np.reshape(W1,(2,1)) #reshaping back in the form 2*1
Y0=observe(R,W1)
Y1=Y0[0]+v #Adding the measurement noise to the measurement of landmark
j=0
for row in Y:
row[i]=Y1[j][0]
j=j+1
m0=np.where(np.transpose(landmarks)!=0)
o=np.transpose(landmarks)[m0] #pointers to landmark in the form of 1*e
m=np.reshape(o,(np.shape(o)[0],1)) #pointers to landmark in the form of e*1
rm=np.union1d(r,o) # all used states
rm=rm.astype(int) # converting to int type for index usage
delta=x[r]
Estim_r=move(delta,U,n,3) #Estimator perturbated
x[r]=Estim_r[0]
o=o.astype(int)
A1=np.matrix(Estim_r[1])
A2=np.matrix(P[np.ix_(r,o)])
A3=np.matrix(P[np.ix_(r,r)])
A4=np.matrix(Q)
A5=np.matrix(Estim_r[2])
P[np.ix_(r,o)]=np.array(A1*A2)
P[np.ix_(o,r)]=np.transpose(P[np.ix_(r,o)])
P[np.ix_(r,r)]=A1*A3*np.transpose(A1)+A5*A4*np.transpose(A5) # The above part involves updation of Covariance Matrix
G0=np.where(landmarks[1]!=0)
G=G0[0] # Return landmarks pointer
for i in G:
l=landmarks[:,i]
l=l.astype(int)
E=observe(x[r],x[l],3) # This part is equivalent to h(x) in EKF slam
rl=np.union1d(r,l) #Taking union of r and l
rl=rl.astype(int) # converting to int type for index usage
e=E[0]
E_r=np.array(E[1])
E_l=np.array(E[2])
E_rl=[[E_r[0][0],E_r[0][1],E_r[0][2],E_l[0][0],E_l[0][1]],[E_r[1][0],E_r[1][1],E_r[1][2],E_l[1][0],E_l[1][1]]]
A1=np.matrix(E_rl)
A2=np.matrix(P[np.ix_(rl,rl)])
Ealpha=A1*A2*np.transpose(A1)
Yi=Y[:,i] # Measurement of landmark i
Yi=np.reshape(Yi,(2,1))
z=np.subtract(Yi,e) # innovation Gaussian(z,Z)
if z[1][0]>(math.pi): #We need values around zero for angles
z[1][0]=z[1][0]-2*(math.pi)
if z[1][0]<-(math.pi):
z[1][0]=z[1][0]+2*(math.pi)
Z=S+Ealpha
A3=np.matrix(Z)
A4=np.matrix(z)
A5=np.linalg.inv(A3)
A6=np.matrix(P[np.ix_(rm,rl)])
A8=np.matrix(P[np.ix_(rm,rm)])
Qw=np.transpose(A4)*A5*A4
print(Qw)
if Qw<9:
K=A6*np.transpose(A1)*A5 # Kalman Gain
A7=np.matrix(K)
# Map updation using rm
x[rm]=x[rm]+A7*A4
P[np.ix_(rm,rm)]=A8-(A7*A3*np.transpose(A7))
# Landmark initialization-one new landmark at each iteration
lids=np.where(landmarks[0]==0) # all non initialized landmarks
if len(lids[0])!=0: # there are still landmarks to initialize
rand_index=randrange(0,len(lids[0]))
i=lids[0][rand_index] #pick one landmark randomly
l=np.where(mapspace==False)
l=l[1][0:2]
if len(l)!=0:
mapspace[0][l]=True
landmarks[:,i]=l
Yi=Y[:,i]
Yi=np.reshape(Yi,(2,1))
Landalpha=invobserve(x[r],Yi,3)
x[l]=Landalpha[0]
L_r=Landalpha[1]
L_y=Landalpha[2]
A1=np.matrix(L_r)
A2=np.matrix(L_y)
A3=np.matrix(P[np.ix_(r,rm)])
A4=np.matrix(P[np.ix_(r,r)])
A5=np.matrix(S)
P[np.ix_(l,rm)]=A1*A3
P[np.ix_(rm,l)]=np.transpose(P[np.ix_(l,rm)])
P[np.ix_(l,l)]=A1*A4*np.transpose(A1)+A2*A5*np.transpose(A2)
#print(mapspace)
#plt.figure(figsize=(6,6))
plt.xlim(0,6)
plt.ylim(0,6)
plt.scatter(Xscatter,Yscatter,marker='*',color='red')
Xgraph1=np.zeros(4)
Ygraph1=np.zeros(4)
Xgraph2=np.zeros(4)
Ygraph2=np.zeros(4)
print(r)
for i in range(4):
k=np.reshape(Rshape[:,i],(2,1))
tin=fromFrame(R,k)
pin=fromFrame(x[r],k)
Xgraph1[i]=tin[0][0][0]
Ygraph1[i]=tin[0][1][0]
Xgraph2[i]=pin[0][0][0]
Ygraph2[i]=pin[0][1][0]
plt.plot(Xgraph1,Ygraph1,'-y')
plt.plot(Xgraph2,Ygraph2,'-b')
subset=[0,1]
omega=x[subset]
OMEGA=P[np.ix_(subset,subset)]
xellip,yellip=convert_2_ellipse(omega,OMEGA)
plt.plot(xellip,yellip,'-m')
zz=np.where(landmarks[0]!=0)[0]
sizealpha=np.size(zz)
xx=np.zeros(sizealpha)
yy=np.zeros(sizealpha)
jj=0
for i in zz:
xx[jj]=x[landmarks[0][i].astype(int)][0]
yy[jj]=x[landmarks[1][i].astype(int)][0]
jj=jj+1
plt.scatter(xx,yy,marker='*',color='blue')
for i in zz:
l=[landmarks[0][i].astype(int),landmarks[1][i].astype(int)]
le=x[l]
LE=P[np.ix_(l,l)]
xdatael,ydatael=convert_2_ellipse(le,LE)
plt.plot(xdatael,ydatael,'-c')
plt.pause(0.5)
#plt.show()
#plt.close('all')
plt.clf()