Teach you to understand and understand the Arduino PID control library - differential impact

Introduction

This article will analyze the second question in " Teach you to read and understand the Arduino PID control library ": the effect of setting value changes on the differential term. The language is not good. It is called Derivative Kick in the original text. I really can't think of a good name.

problem definition

Since it is called differential shock, this problem must be related to the differential term, and the differential term will be affected by an unexpected situation, resulting in a larger shock, as shown in the figure below.

It can be seen from the first picture that when the set value generates a step, the controlled quantity Input slowly approaches the set value with time, and the second picture reflects the control quantity Output after the set value has a step. It can be found that the Output will suddenly produce a large step. For the specific reason, please refer to the "all evil" PID classic control equation. The third picture describes the gradient of Output (that is, rate of change: value change: time change), and a pulse can also be found, and this pulse may be very large (dt is very small), far exceeding the amount of Output change. In the same way, the page in the figure describes the change of Output and its corresponding gradient when the set value is suddenly reduced. For a general system, we do not want such a sudden change to occur (it is conceivable that if the sampling period is very long, the shock will last for a long time, and the system estimate will fly). Of course, if your system requires this shock, then this problem can be ignored, and the solutions below do not require browsing.

solution

First of all, thanks to our great mathematicians, combined with the classical PID control equation, let's see a formula:

The Setpoint item will have a huge impact, and it will only occur once, and it will disappear in the next calculation cycle. There are many ways to deal with it. The simplest is to remove the Serpoint item, which means that there is no change in the set value for the differential term. If this is done, will the system get out of control? This kind of gameplay is already different from our deep-rooted classic PID theory! ! How is this done!

The answer to the above question is no, we write the above process as a mathematical expression:

 

Use the inversion of the Input change to represent the change of Error (in fact, ignore the change of Setpoint). The classic PID control equation is not discussed here. Our goal is to make the Input move closer to the direction we expect according to the Setpoint. The above approach is undeniably inconsistent with the classic PID equation, but the core idea is the same. Since the change of Setpoint only affects the Output once (that is, the peak is generated), once the next calculation cycle is entered, the Output will no longer be affected by the change of Setpoint, return to normal, and will not affect the PID adjustment (for the specific impact diagram, please refer to refer to the end of the text).

code

/*working variables*/
unsigned long lastTime;
double Input, Output, Setpoint;
double errSum, lastInput;
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 dInput = (Input - lastInput);
 
      /*Compute PID Output*/
      Output = kp * error + ki * errSum - kd * dInput;
 
      /*Remember some variables for next time*/
      lastInput = Input;
      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;
   }
}

The code replaces the change in Error with the change in Input.

in conclusion

  1. Output gradient spikes are removed
  2. The control amount that originally existed a spike was eliminated
  3. The derivative of the controlled quantity becomes flatter

NOTE: Please let us know if there are any inadequacies. ^.^

The next chapter will introduce the influence on the system if the PID control parameters are suddenly changed during the system operation.

NEXT

PS: Please indicate the source for reprinting: Ouyang Tianhua

Guess you like

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