7.1.3 PID Library Functions and Data Stuctures

The DSP library provides a PID Controller function, PID ( tPID* ), to perform a PID operation. The function uses a data structure defined in the header file dsp.h, which has the following form:

 typedef struct { 
  fractional* abcCoefficients; 
  fractional* controlHistory; 
  fractional controlOutput; 
  fractional measuredOutput; 
  fractional controlReference; 
} tPID; 

Prior to invoking the PID() function, the application should initialize the data structure of type tPID. This is done in the following steps:

  1. Calculate Coefficients from PID Gain Values. The element abcCoefficients in the data structure of type tPID is a pointer to A, B and C coefficients located in X-Data space. These coefficients are derived from the PID gain values, Kp, Ki and Kd, shown in Figure 7-1, as follows: A = Kp + Ki + Kd B = -(Kp + 2*Kd) C = Kd . To derive the A, B and C coefficients, the DSP library provides a function, PIDCoeffCalc.
  2. Clear the PID State Variables. The structural element controlHistory is a pointer to a history of three samples located in Y-Data space, with the first sample being the most recent (current). These samples constitute a history of current and past differences between the Reference Input and the Measured Output of the plant function. The PIDInit function clears the elements pointed to by controlHistory. It also clears the controlOutput element in the tPID data structure.