PID算法的极好学习资料,匿名四轴资料
STM32单片机风力摆源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "key.h"
- #include "mpu6050.h"
- #include "pwmset.h"
- #include "niming.h"
- #include "pid.h"
- #include "usmart.h"
- #include "inv_mpu.h"
- #include "inv_mpu_dmp_motion_driver.h"
- volatile float pitch,roll,yaw; //欧拉角
- volatile u8 mode = 1; //当前工作方式
- volatile float step = 0;
- int main(void)
- {
- Stm32_Clock_Init(9); //系统时钟设置
- uart_init(72,500000); //串口初始化为500000
- delay_init(72); //延时初始化
- usmart_dev.init(72); //初始化USMART
- LED_Init(); //初始化与LED连接的硬件接口
- KEY_Init(); //初始化按键
- MPU_Init(); //初始化MPU6050
- TIM3_PWM_Init(999,8); //初始化脉宽波输出
- TIM2_Int_Init(4999,71); //初始化定时器2,每5ms产生一次中断
- while(mpu_dmp_init())
- {
- delay_ms(20);
- }
- while(1)
- {
- key=KEY_Scan(0);
- if(key==KEY2_PRES) //测试发送pid数据给上位机
- {
- pid_send_data(rol_p,rol_i,rol_d,pit_p,pit_i,pit_d);
- }
- if(key==KEY0_PRES) //开启或关闭下位机数据发送开关
- {
- report=!report;
- }
- if(key==KEY1_PRES) //开启或关闭下位机数据发送开关
- {
- Rol_SumError = 0;
- }
- if(key == KEY8_PRES) //PE8按下
- {
- rol_p += 1;
- LED0 = ~LED0;
- }
- if(key == KEY9_PRES)
- {
- rol_i += 0.1;
- LED0 = ~LED0;
- }
- if(key == KEY10_PRES)
- {
- R += 0.05;
- LED0 = ~LED0;
- }
- if(key == KEY11_PRES) //模式切换
- {
- mode++;
- LED0 = ~LED0;
- }
- if(key == KEY12_PRES)
- {
- rol_p -= 1;
- LED0 = ~LED0;
- }
- if(key == KEY13_PRES)
- {
- rol_i -= 0.1;
- LED0 = ~LED0;
- }
- if(key == KEY14_PRES)
- {
- R -= 0.05;
- LED0 = ~LED0;
- }
- if(key == KEY15_PRES)
- {
- mode--; //模式切换
- LED0 = ~LED0;
- }
- if(USART_RX_STA == 1) //检测是否接收到了数据。
- {
- USART_RX_STA = 0; //清除接收标志,防止多次判断。
- LED1 = ~LED1;
- rol_p = (USART_RX_BUF[4]<<8)|USART_RX_BUF[5];
- rol_i = (USART_RX_BUF[6]<<8)|USART_RX_BUF[7];
- rol_d = (USART_RX_BUF[8]<<8)|USART_RX_BUF[9];
- pit_p = (USART_RX_BUF[10]<<8)|USART_RX_BUF[11];
- pit_i = (USART_RX_BUF[12]<<8)|USART_RX_BUF[13];
- pit_d = (USART_RX_BUF[14]<<8)|USART_RX_BUF[15];
- }
- }
- }
所有资料51hei提供下载:
15电赛风力摆源码及上位机.rar
|