zoukankan      html  css  js  c++  java
  • 电子设计省赛--PID

    //2014年4月17日

    //2014年6月20日入“未完毕”(未完毕)

    //2014年6月21日

    一開始还以为是多难的算法。事实上就是个渣渣。

    当然PID实践中应该会非常难。

    另外在理解PID时有点走了弯路,记载例如以下:

    1.为什么比例控制会有稳态误差

    关键在于我们要考虑的是“加速度的系统”。比方一个不停在散热的东西,而你要保持它的温度。

    最后稳定在一部分散热的热量等于你比例控制的值!所以会有偏差。

    2.为什么积分控制会解决稳态误差?积分控制为什么在达到目标值时还会调整?

    一開始调整到目标值时积分控制的确还在进行调整,由于此时其历史误差总和不为0。

    但这不是问题,关键是它调整中,历史误差总和会不断正负抵消,终于达到稳态。此时历史误差总和不为0,却正好抵消了系统的散热。


    /*====================================================================================================
    这是从网上找来的一个比較典型的PID处理程序,在使用单片机作为控制cpu时。请稍作简化,详细的PID
    參数必须由详细对象通过实验确定。因为单片机的处理速度和ram资源的限制。一般不採用浮点数运算,
    而将所有參数所实用整数,运算到最后再除以一个2的N次方数据(相当于移位),作相似定点数运算,可
    大大提高运算速度。依据控制精度的不同要求。当精度要求非常高时,注意保留移位引起的“余数”。做好余
    数补偿。这个程序仅仅是一般经常使用pid算法的基本架构。没有包括输入输出处理部分。
    =====================================================================================================*/
    #include <string.h>
    #include <stdio.h>
    /*====================================================================================================
    PID Function
    The PID (比例、积分、微分) function is used in mainly
    control applications. PIDCalc performs one iteration of the PID
    algorithm.
    While the PID function works, main is just a dummy program showing
    a typical usage.
    =====================================================================================================*/
    typedef struct PID {
    double SetPoint; // 设定目标 Desired Value
    double Proportion; // 比例常数 Proportional Const
    double Integral; // 积分常数 Integral Const
    double Derivative; // 微分常数 Derivative Const
    double LastError; // Error[-1]
    double PrevError; // Error[-2]
    double SumError; // Sums of Errors
    } PID;
    /*====================================================================================================
    PID计算部分
    =====================================================================================================*/
    double PIDCalc( PID *pp, double NextPoint )
    {
    double dError,
    Error;
    Error = pp->SetPoint - NextPoint; // 偏差
    pp->SumError += Error; // 积分
    dError = pp->LastError - pp->PrevError; // 当前微分
    pp->PrevError = pp->LastError;
    pp->LastError = Error;
    return (pp->Proportion * Error // 比例项
    + pp->Integral * pp->SumError // 积分项
    + pp->Derivative * dError // 微分项
    );
    }
    /*====================================================================================================
    Initialize PID Structure
    =====================================================================================================*/
    void PIDInit (PID *pp)
    {
    memset ( pp,0,sizeof(PID));
    }
    /*====================================================================================================
    Main Program
    =====================================================================================================*/
    double sensor (void) // Dummy Sensor Function
    {
    return 100.0;
    }
    void actuator(double rDelta) // Dummy Actuator Function
    {}
    void main(void)
    {
    PID sPID; // PID Control Structure
    double rOut; // PID Response (Output)
    double rIn; // PID Feedback (Input)
    PIDInit ( &sPID ); // Initialize Structure
    sPID.Proportion = 0.5; // Set PID Coefficients
    sPID.Integral = 0.5;
    sPID.Derivative = 0.0;
    sPID.SetPoint = 100.0; // Set PID Setpoint
    for (;;) { // Mock Up of PID Processing
    rIn = sensor (); // Read Input
    rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation
    actuator ( rOut ); // Effect Needed Changes
    }
    }

    某学长的单片机程序(巡线,pid输入是传感器。pid输出到定时器)

    <pre name="code" class="cpp">void SysTickHandler(void)
    {
      test=(~GPIOF->IDR);
      test=	(test&0x27ff);
    if	(test==1)		//1
    {	 roadinfo=11 ;}
    if	(test==3)		//1
    {	 roadinfo=10 ;}
    if	(test==2)		//1
    {	 roadinfo=9 ;}
    if	(test==6)		//1
    {	 roadinfo=8 ;}
    if	(test==4)		//1
    {	 roadinfo=7 ;}
    if	(test==0xc)		//1
    {	 roadinfo=6 ;}
    if	(test==0x8)		//1
    {	 roadinfo=5 ;}
    if	(test==0x18)		//1
    {	 roadinfo=4 ;}
    if	(test==0x10)		//1
    {	 roadinfo=3;}
    if	(test==0x30)		//1
    {	 roadinfo=2 ;}
    if	(test==0x20)		//1
    {	 roadinfo=1 ;}
    if	(test==0x60)		//1
    {	 roadinfo=0;}
    if	(test==0x40)		//1
    {	 roadinfo=-1 ;}
    if	(test==0xc0)		//1
    {	 roadinfo=-2 ;}
    if	(test==0x80)		//1
    {	 roadinfo=-3 ;}
    if	(test==0x180)		//1
    {	 roadinfo=-4 ;}
    if	(test==0x100)		//1
    {	 roadinfo=-5 ;}
    if	(test==0x300)		//1
    {	 roadinfo=-6 ;}
    if	(test==0x200)		//1
    {	 roadinfo=-7 ;}
    if	(test==0x600)		//1
    {	 roadinfo=-8 ;}
    if	(test==0x400)		//1
    {	 roadinfo=-9 ;}
    if	(test==0x2400)		//1
    {	 roadinfo=-10 ;}
    if	(test==0x2000)		//1
    {	 roadinfo=-11 ;}
    	speedo2=800-roadinfo*kp-(roadinfo-pre_inof)*ki;
    	speedo3=800+roadinfo*kp+(roadinfo-pre_inof)*ki;
    	if(speedo2>max)	   {speedo2=max;}
    	if(speedo2<min)	   {speedo2=min;}
    	if(speedo3>max)	   {speedo3=max;}
    	if(speedo3<min)	   {speedo3=min;}
    	pre_inof=roadinfo;
    
    	if(speedo2>0)	 {	     TIM4->CCR1=0; 			TIM3->CCR2= speedo2;	  }
    	if(speedo2<=0)	  {	     TIM4->CCR1=-speedo2;	TIM3->CCR2=0;	  }
    	if(speedo3>0)	 {	    	TIM3->CCR3= speedo3;	TIM4->CCR2=0;  }
    	if(speedo3<=0)	  {	    	TIM3->CCR3=0;			 TIM4->CCR2=-speedo3;	  }
    }


    
    


  • 相关阅读:
    git
    界面编程与视图(View)组件
    genymotion的安装
    210中断故障分析
    6410/210按键中断编程
    2440按键中断编程
    中断处理流程深度剖析
    scrapy 写文件进行debug调试
    scrapy 爬虫中间件-offsite和refer中间件
    scrapy文件管道
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7204273.html
Copyright © 2011-2022 走看看