Table of Contents
- Introduction to ROS
- Installation
- Creating a ROS Workspace
- Creating a ROS Package
- Understanding the ROS Node
- Publishers and Subscribers
- ROS Services
- Writing the Python Script
- Launching the ROS Nodes
- Conclusion
Introduction to ROS
In the field of robotics, the Robot Operating System (ROS) has become an essential tool for developing robot applications. ROS provides a framework for writing software that controls robots and offers various libraries and tools to aid in robotics development. In this tutorial, we will explore the basics of ROS and how to use Python to interact with the ROS ecosystem. By the end of this tutorial, you will understand the core concepts of ROS and be able to develop simple robot applications.
Prerequisites:
- Basic understanding of Python programming language
- Familiarity with Linux command line
- Ubuntu 18.04 or later (recommended)
Installation
Before we dive into using ROS, we need to install it on our system. Here are the steps to install ROS on Ubuntu 18.04 or later:
- Open a terminal and update the package lists:
sudo apt update
- Install ROS by running the following command:
sudo apt install ros-melodic-desktop-full
- Initialize rosdep:
sudo rosdep init rosdep update
- Source the ROS environment:
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc source ~/.bashrc
Congratulations! You have successfully installed ROS on your system. Now let’s proceed to create a ROS workspace.
Creating a ROS Workspace
A workspace in ROS is a directory where you develop and build ROS packages. Follow these steps to create a ROS workspace:
- Create a directory to hold your workspace:
mkdir -p ~/catkin_ws/src cd ~/catkin_ws/
- Initialize the workspace:
catkin_init_workspace src
- Build the workspace:
catkin_make
Now you have set up your ROS workspace. Let’s move on to creating a ROS package.
Creating a ROS Package
A ROS package contains libraries, executables, scripts, and other files for a specific purpose. Here’s how you can create a ROS package:
- Navigate to your workspace:
cd ~/catkin_ws/src
- Create a package:
catkin_create_pkg my_package rospy
- Build the workspace:
cd ~/catkin_ws/ catkin_make
Now you have created a ROS package named “my_package”. Let’s explore the components of a ROS package.
Understanding the ROS Node
In ROS, a node is a process that performs computation. It can publish or subscribe to topics, provide or use services, and interact with other nodes. To create a basic ROS node in Python, follow these steps:
- Open the Python script for your node:
cd ~/catkin_ws/src/my_package/src touch my_node.py gedit my_node.py
- Import the necessary ROS libraries and define the node:
#!/usr/bin/env python import rospy rospy.init_node('my_node')
In this example, we import the
rospy
library and initialize the node with the name “my_node”. Now let’s explore how to publish and subscribe to topics.
Publishers and Subscribers
In ROS, topics are named buses over which nodes exchange messages. A node can publish messages on a topic, and other nodes can subscribe to those messages. Here’s how you can create a publisher and subscriber in Python:
- Import the message type and necessary libraries:
from std_msgs.msg import Int32
- Create a publisher:
pub = rospy.Publisher('my_topic', Int32, queue_size=10)
- Create a subscriber:
def callback(msg): rospy.loginfo("Received message: %d", msg.data) sub = rospy.Subscriber('my_topic', Int32, callback)
In this example, we import the
Int32
message type from thestd_msgs
package. We create a publisher that publishes messages on the topic “my_topic”, and a subscriber that listens to the same topic and calls thecallback
function whenever a message is received.
ROS Services
Aside from topics, ROS also provides services for synchronous communication between nodes. A service allows one node to request a specific action from another node and receive a response. Here’s how you can create a service in Python:
- Import the necessary libraries and service type:
from std_srvs.srv import Empty, EmptyResponse
- Define the service callback:
def service_callback(request): rospy.loginfo("Service called") # Perform some action return EmptyResponse() rospy.Service('my_service', Empty, service_callback)
In this example, we import the
Empty
service type from thestd_srvs
package. We define a callback functionservice_callback
that performs the desired action when the service is called. The function returns anEmptyResponse
since the service doesn’t require any input or output arguments.
Writing the Python Script
Now that we have learned about publishers, subscribers, and services, let’s put everything together and create a simple Python script for a ROS node. Here’s an example: ```python #!/usr/bin/env python
import rospy
from std_msgs.msg import String
from std_srvs.srv import Empty, EmptyResponse
def callback(msg):
rospy.loginfo("Received message: %s", msg.data)
def service_callback(request):
rospy.loginfo("Service called")
# Perform some action
return EmptyResponse()
rospy.init_node('my_node')
pub = rospy.Publisher('my_topic', String, queue_size=10)
sub = rospy.Subscriber('my_topic', String, callback)
rospy.Service('my_service', Empty, service_callback)
rate = rospy.Rate(10) # 10 Hz
while not rospy.is_shutdown():
pub.publish("Hello, ROS!")
rate.sleep()
``` In this script, we create a ROS node named "my_node" and define a publisher, subscriber, and service callback functions. The node continuously publishes the message "Hello, ROS!" at a rate of 10 Hz.
Launching the ROS Nodes
To run the ROS nodes defined in your package, you can use the roslaunch
command. Here’s how:
- Create a launch file:
cd ~/catkin_ws/src/my_package/launch touch my_launch_file.launch gedit my_launch_file.launch
- Define the launch file content:
<launch> <node name="my_node" pkg="my_package" type="my_node.py" output="screen"/> </launch>
In this example, we create a launch file that launches our
my_node.py
script. Theoutput="screen"
option displays the node’s output in the terminal. - Run the launch file:
roslaunch my_package my_launch_file.launch
Now your ROS nodes should be up and running!
Conclusion
In this tutorial, we have learned the basics of Python programming for robotics using ROS (Robot Operating System). We explored how to set up a ROS workspace, create a ROS package, and write a simple ROS node in Python. We also covered topics like publishers, subscribers, services, and launching ROS nodes using the roslaunch
command. With this knowledge, you can now start developing your own robot applications using Python and ROS.
Remember, this tutorial just scratched the surface of what ROS can do. There is a vast amount of resources available online to explore and further enhance your skills in robotics with Python and ROS. Good luck with your robotic adventures!
For more information about ROS, visit the official ROS website: https://www.ros.org/