# Enforce Constraints for PID Controllers

This example shows how to enforce known constraints for a PID controller application using the Constraint Enforcement block.

### Overview

For this example, the plant dynamics are described by the following equations [1].

`$\begin{array}{l}{\underset{}{\overset{˙}{x}}}_{1}=-{x}_{1}+\left({x}_{1}^{2}+1\right){u}_{1}\\ {\underset{}{\overset{˙}{x}}}_{2}=-{x}_{2}+\left({x}_{2}^{2}+1\right){u}_{2}\end{array}$`

The goal for the plant is to track desired trajectories, defined as:

`$\begin{array}{l}\underset{}{\overset{˙}{\theta }}=0.1\pi \\ {\underset{}{\overset{˙}{x}}}_{1d}=-r\mathrm{cos}\left(\theta \right)\\ {\underset{}{\overset{˙}{x}}}_{2d}=r\mathrm{sin}\left(\theta \right)\end{array}$`

For an example that learns and applies an unknown constraint function for the same PID control application, see Learn and Apply Constraints for PID Controllers.

Configure model parameters and initial conditions.

```r = 1.5; % Radius for desired trajectory Ts = 0.1; % Sample time Tf = 22; % Duration x0_1 = -r; % Initial condition for x1 x0_2 = 0; % Initial condition for x2```

### Design PID Controllers

Before applying constraints, design PID controllers for tracking the reference trajectories. The `trackingWithPIDs` model contains two PID controllers with gains tuned using the PID Tuner app. For more information on tuning PID controllers in Simulink models, see Introduction to Model-Based PID Tuning in Simulink.

```mdl = 'trackingWithPIDs'; open_system(mdl)```

Simulate the PID controllers and plot their tracking performance.

```% Simulate the model. out = sim(mdl); % Extract trajectories. logData = out.logsout; x1_traj = logData{3}.Values.Data; x2_traj = logData{4}.Values.Data; x1_des = logData{1}.Values.Data; x2_des = logData{2}.Values.Data; % Plot trajectories. figure('Name','Tracking') xlim([-2,2]) ylim([-2,2]) plot(x1_des,x2_des,'r') xlabel('x1') ylabel('x2') hold on plot(x1_traj,x2_traj,'b:','LineWidth',2) hold on plot(x1_traj(1),x2_traj(1),'g*') hold on plot(x1_traj(end),x2_traj(end),'go') legend('Desired','Trajectory','Start','end')```

### Constraint Function

For this example, you apply known constraints to the application using the Constraint Enforcement block, which adjusts control actions to satisfy a constraint function.

In this example, the feasible region for the plant is given by $\left\{\mathit{x}:{\mathit{x}}_{1}\le 1,{\mathit{x}}_{\mathit{x}}\le 1\right\}$. Therefore, the plant next-state condition ${\mathit{x}}_{\mathit{k}+1}=\left[\begin{array}{c}{\mathit{x}}_{1}\left(\mathit{k}+1\right)\\ {\mathit{x}}_{2}\left(\mathit{k}+1\right)\end{array}\right]$must satisfy ${\mathit{x}}_{\mathit{k}+1}\le \left[\begin{array}{c}1\\ 1\end{array}\right]$.

You can approximate the plant dynamics by the following equation.

`${\mathit{x}}_{\mathit{k}+1}\approx \left[\begin{array}{cc}\left(1-{\mathit{T}}_{\mathit{s}}\right)& 0\\ 0& \left(1-{\mathit{T}}_{\mathit{s}}\right)\end{array}\right]{\mathit{x}}_{\mathit{k}}+\left[\begin{array}{cc}{\mathit{T}}_{\mathit{s}}\left(1+{\mathit{x}}_{1}^{2}\left(\mathit{k}\right)\right)& 0\\ 0& {\mathit{T}}_{\mathit{s}}\left(1+{\mathit{x}}_{2}^{2}\left(\mathit{k}\right)\right)\end{array}\right]{\mathit{u}}_{\mathit{k}}$`

Applying the constraints to this equation produces the following constraint function.

`$\left[\begin{array}{cc}\left(1-{\mathit{T}}_{\mathit{s}}\right)& 0\\ 0& \left(1-{\mathit{T}}_{\mathit{s}}\right)\end{array}\right]{\mathit{x}}_{\mathit{k}}+\left[\begin{array}{cc}{\mathit{T}}_{\mathit{s}}\left(1+{\mathit{x}}_{1}^{2}\left(\mathit{k}\right)\right)& 0\\ 0& {\mathit{T}}_{\mathit{s}}\left(1+{\mathit{x}}_{2}^{2}\left(\mathit{k}\right)\right)\end{array}\right]{\mathit{u}}_{\mathit{k}}\le \left[\begin{array}{c}1\\ 1\end{array}\right]$`

The Constraint Enforcement block accepts constraints of the form ${\mathit{f}}_{\mathit{x}}+{\mathit{g}}_{\mathit{x}}\mathit{u}\le \mathit{c}$. For this application, the coefficients of this constraint function are as follows.

`${\mathit{f}}_{\mathit{x}}=\left[\begin{array}{cc}\left(1-{\mathit{T}}_{\mathit{s}}\right)& 0\\ 0& \left(1-{\mathit{T}}_{\mathit{s}}\right)\end{array}\right]{\mathit{x}}_{\mathit{k}},{\text{\hspace{0.17em}}\mathit{g}}_{\mathit{x}}=\left[\begin{array}{cc}{\mathit{T}}_{\mathit{s}}\left(1+{\mathit{x}}_{1}^{2}\left(\mathit{k}\right)\right)& 0\\ 0& {\mathit{T}}_{\mathit{s}}\left(1+{\mathit{x}}_{2}^{2}\left(\mathit{k}\right)\right)\end{array}\right],\text{\hspace{0.17em}}\mathit{c}=\left[\begin{array}{c}1\\ 1\end{array}\right]$`

### Simulate PID Controller with Constraint Enforcement

The `trackingWithConstraintPID` model contains the PID controllers, the plant dynamics and the constraint implementation.

```mdl = 'trackingWithKnownConstraintPID'; open_system(mdl)```

To view the constraint implementation, open the Constraint subsystem. Here, the model implements the known constraint function using a MATLAB Function block, and the Constraint Enforcement block enforces the constraint function.

Run the model and plot the simulation results. The plot shows that the plant states are less than one.

```% Simulate the model. out = sim(mdl); % Extract trajectories. logData = out.logsout; x1_traj = zeros(size(out.tout)); x2_traj = zeros(size(out.tout)); for ct = 1:size(out.tout,1) x1_traj(ct) = logData{4}.Values.Data(:,:,ct); x2_traj(ct) = logData{5}.Values.Data(:,:,ct); end x1_des = logData{2}.Values.Data; x2_des = logData{3}.Values.Data; % Plot trajectories. figure('Name','Tracking with Constraint'); plot(x1_des,x2_des,'r') xlabel('x1') ylabel('x2') hold on plot(x1_traj,x2_traj,'b:','LineWidth',2) hold on plot(x1_traj(1),x2_traj(1),'g*') hold on plot(x1_traj(end),x2_traj(end),'go') legend('Desired','Trajectory','Start','End','Location','best')```

The Constraint Enforcement block successfully constrains the control actions such that the plant states remain less than one.

```bdclose('trackingWithPIDs') bdclose('trackingWithKnownConstraintPID')```

### References

[1] Robey, Alexander, Haimin Hu, Lars Lindemann, Hanwen Zhang, Dimos V. Dimarogonas, Stephen Tu, and Nikolai Matni. "Learning Control Barrier Functions from Expert Demonstrations." Preprint, submitted April 7, 2020. https://arxiv.org/abs/2004.03315