目录
- 传感器的方向
- 源码
- Mahony_9.c
- Mahony_9.h
- 使用方法
- 测试
- main.c
- 效果
STC15F2K60S2 22.1184MHz
Keil uVision V5.29.0.0
PK51 Prof.Developers Kit Version:9.60.0.0
上位机:Vofa+ 1.3.10
移植自MPU6050 获取角度理论推导(三)—9轴融合算法 —— shao15232_1
传感器的方向
源码
所用MCU为STC15F2K60S2 使用内部RC时钟,22.1184MHz
stdint.h见【51单片机快速入门指南】1:基础知识和工程创建
软件I2C程序见【51单片机快速入门指南】4: 软件 I2C
串口部分见【51单片机快速入门指南】3.3:USART 串口通信
MPU6050驱动程序见【51单片机快速入门指南】4.3: I2C读取MPU6050陀螺仪的原始数据
HMC5883L/QMC5883L驱动程序见【51单片机快速入门指南】4.4:I2C 读取HMC5883L / QMC5883L 磁力计
磁力计的椭球拟合校准见【51单片机快速入门指南】4.4.1:python串口接收磁力计数据并进行最小二乘法椭球拟合
Kp和Ki要按需调整,我这里取10和0.008
Mahony_9.c
#include <math.h>
#include "MPU6050.h"#define Kp 10.0f // proportional gain governs rate of convergence to accelerometer/magnetometer
#define Ki 0.008f // integral gain governs rate of convergence of gyroscope biases float halfT = 1; // half the sample period采样周期的一半
float GYRO_K = 1;void MPU6050_Mahony_Init(float loop_ms)
{halfT = loop_ms/1000./2; //计算周期的一半,单位sswitch((MPU_Read_Byte(MPU_GYRO_CFG_REG) >> 3) & 3){case 0:GYRO_K = 1./131/57.3;break;case 1:GYRO_K = 1./65.5/57.3;break;case 2:GYRO_K = 1./32.8/57.3;break;case 3:GYRO_K = 1./16.4/57.3;break;}
}float Pitch = 0, Roll = 0, Yaw = 0;
float q0 = 1, q1 = 0, q2 = 0, q3 = 0; // quaternion elements representing the estimated orientation
float exInt = 0, eyInt = 0, ezInt = 0; // scaled integral errorvoid IMUupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz)
{float norm;float hx, hy, hz, bx, bz;float wx, wy, wz;float vx, vy, vz;float ex, ey, ez;// 先把这些用得到的值算好float q0q0 = q0*q0;float q0q1 = q0*q1;float q0q2 = q0*q2;float q0q3 = q0*q3;float q1q1 = q1*q1;float q1q2 = q1*q2;float q1q3 = q1*q3;float q2q2 = q2*q2;float q2q3 = q2*q3;float q3q3 = q3*q3;if (ax*ay*az == 0)return;if (mx*my*mz == 0)return;//将陀螺仪AD值转换为 弧度/sgx = gx * GYRO_K;gy = gy * GYRO_K;gz = gz * GYRO_K;norm = sqrt(ax*ax + ay*ay + az*az); //acc数据归一化ax = ax / norm;ay = ay / norm;az = az / norm;norm = sqrt(mx*mx + my*my + mz*mz); //mag数据归一化mx = mx / norm;my = my / norm;mz = mz / norm;hx = 2 * mx * (0.5 - q2q2 - q3q3) + 2 * my * (q1q2 - q0q3) + 2 * mz * (q1q3 + q0q2);hy = 2 * mx * (q1q2 + q0q3) + 2 * my * (0.5 - q1q1 - q3q3) + 2 * mz * (q2q3 - q0q1);hz = 2 * mx * (q1q3 - q0q2) + 2 * my * (q2q3 + q0q1) + 2 * mz * (0.5 - q1q1 - q2q2);bx = sqrt((hx*hx) + (hy*hy));bz = hz;// estimated direction of gravity and flux (v and w) 估计重力方向和流量/变迁vx = 2 * (q1q3 - q0q2); //四元素中xyz的表示vy = 2 * (q0q1 + q2q3);vz = q0q0 - q1q1 - q2q2 + q3q3;wx = 2 * bx * (0.5 - q2q2 - q3q3) + 2 * bz * (q1q3 - q0q2);wy = 2 * bx * (q1q2 - q0q3) + 2 * bz * (q0q1 + q2q3);wz = 2 * bx * (q0q2 + q1q3) + 2 * bz * (0.5 - q1q1 - q2q2);// error is sum of cross product between reference direction of fields and direction measured by sensors//向量外积在相减得到差分就是误差ex = (ay*vz - az*vy) + (my*wz - mz*wy);ey = (az*vx - ax*vz) + (mz*wx - mx*wz);ez = (ax*vy - ay*vx) + (mx*wy - my*wx);exInt = exInt + ex * Ki; //对误差进行积分eyInt = eyInt + ey * Ki;ezInt = ezInt + ez * Ki;// adjusted gyroscope measurementsgx = gx + Kp*ex + exInt; //将误差PI后补偿到陀螺仪,即补偿零点漂移gy = gy + Kp*ey + eyInt;gz = gz + Kp*ez + ezInt; //这里的gz由于没有观测者进行矫正会产生漂移,表现出来的就是积分自增或自减// integrate quaternion rate and normalise //四元素的微分方程q0 = q0 + (-q1*gx - q2*gy - q3*gz)*halfT;q1 = q1 + (q0*gx + q2*gz - q3*gy)*halfT;q2 = q2 + (q0*gy - q1*gz + q3*gx)*halfT;q3 = q3 + (q0*gz + q1*gy - q2*gx)*halfT;// normalise quaternionnorm = sqrt(q0*q0 + q1*q1 + q2*q2 + q3*q3);q0 = q0 / norm;q1 = q1 / norm;q2 = q2 / norm;q3 = q3 / norm;Pitch = asin(-2 * q1 * q3 + 2 * q0* q2)* 57.3; // pitchRoll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3; // rollYaw = atan2(2 * q1 * q2 + 2 * q0 * q3, -2 * q2*q2 - 2 * q3* q3 + 1)* 57.3; // yaw
}
Mahony_9.h
#ifndef Mahony_9_H_
#define Mahony_9_H_extern float Pitch, Roll, Yaw;
extern float q0, q1, q2, q3; // quaternion elements representing the estimated orientationvoid MPU6050_Mahony_Init(float loop_ms);
void IMUupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);#endif
使用方法
先调用MPU6050_Mahony_Init(dt),参数为一次循环的时间,单位为ms
再使用IMUupdate姿态融合函数。
测试
陀螺仪、磁力计的原始数据经校准后输入IMUupdate函数
main.c
#include <STC15F2K60S2.H>
#include "intrins.h"
#include "stdint.h"
#include "USART.h"
#include "./Software_I2C/Software_I2C.h"
#include "XMC5883L.h"
#include "./MPU6050/MPU6050.h"
#include "./MPU6050/Mahony_9.h"void Delay1ms() //@22.1184MHz
{unsigned char i, j;_nop_();_nop_();i = 22;j = 128;do{while (--j);} while (--i);
}void delay_ms(uint32_t ms)
{while(ms --)Delay1ms();
}#define LED_PORT P0void main(void)
{int16_t mag_x, mag_y, mag_z;int16_t aacx,aacy,aacz; //加速度传感器原始数据int16_t gyrox,gyroy,gyroz; //陀螺仪原始数据MPU_Init();xmc5883lInit();AUXR &= 0xBF; //定时器时钟12T模式 1T的51使用12T的定时器程序时需要加入这两句AUXR &= 0xFE; //串口1选择定时器1为波特率发生器USART_Init(USART_MODE_1, Rx_ENABLE, STC_USART_Priority_Lowest, 22118400, 115200, DOUBLE_BAUD_ENABLE, USART_TIMER_1);MPU6050_Mahony_Init(8.7);while(1){MPU_Get_Accelerometer(&aacx, &aacy, &aacz); //得到加速度传感器数据MPU_Get_Gyroscope(&gyrox, &gyroy, &gyroz); //得到陀螺仪数据xmc5883lRead(&mag_x, &mag_y, &mag_z);IMUupdate(gyrox+7, gyroy+23, gyroz-1, aacx, aacy, aacz, 1.108270606866881 * (mag_x + 297.2882033958856), 0.9218994400020794 * (mag_y + 3088.0092054124193), 0.9871899380641738 * (mag_z + 782.925290575134));printf("%f, ", Pitch);printf("%f, ", Roll);printf("%f\r\n", Yaw);}
}