PID Control with ADwin

by
Doug Rathburn
Keithley Instruments, Inc.



Introduction

Proportional plus Integral and Derivative (PID) controllers are very common in industry. Industrial controllers are used for controlling processes like those found in chemical plants, temperature control, certain automotive applications, etc. The purpose of a controller is to suppress the effects of disturbances on process variables and to force a process variable to track a desired set point.

The PID controller measures the output of the process and calculates the difference (error) between what is measured and the set point. If an error exists, the controller adjusts its output to alter the process to bring it closer to the desired point, thus minimizing the error. Each time an error is calculated, the controller must decide how much to alter the process. If the controller is too aggressive (under damped), it may cause the process to become unstable and oscillate. If control is not aggressive enough (over damped), the system may require too much time to recover.

The aggressiveness of the controller is determined by the PID constants provided to the controller. The proportional, integral, and derivative constants are used to calculate what the output should be relative to the measured error. The proportional constant represents the area in which the controller actually is controlling the process and determines the band of operation. The integral portion corrects for any offset between the set point and process variable by automatically resetting or shifting the proportioning band. The derivative constant determines the rate at which the controller reacts to changes in the process variable.


Test Description

The ADwin product family is useful for PID applications. The ADwin products come with their own DSP processors with nanosecond response timing that controls a variety of I/O. The I/O includes ADCs (analog-to-digital converters), DACs (digital-to-analog converters), digital inputs, and digital outputs. Analog or digital inputs monitor the process and the processor calculates the error between process output and the set point. A new output level for either analog or digital output is calculated to control the process.

ADCs for the ADwin products can be either 12- or 16-bit, while the digital inputs are either TTL, quadrature encoder, PWM, or up/down counters. The ADwin-PRO also has thermocouple and PTD input modules. For highly optimized conditions, the ADwin hardware is capable of acquiring data at a rate of up to 1MHz, and input signals can be measured on both analog and digital inputs. The ADwin-GOLD has two ADCs for measuring two channels simultaneously and can be multiplexed across 16 inputs. The Pro-AIn-F family of input modules are well suited for synchronized sampling. Each channel has a dedicated A/D to allow for parallel measurements, and the measurements can also be simultaneously triggered across multiple input modules.

Once the process is compiled onto the ADwin microprocessor and running, the process will run as long as it is connected to the power supply. The operating system on the host PC can "crash," but as long as ADwin is powered, the process will continue to execute. Therefore, the ADwin systems processes run independent from the load of the PC processor. This continuous execution makes ADwin the ideal solution for fault-intolerant systems or where reliance on the host PC's operating system is impossible. The ADwin-Gold and ADwin-Pro products have boot loader options that allow them to be pre-programmed to run on power-up without the need of a PC.

Flow control is a very common use for PID controllers. This application involves controlling how much fluid or gas is flowing through a pipe with a valve as shown in Figure 1. The volume is monitored and then controlled with the PID. The control steps are summarized here:

  1. Values for the desired flow and PID constants are passed to the ADwin processor.
  2. The flow sensor is measured with one ADwin analog input channel.
  3. The differential between what has been measured and the new set point is calculated.
  4. The error is added to all previous error terms that have been calculated.
  5. The sum of errors is compared to the limit of the valve. If the sum is too large or small, the sum is capped to correspond to a wide open or closed valve.
  6. The new position for the valve is calculated and then changed with one analog output channel on the ADwin.
  7. Return to Step 2 to continue the algorithm.

Figure 1. Example of a Control System using ADwin


Calculating PID Constants

Calculating the constants that are used by the PID controller is the topic of much science and research. Many books and methods exist for "tuning" the controller to provide the desired response. Most methods provide values for each constant, but when the system is actually implemented, the system does not function properly. In fact, even the best methods typically only get you close to the correct constants, but for optimal operation for a specific controller and process variable, there is still an element of trial and error.

The ADwin products allow for easy trial and error. The programming language for the ADwin family is ADbasic. The hardware and ADbasic allow you to define integer and floating-point variables that are stored in registers that can be accessed while ADwin is running. The I/O registers can be easily accessed by most of today's popular programming environments such as TestPoint TM, LabVIEWTM, Visual Basic, or C. The PID constants can then be passed to the appropriate register location and used by the ADwin processor during operation. Also, important process information can be read through one of the programs while ADwin is running.


PID Loop Timing and Multiple Controllers

The PID loop time is determined by how many times per second the output of the controller is updated. For a 1kHz loop, the inputs are measured and outputs changed for the controller 1000 times per second. A general rule of thumb states that the PID control loop should update twice as often as the physical parameter (e.g. temperature, lever position, valve, etc.) If the time constant of the physical parameter is one second, then the control loop should be updated every 0.5 seconds. Typical industrial PID controllers update at 1kHz, while other real-time controllers are generally capable of only about 5kHz.

ADbasic enables quick and simple development of different control structures in addition to the PID, but it is possible to construct a PID loop for ADwin at 500kHz (100% CPU load). As the control loop speed is decreased, the load on the CPU is also reduced. Therefore, a PID control loop running at 50kHz could require only about 10% of the ADwin CPU, and a 20kHz loop uses approximately 4%.

For control loops running at less than 500kHz, excess processor bandwidth is available to run multiple controllers on the same ADwin hardware. These multiple controllers could be either cascaded or independent. The controllers can also run at different frequencies or cycle times.


Basic Programming Guidelines

The following is a sample PID algorithm implemented for ADwin-9 products (implemented using floating-point numbers). The program is written in ADbasic, which is the programming environment for the ADwin products.


' To start the program you must pass values for the defined parameters.
' Parameters can be either be defined statically within the program or 
' passed to ADwin via TestPoint, Visual Basic, or LabVIEW.

' Define Integer Variables that can be passed in and out of ADwin 
' registers
#DEFINE V_IN            PAR_9                   ' Measured input value
#DEFINE SETPOINT        PAR_10          ' Set point - desired output level
#DEFINE PROP            PAR_11          ' Proportional Constant for PID
#DEFINE V_OUT           PAR_12  ' Voltage output value
#DEFINE DELAY           PAR_13  ' Delay between loop executions

' Define Floating Point Variables that can be passed to/from ADwin 
' registers
#DEFINE GAIN            FPAR_1  ' Gain of PID
#DEFINE INTGRL  FPAR_2  ' Integral Constant for PID
#DEFINE DERIV           FPAR_3  ' Derivative Constant for PID
#DEFINE FILTER  FPAR_9  ' Filter for error terms


DIM yd_old, w_old AS INTEGER            ' Hold iterative calculations
DIM sum AS INTEGER                      ' Sum of the error
DIM d_yd AS FLOAT                               ' Derivative constant for PID
DIM index AS INTEGER                    ' Array-Index
DIM DATA_1[2000] AS INTEGER             ' Data array for filtered error term

INIT:                                           ' INIT section runs once at start
SET_MUX(0)                                      ' Set mux for ADC1 to Channel 1
sum = 0                                 ' Set the error term to zero
PROP = ADC(1)                           ' Init parameter with value of ADC1
index = 1                                       ' Initialize array index

'---------------- ATTENTION:  INGRL and DELAY should not be zero. -----------
IF (INTGRL < 1) THEN INTGRL = 10000  ' INTGRL should not be less than 1
IF (DELAY < 40) THEN DELAY = 40              ' GLOBALDELAY no smaller than 40µs
GLOBALDELAY = DELAY                     ' Set GLOBALDELAY


EVENT:                                  ' EVENT define program loop
START_CONV(1)                           ' Start A/D conversion ADC1

'--------------------- The Control Calculation: ----------------------
'  y =      KR   * (   P   +     I       +      D      )
'  |        |        __|_    ____|____       ___|_____ 
'  |        |       /    \  /         \     /         \
V_OUT  =  GAIN  * ( PROP +  sum/(INTGRL) +  d_yd*DERIV) 

DAC(1, V_OUT + 2048)                            ' Set DAC1 output level
yd_old = PROP                                   ' Save old value of PROP constant
WAIT_EOC(1)                                             ' Wait for end of conversion - ADC1
V_IN = READADC(1)                                       ' Read value from ADC1
PROP = V_IN - SETPOINT                          ' Error = Present value - Set point
sum = sum + PROP                                        ' Calculate the sum of the error

'------ The limit for the sum of the error = 5000 * INTGRL / GAIN -----
IF (sum > 2000000) THEN sum = 2000000                ' Upper limit for sum
IF (sum <-2000000) THEN sum = -2000000               ' Lower limit for sum

d_yd = PROP - yd_old                            ' Derivative gain = new error - old

' ___________________________________________________________________
'/  This program will interrupt the PC when the error array is full! \
  FILTER = V_IN*0.9999 + PROP*0.0001            ' Filter for error
  IF (FILTER <> w_old) THEN                       ' Not equal?
    DATA_1[index] = PROP                                ' Write variable to array
    index = index + 1                                   ' Increment index
    IF (index >= 2000) THEN                          ' Check array size
      ACTIVATE_PC                                       ' Give TestPoint interrupt
      index = 1                                         ' Reset counter
      w_old = FILTER                                    ' Old value = New value
    ENDIF
  ENDIF
'\____________________________________________________________________/


FINISH:                                 ' FINISH section runs once at end
DAC (1, 2048)                                   ' Program DAC to 0V after completion

Example Program

A downloadable example program, optimized for the ADwin-9 microprocessor, accompanies this application note.

NOTE: The example program provided is intended to illustrate the concepts presented in this note. The program may need to be altered in order to accommodate desired test parameters and timing.

Equipment List

  1. PC running Windows® 95, 98, NT4, or 2000
  2. ADwin-9 real-time control hardware
  3. ADbasic real-time development environment
  4. Custom wiring harness for connecting to test setup

Alternative Solutions

The ADwin product family offers many different form factors, processors, and I/O in which to create the PID controller. The ADwin family includes:

ADwin-Light   PC ISA bus plug-in boards
  • Analog inputs and outputs (one 12-bit A/D) - expandable
  • Digital I/O
  • Optional counters and quadrature encoder
  • ISA communication with the PC
ADwin-GOLD   Stand-alone, external system
  • 16 analog inputs (16-bit @ 10ms & 12-bit @ 0.8ms) - BNC connections
  • 2 analog outputs (16-bit) - expandable
  • 32 digital I/Os, in blocks of 8 as input or output
  • Optional counter (event counter, encoder interface, period cycle, PWM)
  • ISA or USB communication with the PC
ADwin-PRO   Industrial, modular 19-inch system
  • Up to 4 CPUs per system
  • Modular and expandable:
    • 480 analog inputs (multiplexed or parallel)
    • 120 analog outputs
    • 480 digital I/Os
    • Thermocouples, RTD, counter, filters, isolation, etc.
    • CAN-bus, Profibus, RS-232, RS-485, RS-422
  • ISA or USB for communication with the PC

All three product variations can support up to 32Mbytes of RAM per processor and optional bootloader. The bootloader stores an ADbasic process(es) and begins running the process upon power-up. Therefore, once the ADwin hardware is programmed, it can be used as a stand-alone and without a PC.