Hardware Reference
In-Depth Information
SetTunings(Kp,Ki,Kd) : This is used to change the gains dynamically after the PID has been
initialized.
SetSampleTime(milliseconds) : This sets the amount of time that must pass before the
Compute() function will execute the PID calculation again. If the set time has not passed when
Compute() is called, the function returns back to the calling code without calculating the PID.
SetControllerDirection(direction) : This sets the controller direction. Use DIRECT
for positive movements, such as in motor control or ovens; use REVERSE for systems
like refrigerators.
Listing 7-2 is a modified version of the basic PID example using the PID library given at the library's Arduino
Playground web page ( www.arduino.cc/playground/Code/PIDLibrary/ ). The modifications to the sketch include a
serial output to display what is going on. There is a loss in performance when using the library compared to the direct
implementation of Listing 7-1, and the gains had to be turned down in comparison while using the same hardware
configuration as in Figure 7-1 . The library can easily handle slower-reacting systems; to simulate this. a lager capacitor
can be used in the RC circuit.
Listing 7-2. PID Impemented with the PID Library
#include <PID_v1.h>
double Setpoint, Input, Output;
float Kp = .09;
float Ki = .1;
float Kd = .07;
// set up the PID's gains and link to variables
PID myPID(&Input, &Output, &Setpoint,Kp,Ki,Kd, DIRECT);
void setup(){
Serial.begin(9600);
// variable setup
Input = analogRead(0) / 4; // calculate input to match output values
Setpoint = 100 ;
// turn the PID on
myPID.SetMode(AUTOMATIC);
// myPID.SetSampleTime(100);
}
void loop(){
// read input and calculate PID
Input = analogRead(0) / 4;
myPID.Compute();
analogWrite(3,Output);
// print value to serial monitor
Serial.print(Setpoint);
Serial.print(" : ");
Search WWH ::




Custom Search