Main Content

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].

x˙1=-x1+(x12+1)u1x˙2=-x2+(x22+1)u2

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

θ˙=0.1πx˙1d=-rcos(θ)x˙2d=rsin(θ)

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','traj','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 {x:x11,xx1}. Therefore, the plant next-state condition xk+1=[x1(k+1)x2(k+1)]must satisfy xk+1[11].

You can approximate the plant dynamics by the following equation.

xk+1[(1-Ts)00(1-Ts)]xk+[Ts(1+x12(k))00Ts(1+x22(k))]uk

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

[(1-Ts)00(1-Ts)]xk+[Ts(1+x12(k))00Ts(1+x22(k))]uk[11].

The Constraint Enforcement block accepts constraints of the form fx+gxuc. For this application, the coefficients of this constraint function are as follows.

fx=[(1-Ts)00(1-Ts)]xk,gx=[Ts(1+x12(k))00Ts(1+x22(k))],c=[11]

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 known constraint function is implemented using a MATLAB Function block, and the Constraint Enforcement block enforces the constraint function.

Run the model and plot the simulation results. It is shown in the plot 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','traj','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

See Also

Blocks

Apps

Related Topics