四旋翼飛行器的飛控實(shí)現(xiàn)
嘗試制作這個(gè)四旋翼飛控的過(guò)程,感觸頗多,整理了思緒之后,把重要的點(diǎn)一一記下來(lái);
本文引用地址:http://www.ex-cimer.com/article/201701/342563.htm這個(gè)飛控是基于STM32,整合了MPU6050,即陀螺儀和重力加速計(jì),但沒(méi)有融合電子羅盤(pán);
另外,四旋翼飛行器的運(yùn)動(dòng)方式請(qǐng)百度百科,不太復(fù)雜,具體不再贅述;
這是飛控程序的控制流程(一個(gè)執(zhí)行周期):
比較重要的地方:
1.i2c通信方式;
因?yàn)槲也皇菍W(xué)電類(lèi)專(zhuān)業(yè),最開(kāi)始對(duì)i2c這些是沒(méi)有一點(diǎn)概念,最后通過(guò)Google了解了一些原理,然后發(fā)現(xiàn)STM32的開(kāi)發(fā)庫(kù)是帶有i2c通信的相關(guān)函數(shù)的,但是我最后還是沒(méi)有用這些函數(shù)。
我通過(guò)GPIO模擬i2c,這樣也能獲得mpu6050的數(shù)據(jù),雖然代碼多了一些,但是比較好的理解i2c的原理。
STM32庫(kù)實(shí)現(xiàn)的模擬i2c代碼(注釋好像因?yàn)榫幋a問(wèn)題跪了):
/*******************************************************************************
// file : i2c_conf.h
// MCU : STM32F103VET6
// IDE : Keil uVision4
// date £o2014.2.28
*******************************************************************************/
#include "stm32f10x.h"
#define uchar unsigned char
#define uint unsigned int
#define FALSE 0
#define TRUE 1
void I2C_GPIO_Config(void);
void I2C_delay(void);
void delay5ms(void);
int I2C_Start(void);
void I2C_Stop(void);
void I2C_Ack(void);
void I2C_NoAck(void);
int I2C_WaitAck(void);
void I2C_SendByte(u8 SendByte);
unsigned char I2C_RadeByte(void);
int Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data);
unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address);
/*******************************************************************************
// file : i2c_conf.c
// MCU : STM32F103VET6
// IDE : Keil uVision4
// date £o2014.2.28
*******************************************************************************/
#include "i2c_conf.h"
#define SCL_H GPIOB->BSRR = GPIO_Pin_6
#define SCL_L GPIOB->BRR = GPIO_Pin_6
#define SDA_H GPIOB->BSRR = GPIO_Pin_7
#define SDA_L GPIOB->BRR = GPIO_Pin_7
#define SCL_read GPIOB->IDR & GPIO_Pin_6 //IDR:???úê?è???′??÷?£
#define SDA_read GPIOB->IDR & GPIO_Pin_7
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //?a??ê?3??£ê?
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void I2C_delay(void)
{
int i=6; //?aà??éò?ó??ˉ?ù?è £??2aê?×?μíμ?5?1?üD′è?
while(i)
{
i--;
}
}
void delay5ms(void)
{
int i=5000;
while(i)
{
i--;
}
}
int I2C_Start(void)
{
SDA_H; //II2Dòé1??¨±?D??úê±?ó???aμíμ???μ??°ìá??£?2??éò?è? êy?Y??D?o???±?
SCL_H;
I2C_delay();
if(!SDA_read)
return FALSE; //SDA???aμíμ????ò×ü???|,í?3?
SDA_L;
I2C_delay();
if(SDA_read)
return FALSE; //SDA???a??μ????ò×ü??3?′í,í?3?
SDA_L;
I2C_delay();
return TRUE;
}
void I2C_Stop(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SDA_H;
I2C_delay();
}
void I2C_Ack(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
void I2C_NoAck(void)
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
int I2C_WaitAck(void) //·μ???a:=1óDACK, =0?TACK
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
SCL_L;
I2C_delay();
return FALSE;
}
SCL_L;
I2C_delay();
return TRUE;
}
void I2C_SendByte(u8 SendByte) //êy?Y′ó????μ?μí??//
{
u8 i=8;
while(i--)
{
SCL_L;
I2C_delay();
if(SendByte&0x80) // 0x80 = 1000 0000;
SDA_H;
else
SDA_L;
SendByte<<=1; // SendByte×óò?ò????£
I2C_delay();
SCL_H;
I2C_delay();
}
SCL_L;
}
unsigned char I2C_RadeByte(void) //êy?Y′ó????μ?μí??//
{
u8 i=8;
u8 ReceiveByte=0;
SDA_H;
while(i--)
{
ReceiveByte<<=1; //×óò?ò???£?
SCL_L;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
ReceiveByte"=0x01; //D′è?
}
}
SCL_L;
return ReceiveByte;
}
int Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
if(!I2C_Start())
return FALSE;
I2C_SendByte(SlaveAddress); //·¢?íéè±?μ??·+D′D?o? //I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE); //éè?????eê?μ??·+?÷?tμ??·
if(!I2C_WaitAck())
{
I2C_Stop();
return FALSE;
}
I2C_SendByte(REG_Address ); //éè??μí?eê?μ??·
I2C_WaitAck();
I2C_SendByte(REG_data);
I2C_WaitAck();
I2C_Stop();
delay5ms();
return TRUE;
}
unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address)
{
unsigned char REG_data;
if(!I2C_Start())
return FALSE;
I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//éè?????eê?μ??·+?÷?tμ??·
if(!I2C_WaitAck())
{
I2C_Stop();
return FALSE;
}
評(píng)論