Using the built in functions of Snap-Master’s Analysis element along with A/D and D/A hardware, you can create a PID (Proportional + Integral + Derivative) controller. PID control is often used for devices that must either be controlled to maintain a constant condition under a variable load or to follow a sinusoidal, square or triangular wave from a signal generator. In cases where the controlled variable is a mechanical position, a PID controller can be used to program the position of the output shaft of a DC motor.
A block diagram of a generic PID controller is shown in below. In this block diagram, the Ks are various system gains; the Ss are the Laplace operator where multiplication by S signifies differentiation and division by S signifies integration. The numbers with the # sign refer to Analysis Equation Table line numbers in which the K parameters are defined.

Snap-Master Instrument
Here is a typical Snap-Master Instrument used for PID control. The instrument includes an A/D converter, an Analysis Element, and a D/A converter. The A/D and D/A converter often reside on the same I/O hardware, reducing the cost of the required acquisition hardware.

The A/D converter reads the signal input to the device under control (which might come from a signal generator, a programmer, or a manual voltage input) as well as the output produced by the controlled device (which may come from a transducer). Both of these signals are processed by the Analysis element according to the equations defined in the Equation Table. Finally, the D/A converter outputs the result of the Analysis equations as an analog signal which is used to drive the controlled device.
Analysis Equations
An example Analysis Equation Table used for PID control is shown below. In this particular example, the A/D converter is element A (or a) and has two channels, 0 and 1. Channel 0 of the A/D Converter carries the reference input (desired output) signal and channel 1 of the A/D Converter carries the actual output signal from the controlled device.
| # | Run | Comments | Equation Definition | Label | Units |
| 1 | Proportional Gain | d | |||
| 2 | Integral Gain | define Ival() = 5 | |||
| 3 | Derivative Gain | define Dval() = 0.001 | |||
| 4 | Motor Scale Factor | define ScaleFactor() = 2000/4096 | |||
| 5 | Error Scale Factor | define errScaleFactor() = 409.6 | |||
| 6 | Error Signal | E0 = (A0 – A1) * errScaleFactor() | |||
| 7 | Controller Output | F0 = (Pval() * E0) + (Ival() * intg[t](E0)) + (Dval() * diff[2](E0)) | Calc Output | ||
| 8 | D/A Converter Input | G0 = ScaleFactor() * F0 | D/A Input |
Lines 1 through 5 are constant gains and scale factors. Defining these gains and scale factors separately in this manner makes it easy to change the values while the control loop is being developed.
Line 6 of the Equation Table calculates the error by subtracting channel A1 from A0 and multiplying the difference by the scale factor. The result of the subtraction is assigned to result channel E0.
Line 7 is the equation defining the basic functioning of this PID controller. It states that the controller output is the sum of three quantities: the product of the scaled error and the proportional gain, the product of the integral (using the Trapezoidal method, indicated by the [t] after the function) of the scaled error and the integral gain, and the product of the derivative (using the 2 point method, indicated by the [2] after the function) of the scaled error and the derivative gain. The result is defined as channel F0.
When the integral and derivative gains are set to 0, Line 7 describes the classic closed loop control system that has been used for many years. The proportional loop produces an output proportional to the error. The integral loop produces an output velocity proportional to the error, and makes it possible to have zero error. The derivative loop produces an output proportional the rate of change in the error. Use of the derivative loop improves response, but too much derivative gain destroys stability.
Further discussion of the effects of changing the various gains is beyond the scope of this application note. The user can easily experiment with changing the gain values in Lines 1 through 3 to obtain the desired accuracy and response.
Line 8 scales the controller output by a factor appropriate for the driven device. This result is the input to the D/A converter, element C, which produces the analog output that actually controls the driven device.
Results
Shown below is a typical result using Snap-Master to perform PID control. The test simulated the shaft position of an electric motor whose velocity is proportional to the applied control voltage. (The motor was simulated by an additional Analysis Element integrator.) This PID result was converted to an analog voltage by a D/A converter whose output was fed back into the Analysis Element by the A/D converter. The reference input was a square wave signal provided by a function generator.

The trace on the left shows the reference input from the function generator. The trace on the right shows the shaft position of the simulated motor. As you can see, the simulated motor output closely follows the input square wave with only a minor transient.
This application note describes an application of Snap-Master that is useful in setting up control-oriented tests. When setting up this type of control, we suggest that the derivative and integral gains initially be set to zero. If the proportional gain cannot be adjusted to achieve satisfactory performance, then various values of derivative gain and integral gain should be tried to obtain the best performance.
Finally, it should be noted that Snap-Master is designed to be a program for gathering and analysis of test data. Experience with its use as an element in a control system is limited. In the simulation described here, occasional transient errors have been observed. Snap-Master should not be used as a controller in critical applications where controller failure could potentially result in equipment damage or human injury.
Special thanks to Jim Tarter for his assistance with this application note.





