Skip to content

simone-contorno/mobile_robot_ai

Repository files navigation

Mobile Robot AI

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.

Table of Contents

Overview

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.

Features

  • 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.

Control methods

  • PID Control: Generates control inputs based on proportional, integral, and derivative terms. PID control provides a benchmark for evaluating other methods.
  • OpenAI API: Uses API calls with PID data as inputs, generating controls based on external inference.
  • Linear Regression: A machine learning approach trained on PID data to predict control inputs with a regression model.
  • How it works

  • The program subscribes to goal pose, laser scanner, and path topics.
  • The path is then modified by altering a user-defined percentage of points and republished on a different topic. This modification reduces the path's complexity.
  • Then, the control inputs are computed using a PID controller approach.
  • The PID orientation is computed to orient the robot always againts the goal point.

    Prerequisites

    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
    • sudo apt install ros-[ros2-distro]-navigation2
    • nav2-bringup
    • sudo apt install ros-[ros2-distro]-nav2-bringup
    • turtlebot3-gazebo
    • Jazzy and newer:
      sudo apt install ros-[ros2-distro]-nav2-minimal-tb*
      Iron and older:
      sudo apt install ros-[ros2-distro]-turtlebot3-gazebo
    • tf-transformations
    • sudo apt install ros-[ros2-distro]-tf-transformations

    To use the ML methods, you need to install the following packages:

    You 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.

    Installation

    1. Go into the src folder of your ROS 2 workspace.
    2. Download the repository:
      git clone https://github.com/simone-contorno/mobile_robot_ai
    3. Go into the root folder of your ROS 2 workspace and build it:
      colcon build --packages-select mobile_robot_ai

    Configuration

    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).
    The system prompts are specified in the mobile_robot_ai/ai_prompts.py file.

    Execution

    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.

    Known Problems

    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.

    Further Improvements

    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.

    About

    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.

    Topics

    Resources

    License

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

     
     
     

    Contributors