我正在尝试在Python中复现这里讲解的算法,但是我遇到了一些关于某些参数增长异常的问题。以下是我的尝试。请注意,get_ang()
和get_acc()
分别返回度/秒以及m/s^2的[x,y,z]轴上的角速度和加速度(但是我将数据转换为弧度/秒)。:
import numpy as np
import quaternion
from utils import get_ang, get_acc
#utils
Z=np.zeros([3,3])
I=np.eye(3)
EARTH_GRAVITY_MS2 = -9.80665
#sample parameters
N=1 #DecimationFactor
fs=10 #SampleRate
#noise parameters
beta=3.0462e-13 #GyroscopeDriftNoise
eta=9.1385e-5 #GyroscopeNoise
kappa=N/fs #DecimationFactor/SampleRate
lamb=0.00019247 #AccelerometerNoise
nu=0.5 #LinearAccelerationDecayFactor
csi=0.0096236 #LinearAccelerationNoise
#other parameters initialization
lin_acc_prior=np.zeros(3)
gyro_offset=np.zeros([1,3])
Q=np.diag([0.000006092348396, 0.000006092348396, 0.000006092348396, 0.000076154354947,0.000076154354947, 0.000076154354947,0.009623610000000, 0.009623610000000, 0.009623610000000])
R=(lamb+csi+(kappa**2)*(beta+eta))*I
P=Q
q=quaternion.quaternion(1,0,0,0)
while(1):
"""----------------------------------------------------------Model----------------------------------------------------------"""
"""Predict orientation (q-)"""
gyro_readings=np.array(np.radians([get_ang()])) #rad/s
for i in range(N-1):
gyro_readings=np.append(gyro_readings,np.radians([get_ang()]),axis=0)
delta_phi=(gyro_readings-gyro_offset)/fs #rad/s
delta_q=quaternion.from_rotation_vector(delta_phi)
q=q*np.prod(delta_q)
"""Estimate gravity from orientation (g)"""
r_prior=quat2rotm(q)
g=r_prior[:,2:3].transpose()*(-EARTH_GRAVITY_MS2) #m/s^2
"""Estimate gravity from acceleration (g_acc)"""
accel_readings=get_acc() #m/s^2
g_acc=accel_readings-lin_acc_prior #m/s^2
"""----------------------------------------------------------Error Model----------------------------------------------------------"""
"Error Model (z)"
z=g-g_acc #m/s^2
"""----------------------------------------------------------Kalman Equations----------------------------------------------------------"""
"""Observation model (H)"""
gx=g[0,0]
gy=g[0,1]
gz=g[0,2]
g_cross=np.array([[0, gz, -gy],[-gz, 0, gx],[gy, -gx, 0]])
H=np.block([g_cross, -kappa*g_cross, I])
"""Innovation covariance (S)"""
S=R+np.dot(H,np.dot(P,H.transpose()))
"""Kalman gain (K)"""
K=np.dot(P,np.dot(H.transpose(),np.linalg.inv(S)))
"""Update error estimate covariance (P+)"""
P=P-np.dot(K,np.dot(H,P))
"""Predict error estimate covariance (P-)"""
D1=np.diag(np.diag(P[0:3,0:3])) #first diagonal block P
D2=np.diag(np.diag(P[3:6,3:6])) #second diagonal block P
D3=np.diag(np.diag(P[6:9,6:9])) #third diagonal block P
Q11=D1+kappa**2*D2+(beta+eta)*I
Q12=(D2+beta*I)
Q12[0,0]*=-kappa
Q22=D2+beta*I
Q33=nu**2*D3+csi*I
Q=np.block([[Q11,Q12,Z],[Q12,Q22,Z],[Z,Z,Q33]])
P=Q
"""Update posterior error (x)"""
x=np.dot(K,z.transpose())
"""----------------------------------------------------------Correct----------------------------------------------------------"""
"""Estimate orientation (q+)"""
theta=x[:3].transpose() #rad
q=q*quaternion.from_rotation_vector(-theta)[0]
"""Estimate linear acceleration (lin_acc_prior)"""
b=x[3:6].transpose() #rad/s
lin_acc_prior = lin_acc_prior*nu-b
"""Estimate gyro offset (gyro_offset)"""
a=x[6:].transpose()
gyro_offset=gyro_offset-a
"""----------------------------------------------------------Compute Angular Velocity----------------------------------------------------------"""
"""Angular velocity (angular_velocity)"""
angular_velocity=np.sum(gyro_readings,axis=0)/N-gyro_offset
当我的IMU保持静止状态(get_ang
返回约为[0, 0, 0]
的值,而get_acc
返回约为[0, 0, -9.8]
的值)时,我观察到gyro_offset
的异常增长(可能是由于a
值不够小所致),导致delta_phi
、delta_q
、q
和因此g
和z
的估计错误。
我检查了我的代码很多次,但没有找到任何错误。我认为我可能会误解上面链接中的说明,可能会在测量单位(度、弧度、m / s ^ 2、g)方面混淆,但即使尝试不同的组合,我也得到类似的行为。
请帮助我找出我缺少什么。
附:可以在每个步骤中放置:
gyro_readings=np.random.normal(0,1,[1,3])/50
accel_readings=np.array([0,0,-9.8])+np.random.normal(0,1,[3])/50