A Python-based ROS2 simulation using Nav2 to operate a unicycle robot in a simulated environment. This repository enables path planning and control input generation using multiple methods, including PID, OpenAI's API, and machine learning models such as linear regression. The goal is to compare these methods in terms of control input accuracy and performance.
- Overview
- Features
- Control methods
- How it works
- Prerequisites
- Installation
- Configuration
- Execution
- Known Problems
- Further Improvements
This project leverages ROS2's Nav2 stack to generate paths in a simulated environment, which are then followed by a unicycle robot. The control inputs required to follow the path are produced through various techniques, allowing experimentation and comparative analysis among PID, machine learning models, and OpenAI's API.
- ROS2 & Nav2 Integration: Generates paths for a unicycle robot in a simulated environment.
- Multiple Control Methods:
- PID Control: Generates control inputs through a proportional-integral-derivative approach.
- OpenAI API Integration: Uses OpenAI API with PID system data for control generation.
- Machine Learning: Predicts control inputs using linear regression models trained on PID controller data.
- Data-Driven Approach: Leverages PID data (position and control inputs) to train machine learning models for improved path-following accuracy.
- Comparative Analysis: Enables performance evaluation among various control methodologies.
The PID orientation is computed to orient the robot always againts the goal point.
ROS Distro: Foxy or lower
(see Known Problems if you use Humble or higher).
To run the simulation, the following packages must be installed:
- navigation2
- nav2-bringup
- turtlebot3-gazebo Jazzy and newer:
- tf-transformations
sudo apt install ros-[ros2-distro]-navigation2sudo apt install ros-[ros2-distro]-nav2-bringupsudo apt install ros-[ros2-distro]-nav2-minimal-tb*sudo apt install ros-[ros2-distro]-turtlebot3-gazebosudo apt install ros-[ros2-distro]-tf-transformationsTo use the ML methods, you need to install the following packages:
- Pandas
- transforms3d 0.4.1
- NumPy 1.26.4
- scikit-learn
- OpenAI
pip install pandaspip install transforms3d==0.4.1pip install numpy==1.26.4pip install scikit-learnpip install openaiYou can easily install all of these packages by running the config.sh file, passing the ROS2 version name as argument.
In order to use the OpenAI API you need to create and set the API key.
- Go into the src folder of your ROS 2 workspace.
- Download the repository:
git clone https://github.com/simone-contorno/mobile_robot_ai - Go into the root folder of your ROS 2 workspace and build it:
colcon build --packages-select mobile_robot_ai
The program can be configured using the config.json file by:
- Tuning the PID.
- Change the goal threashold and weights.
- Choosing the control mode:
- 0 for manual PID control.
- 1 for OpenAI API-generated PID control (set the AI model and the system prompt).
- 2 for Linear Regression control (set the dataset).
To run the environment simulation you first need to configure it:
export TURTLEBOT3_MODEL=waffle ;
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:$(ros2 pkg prefix mobile_robot_ai)/share/mobile_robot_ai/worlds/
Then, you can launch it:
ros2 launch mobile_robot_ai simulation_launch.py cmd_vel_remap:=/mobile_robot_ai/cmd_vel plan_remap:=/mobile_robot_ai/plan
Here the remap is necessary to consider the modified plan by data_subscriber.cpp node.
Finally, you can run the following nodes:
ros2 run mobile_robot_ai data_subscriber ;
ros2 run mobile_robot_ai control_handler.py ;
ros2 run mobile_robot_ai [control_method].py
To easily execute all this commands, run the run.py file.
cmd_vel remapping doesn't work from ROS 2 Humble onward.
From ROS 2 Humble, remappings set in a parent launch file (e.g. with SetRemap) do not propagate into included launch files like bringup_launch.py, unless the included file explicitly supports them via LaunchArgument and passes them to Node(..., remappings=[...]).
As a result, remapping cmd_vel externally has no effect on Nav2's controller_server unless you use a patched version of the launch file.
Possible future improvements include:
- Enhancing the logic of the prompts and rewriting them in XML format.
- If the necessary resources are available, attempting to generate control inputs using a locally installed Large Language Model (LLM).
- Extend machine learning approaches to more complex models beyond linear regression.
- Perform a comparative analysis between the control methods, assessing accuracy, computational efficiency, and robustness.