由于硬件上采用了mpu6050模块,这模块是自带姿态解算和卡尔曼滤波功能的,所以省了不少事儿。直接通过串口读模块发送的数据,然后按照模块固定的协议把数据解析出来,就能得到三轴角速度,三轴加速度,三轴角度。其实mpu6050内部只集成了陀螺仪测角速度,加速度计测加速度,没有直接测量角度的传感器,但是角度可以通过角速度积分得到,并且通过重力加速度在各轴上的分量进行校正,通过数据融合的算法(比如卡尔曼滤波),就可以比较准确的得到角度啦。当然这个过程都在模块内部完成,我们只是应用的话不需要太关注(以后做四轴的话应该得好好研究一下姿态解算和数据融合方面的东西)。
好了,废话不多说。先说代码思路,然后就上代码。Stm32F405的usart3连接模块,接收数据,通过DMA直接存到一个11字节的数组GYRO_buffer[11]里面,当数组存满了,也就是接受到了完整的一帧数据(一帧数据包括可能是角度数据、加速度数据或者是角速度数据,要根据帧头的第二个字节加以区分,具体的数据帧格式见附件里面的模块资料),然后触发DMA中断,在中断中对数据进行解析,得到最终数据,存储到一个GYRO结构体变量中,这个变量被设置为全局变量,可以在其他的函数中使用该变量的值,做姿态的控制。
代码:
#include "main.h"
GYRO gyro = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
unsigned char GYRO_buffer[11];
void usart3_configuration(void)
{
//一定注意配置顺序,先除能再使能等等
USART_InitTypeDef usart3;
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
DMA_InitTypeDef dma;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
/*配置GPIO*/
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);
gpio.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_Speed = GPIO_Speed_100MHz;
gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC,&gpio);
/*配置DMA*/
DMA_Cmd(DMA1_Stream1,DISABLE);
while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);//等待DMA可配置
DMA_DeInit(DMA1_Stream1);
dma.DMA_Channel= DMA_Channel_4;
dma.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);
dma.DMA_Memory0BaseAddr = (uint32_t)GYRO_buffer;
dma.DMA_DIR = DMA_DIR_PeripheralToMemory;
dma.DMA_BufferSize = 11;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
dma.DMA_Mode = DMA_Mode_Circular;
dma.DMA_Priority = DMA_Priority_VeryHigh;
dma.DMA_FIFOMode = DMA_FIFOMode_Disable;
dma.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream1,&dma);
DMA_Cmd(DMA1_Stream1,ENABLE);
/*配置NVIC*/
nvic.NVIC_IRQChannel = DMA1_Stream1_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 1;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
DMA_ITConfig(DMA1_Stream1,DMA_IT_TC,ENABLE);
/*配置usart3*/
usart3.USART_BaudRate = 115200;
usart3.USART_WordLength = USART_WordLength_8b;
usart3.USART_StopBits = USART_StopBits_1;
usart3.USART_Parity = USART_Parity_No;
usart3.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
usart3.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART3,&usart3);/*usart3初始化*/
//USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);/*配置usart3中断:读数据寄存器非空则中断*/
USART_Cmd(USART3,ENABLE);/*使能usart3*/
USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);
}
void DMA1_Stream1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream1, DMA_IT_TCIF1))
{
DMA_ClearFlag(DMA1_Stream1, DMA_FLAG_TCIF1);
DMA_ClearITPendingBit(DMA1_Stream1, DMA_IT_TCIF1);
if(GYRO_buffer[0] == 0x55)
{
switch(GYRO_buffer[1])
{
case 0x51:
gyro.AX = ((short)(GYRO_buffer[3]<<8 | GYRO_buffer[2]))/32768.0*16;//g
gyro.AY = ((short)(GYRO_buffer[5]<<8 | GYRO_buffer[4]))/32768.0*16;
gyro.AZ = ((short)(GYRO_buffer[7]<<8 | GYRO_buffer[6]))/32768.0*16;
gyro.Temperature = ((short)(GYRO_buffer[9]<<8 | GYRO_buffer[8])) /340.0f+36.53f;
led_red_on();
led_green_off();
break;
case 0x52:
gyro.GX = ((short)(GYRO_buffer[3]<<8 | GYRO_buffer[2]))/32768.0*2000;//°/s
gyro.GY = ((short)(GYRO_buffer[5]<<8 | GYRO_buffer[4]))/32768.0*2000;
gyro.GZ = ((short)(GYRO_buffer[7]<<8 | GYRO_buffer[6]))/32768.0*2000;
gyro.Temperature = ((short)(GYRO_buffer[9]<<8 | GYRO_buffer[8])) /340.0f+36.53f;
led_red_off();
led_green_on();
break;
case 0x53:
gyro.PITCH = ((short)(GYRO_buffer[3]<<8 | GYRO_buffer[2]))/32768.0*180;//度
gyro.ROLL = ((short)(GYRO_buffer[5]<<8 | GYRO_buffer[4]))/32768.0*180;
gyro.YAW = ((short)(GYRO_buffer[7]<<8 | GYRO_buffer[6]))/32768.0*180;
gyro.Temperature = ((short)(GYRO_buffer[9]<<8 | GYRO_buffer[8])) /340.0f+36.53f;
led_red_on();
led_green_on();
break;
}
}
}
}
其中GYRO结构体的结构是这样的:
typedef struct
{
float AX;
float AY;
float AZ;
float GX;
float GY;
float GZ;
float PITCH;
float ROLL;
float YAW;
float Temperature;
}GYRO;
我开始在调试的过程中一直进不了DMA中断,后来发现是DMA配置的问题。下面把DMA配置部分的代码单独调出来说一下
/*配置DMA*/
DMA_Cmd(DMA1_Stream1,DISABLE);
while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);//等待DMA可配置
DMA_DeInit(DMA1_Stream1);
dma.DMA_Channel= DMA_Channel_4;
dma.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);
dma.DMA_Memory0BaseAddr = (uint32_t)GYRO_buffer;
dma.DMA_DIR = DMA_DIR_PeripheralToMemory;
dma.DMA_BufferSize = 11;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
dma.DMA_Mode = DMA_Mode_Circular;
dma.DMA_Priority = DMA_Priority_VeryHigh;
dma.DMA_FIFOMode = DMA_FIFOMode_Disable;
dma.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream1,&dma);
DMA_Cmd(DMA1_Stream1,ENABLE);
配置的时候首先用DMA_Cmd(DMA1_Stream1,DISABLE);除能DMA,因为DAM在运行过程中是不能配置的。然后用while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);语句等待DMA被除能(可能是因为除能需要一定的时间吧,现在也不是很清楚为啥),确定DMA除能后,开始配置DMA,配置完成后,用DMA_Cmd(DMA1_Stream1,ENABLE);重新使能DMA。然后在使能usart3之后,还要加上USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);语句,使能usart3的DAM功能。
关于stm32的DMA功能具体的配置,网上有很多资料,就不详细说啦。这里只把我看了网上的资料后仍然没有解决,最终基本靠自己摸索解决的问题写出来,供大家参考。