%% Create Parameter Bus for Nonlinear MPC Controller Block
%%
% Create a nonlinear MPC controller with four states, two outputs, and one
% input.
nlobj = nlmpc(4,2,1);

%%
% Specify the sample time and horizons of the controller.
Ts = 0.1;
nlobj.Ts = Ts;
nlobj.PredictionHorizon = 10;
nlobj.ControlHorizon = 5;

%%
% Specify the state function for the controller, which is in the file
% |pendulumDT0.m|. This discrete-time model integrates the continuous-time
% model defined in |pendulumCT0.m| using a multistep forward Euler method.
nlobj.Model.StateFcn = "pendulumDT0";
nlobj.Model.IsContinuousTime = false;

%%
% The prediction model uses an optional parameter, |Ts|, to represent the
% sample time. Specify the number of parameters.
nlobj.Model.NumberOfParameters = 1;

%%
% Specify the output function of the model, passing the sample-time
% parameter as an input argument.
nlobj.Model.OutputFcn = @(x,u,Ts) [x(1); x(3)];

%%
% Define standard constraints for the controller.
nlobj.Weights.OutputVariables = [3 3];
nlobj.Weights.ManipulatedVariablesRate = 0.1;
nlobj.OV(1).Min = -10;
nlobj.OV(1).Max = 10;
nlobj.MV.Min = -100;
nlobj.MV.Max = 100;

%%
% Open Simulink model.
mdl = 'mpc_pendcartNMPC';
open_system(mdl)

%%
% In this model, the Nonlinear MPC Controller block is configured to use
% the controller |nlobj|.

%%
% To use the optional parameter in the prediction model, the model has a
% Simulink Bus block connected to the |params| input port of the Nonlinear
% MPC Controller block. To configure this bus block to use the |Ts|
% parameter, create a Bus object in the MATLAB(R) workspace, and configure
% the Bus Creator block to use this object. Name the Bus object
% |'myBusObject'|.
createParameterBus(nlobj,[mdl '/Nonlinear MPC Controller'],'myBusObject',{Ts});

bdclose(mdl)

% Copyright 2018 The MathWorks, Inc.