Teach you to understand and understand the Arduino PID control library - direction

Introduction

This article will analyze the problem of adjustment direction in " Teach you to understand and understand Arduino PID control library "

problem definition

There should be two ways to adjust a perfect PID controller:

1. Positive regulation, that is, the output increases, then the controlled quantity will also increase

2. Reverse regulation, corresponding to forward regulation, the output increases, but the controlled quantity decreases

The above two methods are easy to understand. For example, in the heating control of temperature control, if the heating output increases, the temperature will increase accordingly (forward regulation), while in the corresponding cooling control, if the cooling output increases , then the temperature should decrease accordingly (direction adjustment).

solution

This problem is easy to solve. In the code, you only need to set Kp, Ki, and Kd to be negative. In the code, a function SetControllerDirection and two macros DIRECT and REVERSE are provided to control the direction of the output. If Forward output is required. After constructing the PID, call SetControllerDirection(DIRECT) to set the output to forward (forward is the default, when constructing the PID, the default is forward regulation), and vice versa.

code

/*working variables*/
unsigned long lastTime;
double Input, Output, Setpoint;
double ITerm, lastInput;
double kp, ki, kd;
int SampleTime = 1000; //1 sec
double outMin, outMax;
bool inAuto = false;
 
#define MANUAL 0
#define AUTOMATIC 1
 
#define DIRECT 0
#define REVERSE 1
int controllerDirection = DIRECT;
 
void Compute()
{
   if(!inAuto) return;
   unsigned long now = millis();
   int timeChange = (now - lastTime);
   if(timeChange>=SampleTime)
   {
      /*Compute all the working error variables*/
      double error = Setpoint - Input;
      ITerm+= (ki * error);
      if(ITerm > outMax) ITerm= outMax;
      else if(ITerm < outMin) ITerm= outMin;
      double dInput = (Input - lastInput);
 
      /*Compute PID Output*/
      Output = kp * error + ITerm- kd * dInput;
      if(Output > outMax) Output = outMax;
      else if(Output < outMin) Output = outMin;
 
      /*Remember some variables for next time*/
      lastInput = Input;
      lastTime = now;
   }
}
 
void SetTunings(double Kp, double Ki, double Kd)
{
   if (Kp<0 || Ki<0|| Kd<0) return;
 
  double SampleTimeInSec = ((double)SampleTime)/1000;
   kp = Kp;
   ki = Ki * SampleTimeInSec;
   kd = Kd / SampleTimeInSec;
 
  if(controllerDirection ==REVERSE)
   {
      kp = (0 - kp);
      ki = (0 - ki);
      kd = (0 - kd);
   }
}
 
void SetSampleTime(int NewSampleTime)
{
   if (NewSampleTime > 0)
   {
      double ratio  = (double)NewSampleTime
                      / (double)SampleTime;
      ki *= ratio;
      kd /= ratio;
      SampleTime = (unsigned long)NewSampleTime;
   }
}
 
void SetOutputLimits(double Min, double Max)
{
   if(Min > Max) return;
   outMin = Min;
   outMax = Max;
 
   if(Output > outMax) Output = outMax;
   else if(Output < outMin) Output = outMin;
 
   if(ITerm > outMax) ITerm= outMax;
   else if(ITerm < outMin) ITerm= outMin;
}
 
void SetMode(int Mode)
{
    bool newAuto = (Mode == AUTOMATIC);
    if(newAuto == !inAuto)
    {  /*we just went from manual to auto*/
        Initialize();
    }
    inAuto = newAuto;
}
 
void Initialize()
{
   lastInput = Input;
   ITerm = Output;
   if(ITerm > outMax) ITerm= outMax;
   else if(ITerm < outMin) ITerm= outMin;
}
 
void SetControllerDirection(int Direction)
{
   controllerDirection = Direction;
}

Epilogue

So far, the introduction of PID controller has been completed. This is just a very common PID controller, which has the basic elements of PID control, and considers all special circumstances, which greatly increases the environmental adaptability.

Then a big problem of PID control is how to select the appropriate Kp, Ki, Kd parameters to meet the ever-changing application scenarios. At present, the adjustment of PID parameters mainly depends on experience and continuous trial and error, and people are constantly seeking how to choose Appropriate PID parameter method. In response to the above problems, a proposition "auto-tuning PID" controller came into being, that is, when we do PID control, we do not need to give very precise PID parameters, the system will automatically help us find a set of "more" suitable PID control parameters, which can greatly reduce the requirements for experience and trial and error.

In subsequent articles, the following propositions will be introduced:

1. How to perform automatic parameter tuning? What are the current mainstream automatic parameter tuning methods?

2. Introduce how the automatic parameter tuning library of RELAY METHOD is implemented

3. PID control library that integrates PID and automatic parameter tuning

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

NEXT

PS: Please indicate the source for reprinting: Ouyang Tianhua

Guess you like

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