Documentation Center

  • Trial Software
  • Product Updates

Tuning Controller Weights

This example shows how to vary the weights on outputs, inputs, and ECR slack variable for soft constraints in real-time.

The weights specified in the MPC object are overridden by the weights supplied to the MPC Controller block. If a weight signal is not connected to the MPC Controller block, then the corresponding weight is the one specified in the MPC object.

Model Predictive Controller Setup

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

Define a multivariable discrete-time linear system with no direct I/O feedthrough, and assume input #4 is a measured disturbance and output #4 is unmeasured.

Ts = 0.1; % sampling time
sys=tf({1,[1 1],5,2;3,[1 5],1,0;0,0,1,[1 1];2,[1 -1],0,0},...
    {[1 1 1],[1 3 4 5],[1 10],[1 5];
      [1 1],[1 2],[1 2 8],[1 1];
      [1 2 1],[1 3 1 1],[1 1],[1 2];
      [1 1],[1 3 10 10],[1 10],[1 1]});
sys=c2d(ss(sys),Ts);
sys.D=0;
sys=setmpcsignals(sys,'MD',4,'UO',4);
-->Assuming unspecified input signals are manipulated variables.
-->Assuming unspecified output signals are measured outputs.

Define MPC object with constraints on manipulated variables.

p = 20;                                     % Prediction horizon
m = 3;                                      % Control horizon
mpc_controller = mpc(sys,Ts,p,m);
mpc_controller.MV(1).Min=-6;
mpc_controller.MV(1).Max=6;
mpc_controller.MV(2).Min=-6;
mpc_controller.MV(2).Max=6;
mpc_controller.MV(3).Min=-6;
mpc_controller.MV(3).Max=6;
-->The "Weights.ManipulatedVariables" property of "mpc" object is empty. Assuming default 0.00000.
-->The "Weights.ManipulatedVariablesRate" property of "mpc" object is empty. Assuming default 0.10000.
-->The "Weights.OutputVariables" property of "mpc" object is empty. Assuming default 1.00000.
   for output(s) y1 y2 y3 and zero weight for output(s) y4 

Closed-Loop Simulation Using Simulink®

Define simulation time.

Tstop=10;

Define reference signal.

ref=[1 0 3 1];
r=struct('time',(0:Ts:Tstop)');
N=numel(r.time);
r.signals.values=ones(N,1)*ref;

Define measured disturbance.

v=.5;

Define run-time weights. Output weights are linearly increasing with time, except for output #2 that is not weighted.

ywt.time=r.time;
ywt.signals.values=(1:N)'*[.1 0 .1 .1];

Weights on input increments are decreasing linearly with time.

duwt.time=r.time;
duwt.signals.values=(1-(1:N)/2/N)'*[.1 .1 .1];

Weights on ECR slacks increase exponentially with time.

ECRwt.time=r.time;
ECRwt.signals.values=10.^(2+(1:N)'/N);

Start simulation.

open_system('mpc_onlinetuning');                   % Open Simulink(R) Model
sim('mpc_onlinetuning',Tstop);                     % Start Simulation
-->Integrated white noise added on measured output channel #1.
-->Integrated white noise added on measured output channel #2.
-->Integrated white noise added on measured output channel #3.
-->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.

Step-by-Step Closed-Loop Simulation with MPCMOVE Command

Define MPC state object.

[A,B,C,D]=ssdata(sys);
x=zeros(size(sys.B,1),1);           % Initial state of the plant
xmpc=mpcstate(mpc_controller);      % Initial state of the MPC controller

We store the closed-loop MPC trajectories in arrays YY,UU,XX.

YY=[];
UU=[];
XX=[];

We use MPCMOVEOPT object to provide weights at run-time.

options=mpcmoveopt;

Start simulation .

for t=0:N-1,

    % Store states
    XX=[XX,x]; %#ok<*AGROW>

    % Compute plant output (note: no feedthrough from MV to Y, D(:,1)=0)
    y=C*x+D(:,4)*v;
    YY=[YY;y'];

    % Obtain reference signal
    ref=r.signals.values(t+1,:)';

    % Update MPCMOVEOPT object with run-time weights
    options.MVRateWeight=duwt.signals.values(t+1,:);
    options.OutputWeight=ywt.signals.values(t+1,:);
    options.ECRWeight=ECRwt.signals.values(t+1,:);

    % Compute control action
    u=mpcmove(mpc_controller,xmpc,y(1:3),ref,v,options);
    UU=[UU;u'];

    % Update plant states
    x=A*x+B(:,1:3)*u+B(:,4)*v;

end

Plot and Compare Simulation Results

figure(1);
clf;
subplot(121)
plot(0:Ts:Tstop,[YY ysim])
grid
title('output')
subplot(122)
plot(0:Ts:Tstop,[UU usim])
grid
title('input')

Simulation results are the same.

fprintf('\n\nDifference between MPC Simulink block and MPCMOVE simulations: %g',norm(UU-usim)+norm(YY-ysim));

Difference between MPC Simulink block and MPCMOVE simulations: 6.73554e-11
bdclose('mpc_onlinetuning');
Was this topic helpful?