KalmanFilter.h卡尔曼滤波头文件+arduino
程序员文章站
2022-07-12 10:19:38
...
#include "KalmanFilter.h"
//////////////////////////yijielvbo////////////////////
void KalmanFilter::Yiorderfilter(float angle_m, float gyro_m,float dt,float K1)
{
angle6 = K1 * angle_m + (1 - K1) * (angle6 + gyro_m * dt);
// return angle6;
}
////////////////////////kalman/////////////////////////
void KalmanFilter::Kalman_Filter(double angle_m, double gyro_m,float dt,float Q_angle,float Q_gyro,float R_angle,float C_0)
{
angle += (gyro_m - q_bias) * dt;
angle_err = angle_m - angle;
Pdot[0] = Q_angle - P[0][1] - P[1][0];
Pdot[1] = - P[1][1];
Pdot[2] = - P[1][1];
Pdot[3] = Q_gyro;
P[0][0] += Pdot[0] * dt;
P[0][1] += Pdot[1] * dt;
P[1][0] += Pdot[2] * dt;
P[1][1] += Pdot[3] * dt;
PCt_0 = C_0 * P[0][0];
PCt_1 = C_0 * P[1][0];
E = R_angle + C_0 * PCt_0;
K_0 = PCt_0 / E;
K_1 = PCt_1 / E;
t_0 = PCt_0;
t_1 = C_0 * P[0][1];
P[0][0] -= K_0 * t_0;
P[0][1] -= K_0 * t_1;
P[1][0] -= K_1 * t_0;
P[1][1] -= K_1 * t_1;
angle += K_0 * angle_err; //最优角度
q_bias += K_1 * angle_err;
angle_dot = gyro_m - q_bias; //最优角速度
}
////////////////////////kalman/////////////////////////
///////////////////////////// Angle test/////////////////////////////////
void KalmanFilter::Angletest(int16_t ax,int16_t ay,int16_t az,int16_t gx,int16_t gy,int16_t gz,float dt,float Q_angle,float Q_gyro,
float R_angle,float C_0,float K1)
{
// int flag;
//平衡参数
float Angle = atan2(ay , az) * 57.3; //角度计算公式,Angle:一阶互补滤波计算出的小车最终倾斜角度
Gyro_x = (gx - 128.1) / 131; //角度转换
Kalman_Filter(Angle, Gyro_x, dt, Q_angle, Q_gyro,R_angle,C_0); //卡曼滤波
//旋转角度Z轴参数
if (gz > 32768) gz -= 65536; //强制转换2g 1g
Gyro_z = -gz / 131; //Z轴参数转换
accelz = az / 16.4;
float angleAx = atan2(ax, az) * 180 / PI; //计算与x轴夹角
Gyro_y = -gy / 131.00; //计算角速度
Yiorderfilter(angleAx, Gyro_y, dt, K1); //一阶滤波
}
上面是KalmanFilter.cpp;下面是KalmanFilter.h
#ifndef KalmanFilter_h
#define KalmanFilter_h
#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
class KalmanFilter
{
public:
void Yiorderfilter(float angle_m, float gyro_m,float dt,float K1);
void Kalman_Filter(double angle_m, double gyro_m,float dt,float Q_angle,float Q_gyro,float R_angle,float C_0);
void Angletest(int16_t ax,int16_t ay,int16_t az,int16_t gx,int16_t gy,int16_t gz,float dt,float Q_angle,float Q_gyro,
float R_angle,float C_0,float K1);
float Gyro_x,Gyro_y,Gyro_z;
float accelz = 0;
float angle;
float angle6;
private:
float angle_err,q_bias;
float Pdot[4] = { 0, 0, 0, 0};
float P[2][2] = {{ 1, 0 }, { 0, 1 }};
float PCt_0, PCt_1, E, K_0, K_1, t_0, t_1;
float angle_dot;
};
#endif
//
// END OF FILE
//