找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1551|回复: 1
打印 上一主题 下一主题
收起左侧

基于STM32帆摆模型源码 加深对PID控制的了解

[复制链接]
跳转到指定楼层
楼主
这个实验主要是加深对PID控制的了解



单片机源程序如下:
  1. #include "stm32f4xx.h"
  2. #include "usart.h"
  3. #include "delay.h"
  4. #include "key.h"
  5. #include "led.h"
  6. #include "PWM.h"
  7. #include "dianji.h"
  8. #include "ADC.h"
  9. #include "oled.h"
  10. #include "PID.h"

  11. #define  angle_1_amendment  -7.5 ; //角度修正


  12. void CNM_ZZ(void);
  13. void CNM_SB(void);
  14. extern float TargetVal;
  15.       
  16. extern float angle_1;
  17.       
  18. extern int PIDout;
  19. float z;
  20.                        
  21.                          
  22.                          
  23. u8 ve[32]={0};
  24. u8 ze[30]={0};
  25. u8 ke[30]={0};
  26. int a=0;
  27. u16 adcx;         //采集值
  28. float temp;             //当前电压值
  29. u16 c;
  30. int main(void)
  31. {
  32.          
  33.          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  34.          uart_init(9600);
  35.          delay_init(168);     //初始化延时函数
  36.    key_init();
  37.          dianji_Init();
  38.          LED_Init();
  39.          TIM14_PWM_Init(5000,0);
  40.          TIM10_PWM_Init(8000,0);
  41.          Adc_Init();
  42.          OLED_Init();


  43.   OLED_Show_Font(0,4,0);                       
  44.         OLED_Show_Font(16,4,1);                           
  45.         OLED_Show_Font(32,4,3);                                  
  46.         OLED_Show_Font(48,4,4);
  47.         OLED_Show_Font(64,4,2);
  48.         OLED_Show_Font(80,4,5);

  49.         OLED_Show_Font(0,22,0);
  50.         OLED_Show_Font(16,22,1);
  51.         OLED_Show_Font(32,22,7);
  52.         OLED_Show_Font(48,22,8);
  53.         OLED_Show_Font(64,22,2);
  54.         OLED_Show_Font(80,22,5);

  55.         OLED_Show_Font(0,40,9);       
  56.         OLED_Show_Font(16,40,10);
  57.         OLED_Show_Font(32,40,11);
  58.         OLED_Show_Font(48,40,12);
  59.         OLED_Show_Font(64,40,13);
  60.         OLED_Show_Font(80,40,14);
  61.         OLED_Show_Font(96,40,5);
  62.         OLED_Show_Font2(110,48,0);

  63.   
  64. while(1)
  65. {
  66.                    c=USART_ReceiveData(USART1);
  67.                 if(c=='A'||c=='B'||c=='H')     
  68.                 CNM_ZZ();    //设定值加1
  69.                 else if(c=='E'||c=='D'||c=='F')
  70.                 CNM_SB();     //设定值减1
  71.                 else if(c=='G')
  72.                 {
  73.                
  74.                 PIDout=0;
  75.                
  76.                 }                        //停止
  77.                
  78.          
  79.        
  80.                      adcx=Get_Adc_Average(ADC_Channel_5,20);  //采集20次的平均通道值
  81.                                  temp=(float)adcx*(3.3/4096);             //当前电压值
  82.                                  angle_1=temp*(360/3.3)+angle_1_amendment;    //当前角度值  
  83.        


  84.                                 PID_Calculate();
  85.                           z=PIDout/50;
  86.    
  87.                 TIM_SetCompare1(TIM14,PIDout);
  88.        
  89.      sprintf((char*)ve,"%.1f",angle_1);
  90.                  sprintf((char*)ze,"%.1f",TargetVal);
  91.            sprintf((char*)ke,"%.1f",z);
  92.                  OLED_ShowString(98,4,ve,12);
  93.                  OLED_ShowString(98,22,ze,12);
  94.            OLED_ShowString(90,48,ke,12);                 
  95.   
  96.            OLED_Refresh_Gram();//更新显示到OLED
  97.        
  98.                
  99.         }

  100. }


  101. void CNM_ZZ(void)
  102. {

  103. TargetVal=TargetVal+1;
  104.         GPIO_ResetBits(GPIOF,GPIO_Pin_8);
  105.         GPIO_SetBits(GPIOF,GPIO_Pin_10);

  106. }
  107. void CNM_SB(void)
  108. {
  109. TargetVal=TargetVal-1;
  110.         GPIO_ResetBits(GPIOF,GPIO_Pin_8);
  111.         GPIO_SetBits(GPIOF,GPIO_Pin_10);

  112. }

复制代码

所有资料51hei提供下载:
帆板2.5版.rar (503.4 KB, 下载次数: 22)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:405033 发表于 2018-10-18 16:15 | 只看该作者
问下楼主, TIM_SetCompare1(TIM14,PIDout);这里面的PIDoout是否为pid算法的返回值
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表