MPU9250使用笔记

使用官方库移植的办法
使用到的两个文件是“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); //延时
			}
			


		 }

}

欢迎大家交流学习

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据引用\[1\],DMP是一个硬件模块,内置了MPU9250芯片,只能获取加速度计和陀螺仪的数据。因此,DMP只支持基于加速度计和陀螺仪数据的算法特性,其他类型的数据无法使用DMP。 根据引用\[2\],如果不使用中断,每次读取MPL一次后需要进行复位操作,这可能是由于延时函数导致总体时间过长。使用中断后,连续读取1000次数据只需要5秒多的时间,这基本上是最快的方法。 根据引用\[3\],MPU-9250和ICM-20948芯片可以在1.8V的VDDIO下工作,无需重新设计电源。如果已经将VDDIO限制在1.71V到1.95V范围内,可以直接替换MPU9250为ICM-20948,无需进行任何板/设计更改。需要注意的是,在通电时不支持将SCL/SCLK和nCS引脚保持低电平,如果使用这种方法,需要在初始化之前使用PWR_管理_1寄存器进行软件重置。 综上所述,关于mpu9250mpl库的具体问题,需要提供更多的信息才能给出详细的回答。 #### 引用[.reference_title] - *1* *3* [STM32入门笔记(02):MPU6050、MPU9250、ICM20948及姿态解算(SPL库函数版)](https://blog.csdn.net/Naiva/article/details/123847174)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [关于MPU9250的六轴DMP和九轴MPL](https://blog.csdn.net/Fei_Yang_YF/article/details/94307921)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值