In [2]:
%pylab inline
import sympy as sp
sp.init_printing()
In [8]:
dt = sp.Symbol('dt')
psi_k1_k1 = sp.Symbol('\psi_{k-1 | k-1}') # roll prior
psi_k_k1 = sp.Symbol('\psi_{k | k-1}') # roll predicted
psi_k_k = sp.Symbol('\psi_{k | k }') # roll post
d_psi = sp.Symbol('\dot{\psi}_{k-1}') # roll rate
psi_m = sp.Symbol('\psi_m') # mesured roll
b_psi_k1_k1 = sp.Symbol('b_{\dot{\psi}}_{k-1 | k-1}') # roll rate bias
b_psi_k_k1 = sp.Symbol('b_{\dot{\psi}}_{k | k-1}') # roll rate bias
b_psi_k_k = sp.Symbol('b_{\dot{\psi}}_{k | k }') # roll rate bias
In [32]:
x_k1_k1 = sp.Matrix([ psi_k1_k1,
b_psi_k1_k1 ]) # prior
x_k_k1 = sp.Matrix([ psi_k_k1,
b_psi_k_k1 ]) # predicted
x_k_k = sp.Matrix([ psi_k_k,
b_psi_k_k ]) # post
x_k_k
Out[32]:
In [10]:
sigma = sp.Symbol('\sigma_{m}')
# Measurement noise covariance
R = sigma * sp.eye(1)
R
Out[10]:
In [11]:
z_k = sp.Matrix([ psi_m ])
z_k
Out[11]:
In [12]:
u_k1 = sp.Matrix([ d_psi ])
u_k1
Out[12]:
In [15]:
P_k1_k1 = sp.Matrix([ [sp.Symbol('p_' + str(i) + str(j) + '_{k-1 | k-1}') for j in [0,1] ] for i in [0,1] ])
P_k_k1 = sp.Matrix([ [sp.Symbol('p_' + str(i) + str(j) + '_{k | k-1}') for j in [0,1] ] for i in [0,1] ])
P_k_k = sp.Matrix([ [sp.Symbol('p_' + str(i) + str(j) + '_{k | k }') for j in [0,1] ] for i in [0,1] ])
P_k_k1
Out[15]:
In [21]:
# Uncertainty on model
sigma_q = sp.Symbol('\sigma_q')
# Uncertainty on bias drift
sigma_b = sp.Symbol('\sigma_{b_{\dot{\phi}}}')
w = sp.Matrix([dt, 1])
Q = sigma_b * w * w.T
Q[0,0] += sigma_q
Q
Out[21]:
In [34]:
def f(x, u, dt):
return sp.Matrix([ x[0] + dt*x[1] + dt*u[0],
x[1] ])
x_k_k1 = f( x_k1_k1, u_k1, dt )
x_k_k1
Out[34]:
In [38]:
F = f( x_k1_k1, u_k1, dt ).jacobian( x_k1_k1 )
F
Out[38]:
In [39]:
P_k_k1 = F * P_k1_k1 * F.T + Q
P_k_k1
Out[39]:
In [40]:
def h(x):
return sp.Matrix([x[0]])
h(x_k_k1)
Out[40]:
In [41]:
H = h(x_k_k1).jacobian(x_k1_k1)
H
Out[41]:
In [42]:
y = z_k - h(x_k_k1)
y
Out[42]:
In [43]:
S = H * P_k_k1 * H.T + R
S
Out[43]:
In [44]:
K = P_k_k1 * H.T * S**(-1)
K
Out[44]:
In [49]:
P_k_k = (sp.eye(2) - K * H) * P_k_k1
P_k_k.simplify()
P_k_k
Out[49]:
In [53]:
x_k_k = x_k_k1 + K * y
x_k_k.simplify()
x_k_k
Out[53]: