rt-thead-studio 中实现 MPU6050基于DMP中断读取数据

rt-thead-studio 中实现 MPU6050基于DMP中断读取数据

目录

前言

本文基于rt-thead-studio 闪灯的工程,使用 mpu6050 和 内部的 mpl高级特性库, 直接读出并计算3轴的转角,性能稳定。

MPU6050具备中断寄存器,有助于精准获取数据。本文将使用 art_pi 配置FIFO中断,从而通过 int 控制中断时间。

mpl高级特性库问题

目前 mpl 高级库只支持 stm32 系列 mcu,因为 MotionDriver 只提供了 mpl 的预编译库,没开源

example 中使用了的 mpl 库,所以非 stm32 系列 mcu 无法运行

一、基于开发板创建闪灯工程

创建成功后,正常编译通过下载蓝灯闪烁。

二、添加 外设 i2c

在这里插入图片描述

运行命令: list_device ,显示 I2C3 设备已经存在。

在这里插入图片描述

三、添加 package - 参考 MotionDriver2RTT

添加package

在这里插入图片描述

修改属性,添加库文件libmplmpu ,详细配置使能 sample。 编译后出错,error: conflicting types for 'run_self_test'

motion_driver_example.c 中添加 函数 定义:

uint8_t run_self_test(void);
uint8_t mpu_mpl_get_data(void);

继续编译,出错 :

../packages/MotionDriver2RTT-latest/examples/motion_driver_example.c:295:9: error: conflicting types for 'mpu_mpl_get_data'
 uint8_t mpu_mpl_get_data(float *pitch,float *roll,float *yaw)
         ^
../packages/MotionDriver2RTT-latest/examples/motion_driver_example.c:44:9: note: previous declaration of 'mpu_mpl_get_data' was here
 uint8_t mpu_mpl_get_data(void);

修改44 行的定义: uint8_t mpu_mpl_get_data(float *pitch,float *roll,float *yaw);

编译,出错 :

/ld.exe: cannot find -llibmpllib

提示找不到库文件,按照下图修改库文件名称和位置:

在这里插入图片描述

再进行编译,提示重复定义:board/drv_mpu.c:15: multiple definition of mpu_init

修改 drv_mpu.c 中的 函数为静态: static int mpu_init(void)

修改使用 i2c3 设备: #define RT_MPU_DEVICE_NAME "i2c3"

编译成功,运行发现:

在1个tick的定时过程中,运行一段时间一定会内存溢出,读取的数据隔几次之后就会出现FIFO溢出错误,读的频率快了之后就会出现FIFO数据和要读取的数据长度不匹配(应该是读取的太快了,转换的数据还没来得及放到FIFO里)错误。

在这里插入图片描述

四、 修改MPU6050 FIFO中断配置

首先在MPU6050初始化函数中配置好中断,这里不用添加配置,因为本来已经配置好了 int 低电平中断。

MPU6050触发的是STM32的外部中断,此处使用的是PA15的外部中断,因为是低电平触发。

#define EVENT_MD_LOOP		(1<<0)
#define EVENT_PRINT		    (1<<1)
#define MPU6050_INT_PIN GET_PIN(A, 15)
static struct rt_event event_motion;

static void mpu6050_int_callback()
{
    rt_event_send(&event_motion, EVENT_MD_LOOP);
}

static void mpu6050_int_pin_init(void)
{
    rt_pin_mode(MPU6050_INT_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(MPU6050_INT_PIN, PIN_IRQ_MODE_FALLING, mpu6050_int_callback, RT_NULL);
    rt_pin_irq_enable(MPU6050_INT_PIN, PIN_IRQ_ENABLE);
}

void motion_loop(float *pitch,float *roll,float *yaw)
{
    mpu_mpl_get_data(pitch,roll,yaw);
}

static struct rt_timer timer_motion;
static struct rt_event event_motion;

static void timer_motion_update(void* parameter)
{
    rt_event_send(&event_motion, EVENT_PRINT);
}

void print_motion(float *pitch,float *roll,float *yaw)
{
    char str[48];
    sprintf(str,"pitch=%0.1f\troll=%0.1f\tyaw=%0.1f",*pitch,*roll,*yaw);
    LOG_I(str);
}

在 motion_init中,启动 motion_entry 线程; 开启中断,当有 int 产生,进行一次mpu_mpl_get_data 获取数据。同时开启定时器,定时发送打印事件。

void motion_entry(void *parameter)
{
	......
	/*添加mpu int 中断回调
     * MPU_Write_Byte(MPU_INT_EN_REG,0X01); //开启FIFO中断
     * MPU_Write_Byte(MPU_INTBP_CFG_REG,0X80);   //INT引脚低电平有效
     */
    mpu6050_int_pin_init();

    /* register timer event */
    rt_timer_init(&timer_motion, "timer_motion",
                    timer_motion_update,
                    RT_NULL,
                    100,
                    RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
    rt_timer_start(&timer_motion);

    while(1)
	{
		res = rt_event_recv(&event_motion, EVENT_MD_LOOP | EVENT_PRINT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
								RT_WAITING_FOREVER, &recv_set);
		
		if(res == RT_EOK){
			if(recv_set & EVENT_MD_LOOP){
				motion_loop(&pitch, &roll ,&yaw);
			}
			else if(recv_set & EVENT_PRINT){
                print_motion(&pitch, &roll, &yaw);
            }
		}
	}
}

五、配置DMP采样时长

在MPU6050初始化函数中配置

MPU_Set_Rate(200); //设置采样率为200Hz

在DMP初始化函数中配置

res = mpu_set_sample_rate(200); //设置采样率
if(res)
	return 3;

res = dmp_set_fifo_rate(200); //设置DMP输出速率(最大不超过200Hz)
if(res)
	return 7;   

此处将采样率设置为200HZ,那么采样时间就是1/200=5ms
根据公式T = 1 / f
实际上采样频率是根据DMP频率,如果两个频率不同最终也以DMP的为主。

用示波器查看 INT输出,可看到周期是10ms ,可以在程序中编写下降沿中断,读取 fifo

缺省DEFAULT_MPU_HZ 为 100

演示结果显示输出结果相当稳定:

在这里插入图片描述

式T = 1 / f
实际上采样频率是根据DMP频率,如果两个频率不同最终也以DMP的为主。

用示波器查看 INT输出,可看到周期是10ms ,可以在程序中编写下降沿中断,读取 fifo

缺省DEFAULT_MPU_HZ 为 100

演示结果显示输出结果相当稳定.

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南工孙冬梅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值