电子设计省赛--PID

来源:互联网 发布:jimmy kimmel知乎 编辑:程序博客网 时间:2024/05/17 06:23

//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 FunctionThe PID (比例、积分、微分) function is used in mainlycontrol applications. PIDCalc performs one iteration of the PIDalgorithm.While the PID function works, main is just a dummy program showinga typical usage.=====================================================================================================*/typedef struct PID {double SetPoint; // 设定目标 Desired Valuedouble Proportion; // 比例常数 Proportional Constdouble Integral; // 积分常数 Integral Constdouble Derivative; // 微分常数 Derivative Constdouble 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 Structuredouble rOut; // PID Response (Output)double rIn; // PID Feedback (Input)PIDInit ( &sPID ); // Initialize StructuresPID.Proportion = 0.5; // Set PID CoefficientssPID.Integral = 0.5;sPID.Derivative = 0.0;sPID.SetPoint = 100.0; // Set PID Setpointfor (;;) { // Mock Up of PID ProcessingrIn = sensor (); // Read InputrOut = PIDCalc ( &sPID,rIn ); // Perform PID Interationactuator ( 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;  }}





0 0
原创粉丝点击