Hardware Reference
In-Depth Information
This method is good for fast systems like servo controllers where the change in time is based on how fast the code
runs through a loop. Sometimes it is necessary to sample at a greater time interval than that at which the code runs
or have more consistency between the time the PID calculates. For these instances, the time change can be assumed
to be 1 and can be dropped out of the calculation for the I and D components, saving the continual multiplication
and division from the code. To speed up the PID calculation, the change in time can be calculated against the gains
instead of being calculated within the PID. The transformation of the calculation is Ki * settime and Kd / settime .
The code then looks like this, with gains of .5 picked as a general untuned starting point:
// setup
settime = 1000 ; // 1000 milliseconds is 1 second
Kp = .5;
Ki = .5 * settime;
Kd = .5 / settime;
// loop
now = millis() ;
timechage = (now - lasttime);
if (timechange >= time change){
error = Setpoint - Input;
errorsum = errorsum + error;
Derror = (Input - lastinput);
Pout = Kp * error;
Iout = Ki * errorsum ;
Dout = Kd * Derror ;
Output = Pout + Iout + Dout ;
}
PID Controller Setup
Now that the math and the framework are out of the way, it is time to set up a basic PID system on an Arduino. This
example uses an RC low-pass filter (from Chapter 6) with an added potentiometer to simulate external disturbance.
Wiring the Hardware
Set up an Arduino as per Figure 7-1 . After the Arduino is set up with the components, upload the code in Listing 7-1.
 
Search WWH ::




Custom Search