Model Predictive Control Toolbox

Using MPC Controller Block Inside Function-Call and Triggered Subsystems

This example shows how to configure and simulate MPC Controller blocks placed inside Function-Call and Triggered subsystems.

Required Products

To run this example, Simulink® is required.

if ~mpcchecktoolboxinstalled('simulink')
    disp('Simulink(R) is required to run this example.')
    return
end

Define Plant Model and MPC Controller

Define a SISO plant.

sys = ss(tf([3 1],[1 0.6 1]));

Define the MPC controller for the plant.

Ts = 0.1;   %Sampling time
p = 10;     %Prediction horizon
m = 2;      %Control horizon
Weights = struct('MV',0,'MVRate',0.01,'OV',1); % Weights
MV = struct('Min',-Inf,'Max',Inf,'RateMin',-100,'RateMax',100); % Input constraints
OV = struct('Min',-2,'Max',2); % Output constraints
mpccon = mpc(sys,Ts,p,m,Weights,MV,OV);

Configure and Simulate MPC Controller Block Inside Function-Call Subsystem

Function-Call subsystem is invoked directly by another block during simulation. To ensure that the MPC controller works properly inside a Function-Call subsystem, you must configure the MPC Controller block to use inherited sample time and invoke the Function-Call subsystem periodically with the same sample time defined in the MPC controller object.

Open the model.

mdl = 'mpc_rtwdemo_functioncall';
open_system(mdl);

Configure the MPC block to use inherited sample time (-1).

Double-click the MPC block. Select the "Block uses inherited sample time (-1)" checkbox.

open_system([mdl '/MPC in Function-Call Subsystem/MPC Controller']);

Invoke the Function-Call subsystem periodically with the correct sample time.

For this example, since the controller has a sample time of 0.1 seconds, configure the trigger block inside the Function-Call subsystem to use the the same sample time.

For this example, use the "Function-Call Generator" block to execute the Function-Call subsystem at the sample rate as 0.1 seconds.

Simulate the model.

close_system([mdl '/MPC in Function-Call Subsystem/MPC Controller']);
sim(mdl);
-->Converting model to discrete time.
-->Integrated white noise added on measured output channel #1.
-->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.

The controller effort and the plant output are saved into base workspace as variables u_fc and y_fc, respectively.

Close the Simulink model.

bdclose(mdl);

Configure and Simulate MPC Controller Block Inside Triggered Subsystem

Triggered subsystem executes each time a trigger event occurs. To ensure that the MPC controller works properly inside a Triggered subsystem, you must configure the MPC Controller block to use inherited sample time and invoke the Triggered subsystem periodically with the same sample time defined in the MPC controller object.

Open the model.

mdl = 'mpc_rtwdemo_triggered';
open_system(mdl);

Configure the MPC block to use inherited sample time (-1) as you did earlier.

Execute the Triggered subsystem periodically with the correct sample time.

For this example, configure the trigger block inside the Triggered subsystem to use either a falling trigger type.

For this example, use the "Pulse Generator" block to provide a periodic triggering signal at the sample rate as 0.1 seconds.

Simulate the model.

sim(mdl);

The controller effort and the plant output are saved into base workspace as variables u_tr and y_tr, respectively.

Close the Simulink model.

bdclose(mdl);

Compare Responses

Compare the simulation results from the Function-Call subsystem and the Triggered subsystem with the result generated by an MPC Controller block that is not placed inside a subsystem and does not inherit sample time.

mdl = 'mpc_rtwdemo';
open_system(mdl);
sim(mdl);

Compare the responses of manipulated variable.

f1 = figure;
plot(t,u,'b-',t,u_fc,'ro',t(1:end-1),u_tr,'k.');
title('Manipulated Variable');
legend('No Subsystem','Function-Call','Triggered');

Compare the responses the plant output.

f2 = figure;
plot(t,y,'b-',t,y_fc,'ro',t(1:end-1),y_tr,'k.');
title('Plant Output');
legend('No Subsystem','Function-Call','Triggered');

The results of all three models are numerically equal.

Close the Simulink model.

bdclose(mdl);