This repository provides a full pipeline that enables autonomous driving on a hatchback vehicle in Gazebo Sim, from environment mapping to autonomous navigation. The vehicle is equipped with a roof-mounted GPU LiDAR with a 10Hz frequency and 360° visibility. Users can manually operate the vehicle via a keyboard interface to explore the Gazebo world we built for this project.
With the generated map, the vehicle can calculate an optimal trajectories to reach user-defined goals using the MPPI (Model Predictive Path Integral) controller. The vehicle will move autonomously based on the predicted trajectory.
Note
This project represents the final project for the Automated Systems course at Huazhong University of Science and Technology (HUST). You can find my personal ROS 2 study notes here.
Pick a ROS 2 release that is compatible with your Ubuntu version. For Ubuntu 24.04, install Kilted Kaiju. This is the targeted version for this repository. For long-term support on Ubuntu 24.04, install Jazzy Jalisco. For Ubuntu 22.04, install Humble Hawksbill.
After installing your ROS 2 release, install the matching Gazebo version (see possible combinations). You must also install the ros_gz package suite to bridge Gazebo and ROS together. Refer to the Gazebo/ROS 2 pairing guide to install the correct version of Gazebo and ros_gz for your ROS installation on a Linux system.
Install Navigation 2 via the official Nav2 page. This installation also includes RViz, which visualizes the generated maps in this project.
We highly recommend following the Turtlebot 3 setup instructions on the Nav2 page. Running the Turtlebot navigation example is the best way to verify that your ROS 2 and Gazebo bridge are communicating correctly before attempting to launch this repository.
Next, install the SLAM Toolbox. We suggest following the tutorial to test the toolbox in a standard environment to ensure all binaries were downloaded and configured properly.
Clone this repository with:
git clone https://github.com/jianglanwei/slam
cd slam
This repository includes two ROS 2 packages located in the src/ folder:
traffic_ctrlmanages the logic and timing for all traffic lights within the Gazebo world.keyboard_listenercaptures the keyboard inputs and publishes motion commands and trigger service calls.
To build these packages, navigate to the root of your workspace (slam/) and run:
colcon build --symlink-install --packages-select traffic_ctrl keyboard_listener
After the build completes, colcon will have created three new folders: build/, log/ and install/. To allow ROS 2 to recognize the packages, you must source the setup file in every new terminal you open:
source install/setup.bash
Important
Do not forget to source the setup file in every new terminal you open:
source install/setup.bash
Launch slam_launch.py to start the SLAM mapping pipeline:
ros2 launch slam_launch.py
Refer to this teaser video to ensure the environment initializes correctly. Two windows should appear:
- Gazebo Sim: A simulation window featuring a hatchback parked on a tree-lined road, with the camera following the vehicle.
- RViz2: A visualization window displaying the real-time occupancy grid map as it is generated by the LiDAR data.
To drive the hatchback and explore the world, ensure the Gazebo window is selected (has focus) so it can capture your keystrokes:
| Key | Action | Lin. Vel. | Ang. Vel. |
|---|---|---|---|
| w | Move Forward | 3.0 | 0.0 |
| x | Move Backward | -3.0 | 0.0 |
| a | Turn Left | 3.0 | 0.5 |
| d | Turn Right | 3.0 | -0.5 |
| s | Stop | 0.0 | 0.0 |
Once you have mapped the environment, press m to save the current map. Your map will be saved in the maps/ directory as map_[timestamp].yaml (the metadata) and map_[timestamp].pgm (the image file).
Warning
To shut down the simulation and all associated nodes, always press Ctrl + C in the terminal where the launch command is running.
Do not close the windows directly. Closing windows manually leave "ghost nodes" active in the background, which may cause malfunction (such as traffic light timing errors) in your next session. If you suspect nodes from a previous session are still active, please refer to the troubleshooting section.
Important
Do not forget to source the setup file in every new terminal you open:
source install/setup.bash
Launch nav_launch.py to initialize everything for navigation:
ros2 launch nav_launch.py
By default, this loads map_default.yaml. To use a custom map you generated earlier:
ros2 launch nav_launch.py map_file:=maps/[your_map].yaml
The teaser video illustrates how to select the start and goal positions once the Gazebo Sim and RViz2 windows appear. First, press 2D Pose Estimate in the RViz upper sidebar to set the vehicle's starting location (this must match its actual location in Gazebo). Then, use 2D goal pose to select your destination. The hatchback should begin driving towards the goal once both positions are selected.
Tip
If the vehicle does not move after you select both positions, Nav2 may have missed one of the signals. Selecting the start the goal positions again usually resolves this.
If the simulation is terminated improperly (e.g., closing the Gazebo window directly instead of pressing Ctrl + C in the terminal), some ROS 2 processes may continue to run as "ghost nodes". These background processes can interfere with subsequent runs, leading to issues like malfunctioning traffic lights or communication failures between nodes.
If you suspect ghost nodes are interfering with your simulation, follow these steps to reset your environment:
-
Shut down the current process by pressing Ctrl + C in your terminal.
-
List all active ROS 2 nodes:
ros2 node listIf the list is not empty despite no active launch files, nodes from previous sessions are still hanging in the background.
-
Stop the ROS 2 daemon to clear the discovery cache:
ros2 daemon stop -
Verify the environment is clean by listing the nodes again:
ros2 node list -
Re-launch the pipeline. Your next run should now initialize successfully.


