先看串口2,3初始化
USART2_Init(19200);//串口2、备用
USART3_Init(115200);//串口3、解析GPS
串口2初始化定义
void USART2_Init(unsigned long bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO
|RCC_APB2Periph_GPIOA , ENABLE);//串口2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//串口2 低速
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;//
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8bits
USART_InitStructure.USART_StopBits = USART_StopBits_1;//stop bit is 1
USART_InitStructure.USART_Parity = USART_Parity_No;//no parity
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//no Hardware Flow Control
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//enable tx and rx
USART_Init(USART2, &USART_InitStructure);//
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//rx interrupt is enable
USART_Cmd(USART2, ENABLE);
//USART2_Send((unsigned char *)Buffer,2);
UART2_Send(0xAA);
}
USART3_Init类似
接下来是pid初始化
首先是:
Total_PID_Init();//PID控制器初始化
void Total_PID_Init(void)
{
PID_Init(&Total_Controler.Pitch_Angle_Control,Pitch_Angle_Controler);
PID_Init(&Total_Controler.Pitch_Gyro_Control,Pitch_Gyro_Controler);
PID_Init(&Total_Controler.Roll_Angle_Control,Roll_Angle_Controler);
PID_Init(&Total_Controler.Roll_Gyro_Control,Roll_Gyro_Controler);
PID_Init(&Total_Controler.Yaw_Angle_Control,Yaw_Angle_Controler);
PID_Init(&Total_Controler.Yaw_Gyro_Control,Yaw_Gyro_Controler);
PID_Init(&Total_Controler.High_Position_Control,High_Position_Controler);
PID_Init(&Total_Controler.High_Speed_Control,High_Speed_Controler);
PID_Init(&Total_Controler.Longitude_Position_Control,Longitude_Position_Controler);
PID_Init(&Total_Controler.Longitude_Speed_Control,Longitude_Speed_Controler);
PID_Init(&Total_Controler.Latitude_Position_Control,Latitude_Position_Controler);
PID_Init(&Total_Controler.Latitude_Speed_Control,Latitude_Speed_Controler);
PID_Init(&Total_Controler.High_Acce_Control,High_Acce_Controler);
PID_Init(&Total_Controler.Longitude_Acce_Control,Longitude_Acce_Controler);
PID_Init(&Total_Controler.Latitude_Acce_Control,Latitude_Acce_Controler);
}
它调用了PID_Init
void PID_Init(PID_Controler *Controler,Controler_Label Label)
{
Controler->Err_Limit_Flag=(uint8)(Control_Unit[Label][0]);//1偏差限幅标志
Controler->Integrate_Limit_Flag=(uint8)(Control_Unit[Label][1]);//2积分限幅标志
Controler->Integrate_Separation_Flag=(uint8)(Control_Unit[Label][2]);//3积分分离标志
Controler->Expect=Control_Unit[Label][3];//4期望
Controler->FeedBack=Control_Unit[Label][4];//5反馈值
Controler->Err=Control_Unit[Label][5];//6偏差
Controler->Last_Err=Control_Unit[Label][6];//7上次偏差
Controler->Err_Max=Control_Unit[Label][7];//8偏差限幅值
Controler->Integrate_Separation_Err=Control_Unit[Label][8];//9积分分离偏差值
Controler->Integrate=Control_Unit[Label][9];//10积分值
Controler->Integrate_Max=Control_Unit[Label][10];//11积分限幅值
Controler->Kp=Control_Unit[Label][11];//12控制参数Kp
Controler->Ki=Control_Unit[Label][12];//13控制参数Ki
Controler->Kd=Control_Unit[Label][13];//14控制参数Ki
Controler->Control_OutPut=Control_Unit[Label][14];//15控制器总输出
Controler->Last_Control_OutPut=Control_Unit[Label][15];//16上次控制器总输出
Controler->Control_OutPut_Limit=Control_Unit[Label][16];//16上次控制器总输出
}
在调用之前这个用了几个结构体和数组
结构体有:
typedef struct
{
uint8 Err_Limit_Flag :1;//偏差限幅标志
uint8 Integrate_Limit_Flag :1;//积分限幅标志
uint8 Integrate_Separation_Flag :1;//积分分离标志
float Expect;//期望
float FeedBack;//反馈值
float Err;//偏差
float Last_Err;//上次偏差
float Err_Max;//偏差限幅值
float Integrate_Separation_Err;//积分分离偏差值
float Integrate;//积分值
float Integrate_Max;//积分限幅值
float Kp;//控制参数Kp
float Ki;//控制参数Ki
float Kd;//控制参数Kd
float Control_OutPut;//控制器总输出
float Last_Control_OutPut;//上次控制器总输出
float Control_OutPut_Limit;//输出限幅
/***************************************/
float Last_FeedBack;//上次反馈值
float Dis_Err;//微分量
float Dis_Error_History[5];//历史微分量
Butter_BufferData Control_Device_LPF_Buffer;//控制器低通输入输出缓冲
}PID_Controler;
typedef struct
{
PID_Controler Pitch_Angle_Control;
PID_Controler Pitch_Gyro_Control;
PID_Controler Roll_Angle_Control;
PID_Controler Roll_Gyro_Control;
PID_Controler Yaw_Angle_Control;
PID_Controler Yaw_Gyro_Control;
PID_Controler High_Position_Control;
PID_Controler High_Speed_Control;
PID_Controler Longitude_Position_Control;
PID_Controler Longitude_Speed_Control;
PID_Controler Latitude_Position_Control;
PID_Controler Latitude_Speed_Control;
/*************加速度控制器,新加****************/
PID_Controler High_Acce_Control;
PID_Controler Longitude_Acce_Control;
PID_Controler Latitude_Acce_Control;
}AllControler;
typedef enum
{
Pitch_Angle_Controler=0,
Pitch_Gyro_Controler=1,
Roll_Angle_Controler=2,
Roll_Gyro_Controler=3,
Yaw_Angle_Controler=4,
Yaw_Gyro_Controler=5,
High_Position_Controler=6,
High_Speed_Controler=7,
Longitude_Position_Controler=8,
Longitude_Speed_Controler=9,
Latitude_Position_Controler=10,
Latitude_Speed_Controler=11,
High_Acce_Controler=12,
Longitude_Acce_Controler=13,
Latitude_Acce_Controler=14
}Controler_Label;
定义了一个枚举类型的变量
typedef enum
{
Pitch_Angle_Controler=0,
Pitch_Gyro_Controler=1,
Roll_Angle_Controler=2,
Roll_Gyro_Controler=3,
Yaw_Angle_Controler=4,
Yaw_Gyro_Controler=5,
High_Position_Controler=6,
High_Speed_Controler=7,
Longitude_Position_Controler=8,
Longitude_Speed_Controler=9,
Latitude_Position_Controler=10,
Latitude_Speed_Controler=11,
High_Acce_Controler=12,
Longitude_Acce_Controler=13,
Latitude_Acce_Controler=14
}Controler_Label;
一个大数组:
1偏差限幅标志; 2积分限幅标志;3积分分离标志; 4期望;
5反馈 6偏差; 7上次偏差; 8偏差限幅值;
9积分分离偏差值;10积分值 11积分限幅值; 12控制参数Kp;
13控制参数Ki; 14控制参数Kd; 15控制器总输出; 16上次控制器总输出
17总输出限幅度
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,35 ,0 ,0 , 20, 1.2 ,0.000 ,0.00 ,0 ,0 , 250},//Pitch_Angle;偏航角度
{0 ,1 ,0 ,0 ,0 ,0 , 0 ,500 ,0 ,0 ,150, 1.0 ,0.006 ,0.5 ,0 ,0 ,500},//Pitch_Gyro;偏航角速度*/
const float Control_Unit[15][17]=
{
/* Kp Ki Kd */
/*1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17*/
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,35 ,0 ,0 , 20, 4.20 ,0.000 ,0.00 ,0 ,0 , 250},//Pitch_Angle;偏航角度
{0 ,1 ,0 ,0 ,0 ,0 , 0 ,500 ,0 ,0 , 80, 0.40 ,0.003 ,4.5 ,0 ,0 ,500},//Pitch_Gyro;偏航角速度
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,35 ,0 ,0 , 20, 4.20 ,0.000 ,0.00 ,0 ,0 , 250},//Roll_Angle;横滚角
{0 ,1 ,0 ,0 ,0 ,0 , 0 ,500 ,0 ,0 , 80, 0.40 ,0.003 ,4.5 ,0 ,0 ,500},//Roll_Gyro;横滚角速度
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,35 ,0 ,0 , 0 , 6.5 ,0 ,0.00 ,0 ,0 ,100},//Yaw_Angle;偏航角
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,100 ,0 ,0 ,30 , 2.5 ,0.002 ,4.5 ,0 ,0 ,120},//Yaw_Gyro;偏航角速度
//定高参数
//高度单项比例控制,有偏差限幅、总输出即为最大攀升、下降速度45cm/s
//Z轴速度比例+积分控制,无偏差限幅
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,50 ,0 ,0 ,15 , 1.2 ,0.0 ,0 ,0 ,0 ,70},//High_Position;海拔高度位置
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,80 ,0 ,0 ,80, 2.5 ,0.01 ,0.6 ,0 ,0 ,200},//High_Speed;海拔攀升速度
/*
1偏差限幅标志; 2积分限幅标志;3积分分离标志; 4期望;
5反馈 6偏差; 7上次偏差; 8偏差限幅值;
9积分分离偏差值;10积分值 11积分限幅值; 12控制参数Kp;
13控制参数Ki; 14控制参数Kd; 15控制器总输出; 16上次控制器总输出
17总输出限幅度
*/
/* Kp Ki Kd */
/*1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17*/
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,100 ,0 ,0 ,0, 0.5 ,0.000 ,0 ,0 ,0 ,100},//Longitude_Position;水平经度位置
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,80 ,0 ,0 , 8, 0.4 ,0.000 ,0.3 ,0 ,0 ,25},//Longitude_Speed;水平经度速度
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,100 ,0 ,0 ,0, 0.5 ,0.000 ,0 ,0 ,0 ,100},//Latitude_Position;水平纬度位置
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,80 ,0 ,0 ,8, 0.4 ,0.000 ,0.3 ,0 ,0 ,25},//Latitude_Speed;水平纬度速度
/*************加速度控制器,新加****************/
//最大加速度200cm/s^2
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,400 ,0 ,0 ,50, 0.575 ,0.002 ,0.15 ,0 ,0 ,150},//垂直加速度控制器
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,100 ,0 ,0 ,3, 0.300 ,0.0001 ,0 ,0 ,0 ,150},//水平经度方向加速度控制器
{1 ,1 ,0 ,0 ,0 ,0 , 0 ,100 ,0 ,0 ,15, 0.450 ,0.0015 ,0.3 ,0 ,0 ,25},//水平维度方向加速度控制器
};
输出化过程以Total_PID_Init第一句对俯仰角的PID控制
PID_Init(&Total_Controler.Pitch_Angle_Control,Pitch_Angle_Controler);为例
Total_Controler是一个结构体内部定义了几个PID_Controler型的结构体,PID_Controler里面定义了每个要用到的量
Pitch_Angle_Controler其实代指0,于是这句话的意思也就是将把Control_Unit的第0行赋给Total_Controler.Pitch_Angle_Control
然后是对TIM2的初始化
TIM2_Configuration_Cnt();//TIM2
接着Timer4_Configuration
Timer4_Configuration();//TIM4
主要的控制都由它发起
最后是对中断优先级设置
NVIC_Configuration();//
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure; //定义NVIC初始化结构体
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //优先级组别2,具体参见misc.h line80
/*
//-----NRF24L01数据中断-----//
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //IRQ中断通道-->NRF24L01,PB12
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢先式优先级别
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级别
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道
NVIC_Init(&NVIC_InitStructure); //初始化NVIC
*/
//飞控系统定时器
NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn ;//计数定时器
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//超声波
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//PPM接收机
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//GPS数据接收中断
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//串口中断3
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//串口2、光流模块
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口中断1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART_Cmd(USART3, ENABLE);
//飞控控制定时器
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;//主控制定时器
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}