使用官方库移植的办法
使用到的两个文件是“inv_mpu.c”和“inv_mpu_dmp_motion_driver.c”
在学习的过程中,觉得比较好的学习资料已经整理出来,以方便以后学习起来更加方便。
官方资料及代码下载入口
这里使用的是某宝买MPU6050模块给的代码经过移植和修改后能够实现读取9轴数据
首先初始化MPU9250
void MPU9250_Init(void)
{
int result=0;
result = mpu_init();
rt_thread_delay(10);
if(!result) //返回0代表初始化成功
{
rt_kprintf("mpu initialization complete......\n ");
//mpu_set_sensor
if(!mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS)) //打开/关闭特定的传感器。
{
rt_kprintf("mpu_set_sensor complete ......\n");
}
else
{
rt_kprintf("mpu_set_sensor come across error ......\n");
}
//mpu_configure_fifo
if(!mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL)) //选择将哪些传感器推入FIFO。
{
rt_kprintf("mpu_configure_fifo complete ......\n");
}
else
{
rt_kprintf("mpu_configure_fifo come across error ......\n");
}
//mpu_set_sample_rate
if(!mpu_set_sample_rate(DEFAULT_MPU_HZ)) //设置采样率。采样率必须在4Hz到1kHz之间。
{
rt_kprintf("mpu_set_sample_rate complete ......\n");
}
else
{
rt_kprintf("mpu_set_sample_rate error ......\n");
}
//dmp_load_motion_driver_firmvare
if(!dmp_load_motion_driver_firmware()) //用此映像加载DMP。
{
rt_kprintf("dmp_load_motion_driver_firmware complete ......\n");
}
else
{
rt_kprintf("dmp_load_motion_driver_firmware come across error ......\n");
}
//dmp_set_orientation
if(!dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation))) //将陀螺仪和加速度方向推向DMP。 compass_orientation
{
rt_kprintf("dmp_set_orientation complete ......\n");
}
else
{
rt_kprintf("dmp_set_orientation come across error ......\n");
}
//
/*
inv_set_compass_orientation_and_scale(inv_orientation_matrix_to_scalar(compass_pdata.orientation),(long)compass_fsr<<15);
*/
if(!dmp_set_orientation(inv_orientation_matrix_to_scalar(compass_orientation))) //将compass方向推向DMP。 compass_orientation
{
rt_kprintf("compassdmp_set_orientation complete ......\n");
}
else
{
rt_kprintf("compass dmp_set_orientation come across error ......\n");
}
//dmp_enable_feature
if(!dmp_enable_feature(DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP |
DMP_FEATURE_ANDROID_ORIENT | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO |
DMP_FEATURE_GYRO_CAL))
{
rt_kprintf("dmp_enable_feature complete ......\n");
}
else
{
rt_kprintf("dmp_enable_feature come across error ......\n");
}
//dmp_set_fifo_rate
if(!dmp_set_fifo_rate(DEFAULT_MPU_HZ))
{
rt_kprintf("dmp_set_fifo_rate complete ......\n");
}
else
{
rt_kprintf("dmp_set_fifo_rate come across error ......\n");
}
//不开自检,以水平作为零度
//开启自检以当前位置作为零度
run_self_test();
if(!mpu_set_dmp_state(1))
{
rt_kprintf("mpu_set_dmp_state complete ......\n");
}
else
{
rt_kprintf("mpu_set_dmp_state come across error ......\n");
}
rt_kprintf("MPU9250_Init ok\n");
}
}
初始化成功后可以开始读取数据
void MPU9250_Pose(void)
{
dmp_read_fifo(gyro, accel, quat, &sensor_timestamp, &sensors,&more);
/* 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 )
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 )
{
q0 = quat[0] / q30;
q1 = quat[1] / q30;
q2 = quat[2] / q30;
q3 = quat[3] / q30;
Pitch = asin(-2 * q1 * q3 + 2 * q0* q2)* 57.3 - 1; // 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
/*这里使用的RT-Thread嵌入式开发系统,说以打印函数是rt_kprintf*/
rt_kprintf("\n==*****************************==\n");
rt_kprintf("Pitch:%d,Roll:%d,Yaw:%d \n",(int)Pitch,(int)Roll,(int)Yaw);
rt_kprintf("gyro[0]:%d,gyro[1]:%d,gyro[2]:%d\n",gyro[0],gyro[1],gyro[2]);
/* For any MPU device with an AKM on the auxiliary I2C bus, the raw
* magnetometer registers are copied to special gyro registers.
*/
// rt_kprintf("%d\n",mpu_get_compass_reg(compass_short, &sensor_timestamp));
if (!mpu_get_compass_reg(compass_short, &sensor_timestamp))
{
compass[0] = (long)compass_short[0];
compass[1] = (long)compass_short[1];
compass[2] = (long)compass_short[2];
rt_kprintf("compass[0]:%d,compass[1]:%d,compass[2]:%d\n",compass[0],compass[1],compass[2]);
/* NOTE: If using a third-party compass calibration library,
* pass in the compass data in uT * 2^16 and set the second
* parameter to INV_CALIBRATED | acc, where acc is the
* accuracy from 0 to 3.
*/
//inv_build_compass(compass, 0, sensor_timestamp);
rt_thread_delay(500); //延时
}
}
}
欢迎大家交流学习