Mas*_*can 5 c++ eigen kalman-filter
我使用C ++中的Eigen库编写了一个kalman过滤器实现,并在此链接处使用该实现来测试我的过滤器:我的预测步骤如下所示:
void KalmanFilter::Predict()
{
// state Estimate = state transition matrix * previous state
// No control input present.
x = A * x;
// State Covariance Matrix = (State Transition Matrix * Previous State
Covariance matrix * (State Transition Matrix)^T ) + Process Noise
P = A * P * A.transpose() + Q;
}
Run Code Online (Sandbox Code Playgroud)
而我的更新步骤是:
void KalmanFilter::Update(VectorXd z)
{
//Kalman Gain = (State Covariance Matrix * Measurement matrix.transpose) * (H*P*H^T + Measurement Noise)^-1
K = (P * H.transpose()) * (H * P * H.transpose()+ R).inverse();
//Estimated Stated = Estimated state + Kalman Gain (Measurement Innovation)
x = x + K*(z - H * x);
//State Covariance matrix = (Identity Matrix of the size of x.size * x.size) - K* H * P;
long x_size = x.size();
MatrixXd I = MatrixXd::Identity(x_size, x_size);
P = (I - K * H) * P ;
}
Run Code Online (Sandbox Code Playgroud)
我的初始值为:
pos_x = 0.0;
pos_y = 0.0;
pos_z = 1.0;
vel_x = 10.0;
vel_y = 0.0;
vel_z = 0.0;
acc_x = 0.0;
acc_y = 0.0;
acc_z = -9.81;
Run Code Online (Sandbox Code Playgroud)
我通过循环执行以下操作来生成“伪数据”:
double c = 0.1; // Drag resistance coefficient
double damping = 0.9 ; // Damping
double sigma_position = 0.1 ; // position_noise
// Create simulated position data
for (int i = 0; i < N; i ++)
{
acc_x = -c * pow(vel_x, 2); // calculate acceleration ( Drag Resistance)
vel_x += acc_x * dt; // Integrate acceleration to give you velocity in the x axis.
pos_x += vel_x * dt; // Integrate velocity to return the position in the x axis
acc_z = -9.806 + c * pow(vel_z, 2); // Gravitation + Drag
vel_z += acc_z * dt; // z axis velocity
pos_z += vel_z * dt; // position in z axis
// generate y position here later.
if(pos_z < 0.01)
{
vel_z = -vel_z * damping;
pos_z += vel_z * dt;
}
if (vel_x < 0.1)
{
acc_x = 0.0;
acc_z = 0.0;
}
// add some noise
pos_x = pos_x + sigma_position * process_noise(generator);
pos_y = pos_y + sigma_position * process_noise(generator);
pos_z = pos_z + sigma_position * process_noise(generator);
Run Code Online (Sandbox Code Playgroud)
然后,我按以下步骤运行预测和更新:
// Prediction Step
kalmanFilter.Predict();
// Correction Step
kalmanFilter.Update(z);
Run Code Online (Sandbox Code Playgroud)
其中z是3 x 1的向量,包含 pos_x, pos_y and pos_z
我的状态转换矩阵A如下所示:
A << 1, 0, 0, dt, 0, 0, dt_squared, 0 , 0,
0, 1, 0, 0, dt, 0, 0, dt_squared, 0,
0, 0, 1, 0, 0, dt, 0, 0, dt_squared,
0, 0, 0, 1, 0, 0, dt, 0, 0,
0, 0, 0, 0, 1, 0, 0 , dt, 0,
0, 0, 0, 0, 0, 1, 0, 0, dt,
0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1;
Run Code Online (Sandbox Code Playgroud)
这里dt_squared是(dt * dt) /2;
P is
P<< 100, 0, 0, 0, 0, 0, 0, 0, 0,
0, 100, 0, 0, 0, 0, 0, 0, 0,
0, 0, 100, 0, 0, 0, 0, 0, 0,
0, 0, 0, 100, 0, 0, 0, 0, 0,
0, 0, 0, 0, 100, 0, 0, 0, 0,
0, 0, 0, 0, 0, 100, 0, 0, 0,
0, 0, 0, 0, 0, 0, 100, 0, 0,
0, 0, 0, 0, 0, 0, 0, 100, 0,
0, 0, 0, 0, 0, 0, 0, 0, 100;
Run Code Online (Sandbox Code Playgroud)
和
R << 1, 0, 0,
0, 1, 0,
0, 0, 1;
Run Code Online (Sandbox Code Playgroud)
和
Q = G * G.transpose()* a * a;
Run Code Online (Sandbox Code Playgroud)
G9×1的矩阵在哪里
G << dt_squared, dt_squared, dt_squared, dt, dt, dt, 1, 1, 1;
a = 0.1 //( acceleration process noise)
Run Code Online (Sandbox Code Playgroud)
我的问题是我对y和z的估计头寸处于关闭状态,并且偏离了“真实”头寸。如果您查看以下图表,
这是我第一次尝试使用Kalman滤波器,但不确定在这里做错了什么。我的最终目标是使用它来估计无人机的位置。此外,我还有以下问题:
例如,在无人驾驶飞机的现实生活中,如果您不能直接观察过程,该如何选择过程噪声?您是否只是选择任意值?
对于长期的帖子,我深表歉意。任何帮助表示赞赏。
小智 2
我不确定这是代码相关问题、算法实现问题还是期望问题。
您确实意识到,如果假数据中有太多的操作,这样的过滤器将不会再现真实数据,甚至不会再现任何接近真实数据的数据。
另外,您的图表不存在。
我知道我的回答不符合社区标准,但我无法发表评论,否则我会这样做。
在您提供绘图并根据更新率检查路径曲率之前,我不会尝试详细说明。过滤器还需要针对特定系统进行“调整”。您可能需要使用噪声参数来更好地调整它。对于机动轨道,人们可能需要使用更高阶的滤波器、Singer 滤波器或 Jerk 滤波器……滤波器需要对系统进行足够好的建模。根据您的更新矩阵,您似乎有一个抛物线(二阶)估计。您可能还想在其他非软件或代码特定的论坛中询问此问题。