A Python-based demonstration of Model Predictive Path Integral (MPPI) control for navigating a simple 2D car model around multiple circular obstacles. This repository showcases how to implement MPPI to minimize a custom cost function while avoiding collisions, and includes an animated Matplotlib visualization of the resulting trajectory.
- 2D car dynamics: A simple kinematic model with constant forward velocity and steering angle control.
- MPPI planning: Generates multiple control (steering) sequences, rolls them out, computes costs, and uses weighted samples to refine control.
- Collision avoidance: Penalizes collisions with circular obstacles scattered in the environment.
- Animation: Visualizes the car’s path, obstacles, and goal state.
- Python 3.x
- NumPy
- Matplotlib
To install the required libraries via pip:
pip install numpy matplotlib
Clone or download this repository:
git clone https://github.com/taherfattahi/mppi-2d-car-navigation-with-obstacles.git
Navigate into the project directory:
cd mppi-2d-car-navigation-with-obstacles
Run the main script to see the animation:
python mppi_2d_car_navigation.py
When you run the script, a Matplotlib window will appear, showing:
- The start state (green dot)
- The goal state (red “x”)
- Obstacles (orange circles)
- The computed trajectory (blue line)
Each iteration of the MPPI planner selects a control sequence, rolls out, and updates the car’s path until the goal is reached.
-
car_dynamics(state, steering_angle, dt, v)
Simple car model that updates position and heading based on the current steering angle and velocity. -
compute_cost(states, goal, obstacles, penalty)
Calculates the cost of a trajectory, penalizing collisions and final distance to goal. -
mppi_iteration(...)
The core MPPI step: generates random control sequences, rolls them out, computes costs, and computes weighted averages. -
run_mppi_control_loop(...)
Runs multiple MPPI iterations in a loop to drive from the initial to the goal state. -
main_animation_demo()
Sets up the problem (initial state, goal state, obstacles) and runs the MPPI loop. Renders an animation of the car’s path.
- Obstacle configuration: You can add, remove, or change
obstacles
in the obstacles list. - Car speed and steering: Adjust the velocity
v
or the random steering distribution to see different behaviors. - Cost function: Change penalty values or the cost term to account for intermediate states, time cost, etc.
This project is released under the MIT License. Feel free to use, modify, and distribute for your own projects.