getCodeGenerationData

Create data structures for mpcmoveCodeGeneration

Description

example

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

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);
-->The "PredictionHorizon" property of "mpc" object is empty. Trying PredictionHorizon = 10.
-->The "ControlHorizon" property of the "mpc" object is empty. Assuming 2.
-->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 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 of "mpc" object is empty:
   Assuming unmeasured input disturbance #2 is integrated white noise.
   Assuming no disturbance added to measured output channel #1.
-->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.
-->Converting model to discrete time.
-->The "Model.Disturbance" property of "mpc" object is empty:
   Assuming unmeasured input disturbance #2 is integrated white noise.
   Assuming no disturbance added to measured output channel #1.
-->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.

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);
-->The "PredictionHorizon" property of "mpc" object is empty. Trying PredictionHorizon = 10.
-->The "ControlHorizon" property of the "mpc" object is empty. Assuming 2.
-->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.

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.

  • Use 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 channel #1 is integrated white noise.
-->Assuming output disturbance added to measured output channel #2 is integrated white noise.
-->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.
-->Converting model to discrete time.
-->Assuming output disturbance added to measured output channel #1 is integrated white noise.
-->Assuming output disturbance added to measured output channel #2 is integrated white noise.
-->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.

Input Arguments

collapse all

Model predictive controller, specified as one of the following:

  • Implicit MPC controller object — To create an implicit MPC controller, use mpc.

  • Explicit MPC controller object — To create an explicit MPC controller, design an implicit controller and then use generateExplicitMPC.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

Initial controller state, 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, 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, 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, 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, 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, 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 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.

Introduced in R2016a