# getCodeGenerationData

Create data structures for `mpcmoveCodeGeneration`

## Syntax

## 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`

.

`[`

creates data structures for use with `configData`

,`stateData`

,`onlineData`

]
= getCodeGenerationData(`mpcobj`

)`mpcmoveCodeGeneration`

.

`[___] = getCodeGenerationData(___,`

specifies additional options using one or more `Name,Value`

)`Name,Value`

pair
arguments.

## Examples

### Create MPC Code Generation Data Structures

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.

### Specify Options for Creating MPC Code Generation Structures

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.

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

`mpcobj`

— Model predictive controller

`mpc`

object | `explicitMPC`

object

Model predictive controller, specified as one of the following:

`mpc`

object — Implicit MPC controller`explicitMPC`

object — Explicit MPC controller created using`generateExplicitMPC`

.

### Name-Value 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.`DataType`

— Data type used in generated code

`'double'`

(default) | `'single'`

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.

`OnlyComputeCost`

— Toggle for computing only optimal cost

`false`

(default) | `true`

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`

.

`IsAdaptive`

— Adaptive MPC indicator

`false`

(default) | `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.

`IsLTV`

— Time-varying MPC indicator

`false`

(default) | `true`

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.

`UseVariableHorizon`

— Variable horizon indicator

`false`

(default) | `true`

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

`configData`

— MPC configuration parameters

structure

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.

`stateData`

— Initial controller states

structure

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.

Field | Description |
---|---|

`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 |

`onlineData`

— Online MPC controller data

structure

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

Field | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|

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

`limits` | Input and output constraints, returned as a structure with the following fields:
When | ||||||||||||

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

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

`horizons` | Updated controller horizon values, returned as a structure with the following fields:
The When
| ||||||||||||

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

`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`

.

## See Also

**Introduced in R2016a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)