Arduino PID Library - Advanced Methods
by Brett Beauregard
contact: br3ttb@gmail.com

ADVANCED CREATION

PID(double *Input, double *Output, double *SetPoint, double *FFBias, double P_Param, double I_Param, double D_Param)

Description: Same as the standard creation function, with an added parameter to allow the implementation of Feed Forward control (see Example 3 below)

Arguments:

  • *Input : Link (pointer) with the double that will be the PID's Input
  • *Ouput : Link to the Output
  • *SetPoint : Link to the Set Point
  • *FFBias : Link to the Feed Forward Bias
  • P_Param : Proportional Parameter. MUST BE NON-ZERO.
  • I_Param : Integral Parameter. Must be non-negative.
  • D_Param : Derivative Parameter. Must be non-negative.

ADVANCED METHODS

void SetSampleTime(int NewSampleTime)

Description: Sets the period, in Milliseconds, with which the PID calculation is performed. default is 1000.

Arguments:

  • NewSampleTime : Desired Sample Time. Must be greater than 0. in practice, 1000 milliseconds is adequate for most processes (temperature, pressure, flow).

void Reset()

Description: Reinitializes the PID working variables. (Input and Output limits remain unchanged.) This function is automatically called on a manual to auto transition

void SetInputLimits(double INMin, double INMax)

Description: This function tells the controller What 0% and 100% are for the Input. By default these limits are 0-1023: the Arduino analog input limits. Usually the input Min and Max should stay at 0 and 1023, but if you expect your input to be outside this range, you can adjust the limits using this function.

Arguments:

  • INMin : Value Corresponding to 0% of the Input Span. must be < INMax
  • INMax : Value Corresponding to 100% of the Input Span. must be > INMin

bool JustCalculated()

In certain situations, it helps to know when the PID has computed this bit will be true for one cycle after the pid calculation has occured.

int GetMode()

Returns the current Mode

double GetINMin()

Returns the current Input Minimum

double GetINMax()

Returns the current Input Maximum

double GetOUTMin()

Returns the current Output Minimum

double GetOUTMax()

Returns the current Output Maximum

int GetSampleTime()

Returns the current Sample Time

double GetP_Param()

Returns the current P_Param

double GetI_Param()

Returns the current I_Param

double GetD_Param()

Returns the current D_Param

EXAMPLE 3

/***************************************************
 * Feed Forward Example : RepRap Extruder Nozzle Temperature
 * PID input: Nozzle Temperature
 * PID output: PWM signal (0-255) to the heater control
 * Other Input: Plastic Feed rate is being read on input 1
 * For the feed forward piece...
 *  The amount of heat we want to add to the nozzle is largely dependent
 *  on how fast plastic is being fed.  if the feed is stopped we're going
 *  to want a lot less heat than when the system is running.  A pid 
 *  control by itself will eventually adjust the heat, but by then the temp
 *  will be really high.  feed forward can help with this.
 *    so to make Feed Forward work, at every cycle we look at the current feed speed
 *  and compute a "bias" which we feed to the pid controller.  sort of: "Based on my
 *  knowledge, your output should be about X.  adjust off of that as you see fit"
 *    What does it get you?  well, the instant the feed stops, the baseline drops. That
 *  means that the controller output immediately drops. if you had been using just a pid, 
 *  you'd have to wait for the temperature to rise quite a bit before seeing the same
 *  drop in output.
 * 
 * (for this example, we're assuming that for a feed rate of 1023 the output should be
 *  about 130, and at a rate of 0 the output should be around 10)
 ***************************************************/

 #include <PID_Beta6.h>
 int FeedRate;

 double Input, Output, Setpoint, Bias;
 PID pid(&Input, &Output, &Setpoint, &Bias, 3, 4, 1);

 void setup()
 {
   Setpoint = 400;
   pid.SetMode(AUTO); //turn on the PID
 }

 void loop()
 {
   //Read in the Feed rate and compute a baseline output to the heater
   FeedRate = analogRead(1);  //read the feed rate
   Bias = (FeedRate/1023)*(120)+10;

   //give the PID the opportunity to compute if needed
   Input = analogRead(0);
   pid.Compute();

   analogWrite(0, Output);
 }

Share