# Understanding Model Predictive Control, Part 7: Adaptive MPC Design with Simulink and Model Predictive Control Toolbox

From the series: Understanding Model Predictive Control

In this video, we’ll use adaptive MPC to autonomously steer a car whose lateral vehicle dynamics is changing with time due to the varying longitudinal velocity. In the previous video, we discussed the linear lateral vehicle dynamics and assumed that the car has a constant longitudinal velocity. So, the plant dynamics wasn’t changing and the state matrix A, was constant. To control this system, we used a traditional MPC controller. But now we will let the longitudinal velocity vary as the car travels. So, the state matrix A will be changing, too. A traditional MPC controller is not effective at handling the varying dynamics as it uses a constant internal plant model. So, how can we deal with the changing plant dynamics? In the part-4 video, we had discussed that adaptive MPC lets you provide a new linear plant model at each time step as the operating conditions change, therefore it makes more accurate predictions at the new operating conditions. So, to deal with the changing plant dynamics, we’re going to use adaptive MPC.

We open a new Simulink model and start by adding the plant from this custom library. As in the previous video, the plant is developed as a state space system with an input of steering angle and outputs of lateral position and yaw angle. This time its dynamics is changing with the longitudinal velocity. Therefore, this now becomes an input to the plant block. We’re going to connect a constant block for the longitudinal velocity which we’ll set to 15 m/s initially and change to another value later on. The other output is the states which we will use later. If you want to look under the blocks to see how they’re built, you can download this Simulink model from the link given in the video description. Next, we’ll connect the adaptive MPC block that is under the Model Predictive Control Toolbox. This block has the same inputs and outputs as the regular MPC block except that it also takes the plant model that is updated at each time step for the current operating conditions. Previously, we had designed a custom reference for the lateral position and yaw angle. We’ll first connect this reference to the controller. Then we connect the plant output to the measured outputs and the steering angle to the controller output. To implement the adaptive MPC, we can simply start with the MPC controller that we designed in the previous video for a longitudinal velocity of 15 m/s. We already have the MPC controller object in our workspace. By typing it in the command window, we can see the design parameters such as the prediction and control horizons, constraints and weights. One thing to note is that the adaptive MPC block requires a discrete plant model. So, we need to convert the continuous time state space model used by mpc1 to discrete time. There are different ways to do it. Here, we use the c2d command and update the plant model of the MPC object with the discretized plant. Now, we go back to the adaptive MPC block and type in the MPC object. Next, we need to provide the controller with a plant model that is updated at each time step for the current operating condition. The pre-built update plant model block takes care of this calculation. When we double click on it, we see that it has been implemented as a MATLAB function. As inputs, this function takes Vx, u and x and first calculates the state space matrices. It then computes the discrete model and also updates the nominal conditions with the current operating conditions. Now it’s time to connect the inputs and outputs for this block. We already have all the inputs here, longitudinal velocity, the steering angle and the states. The “model” input of the adaptive MPC control block requires the discrete-time model and nominal conditions in this order that we’ve created in the MATLAB function. To connect the outputs to the controller, we select the block, and create a bus signal. Now, we’re ready to try different longitudinal velocities and see how the controller handles the varying plant dynamics. In the previous video, the traditional MPC controller designed for an operating condition of 15 m/s had worked well while it failed to control the system at a different longitudinal velocity of 35 m/s. With adaptive MPC, we get a good controller performance when longitudinal velocity is 15 m/s. If we now change it to 35 m/s, we still get a good tracking of the lateral position and the yaw angle.  We can even replace this constant block with a continuously changing signal such a sine wave and see that adaptive MPC still can deal with the changing plant dynamics and successfully control the system. We designed an adaptive MPC controller, ran several simulations to evaluate the controller performance. Now if you want to run your controller on your autonomous car, you can simply generate code using Embedded Coder and deploy it to your car. Here’s the generated C code. You can call the MPC controller code from your real-time scheduler using the entry points shown in the code interface report. Embedded Coder also lets you customize the call interfaces as required by your software framework and architecture.

This video here shows an example of how generated code for an MPC controller and an image processing algorithm run on a self-driving car to keep it within the lane. The image processing and lane detection algorithms developed outside of Simulink, provide these inputs to the MPC controller. Here’s how these algorithms work. The front view of the car is captured with a camera that is mounted on the top of the car. The image processing algorithm identifies the solid and dashed line markings, and detects the lane the car is traveling in. The middle of the lane is the centerline and it is used to compute the offset of the car’s position from this line as well as the yaw angle. This information is used by the MPC controller that tries to keep the car on the centerline. The plot on the left shows the deviation of the car from the centerline with red and yaw angle with green whereas the plot on the right shows the steering angle.

In this video, we discussed how you can use adaptive MPC to control your plant with changing dynamics and also talked about how you can generate C code and deploy it for real-time control. For more information about model predictive control, check out our previous Tech Talk videos.