Step-by-step Python examples for the CARLA simulator (version 0.9.15) demonstrating vehicle control algorithms, sensor usage, and visualization techniques. This project aims to provide a clear progression from basic CARLA interactions to more complex scenarios involving waypoint following, PID speed control, Stanley steering, obstacle detection, and real-time plotting.
This repository provides demo code for the following publications:
- Build a Self-Driving Car in Carla Simulator with Python (Step-by-Step) (Medium.com)
- Hello self driving world! (Carla Simulator) (Habr.com)
This project demonstrates:
- Connecting to the CARLA simulator.
- Spawning and removing vehicles (
vehicle.tesla.model3,vehicle.audi.tt). - Setting spectator camera views (fixed and relative).
- Basic vehicle control (constant throttle).
- Waypoint definition and following logic.
- Longitudinal speed control using a PID Controller.
- Lateral steering control using the Stanley Method.
- Basic obstacle detection using
sensor.other.obstacle. - Emergency braking upon obstacle detection.
- Real-time visualization:
- Pygame: Displaying RGB camera feed attached to the vehicle.
- Matplotlib: Plotting vehicle trajectory (X-Y), X-coordinate vs. time, speed vs. time, and detected obstacle positions.
- Resource management and cleanup.
The project is organized into sequential examples (sd_1 to sd_7) located in their respective folders, demonstrating increasing complexity:
sd_1/: Basic vehicle spawn and fixed spectator setup.sd_2/: Constant vehicle throttle and smooth spectator following.sd_3/: Integration of Pygame camera display and basic Matplotlib plotting (X-coord, Speed).sd_4/: Simple waypoint following with a basic 3-state speed controller.sd_5/: Waypoint following using a PID controller for speed regulation.sd_6/: Waypoint following with PID speed control and Stanley steering control, including path plotting.sd_7/: Adds obstacle detection (sensor.other.obstacle), spawns a static obstacle, and implements emergency braking logic, showing the obstacle on the plot.tools/: Contains reusable helper modules:pid_controller.py: PID controller implementation.pygame_display.py: Manages the Pygame camera view window.plotter_x.py: Matplotlib plotter for X-coordinate and speed.plotter_path.py: Matplotlib plotter for X-Y trajectory and speed.plotter_path_obstacle.py: Matplotlib plotter including obstacle visualization.
- CARLA Simulator: Version 0.9.15. Download from the official CARLA releases. Ensure the simulator is running before executing the scripts.
- Python: Version 3.8
Ensure the CARLA simulator (version 0.9.15) is running.
Navigate to the repository's root directory in your terminal. You can run each example script as a Python module:
-
Example 1 (Basic Spawn):
python -m sd_1
Spawns a vehicle and sets the spectator view. Close with Ctrl+C.
-
Example 2 (Constant Throttle, Spectator Follow):
python -m sd_2
Vehicle moves forward, spectator follows. Close with Ctrl+C.
-
Example 3 (Pygame Display, Basic Plot):
python -m sd_3
Shows Pygame window and Matplotlib plot (X-coord, Speed). Close the Pygame window or press ESC to exit cleanly.
-
Example 4 (Simple Waypoint Following):
python -m sd_4
Follows waypoints using a basic speed controller.
-
Example 5 (PID Speed Control):
python -m sd_5
Follows waypoints using PID for speed control.
-
Example 6 (PID + Stanley Steering):
python -m sd_6
Uses PID for speed and Stanley for steering, plots X-Y trajectory.
-
Example 7 (Obstacle Avoidance):
python -m sd_7
Adds a static obstacle, uses an obstacle sensor for detection, and implements emergency braking. Plots include the obstacle position.
Press ESC or close the Pygame window (in examples sd_3 to sd_7) to stop the simulation gracefully and clean up spawned actors. Use Ctrl+C as a fallback, but it might leave actors in the simulation.
futurenumpy; python_version < '3.0'numpy==1.18.4; python_version >= '3.0'pygamematplotlibopen3dPillowcarla
(See requirements.txt for details)
This project is licensed under the MIT License - see the LICENSE file for details.