forked from ShengjieLuo/SWatchPad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkalman.py
53 lines (43 loc) · 800 Bytes
/
kalman.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
'''
__author__ = 'lab126'
A=[
[1,0,T,0],
[0,1,0,T],
[0,0,1,0],
[0,0,0,1]]
B=[
[0.5*T*T,0],
[0,0.5*T*T],
[T,0],
[0,T]]
'''
import numpy as np
def xp_update(A,xr,B,u):
xp=A*xr+B*u
return xp
def pp_update(A,pr,Q):
pp=A*pr*A.T+Q
return pp
def k_update(pp,R):
k=pp/(R+pp)
return k
def pr_update(k,pp):
pr=(1-k)*pp
return pr
def xr_update(A,xp,k,z):
xr=A*xp+k*(z-xp)
return xr
def kalman(xr,pr,u,z,Q,R):
T=0.1
A=np.matrix([[1,0,T,0],[0,1,0,T],[0,0,1,0],[0,0,0,1]])
B=np.matrix([[0.5*T*T,0],[0,0.5*T*T],[T,0],[0,T]])
xp=xp_update(A,xr,B,u)
pp=pp_update(A,pr,Q)
k=k_update(pp,R)
pr=pr_update(k,pp)
xr=xr_update(A,xp,k,z)
return xr,pr
'''
a=np.matrix([5,6])
print a*T
'''