Teach you to understand and understand the Arduino PID control library - sampling time

Introduction

The ArduinoPID control library provided by Brett Beauregard has been briefly introduced in "Teach you to understand and understand the Arduino PID control library". This library can not only be used in Arduino, but can be ported to other platforms with a little simple modification . . Then the first question of the 7 questions will be explained below.

problem definition

Generally speaking, PID control is called periodically (that is, the interval of each calculation is a fixed constant), but more or less, due to various requirements, it will be called by strange aperiodic. If you have to modify the sampling time and make aperiodic calls to the PID control, this will lead to the following problems:

Look at this "odious" equation

If the sampling time changes, then for the integral term and the derivative term (that is, the terms corresponding to KI and KD), these two terms are related to the sampling time interval, then additional calculus operations are required (can no longer be written as originally written) Good code to operate, the time parameter must be adjusted).

solution

If the algorithm is called at fixed periodic intervals, the operation will become very simple, and accurate operation results can be obtained periodically. So towards this line of thinking. It is necessary to find a way to make the PID controller think that the sampling period that has been changed has not changed, and still use the original operation process.

code

/*working variables*/
unsigned long lastTime;
double Input, Output, Setpoint;
double errSum, lastErr;
double kp, ki, kd;
int SampleTime = 1000; //1 sec
void Compute()
{
   unsigned long now = millis();
   int timeChange = (now - lastTime);
   if(timeChange>=SampleTime)
   {
      /*Compute all the working error variables*/
      double error = Setpoint - Input;
      errSum += error;
      double dErr = (error - lastErr);
 
      /*Compute PID Output*/
      Output = kp * error + ki * errSum + kd * dErr;
 
      /*Remember some variables for next time*/
      lastErr = error;
      lastTime = now;
   }
}
 
void SetTunings(double Kp, double Ki, double Kd)
{
  double SampleTimeInSec = ((double)SampleTime)/1000;
   kp = Kp;
   ki = Ki * SampleTimeInSec;
   kd = Kd / SampleTimeInSec;
}
 
void SetSampleTime(int NewSampleTime)
{
   if (NewSampleTime > 0)
   {
      double ratio  = (double)NewSampleTime
                      / (double)SampleTime;
      ki *= ratio;
      kd /= ratio;
      SampleTime = (unsigned long)NewSampleTime;
   }
}

Observe the two functions SetTunings and SetSampleTime, these two functions complete the function of adapting to the change of sampling interval. Among them, SetSampleTime is scaled up/down by the same multiple as the sampling interval change after the sampling interval is changed. Do normalization in SetTunings. Why only Ki and Kd are processed here has been said before, then you may have the following questions:

If Ki changes, wouldn't it be very different from the classic PID control formula? The answer is not much difference! !

Observe the integral term and rewrite it in discrete form:

If Ki is a constant during the adjustment process, it can be further rewritten as:

The first term of the above formula observes Ki and e(t) respectively. After changing the sampling interval, e(t) will change in the corresponding time due to the influence of the sampling interval, while ki is proportionally reversed to enlarge/reduce, and the effect is equivalent. must.

in conclusion

No matter how often the PID algorithm is called, the algorithm will only be calculated periodically. The advantage of this is that the PID controller can follow its familiar path to the end.

It should be noted here that the system must be maintained before the timechange overflows. Alternatively, a simple protection mechanism can be added to reset the time count after the boundary is crossed. Of course, unsigned int is a 32-bit integer, which can tolerate almost 50 days of operation, which is enough.

NOTE: If there is any inadequacy, please inform'

The next chapter will analyze the effect of changes in the setpoint on the derivative term ^.^

NEXT

PS: Please indicate the source for reprinting: Ouyang Tianhua

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325430740&siteId=291194637