A comprehensive Model Predictive Control system for autonomous racing vehicles, optimizing lap time while maintaining safe track boundaries and managing battery energy.
This project implements a nonlinear Model Predictive Control (MPC) framework for autonomous race vehicles using the ACADOS optimal control solver. The system simultaneously optimizes for:
- Minimum lap time via velocity maximization
- Track stability by constraining lateral errors
- Energy efficiency by tracking battery state of charge (SoC)
The implementation uses a curvilinear reference frame based on arc-length parameterization, enabling natural path following on complex track geometries.
✅ Nonlinear Bicycle Model with realistic tire dynamics
✅ Curvilinear Coordinate System for track-relative state representation
✅ Real-time MPC Solver using SQP-RTI method
✅ Dynamic Curvature Tracking with track geometry interpolation
✅ Battery Management - SoC dynamics with drag and rolling resistance
✅ Obstacle Avoidance - Track-width constraints with obstacle handling
✅ Velocity Profile Generation - Reference speed computation based on track curvature
racing_car/
├── main.m # Full MPC closed-loop simulation with track
├── main_custom_cost.m # Variant with custom cost function
├── main_new_acados.m # Alternative ACADOS setup
├── main_off_vel.m # Offline velocity profile computation
│
├── racecar_model.m # Vehicle dynamics (7 states, 2 inputs)
├── racecar_ocp_setup.m # OCP problem definition
├── racecar_mpc_test.m # Standalone MPC test with warm-start
├── race_car_model_mod.m # Modified dynamics variant
│
├── feasibility_check.m # Constraint feasibility analysis
├── LMS_Track.txt # Track geometry data
│
├── c_generated_code/ # Auto-generated C code for solver
├── helper_functions/ # Utility functions for visualization
└── build/ # CMake build directory
x = [s; vx; vy; wz; ye; theta_e; SoC]
s - Progress along track (arc-length) [m]
vx - Longitudinal velocity [m/s]
vy - Lateral velocity [m/s]
wz - Yaw rate [rad/s]
ye - Lateral track error [m]
theta_e - Heading error relative to track [rad]
SoC - Battery state of charge [%]u = [delta; a]
delta - Steering angle [rad] (constrained: -0.6 to 0.6)
a - Longitudinal acceleration [m/s²]| Parameter | Value | Unit |
|---|---|---|
| Mass | 1.8 | kg |
| Yaw inertia | 0.03 | kg⋅m² |
| Front axle distance | 0.125 | m |
| Rear axle distance | 0.125 | m |
| Front cornering stiffness | 68 | N/rad |
| Rear cornering stiffness | 71 | N/rad |
| Drag coefficient | 0.35 | - |
| Tire friction coefficient | 1.2 | - |
The vehicle follows a nonlinear kinematic-dynamic bicycle model with:
-
Lateral Tire Forces (Pacejka-inspired):
Fy_f = Cf × (δ - vy/vx + lf·ωz/vx) Fy_r = Cr × (-vy/vx - lr·ωz/vx) -
Longitudinal-Lateral-Yaw Dynamics:
vx_dot = a - (Fy_f·sin(δ))/m - μ·g + ωz·vy vy_dot = (Fy_f·cos(δ) + Fy_r)/m - ωz·vx ωz_dot = (Fy_f·lf·cos(δ) - Fy_r·lr)/Iz -
Curvilinear Track Dynamics:
s_dot = (vx·cos(θe) - vy·sin(θe)) / (1 - ye·κ) ye_dot = vx·sin(θe) + vy·cos(θe) θe_dot = ωz - κ·s_dotwhere κ is the track curvature parameter.
-
Battery Energy Model:
P = ½·Cd·ρ·A·vx² + μ_rr·m·g·vx (aerodynamic + rolling losses) SoC_dot = -(P / C_alpha) (discharge rate)
Prediction Horizon: N = 50 steps, T = 1.0 s total (Δt = 0.02 s per step)
Cost Function (Linear-Least Squares form):
J = Σ ||y_k - y_ref_k||²_W + ||y_N - y_ref_N||²_W_e
y_k = [x_k; u_k] = [s, vx, vy, ωz, ye, θe, SoC, δ, a]
State Weights (Q):
- s: 1.0 (arc-length tracking)
- vx: 1e-8 (encourage high speed)
- vy: 1e-2 (lateral stability)
- ωz: 1e-2 (yaw damping)
- ye: 1e-8 (tight lateral error tracking)
- θe: 5e-3 (heading alignment)
- SoC: 5e-1 (energy preservation)
Input Weights (R):
- δ: 1e-4 (smooth steering)
- a: 1e-3 (smooth acceleration)
% Steering angle bounds
-0.6 ≤ δ ≤ 0.6 [rad]
% Lateral error bounds (track-width dependent)
ye_min ≤ ye ≤ ye_max [m]
% Obstacle avoidance in curvilinear coordinates
track_bounds_with_obstacle(s, width, s_obs, ye_obs, obs_w, obs_L)| Setting | Value |
|---|---|
| NLP Solver | SQP-RTI |
| QP Solver | Partial Condensing HPIPM |
| Integrator | Explicit Runge-Kutta (4 stages, 3 steps) |
| Tolerance | 1e-4 |
| Hessian | Gauss-Newton (exact off) |
Full closed-loop racing simulation with:
- Track geometry loading from
LMS_Track.txt - Curvature interpolation and smoothing
- Velocity profile generation
- Dynamic obstacle handling
- Live trajectory visualization
- Lap time computation
Typical runtime: ~10 seconds closed-loop simulation
Alternative cost function formulation for different optimization objectives.
Experimental ACADOS interface with different problem setup approach.
Offline velocity profile generator - pre-computes optimal speed at each track position based on track curvature and vehicle limits.
Defines the vehicle dynamics model using CasADi symbolic math:
- State vector definition (7 states)
- Input vector definition (2 inputs)
- Explicit dynamics formulation
- Parameter setup (track curvature κ)
- Returns ACADOS-compatible model structure
Configures the Optimal Control Problem:
- OCP model definition
- Solver options
- Constraint setup
- Returns ACADOS OCP solver object
Standalone MPC testing function with:
- Reduced state space (6 states, no arc-length)
- Warm-start from previous solution
- Real-time solver performance metrics
- Live 6-panel visualization
Analyzes constraint feasibility - checks if vehicle can satisfy all constraints given vehicle limits.
Modified vehicle dynamics variant - alternative model formulation.
Track geometry definition with 250+ waypoints:
s(m) x(m) y(m) psi(rad) kappa(rad⁻¹)
Defines a closed loop track with straight sections and high-curvature turns.
MATLAB Toolboxes:
- MATLAB R2020b or later
- ACADOS (with MATLAB interface)
- CasADi (for symbolic computation)
Installation:
# Clone ACADOS
git clone https://github.com/acados/acados.git
cd acados
git submodule update --recursive --init
# Build with CMake
mkdir build && cd build
cmake ..
make install
# Install MATLAB interface
cd ../interfaces/acados_matlab_octave
make clean
makeFull MPC Simulation (recommended):
>> mainOutputs:
- Lap time completion
- 4-panel trajectory plots (speed, track error, SoC, inputs)
- 2D track visualization with acceleration heatmap
MPC Testing:
>> racecar_mpc_test()Outputs:
- Real-time 6-panel solver performance
- Warm-start effectiveness metrics
Offline Velocity Profile:
>> main_off_velGenerates optimal reference velocity based on track curvature.
The simulation generates comprehensive results visualizing the MPC controller performance across multiple dimensions:
Nonlinear bicycle model showing vehicle states, tire forces, and coordinate systems
2D track visualization with optimal racing line, vehicle trajectory, and acceleration heatmap showing high-speed zones
Velocity profile over time demonstrating acceleration and braking sequences along the track
Lateral position error (ye) relative to the reference racing line, confirming constraint satisfaction
Commanded longitudinal acceleration from the MPC solver, showing smooth control transitions
Steering angle commands during the lap, demonstrating smooth steering transitions during cornering
Battery state-of-charge dynamics during lap execution, illustrating energy-aware control management
- Constraint Satisfaction: Lateral tracking error remains within defined bounds throughout the entire lap
- Smooth Control: MPC produces smooth steering and acceleration commands via SQP-RTI solver
- Energy Efficiency: Battery management successfully tracks SoC while maintaining lap time performance
- Solver Performance: ~5-20 ms computation time per control step enables real-time execution
- Track Completion: Full lap circuit closure with consistent reference tracking
- Lap completion: Track circuit closure detection
- Lap time: Computed from arc-length and velocity
- Constraint satisfaction: ye bounds checking
- Solver statistics: Iterations, computation time per step
- SQP-RTI solver: ~5-20 ms per MPC step
- Warm-start recovery: 2-3 iterations typical
- Feasibility: 99%+ satisfaction rate
kappa_scaled = kappa_raw / 10;
kappa_smooth = movmean(kappa_scaled, 7); % Moving average filter
kapparef_s = interpolant('kapparef_s', 'bspline', {s_ext}, kappa_ext);Pre-computes longitudinal speed reference based on:
- Track curvature
- Vehicle lateral acceleration limits (μ·g)
- Tire friction constraints
Dynamic track-width constraints in curvilinear coordinates:
[ye_min_s, ye_max_s] = track_bounds_with_obstacle(s, track_width, s_obs, ye_obs, obs_w, obs_L)Reuses previous MPC solution for:
- Faster solver convergence
- Real-time performance
- Consistent control
- Multi-vehicle racing - Add inter-agent constraints
- Real tire model - Integrate Pacejka tire model
- Slip control - Add wheel slip dynamics
- Road friction - Adaptive μ based on track surface
- Wind effects - Lateral wind disturbances
- Hardware integration - ROS2 interface for real vehicles
- Advanced solvers - iLQG, DDP for faster computation
| Issue | Solution |
|---|---|
| ACADOS solver not found | Verify installation path in MATLAB |
| Track data loading fails | Check LMS_Track.txt path and format |
| Constraint infeasibility | Relax ye bounds or increase δ_max |
| Slow convergence | Increase horizon N or adjust weights |
| Simulation diverges | Reduce acceleration limits or dt |
- ACADOS: https://github.com/acados/acados - Fast optimal control solver
- CasADi: https://casadi.org - Symbolic computation framework
- Curvilinear MPC: Race car trajectory optimization using track-relative coordinates
- Nonlinear Bicycle Model: Standard vehicle dynamics in autonomous racing
Created by: bhimray
Repository: https://github.com/bhimray/racing_car
Last Updated: December 2025
This project is provided as-is for research and educational purposes.
| Scenario | Script | Output |
|---|---|---|
| Full racing MPC | main.m |
Lap time, trajectory plots |
| MPC performance | racecar_mpc_test.m |
Solver metrics, stability plots |
| Velocity optimization | main_off_vel.m |
Reference speed profile |
| Constraint analysis | feasibility_check.m |
Feasibility report |
For detailed documentation on dynamics, constraints, or ACADOS setup, refer to the inline comments in each MATLAB file.