Main Content

getCodeGenerationData

Create data structures for mpcmoveCodeGeneration

Description

Use this function to create data structures for the mpcmoveCodeGeneration function, which computes optimal control moves for implicit and explicit linear MPC controllers.

For information on generating data structures for nlmpcmoveCodeGeneration, see getCodeGenerationData.

[configData,stateData,onlineData] = getCodeGenerationData(mpcobj) creates data structures for use with mpcmoveCodeGeneration.

example

[___] = getCodeGenerationData(___,Name,Value) specifies additional options using one or more Name,Value pair arguments.

example

Examples

collapse all

Create a plant model, and define the MPC signal types.

plant = rss(3,2,2);
plant.D = 0;
plant = setmpcsignals(plant,'mv',1,'ud',2,'mo',1,'uo',2);

Create an MPC controller.

mpcObj = mpc(plant,0.1);
-->"PredictionHorizon" is empty. Assuming default 10.
-->"ControlHorizon" is empty. Assuming default 2.
-->"Weights.ManipulatedVariables" is empty. Assuming default 0.00000.
-->"Weights.ManipulatedVariablesRate" is empty. Assuming default 0.10000.
-->"Weights.OutputVariables" is empty. Assuming default 1.00000.
   for output(s) y1 and zero weight for output(s) y2 

Configure your controller parameters. For example, define bounds for the manipulated variable.

mpcObj.ManipulatedVariables.Min = -1;
mpcObj.ManipulatedVariables.Max = 1;

Create code generation data structures.

[configData,stateData,onlineData] = getCodeGenerationData(mpcObj);
-->Converting model to discrete time.
-->The "Model.Disturbance" property is empty:
   Assuming unmeasured input disturbance #2 is integrated white noise.
   Assuming no disturbance added to measured output #1.
-->"Model.Noise" is empty. Assuming white noise on each measured output.
-->Converting model to discrete time.
-->The "Model.Disturbance" property is empty:
   Assuming unmeasured input disturbance #2 is integrated white noise.
   Assuming no disturbance added to measured output #1.
-->"Model.Noise" is empty. Assuming white noise on each measured output.

Create a plant model, and define the MPC signal types.

plant = rss(3,2,2);
plant.D = 0;

Create an MPC controller.

mpcobj = mpc(plant,0.1);
-->"PredictionHorizon" is empty. Assuming default 10.
-->"ControlHorizon" is empty. Assuming default 2.
-->"Weights.ManipulatedVariables" is empty. Assuming default 0.00000.
-->"Weights.ManipulatedVariablesRate" is empty. Assuming default 0.10000.
-->"Weights.OutputVariables" is empty. Assuming default 1.00000.

Create code generation data structures. Configure options to:

  • Use single-precision floating-point values in the generated code.

  • Improve computational efficiency by not computing optimal sequence data.

  • Run your MPC controller in adaptive mode.

[configData,stateData,onlineData] = getCodeGenerationData(mpcobj,...
    'DataType','single',OnlyComputeCost=true,IsAdaptive=true);
-->Converting model to discrete time.
-->Assuming output disturbance added to measured output #1 is integrated white noise.
-->Assuming output disturbance added to measured output #2 is integrated white noise.
-->"Model.Noise" is empty. Assuming white noise on each measured output.
-->Converting model to discrete time.
-->Assuming output disturbance added to measured output #1 is integrated white noise.
-->Assuming output disturbance added to measured output #2 is integrated white noise.
-->"Model.Noise" is empty. Assuming white noise on each measured output.

Input Arguments

collapse all

Model predictive controller, specified as one of the following:

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'DataType','single' specifies that the generated code uses single-precision floating point values.

Initial controller state when using mpcmoveCodeGeneration, specified as the comma-separated pair consisting of 'InitialState' and an mpcstate object. This state is used in place of the default state information from mpcobj.

Data type used in generated code when using mpcmoveCodeGeneration, specified as specified as the comma-separated pair consisting of 'DataType' and one of the following:

  • 'double' — Use double-precision floating point values.

  • 'single' — Use single-precision floating point values.

Toggle for computing only optimal cost during simulation when using mpcmoveCodeGeneration, specified as specified as the comma-separated pair consisting of 'OnlyComputeCost' and either true or false. To reduce computational load by not calculating optimal sequence data, set OnlyComputeCost to true.

Adaptive MPC indicator when using mpcmoveCodeGeneration, specified as specified as the comma-separated pair consisting of 'IsAdaptive' and either true or false. Set IsAdaptive to true if your controller is running in adaptive mode.

For more information on adaptive MPC, see Adaptive MPC.

Note

IsAdaptive and IsLTV cannot be true at the same time.

Time-varying MPC indicator when using mpcmoveCodeGeneration, specified as the comma-separated pair consisting of 'IsLTV' and either true or false. Set IsLTV to true if your controller is running in time-varying mode.

For more information on time-varying MPC, see Time-Varying MPC.

Note

IsAdaptive and IsLTV cannot be true at the same time.

Variable horizon indicator when using mpcmoveCodeGeneration, specified as the comma-separated pair consisting of 'UseVariableHorizon' and either true or false. To vary your prediction and control horizons at run time, set UseVariableHorizons to true.

When you use variable horizons, mpcmoveCodeGeneration ignores the horizons specified in configData and instead uses the prediction and control horizon specified in onlineData.horizons.

For more information, see Adjust Horizons at Run Time.

Output Arguments

collapse all

MPC configuration parameters that are constant at run time, returned as a structure. These parameters are derived from the controller settings in mpcobj. When simulating your controller, pass configData to mpcmoveCodeGeneration without changing any parameters.

For more information on how generated MPC code uses constant matrices in configData to solve the QP problem, see QP Problem Construction for Generated C Code.

Initial controller states, returned as a structure. To initialize your simulation with the initial states defined in mpcobj, pass stateData to mpcmoveCodeGeneration. To use different initial conditions, modify stateData. You can specify nondefault controller states using InitialState.

For more information on the stateData fields, see mpcmoveCodeGeneration.

stateData has the following fields.

FieldDescription
Plant

Plant model state estimates

Disturbance

Unmeasured disturbance model state estimates

Noise

Output measurement noise model state estimates

LastMove

Manipulated variable control moves from previous control interval

Covariance

Covariance matrix for controller state estimates

iA

Active inequality constraints

Online MPC controller data that you must update at each control interval, returned as a structure with the following fields.

FieldDescription
signals

Input and output signals, returned as a structure with the following fields.

FieldDescription
ymMeasured outputs
refOutput references
mdMeasured disturbances
mvTargetTargets for manipulated variables
externalMVManipulated variables externally applied to the plant

limits

Input and output constraints, returned as a structure with the following fields:

FieldDescription
yminLower bounds on output signals
ymaxUpper bounds on output signals
uminLower bounds on input signals
umaxUpper bounds on input signals

When mpcobj is an explicit MPC controller, mpcmoveCodeGeneration ignores the limits field.

weights

Updated QP optimization weights, returned as a structure with the following fields:

FieldDescription
ywtOutput weights
uwtManipulated variable weights
duwtManipulated variable rate weights
ecrWeight on slack variable used for constraint softening

When mpcobj is an explicit MPC controller, mpcmoveCodeGeneration ignores the weights field.

customconstraints

Updated custom mixed input/output constraints, returned as a structure with the following fields:

FieldDescription
E Manipulated variable constraint constant
FControlled output constraint constant
GMixed input/output constraint constant
SMeasured disturbance constraint constant

When mpcobj is an explicit MPC controller, mpcmoveCodeGeneration ignores the customconstraints field.

horizons

Updated controller horizon values, returned as a structure with the following fields:

FieldDescription
p Prediction horizon
mControl horizon

The horizons field is returned only when the UseVariableHorizon name-value pair is true.

When mpcobj is an explicit MPC controller, mpcmoveCodeGeneration ignores the horizons field.

model

Updated plant and nominal values for adaptive MPC and time-varying MPC, returned as a structure with the following fields:

FieldDescription
A, B, C, DState-space matrices of discrete-time state-space model.
XNominal plant states
UNominal plant inputs
YNominal plant outputs
DXNominal plant state derivatives

The model field is returned only when either the IsAdaptive or IsLTV name-value pair is true.

getCodeGenerationData returns onlineData with empty matrices for all structure fields, except signals.ref, signals.ym, and signals.md. These fields contain the corresponding nominal signal values from mpcobj. If your controller does not have measured disturbances, signals.md is returned as an empty matrix.

For more information on configuring onlineData fields, see mpcmoveCodeGeneration.

Version History

Introduced in R2016a