In this video, you will see how in release 2022b,
you can design and simulate a nonlinear
Model Predictive Controller that uses deep-learning based
internal prediction model.
This internal prediction model is a neural state space,
where nonlinear functions f and g,
and our state space equations will be neural networks
that will learn from data.
In this example, we will control a pendulum.
Pendulum dynamics is nonlinear The equations are well known.
We could write those equations in MATLAB,
and use them as nonlinear prediction model for our MPC.
However, the point of this example
is to show you the new capabilities that
would come handy if you don't know the nonlinear
dynamics of the system you want to control,
but could obtain measurements of system input states
and outputs, from either hardware experiments
or from a high-fidelity simulation model that is not
suitable for use as MPC internal prediction model.
So let's look at the system input states and outputs.
Torque is the input to the system.
System states are pendulum angle and angular velocity.
And the outputs that we measure are point mass, horizontal,
and vertical positions.
Vertical position is 0 when angle is zero, and is greater
than 0 otherwise.
The pendulum is undamped.
So if you let it swing from non-zero angle,
it will keep oscillating.
We will design the controllers that
will bring the pendulum to stable equilibrium
while respecting bounds on system input and states.
The workflow we will follow is simple.
It will load the input, output, and state data we collected.
Then, we will define our neural state space model,
and we'll train it from data.
We will generate state and output functions
required by our nonlinear MPC.
We will design our controller.
We will simulate it in Simulink.
And we will generate C code for embedded implementation.
So let's get going OK, first we load the data.
Will use some of the data for training,
and some for validation.
Next up is defining and training a neural state space model.
Training a neural network is not necessarily something
that control and system engineers are familiar with.
So the API you will see here was designed
to be accessible to people who are not deep learning experts.
We start with defining a neural state space model.
In our case, it has two states, one input, and two outputs.
We then configure state and output neural networks,
with specified training options for state network
and for output network.
We specify validation data we want to use.
And then we train.
In this one line of code, we train both the state and output
neural networks.
And that's it.
As you can see, the code is very simple.
So let's start training.
It takes about five minutes.
During the training, we can see training [? laws ?]
improving for both state and output networks.
Also we see how the networks we are learning
do on validation data we provided.
Our next step is to generate representations
of state and output functions that we
can provide to our nonlinear Model Predictive Controller.
Once we run that section of the script,
we can look at autogenerated functions.
Here we can see a state function, stage equipment
function, output function, and output function, Jacobian.
All these functions support code generation.
Stage equipment and output Jacobian functions
help us significantly speed up the execution speed
of Nonlinear MPC, as Jacobians don't
need to be computed using numerical perturbation.
We are now ready to design a nonlinear MPC.
Our controller uses two states, two outputs, and one input.
We specify sampling time, next.
We also specify bonds on manipulated variables
and states.
Next, we instruct our controller to use autogenerated state
output and Jacobian functions we just looked at.
We didn't specify prediction and control horizon
so default values will be used.
We can now simulate our nonlinear MPC in Simulink.
The pendulum subsystem takes torque provided by MPC
and produces outputs we measure.
From those outputs, we compute state estimates
using an observer, and feed those estimates
to our controller.
We can simulate our closed-loop model
and look at simulation results in the Data Inspector.
We see outputs in the upper plot showing us
that our controller successfully brings a pendulum
to the equilibrium position.
Torque requested by the controller
is in the lower plot.
And we can see MPC respects the bounds we specified.
After validating controller in Simulink,
we can generate C code for embedded implementation.
To recap, we showed how you can use deep learning
to train an internal prediction model for nonlinear Model
Predictive Controller.
You can use this neural net based model
to design the controller, simulate it, and generate
C code for embedded implementation.
And this concludes the video.