# C2000 PID Controller

Digital PID controller

## Library

Embedded Coder® Support Package for Texas Instruments™ C2000™ Processors/ Optimization/ C28x DMC

Embedded Coder Support Package for Texas Instruments C2000 F28M3x Concerto™ Processors Processors/ Optimization/ C28x DMC

## Description

This block implements a 32-bit digital PID controller with antiwindup correction. The inputs are a reference input (`ref`) and a feedback input (`fdb`) and the output (`out`) is the saturated PID output. The following diagram shows a PID controller with antiwindup.

The differential equation describing the PID controller before saturation that is implemented in this block is

upresat(t) = up(t) + ui(t) + ud(t)

where upresat is the PID output before saturation, up is the proportional term, ui is the integral term with saturation correction, and ud is the derivative term.

The proportional term is

up(t) = Kpe(t)

where Kp is the proportional gain of the PID controller and e(t) is the error between the reference and feedback inputs.

The integral term with saturation correction is

`${u}_{i}\left(t\right)=\underset{0}{\overset{t}{\int }}\left\{\frac{{K}_{p}}{{T}_{i}}e\left(\tau \right)+{K}_{c}\left(u\left(\tau \right)-{u}_{presat}\left(\tau \right)\right)\right\}d\tau$`

where Kc is the integral correction gain of the PID controller.

The derivative term is

`${u}_{d}\left(t\right)={K}_{p}{T}_{d}\frac{de\left(t\right)}{dt}$`

where Td is the derivative time of the PID controller. In discrete terms, the derivative gain is defined as Kd = Td/T, and the integral gain is defined as Ki = T/Ti, where T is the sampling period and Ti is the integral time of the PID controller.

Using backward approximation, the preceding differential equations can be transformed into the following discrete equations.

`$\begin{array}{l}{u}_{p}\left[n\right]={K}_{p}e\left[n\right]\\ {u}_{i}\left[n\right]={u}_{i}\left[n-1\right]+{K}_{i}{K}_{p}e\left[n\right]+{K}_{c}\left(u\left[n-1\right]-{u}_{presat}\left[n-1\right]\right)\\ {u}_{d}\left[n\right]={K}_{d}{K}_{p}\left(e\left[n\right]-e\left[n-1\right]\right)\\ {u}_{presat}\left[n\right]={u}_{p}\left[n\right]+{u}_{i}\left[n\right]+{u}_{d}\left[n\right]\\ u\left[n\right]=SAT\left({u}_{presat}\left[n\right]\right)\end{array}$`

Note

• To generate optimized code from this block, enable the ```TI C28x``` or `TI C28x (ISO)` Code Replacement Library.

The implementation of this block does not call the corresponding Texas Instruments library function during code generation. The TI function uses a global Q setting and the MathWorks® code used by this block dynamically adjusts the Q format based on the block input. See Using the IQmath Library for more information.

This block implements a 32-bit digital PID controller with antiwindup correction. The inputs are a reference input (`ref`) and a feedback input (`fdb`) and the output (`out`) is the saturated PID output. The following diagram shows a PID controller with antiwindup.

The differential equation describing the PID controller before saturation that is implemented in this block is

upresat(t) = up(t) + ui(t) + ud(t)

where upresat is the PID output before saturation, up is the proportional term, ui is the integral term with saturation correction, and ud is the derivative term.

The proportional term is

up(t) = Kpe(t)

where Kp is the proportional gain of the PID controller and e(t) is the error between the reference and feedback inputs

`${u}_{i}\left(t\right)=\underset{0}{\overset{t}{\int }}\left\{\frac{{K}_{p}}{{T}_{i}}e\left(\tau \right)+{K}_{c}\left(u\left(\tau \right)-{u}_{presat}\left(\tau \right)\right)\right\}d\tau$`

where Kc is the integral correction gain of the PID controller.

The derivative term is

`${u}_{d}\left(t\right)={K}_{p}{T}_{d}\frac{de\left(t\right)}{dt}$`

where Td is the derivative time of the PID controller. In discrete terms, the derivative gain is defined as Kd = Td/T, and the integral gain is defined as Ki = T/Ti, where T is the sampling period and Ti is the integral time of the PID controller.

Using backward approximation, the preceding differential equations can be transformed into the following discrete equations.

`$\begin{array}{l}{u}_{p}\left[n\right]={K}_{p}e\left[n\right]\\ {u}_{i}\left[n\right]={u}_{i}\left[n-1\right]+{K}_{i}{K}_{p}e\left[n\right]+{K}_{c}\left(u\left[n-1\right]-{u}_{presat}\left[n-1\right]\right)\\ {u}_{d}\left[n\right]={K}_{d}{K}_{p}\left(e\left[n\right]-e\left[n-1\right]\right)\\ {u}_{presat}\left[n\right]={u}_{p}\left[n\right]+{u}_{i}\left[n\right]+{u}_{d}\left[n\right]\\ u\left[n\right]=SAT\left({u}_{presat}\left[n\right]\right)\end{array}$`

Note

• To generate optimized code from this block, enable the ```TI C28x``` or `TI C28x (ISO)` Code Replacement Library.

The implementation of this block does not call the corresponding Texas Instruments library function during code generation. The TI function uses a global Q setting and the MathWorks code used by this block dynamically adjusts the Q format based on the block input. See Using the IQmath Library for more information.

## Parameters

Proportional gain

Amount of proportional gain (Kp) to apply to the PID

Integral gain

Amount of gain (Ki) to apply to the integration equation

Integral correction gain

Amount of correction gain (Kc) to apply to the integration equation

Derivative gain

Amount of gain (Kd) to apply to the derivative equation.

Minimum output

Minimum allowable value of the PID output

Maximum output

Maximum allowable value of the PID output

## References

For detailed information on the DMC library, see C/F 28xx Digital Motor Control Library, Literature Number SPRC080, available at the Texas Instruments Web site.