This repository contains a ROS (Robot Operating System) node implementation for controlling a differential drive robot using a basic reinforcement learning (RL) framework. The node publishes velocity commands to the robot and processes sensor data such as LaserScan and Odometry. The code is designed to be used with ROS 2.
The RL node is designed to:
- Publish velocity commands (
Twistmessages) to control the robot's movement. - Optionally subscribe to sensor topics such as
LaserScanfor obstacle detection andOdometryfor localization. - The node can be extended to integrate a reinforcement learning agent for intelligent control, but as of now, it sends constant velocity commands for basic movement.
The core class RL_NODE_CLASS is a subclass of rclpy.node.Node, representing the ROS node in Python. The following components are implemented:
-
Publisher (
vel_pub): A publisher to the topic/diff_cont/cmd_vel_unstampedwhich sendsTwistmessages. These messages are used to control the robot's linear and angular velocity. -
Timer (
self.timer): The timer triggers thepublish_twistfunction every 0.2 seconds to continuously send velocity commands to the robot. -
Subscriptions (commented out): The node has the option to subscribe to
LaserScanandOdometrytopics, but this part is currently commented out. When uncommented, it will allow the node to receive laser scan data (for obstacle detection) and odometry data (for robot position tracking).scan_callback: Placeholder function for processing laser scan data.odom_callback: This function prints the robot'sxandyposition (from the Odometry message) every time a new Odometry message is received.
This method publishes a fixed Twist message every 0.2 seconds. The message sets the following robot velocities:
- Linear velocity (x-axis): 0.2 m/s
- Linear velocity (y-axis): 0.0 m/s (no movement in the y-direction)
- Angular velocity (z-axis): 0.1 rad/s (gentle rotation)
This function is called periodically by the timer to continuously control the robot’s movement.
Currently, this is a placeholder for processing LaserScan messages. The function is designed to be expanded later with logic to process sensor data for obstacle detection or other RL-related tasks.
This method processes Odometry messages. It prints the robot's x and y coordinates based on the received Odometry data. The position is rounded to three decimal places.
- Initializes the ROS 2 communication system with
rclpy.init(). - Creates an instance of the
RL_NODE_CLASSnode. - Starts the ROS event loop with
rclpy.spin(), keeping the node alive and handling messages. - Shuts down the ROS communication system when the node is destroyed.
-
Install ROS 2: Follow the ROS 2 installation guide for your operating system.
-
Install dependencies: Make sure to have the necessary ROS 2 packages for working with messages like
geometry_msgs,sensor_msgs, andnav_msgs. You can install them using:sudo apt-get install ros-foxy-geometry-msgs ros-foxy-sensor-msgs ros-foxy-nav-msgs # ROS Reinforcement Learning Node
Clone this repository into your ROS 2 workspace's src directory:
cd ~/ros2_ws/src
git clone https://github.com/eagle-Ji/course_experiment.git
cd ~/ros2_ws
colcon build
source ~/ros2_ws/install/setup.bash
ros2 launch <your_launch_file>
ros2 run <your_package_name> rl_node_class