应将MPU6050模块有引脚的那排向内,前方为正
pitch:俯仰角 精度:0.1° 范围:-90.0° <---> +90.0°
roll:横滚角 精度:0.1° 范围:-180.0°<---> +180.0°
yaw:航向角 精度:0.1° 范围:-180.0°<---> +180.0°
1,使用硬件I2C,DMP;(使用mpu6050自带的DMP进行解算,直接输出四元数,然后通过四元数转化转换为对应的姿态角,但是没有Z轴上的参考量,所以Yaw是不准的;200Hz)
当MPU6050只转动俯仰角,俯仰角接近90度或者等于90度时,偏航角的角度会发生很大的偏差。那么这个就是万向节锁,此时如果比作一架飞机,当飞机垂直90度向上时,按照Z-Y-X顺序无论怎么改变方向飞机都只能向上飞。所以不要让俯仰角垂直,这样可以减少bug。
2,开启I2C1与中断,USART1串口打印输出;
3,加入MPU6050驱动.c文件:
添加头文件:
#include "stdio.h"
#include "mpu6050.h"
#include "delay.h"
#include "inv_mpu.h"
#include "inv_mpu_dmp_motion_driver.h"
#include "common.h" //u8,u16...
还有一些hal库文件,需要手动修改即可
float pitch,roll,yaw; //欧拉角
short aacx,aacy,aacz; //加速度传感器原始数据
short gyrox,gyroy,gyroz; //陀螺仪原始数据
short temp; //温度
printf("ok");
while(MPU_Init()); //初始化MPU6050,需要水平(DMP自检可能会失败)
while(mpu_dmp_init())
{
delay_ms(200);
printf("%s\r\n","Mpu6050 Init Wrong!");
}
printf("%s\r\n","Mpu6050 Init OK!");
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
{
//下面三行非必要,可以直接打印
//temp=MPU_Get_Temperature(); //得到温度值
//MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度传感器数据
//MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺仪数据
// printf("Pitch:%.1f Roll:%.1f Yaw:%.1f\r\n",pitch,roll,yaw); //姿态角(yaw不准)
// printf("AX:%d AY:%d AZ:%d\r\n",aacx,aacy,aacz);
// printf("GX:%d GY:%d GZ:%d\r\n",gyrox,gyroy,gyroz);
// printf("Temp:%.2f\r\n",temp/100.0f);
}
HAL_Delay(100);
//==================================================2024/10月============推荐方法=================================
(一),添加MPU6050库(Yaw库)
(二),添加头文件
#include "stdio.h"
#include "string.h"
#include "MPU6050.h"
(三),初始化,在MPU6050.h中更改开启 IIC 的句柄(具体在#include "inv_mpu.c")
int res=1;
float pitch;
float roll;
float yaw;
while(res)
{
res = MPU6050_DMP_Init();
HAL_Delay(1000);
}
(四),循环获取角度
if(My_Angle(&pitch, &roll, &yaw))
{
//
}