next up previous
Next: Conclusion Up: number7 Previous: Fine tuning a PID

Experimenting with the sample code

The code example in Listing 1 implements an integer PID controller. The code uses scaled integer arithmetic, scaled such that 128 is effectively a scaled 1. This is a useful scaling for 8-bit sampling (by putting 1 in the mid-range). I am simulating the sensor input that would be coming from, say, a frequency counter measuring motor RPM. This simulation consists of a sensor that returns a noise contaminated version of the actual setting (to turn off the random noise set noise_range to zero). One sets the PID gains with a sequence such as:
64 32 10 gains!
which will set, respectively, the proportional gain to 64, the integral gain to 32 and the derivative gain to 10 (effectively 0.5, 0.25 and 0.08). After the gains are set, a run of the controller is done by typing,
88 pid-run output.1
This will run the controller with the current gain settings for a desired output set point of 88 and send the results to a file called output.1. The code is set up so that the controller can be continued, with output to another file for comparison, e.g.,
160 pid-run output.2
The output is in a form that is convienient for plotting with the program gnuplot by using a command like,
gnuplot> plot 'output.1' with linespoints,'output.2' with linespoints

If you run the program with different gain settings you will see several things going on. First with just the proportional gain nonzero, the steady state is an oscillation around the ratio between the set point and the (fractional) gain, ( set_point @ kp @ 128 */ ) . Also the oscillations damp out at a rate that is inversely proportional to the gain. Consequently the ability to quickly damp out the oscillations competes against the ability to reach the desired output setting. The DC level of the output can be tweaked by adjusting the initial output value, but simply using the integral term takes care of that automatically. Setting the P and I terms to nonzero makes the output actually settle out at the desired setting. If you now add the derivative term (by setting its gain to nonzero), the controller will respond more quickly to changes in the set point. The problem with the derivative term is that it is very sensitive to noise in the data, it looks pretty helpful if the noise is zero, but it amplifies the noise if its not zero.

Play around with different settings and see how the controller responds. For extra credit, replace my sensor/controller simulator with the real thing, try replacing the sensor with the frequency counter from last time (measuring the RPM of a motor by counting revolutions) and the controller with the PWM motor controller from FD XVIII Number 2, giving you a speed controlled motor that will automatically adjust for varying loads.


next up previous
Next: Conclusion Up: number7 Previous: Fine tuning a PID
Skip Carter 2008-08-20