Hardware Reference
In-Depth Information
if (Iout < 0)
Iout = 0;
Output = Pout + Iout + Dout ; // prep the output variable
if (Output > 255) // sanity check of the output, keeping it within the
Output = 255; // available output range
if (Output < 0)
Output = 0;
lastinput = Input; // save the input and time for the next loop
lasttime = now;
analogWrite (3, Output); // write the output to PWM pin 3
Serial.print (Setpoint); // print some information to the serial monitor
Serial.print (" : ");
Serial.print (Input);
Serial.print (" : ");
Serial.println (Output);
} // end if (timechange >= settime)
} // end void loop ()
Verifying the Code
Run the code uploaded to the Arduino and start the serial monitor. The code will print one line containing the
Setpoint : Input : Output values, and print one line per iteration of the running PID about every second. The
system will stabilize around a value of the setpoint—the first value of every printed line in the serial monitor. However,
because of the inherent noise in the RC filter, it will never settle directly at the setpoint. Using an RC circuit is one
of the easier ways to demonstrate a PID controller in action, along with the noise simulating a possible jitter in the
system. The potentiometer is used to simulate a negative external disturbance; if the resistance on potentiometer is
increased, the controller will increase the output to keep the input at the setpoint.
â–  If the arduino were fast enough and had a higher precision on the pWM, it would be possible to eliminate the
jitter in the rC filter with a pID controller.
Note
PID Tuner
To graphically represent the different controllers in real time and on actual hardware, there is an app called PID tuner
available at the topics github repository ( https://github.com/ProArd/Pidtuner ) . PID Tuner implements the P, I, and
D types of controllers with the openFrameworks-and-Firmata combination (as in Chapter 3). Figures 7-2 through 7-4
were made from the PID Tuner app (see the next section, in which we'll start to examine different types of controllers
in more detail). The PID Tuner application was developed to provide a functional graphical front end to the Arduino
hardware and implement a few control algorithms for testing and tuning purposes. With PID Tuner, it is possible to
test many different gain values without having to upload a new sketch to the Arduino each time.
After downloading the file, do the following:
1.
Unzip it to the openFrameworks apps /myapps folder.
2.
Change the serial port connection to connect to an Arduino configured as shown in
Figure 7-1 and loaded with the standard Firmata sketch.
Open the PID folder and compile the project.
3.
 
 
Search WWH ::




Custom Search