STM32F1基于STM32CubeMX配置移植dmp库通过串口打印MPU6050数据
- ✨基于5.1版本以及6.12版本测试
- 📜串口打印mpu6050数据信息
✨本文将分享基于
STM32CubeIDE
和Keil MDK两个版本的工程。(当然你也可以根据STM32CubeMX自行重新配置自己所需版本的工程)
✨本文不做代码移植讲解,DMP库移植STM32可以翻阅本论坛其他开发者分享的相关讲解。
🚩移植参考说明文章推荐
📋补充说明
- 🔖测试时,如果调用mpu自检校验函数
int mpu_run_self_test(long *gyro, long *accel)
,那么mpu6050一定要放平,否则自检不通过,但是这不影响使用。 - 🌿获取数据,调用的接口函数都在
inv_mpu.h
。 - 🌿欧拉角需要通过四元组转换。通过调用
int dmp_read_fifo(short *gyro, short *accel, long *quat, unsigned long *timestamp, short *sensors, unsigned char *more)
函数,来获取更新数据。四元组数据格式为:q30,转换为浮点数,除以2^30次方。温度为q16格式,转换为浮点数,除以 2^16次方;加速度和角速度无需格式转换。 - 📝四元组转欧拉角:
#define q30 1073741824.0f //用于转换四元组数据
#define q16 65536.0f //用于转换温度数据
//得到dmp处理后的数据(注意,本函数需要比较多堆栈,局部变量有点多)
//pitch:俯仰角 精度:0.1° 范围:-90.0° <---> +90.0°
//roll:横滚角 精度:0.1° 范围:-180.0°<---> +180.0°
//yaw:航向角 精度:0.1° 范围:-180.0°<---> +180.0°
//返回值:0,正常
// 其他,失败
uint8_t mpu_dmp_get_data(float* pitch, float* roll, float* yaw, float* Temp)
{
float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f;
unsigned long sensor_timestamp;
long temperature;
short gyro[3], accel[3], sensors;
unsigned char more;
long quat[4];
if(dmp_read_fifo(gyro, accel, quat, &sensor_timestamp, &sensors, &more))return 1;
/* Gyro and accel data are written to the FIFO by the DMP in chip frame and hardware units.
* This behavior is convenient because it keeps the gyro and accel outputs of dmp_read_fifo and mpu_read_fifo consistent.
**/
if(sensors & INV_XYZ_GYRO) //
{
/* Temperature only used for gyro temp comp. */
mpu_get_temperature(&temperature, &sensor_timestamp);
*Temp = temperature / q16; //温度数据为q16格式
My_Mpu6050.gyro[0] = gyro[0]; //加速度rps
My_Mpu6050.gyro[1] = gyro[1];
My_Mpu6050.gyro[2] = gyro[2];
}
else return 2;
if(sensors & INV_XYZ_ACCEL) //加速度
{
My_Mpu6050.accel[0] = accel[0];
My_Mpu6050.accel[1] = accel[1];
My_Mpu6050.accel[2] = accel[2];
}
/*
send_packet(PACKET_TYPE_GYRO, gyro);
if (sensors & INV_XYZ_ACCEL)
send_packet(PACKET_TYPE_ACCEL, accel); */
/* Unlike gyro and accel, quaternions are written to the FIFO in the body frame, q30.
* The orientation is set by the scalar passed to dmp_set_orientation during initialization.
**/
if(sensors & INV_WXYZ_QUAT)
{
// q30 格式四元数转换成欧拉角,57.3参数是将弧度换算成角度
q0 = quat[0] / q30; //q30格式转换为浮点数
q1 = quat[1] / q30;
q2 = quat[2] / q30;
q3 = quat[3] / q30;
//计算得到俯仰角/横滚角/航向角
*pitch = asin(-2 * q1 * q3 + 2 * q0 * q2) * 57.3; // pitch
*roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2 * q2 + 1) * 57.3; // roll
*yaw = atan2(2 * (q1 * q2 + q0 * q3), q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3) * 57.3; //yaw
My_Mpu6050.pitch = *pitch;
My_Mpu6050.roll = *roll;
My_Mpu6050.yaw = *yaw;
}
else return 2;
return 0;
}
🛠接线说明
stm32 ------ MPU6050
PB6(SCL) ---- SCL
PB7(SDA) ---- SDA
VCC ---- VCC(3.3V)
GND ---- GND
-------
串口1:PA9 、 PA10
🌼Keil MDK版本工程源码
复制这段内容后打开百度网盘手机App,操作更方便哦
链接: https://pan.baidu.com/e/1-ApNiRBUIrl4hWi1KASoDw
提取码: ak7q
🌻STM32CubeIDE版本工程源码(5.1版本)
链接: https://pan.baidu.com/e/1gq24KmwVS-yxxu6f3bjE0A
提取码: rawk
- 🔖以上工程都经测试可以读取传感器数据。
- 📌InvenSense Embedded Motion Tracker Driver 5.1驱动库
https://drivers.softpedia.com/get/Other-DRIVERS-TOOLS/Others/InvenSense-Embedded-Motion-Tracker-Driver-51.shtml
- 📍TDK官网资源下载地址:
https://invensense.tdk.com/developers/software-downloads/
- 🌿EDK官网5.13版本资源
链接:https://pan.baidu.com/s/1V2xOEVXSvcZUe6MJeS_QAg
提取码:3oex
- 🌿TDK官网6.12版本资源
链接:https://pan.baidu.com/s/1HgjoS_X1FQ6yeg07Or8fMw
提取码:drsc