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