Vehicle Path Tracking Using Model Predictive Control
From the series: Improving Your Racecar Development
Learn how to implement model predictive control for path tracking and the steps needed to control the path of an autonomous vehicle. Create waypoints using the Driving Scenario Designer app and build a path-tracking model in Simulink® using Automated Driving Toolbox™ and Vehicle Dynamics Blockset™. See how you can visualize and compare the vehicle’s trajectory in 2D, 3D, and bird’s-eye-view. Discover how to simulate a three-degrees-of-freedom (3DOF) vehicle driving on a virtual US Highway.
You can find the example models used in this video here: Vehicle Path Tracking Using Model Predictive Control (MPC).
Published: 16 Mar 2022
Hi, everyone. Welcome to MATLAB and Simulink Racing Lounge. In today's video, we are going to talk about vehicle path tracking using model predictive control. And to talk about this, we again have our guest, David Barnes. So David, why don't you give an overview of what you're going to present today?
Certainly. Let's go ahead and take a look at the video.
So we have this car that's driving around in our unreal environment being controlled laterally and longitudinally as it comes around the curves going back towards the right originally and then back towards the left as it's following the road.
That's great, David. I think we are talking about the implementation of model predictive control for this path tracking application, and just to mention that we are not going in-depth for the understanding of model predictive control. Rather, we would strongly recommend that you go through the MATLAB Tech Talk titled, Understanding Model Predictive Control, which is a series of seven videos, and you'll get an understanding why MPC, what is MPC, and even the topic of the series also covers regarding tuning of design parameters needed for model creative control.
And to find the link for the series, you can check the video description. And with this over to you, David. We can now start with the agenda part for the video.
Certainly. So today, we've talked a little bit about an introduction to MPC, but we'll talk a little bit further into how is it working in terms of our model, and then we'll actually get to look at the implementation. Going through, looking at how we're generating waypoints, building the models in Simulink, and then visualizing the vehicle motion again, and then we'll wrap up with some of the key takeaways. MPC controller is a really unique controller insofar as that it's an optimization-based problem, which means that this is getting solved at every time step of the simulation.
However, it offers a lot of benefits to us as controllers engineers. It allows us to put constraints on our model, and it does this by taking a look at an on board small plant model. This is implemented either with a linear or a linear model, and one of the downsides to this controller is that it is computationally expensive to put on board.
Yeah. And just to mention, David, MPC also tackles the multiple input and multiple output systems very well, so yeah, let's move to the implementation part.
The example that we're going to walk through with you today, we're going to start with some waypoints that we've generated using Driving Scenario Designer, then we're going to take those and use them to create and condition the inputs for our controller as well as load in any vehicle data that's needed for our vehicle and controller to be implemented in Simulink, and finally, we'll get to visualize how our vehicle is performing in birdseye scope 2D and 3D environments. So let's get started with MATLAB.
We're going to get started by taking a look at this live script to walk through a simple example of using our controller in a figure eight course. One of the first things we're going to do is we're going to add some images to the paths so that we can use those in our model. We're also then going to define the reference points of this figure eight circle as it's going to go around each side twice, then we're going to load in some of the vehicle parameters that's used in the models, and then we're going to condition all of our inputs that we're going to need to be able to run this figure eight course.
Next, we're going to condition all of our inputs for us to be prepared for the figure eight course. As we mentioned earlier, the MPC uses a linear model or a non-linear model when actually doing the optimization on a step-by-step basis. In this case, we have the equations from the documentation to calculate A, B, C matrices that can be used for this.
All we're doing here is creating this linear time invariant system that you've seen in state space equations. Next, we'll then create this epic pedal map which will be used to condition some of our inputs. We'll talk about this a little bit more when we go through the Simulink model itself, but the important thing here is that it's going to be based on the balances of forces based off of simple vehicle dynamics models.
So I mean, what it means to it is that we already have all the required data in the workspace, and now we are heading towards a Simulink model where we are using all these initial data.
Yes, exactly. Let's go ahead and get it opened up. Some of you may already have a Simulink based vehicle model. In this case, we're going to show you how we have this model set up, where we have our vehicle powertrain and driveline model set up so that it accepts pedal commands and then outputs the forces that are applied to our vehicle body. In this case, we're going to simulate that body using the three degree of freedom dual track model.
Let's take a look at this vehicle model. In this subsystem, we've modeled a simple motor and differential, as well as a braking system so that we can then accurately calculate our forces that are applied to our vehicle body through the tires.
So David, can you get into that brakes and wheel subsystem?
Certainly.
And here, it is worth mentioning that the tire of what you see over here that has been taken from vehicle directed blocks it. OK, so let's go back and explore the model. Now that we have our vehicle model, we can take a look at our inputs and outputs. The outputs that we're going to use for our controller or the steering angle that we're going to apply to the front wheels, as well as the accelerator and decelerator pedal commands.
Coming out of our vehicle body block, we're able to get not only the vehicle velocity but also its pose off of the bus. With these signals coming through as well as a reference velocity we want to drive for our vehicle, we can go ahead and take a look at our controller subsystem. Now that we're coming in to the controller subsystem, let's take a look at some of the signals that we're using.
Starting at the top, we have a vehicle reference velocity that's coming in, which is from a constant block. Then we connect our vehicle velocity. Using our live script, we had created a lookup table based on the vehicle's position to know what the curvature we were looking for, and then we're using the sensor dynamics block from the lane keeping system using model predictive control example.
So this block basically is used to output the lateral deviation and the relative angle?
Yes. The heart of this controller is, in fact, the MPC controller. In this case, we're using the path following control system from the MPC control toolbox. Let's go ahead and take a look at the block parameters.
By using this block, all we have to do is input our vehicle parameters. In this case, we're going to set the mass the vehicle, the inertia, the distance from the tires to the center of gravity, as well as the cornering stiffness. If you wanted to, you could use the vehicle model like we had specify before in specified A, B, and C matrices.
So it means that in the live script, the ABC matrix is what we have calculated we can feed it over here, right?
Exactly. Let's actually go ahead and take a look at the documentation for this block. By looking at the description of the block, we can get a little bit of a sense of how this block is being used. In this case, it's used to control, not only the longitudinal, but also the combined lateral dynamics of the vehicle.
It does this by looking at the ports. We can look at some of the specifics for the ports that we have here and the different options that are available and we can also look at the output ports. But in this case, we want to take a look at some of the specifications for the output ports. As we look here, we can see that the longitudinal acceleration is what's actually implemented in our controller, and the signal is in meters per second squared and the steering angle is actually the angle of the front tires on the vehicle itself.
Taking a look through we can see what all of the parameters that are available for us to use with this model, then we can take a look at some of the parameters on how this block is being used and take a look at some examples. In this case this is the model that is used for the online optimization. This is where we got our equations that we use to set up our ABC matrices and our live script.
As you can see, this does this in stages, where at first finds the state space equations for the longitudinal model then for the lateral model. And finally, combines them together. In this case, this is showing you how the vehicle parameters that we put in are then going to map to the matrices that we have. You can always define a custom model if you need to.
As we found from the documentation, this longitudinal acceleration is in meters per second squared. However, for a vehicle model, we're expecting a pedal input. There are definitely different ways for you to change this command, however, in this case, we decided to use the lookup table. This is because it's really popular for using calibration workflows.
Let's go ahead and take a look at the table that we created in the live script. Looking at this pedal map, we can then come to the Edit table and breakpoints, and then we can definitely take a look through some of the values here, but sometimes it's best to just take a look at plot. As we mentioned before, this is a really simple map, and you can definitely play around with the points and do calibration workflows or change the algorithm that's underlying for the different outputs that you need.
In this case, we had swept through the vehicle velocity, as well as the command and accelerations for us to get the table data. In this case, it's the output of pedal command.
So David, over here, what we can see is that it's a very simple little map. But depending upon the vehicle requirements, you can have more complex and then we can replace that with a complex 2D lookup table. Yes. The output of our lookup table is from negative 1 to positive 1, so the first thing that we do is split the signal up between its positive and negative values, and then condition the input so that it's ready to go into our vehicle model.
Great. So why don't we run the model, David, and see how it is tracking. The figure eight cause.
Yeah. Now, let's go ahead and run the simulation. Here, we can take a look at the vehicle as it goes around the figure eight. It's going to go around each side of the circle twice.
So it is tracking very well I would say.
Yes, definitely.
OK, so that's perfect, David. I think one of the reasons for this perfect tracking is also the proper selection of design parameters. So if you can get into the block again and show the viewers where they can define the design parameters, that would be really helpful.
Definitely. Let's go back and take a look at the block.
Here, we had already gone through and set up some of the parameters for the vehicle, but let's take a look at the actual controller settings. Here, going through the MATLAB Tech Talk on MPC control is going to be really helpful for making sure that you're defining these constraints and setting the model predictive controllers settings properly.
Yeah, I must say that one of the videos which talks about the design parameters is really helpful to understand and decide these variables.
Yeah, OK. Perfect. So now that we've taken a look at this simple example, let's look at one that's a little bit more interesting. So for this example, let's go ahead and get started with driving Scenario Designer.
So since we already have our file, we can go ahead and open it and load it into driving Scenario Designer. In this case, we're going to click on our US Highway map and go ahead and let the scene populate into the Canvas.
So meanwhile David it is loading, so we also wanted to share that this is a pre-built scene. And similarly, we have other scenes as well, which the viewers can leverage.
Yes, definitely. As you can see, we have a very large map, but this mimics what we have in our Unreal Visualization tool as well. We'll get a little bit more into that once we open the Simulink model.
And just to summarize, in the first part of what we did to generate the figure eight course, we just used basic mathematical equations and over here, we are leveraging the driving Scenario Designer to generate the waypoints using a pre-built scene.
Yes, that's correct. Let's go ahead and take a look at our live script. From here, we're going to have a very similar setup that we have previously, where we set up the images that we're going to use, and instead of generating those waypoints like you said before, we're going to read those in from a map file, defining our reference points, then taking a look at some of our vehicle parameters, and then calculating all of the necessary inputs for our controller once again.
So as you can see, we add our images to the path, then we load in the scene file, as well as grab some of the data from that map file that's there and defining those reference positions, taking in our vehicle parameters for the model, and then calculating all of our necessary inputs. And the rest of this live script is identical to the one we had before, so we'll go ahead and run through that now. Let's go ahead and take a look at the model.
This model is almost identical to the one that we had before, where our controller and vehicle model remain unchanged, but instead of doing a constant vehicle velocity that we're trying to follow, we're instead doing this through a velocity distribution lookup table. And finally, we added another area for our visualization, where we have, again, our 2D plotter. We've also added in our visualization using Unreal.
This is set up using the 3D scene configuration block. As we mentioned, there are a lot of different options that you have for default scenes. In this case, we're using the US Highway. There are a lot of options that you can choose from. However, because this matches the scene that we're using, we're going to go ahead and use the US Highway.
So let's go ahead and simulate this model. With the model up and running, we're able to watch the visualization of this vehicle, both in the 2D version, as well as an Unreal in a 3D environment. In this case, we're able to watch as the vehicle controls itself both laterally and keeping the specified velocity.
Yeah, that that's great, David, because also to mention that, because of these two blocks a simulation 3D vehicle with ground following and simulation 3D scene configuration, it's very easy to set up this whole Simulink model with Unreal. So going ahead with that, so do we have a possibility to create a custom scene and a custom vehicle to run with the Simulink model?
Yeah. Using Simulink, you can actually connect into Unreal, using custom environments that you can set up as custom executables or running this through the Unreal Engine itself, as well as putting in custom actors such as vehicles or doing other systems if you want to do as well.
Yeah, and I think what we have done is to get a more proper understanding of the workflow of custom scene and custom vehicle we have provided the documentation link in the video description. So David, can you also share the examples for model predictive control for automated driving applications?
Yeah, definitely. Going in, all we have to do is just type in demos into the command window to pull up some of the documentation that's there. This is a great place for you to find examples for all of our different toolbox, but in this case, let's take a look at some of those for model predictive control. Looking here, you can see that there's some in different industries and things, but we can also take a look at those that are specifically for automated driving. One of these 17 examples might be able to help you in your next application.
Oh, great. I mean, there are plenty of examples over here.
Exactly.
So thanks, David, for demonstrating these two models, and if you're interested to explore this, we have provided the link to the GitHub repository you can download the models from there. Now, let's check out the key takeaways.
In this example, we walk you through how to set up your reference signals, generate petal maps as well, as creating the state space model for your PC controller. We also talked a little bit about selecting the right parameters and how it's important for PC controllers and how the MATLAB Tech Talks can help you as you're getting started with this new algorithm, and of course MATLAB and Simulink providing various algorithms for generating your waypoints, integrating your vehicle model, as well as your controller and visualizing your system in unreal or other various environments.
OK, that summarizes all the important points of it. If you want to get in touch with us, if you have any queries you can reach out to us at racinglounge@mathworks.com. So with this, we come at the end of the video, and thanks, David, for showcasing how to implement MPC control for vehicle tracking applications. So yeah, thank you.
Thanks for having me.