How to Simulate Trajectory Tracking Controllers for Driverless Cars Using MATLAB
Trajectory tracking plays a vital role in autonomous vehicles, where the ability to follow a predefined path with precision is essential. To maintain control over steering and speed across various driving conditions, advanced algorithms are required. MATLAB and Simulink provide a robust environment for simulating these systems, particularly for students and engineers developing control strategies for self-driving car models.
This blog walks through a practical approach to simulating trajectory tracking controllers using MATLAB. The workflow begins with generating a reference trajectory, followed by building a dynamic vehicle model that responds to control inputs like steering angle and acceleration. Then, three popular controllers—Pure Pursuit, Stanley, and Model Predictive Control (MPC)—are implemented to follow the path.
Each controller offers unique advantages depending on vehicle speed, path complexity, and system constraints. These simulations not only help visualize the vehicle’s performance but also offer valuable insights into controller tuning and optimization. Whether you're working on a university project or need assistance with MATLAB assignment tasks related to autonomous systems, understanding these controllers can be incredibly helpful. This blog aims to provide a strong foundation for building your own simulations and gaining deeper knowledge of vehicle path tracking using MATLAB and Simulink.
Understanding the Importance of Trajectory Tracking
The motivation behind trajectory tracking is simple—it ensures that an autonomous vehicle can maintain its path while adapting to driving conditions. Whether it's racing in competitions like Formula Student Driverless or navigating everyday roads, the vehicle needs to plan and follow paths accurately and at variable speeds. The accuracy of the tracking controller directly affects the vehicle's stability and ability to avoid collisions or off-path deviations. In our discussion, we focus on three major control strategies, namely Pure Pursuit, Stanley, and MPC, which offer different advantages based on performance demands. The process starts by establishing a reference trajectory for the vehicle to follow.
Creating a Reference Trajectory
To simulate a realistic driving scenario, we first need a reference trajectory. If you already have recorded vehicle pose data, you can use that directly. However, if not, MATLAB provides a useful tool—the Driving Scenario Designer app—to help you generate one from scratch. Using this app, we selected a portion of the US Highway from the available scene library and configured a virtual vehicle to travel along a specific path. This synthetic trajectory was then exported into a .mat file. The path data becomes the basis for the controllers to follow during the simulation and is used consistently across all models we build throughout the project.
Building the Vehicle Dynamics Model
Next comes building the vehicle model, which is critical for understanding how control inputs affect motion. We used MATLAB's Vehicle Dynamics Blockset to construct a 3DOF dual-track model that includes tire dynamics and a basic powertrain. The model simulates both longitudinal and lateral dynamics, allowing us to evaluate the vehicle’s behavior under various control strategies. The accuracy of this model plays a major role in determining how well a controller performs, especially under different speeds and turn conditions. This same model will be used for simulating all three controllers to maintain consistency in the results.
Pure Pursuit Controller
We began the controller implementation with the Pure Pursuit method, which is intuitive and relatively easy to set up. This algorithm computes the curvature of the path and uses a lookahead point to determine the required steering angle. The lookahead distance is a tunable parameter that greatly influences performance—a smaller value improves precision but may cause oscillations, while a larger value reduces responsiveness. In Simulink, we implemented the controller using the Pure Pursuit block, which calculates the steering angle based on vehicle pose and trajectory input. By adjusting the lookahead distance, we observed the vehicle successfully tracking the reference path with reasonable accuracy and stability.
Stanley Controller
Following Pure Pursuit, we explored the Stanley Controller, known for its successful application in real-world challenges like the DARPA Grand Challenge. Unlike the previous method, Stanley focuses on reducing both cross-track and heading errors, making it suitable for higher-speed environments. The Lateral Controller Stanley block in Simulink allows configuration based on vehicle dynamics. We set it up using the same reference trajectory and vehicle model. The controller’s parameters—such as position gain and yaw rate gain—were fine-tuned to improve tracking quality. Simulation results showed that the Stanley Controller maintained close adherence to the path, particularly effective at higher velocities like 30 m/s.
Model Predictive Controller (MPC)
The final control strategy we tested is Model Predictive Control, a more advanced and computation-heavy method. MPC works by solving an optimization problem at every time step to compute future control inputs while satisfying system constraints. In Simulink, we initially started with the generic MPC Controller block but later used the Path Following Control System block, which offers better integration with vehicle models for this application. We customized the block to follow our reference trajectory by configuring set velocities and feeding curvature data. After choosing appropriate prediction and control horizons and connecting the output to a pedal map, we observed that MPC delivered smooth and highly accurate trajectory tracking, particularly useful for multivariable constrained systems.
Visualizing Results in Simulink
To assess controller performance, we used Simulink’s visualization capabilities to compare the vehicle’s actual trajectory with the reference path. The models were run under the same conditions, and the outputs—including steering angles, lateral error, and velocity—were plotted for analysis. These visualizations made it easy to evaluate which controller worked best under various scenarios. For instance, the Pure Pursuit controller was quick and simple but struggled with high-speed accuracy. The Stanley Controller offered better control at higher speeds, and MPC provided the most refined tracking, especially when dealing with constraints and complex dynamics.
Comparing the Controllers
From our simulations, it's clear that each controller has its strengths. Pure Pursuit is ideal for low-speed or basic applications and offers a good starting point for beginners. Stanley performs better at higher speeds and is suitable for more advanced dynamic situations. MPC is the most flexible and powerful, especially when you need to manage multiple inputs, outputs, and system constraints. However, its computational cost makes it more suitable for applications where resources and processing time are not a major concern. Choosing the right controller depends on the specific needs of your simulation or real-world project.
Get Started with Your Own Models
This overview should give you a clear idea of how to simulate and implement trajectory tracking controllers in MATLAB and Simulink. You can now take this foundation and start building your own models. Experiment with different trajectories, tune controller parameters, and challenge your system with complex curves and variable speeds. This not only builds your MATLAB skills but also deepens your understanding of autonomous vehicle dynamics. If you're working on a university assignment, applying these methods can provide you with a well-rounded and technically sound project that showcases practical application of control theories.
Final Thoughts
Simulating trajectory tracking controllers using MATLAB and Simulink opens the door to understanding and designing complex autonomous driving systems. Through reference trajectory generation, vehicle modeling, and controller implementation, you can build powerful simulations that mirror real-world scenarios. Each controller—from Pure Pursuit to Stanley to MPC—offers different strengths and applications. By reusing the same vehicle model and trajectory, you can compare their performance directly and decide which best fits your use case. The tools provided by MATLAB make it easier than ever to create, test, and refine these systems, providing valuable experience for engineering students and researchers alike.