Python vs C++ – see the major difference between running code in the two major languages used by ROS. Let’s go!
Step 1: Create an account and Login to Robot Ignite Academy
On Robot Ignite Academy, you get access to the best online ROS courses and environment. No need to install and set up ROS locally – the only thing you need is a browser!
You’ll now have access to the simulation screen with a Notebook, an Editor, four Shells, and a Simulation Window. We are only using the Editor and Shells for this demo.
Step 2: Create a ROS package and some Python and C++ code
2. 1 Create a package by typing the following command in the shell
cd ~/catkin_ws/src
catkin_create_pkg obiwan rospy roscpp
2.2 Then in the src folder under the obiwan package. Let’s put the two source file obiwancpp.cpp and obiwanpy.py.
#! /usr/bin/env python
import rospy
rospy.init_node("Obiwan")
rate = rospy.Rate(2)
while not rospy.is_shutdown():
print "Help me Obi-Wan Kenobi, you're my only hope"
rate.sleep()
#include <ros/ros.h>
int main(int argc, char** argv) {
ros::init(argc,argv,"ObiWan");
ros::NodeHandle nh;
ros::Rate loop_rate(2);
while(ros::ok())
{
ROS_INFO("Help me Obi-Wan Kenobi, you'are my only hope");
}
return 0;
}
2.3 Type the following command to compile the package
cd ~/catkin_ws
catkin_make
source devel/setup.bash
2.4 After compile, let’s try to run the python script first
rosrun obiwan obiwanpy.py
You should see the output. The script executed without any problem.
2.5 Now try to run the C++ file in the same way. I bet it didn’t run! It turns out, to use the C++ file, you have to uncomment the following part in CMakeLists.txt.
Then compile and run again. You’ll see the output now.
Step 3: Master the Concept – Python vs C++ in ROS
The main difference between using C++ and Python in ROS is that you have to write down comments every time to include all the source files in CMakeLists.txt to compile and generate the executable.
QED 🙂
Extras: Video
Here is a sights and sounds version of the post, in case you prefer to see the action that way!
Further learning
If you are a ROS beginner and would like to learn more about programming ROS, please check one of the courses below, depending on which programming language you’re more familiar with:
Step 2: Examine the structure of the String message type
In the code, we used a ROS message type called String. Let’s examine the structure of this message.
Pick a Shell from the Tools menu and run the following command:
user:~$ rosmsg show String
[std_msgs/String]:
string data
[roseus/String]:
string data
Pick the IDE from the Tools menu, navigate to catkin_ws/src and open the obiwan_pub.pyprogram. You should see something like:
obiwan_pub.py
#! /usr/bin/env python
import rospy
from std_msgs.msg import String
rospy.init_node("sos_2")
rate = rospy.Rate(2)
help_msg = String()
help_msg.data = "help me Obi-Wan Kenobi, you're my only hope"
pub = rospy.Publisher('/help_msg', String, queue_size = 1)
while not rospy.is_shutdown():
pub.publish(help_msg)
rate.sleep()
We see that the String message is defined in two different packages (roseus and std_msgs), but we used the one defined in the std_msgs package, as can be seen on line 4 of the code block.
We also see that the String message has a single attribute named data, which should be a string. So, when creating a message of type String, we need to set a value for data, and the value must be a string (lines 8-9).
Now also take a look at the obiwan_sub.py program:
We see that on lines 6-7 when accessing a message of type String, we take it for granted that it has an attribute data, therefore we could reference msg.data.
Step 3: Determine the proper ROS message type to use for a given topic
In the obiwan_pub program above, we created a new topic called /help_msg. What if you are trying to publish to an existing topic and you are not sure the kind of message to send over it or you’re trying to subscribe to the topic and want to know what to expect? PS: every topic has a message type you must publish over it, or things will not work properly.
Let’s pretend we didn’t know the kind of message /help_msg uses – we just know the name of the topic. Let’s see how we can find out:
Start the obiwan_pub node, so that the help_msg topic is created.
user:~$ cd catkin_ws/src
user:~/catkin_ws/src$ ./obiwan_pub.py
Now pick another Shell from the Tools menu and run rostopic info /help_msg to find out the type of message it uses:
So now we see that it uses the message std_msgs/String, and we can know about this message by running. Here, we are specifying the package name (std_msgs) also, since we know it for sure:
user:~$ rosmsg show std_msgs/String
string data
Step 4: Wrapping it up
In summary, in order to use a ROS message properly, you need to know its structure. If you are working with an existing ROS topic, you can check what kind of message it uses and then check the structure of that message.
Extra: Video
Prefer to watch a video demonstrating the steps above? We have one for you below!
Related Resources
If you are a ROS beginner and want to learn ROS basics fast, we recommend you take any of the following courses on Robot Ignite Academy:
In today’s video we are going to see what are ROS Topics and how they work.
Before we start, if you are new to ROS and want to Learn ROS Fast, I highly recommend you to take any of the following courses on Robot Ignite Academy:
We love feedback, so, whether you like the video or not, please leave a comment on the comments section below so that we can interact and learn from each other.
Step1. Create a project in Robot Ignite Academy(RIA)
We have the best online ROS course available in RIA. It helps you learn ROS in the easiest way without setting up ROS environment locally. The only thing you need is a browser! Create an account here and start to browse the trial course for free now! We’ll use the ROS Basics in 5 Days course as an example today.
Step2. What is a ROS topic
rqt_graph is a great tool to visualize the nodes and topics in the system. Run the command in a shell and open the graphical tool(you can find it by clicking the computer icon on the edge of the simulation). You should see the gazebo is publishing the joint state.
Then we run roslaunch publisher_example move.launch and examine the graph again. You should see that a new node called move_node pops out and publishing to the topic cmd_vel topic.
The last step is using rosrun tutorial subscriber.py command to subscribe the cmd_vel topic. You can see that another new node comes out with the name i_am_the_7th_robot. Actually you can launch as many subscribers as you want and subscribe to the same topic.
In conclusion, the topic is a way that the nodes communicate with each other.
Want to learn more?
If you are interested in this topic, please check our ROS Basics In 5 Days (Python) course. You’ll learn how to use publisher, subscriber, service, and action to control a robot.
Whether you like the video or not, please leave a comment on the comments section below, so we can interact and learn from each other.
Step1. Create a project in Robot Ignite Academy(RIA)
We have the best online ROS course available in RIA. It helps you learn ROS in the easiest way without setting up ROS environment locally. The only thing you need is a browser! Create an account here and start to browse the trial course for free now!
Step2. Create a package
Let’s go to the Robot Creation with URDF course today, this course teaches you how to create robot description in the URDF format.
Then we type the following command to create a package with dependencies.
cd ~/catkin_ws/src
catkin_create_pkg tutorial rospy
We’ll use a publisher as an example. Let’s call it publisher.py and put it under the tutorial/src directory. Remember to give it permission to execute with chmod +x publisher.py . The code for publisher.py is the following:
#! /usr/bin/env python
import rospy
from std_msgs.msg import String
pub = rospy.Publisher('topic_name', String, queue_size=10)
rospy.init_node('node_name')
r = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
pub.publish("hello world")
r.sleep()
After that, we have created a file remap_demo.launch with the following content under the tutorial/launch folder.
Head to http://rosds.online and create a project named “ros_message” or whatever you like. Please ensure you select “Ubuntu 16.04 + ROS Kinetic + Gazebo 7” under “Configuration”.
Step 2: Create source files
Pick a Shell from the Tools menu and run the following commands:
user:~$ cd catkin_ws/src
user:~/catkin_ws/src$ touch obiwan.py obiwan_pub.py obiwan_sub.py #create the files
user:~/catkin_ws/src$ chmod +x obiwan.py obiwan_pub.py obiwan_sub.py # make them executable
Pick the IDE from the Tools menu. Locate the folder catkin_ws/src in the IDE’s file navigator, locate the corresponding files and paste the following code into them.
obiwan.py
#! /usr/bin/env python
import rospy
rospy.init_node("sos_1")
rate = rospy.Rate(2)
while not rospy.is_shutdown():
print "Help me Obi-Wan Kenobi, you're my only hope"
rate.sleep()
obiwan_pub.py
#! /usr/bin/env python
import rospy
from std_msgs.msg import String
rospy.init_node("sos_2")
rate = rospy.Rate(2)
help_msg = String()
help_msg.data = "help me Obi-Wan Kenobi, you're my only hope"
pub = rospy.Publisher('/help_msg', String, queue_size = 1)
while not rospy.is_shutdown():
pub.publish(help_msg)
rate.sleep()
First, start the ROS master in the background. Then check the ROS topics currently available:
user:~/catkin_ws/src$ nohup roscore &
user:~/catkin_ws/src$ rostopic list
/rosout
/rosout_agg
obiwan.py
Run this command in the current terminal. You should see the following output:
ser:~/catkin_ws/src$ ./obiwan.py
Help me Obi-Wan Kenobi, you're my only hope
Help me Obi-Wan Kenobi, you're my only hope
...
Pick another Shell from the Tools menu and run the following command. You should see that no additional topic has been created:
user:~$ rostopic list
/rosout
/rosout_agg
obiwan_pub.py
Stop the obiwan.py program in the first terminal by pressing Ctrl + C. Then run the obiwan_pub.py program:
user:~/catkin_ws/src$ ./obiwan_pub.py
Program is doing nothing? Nah, we shall see shortly. In the second terminal, rerun the last command:
user:~$ rostopic list
/help_msg
/rosout
/rosout_agg
Ah, now we have a new topic: help_msg! But where’s the help message? We’ll see that in the third program.
obiwan_sub.py
In the second terminal, run the obiwan_sub program:
user:~$ cd catkin_ws/src
user:~/catkin_ws/src$ ./obiwan_sub.py
SOS received: 'Help me Obi-Wan Kenobi, you're my only hope'!
SOS received: 'Help me Obi-Wan Kenobi, you're my only hope'!
That’s the message being generated by the obiwan_pub.py program.
Now, let’s put everything together.
Step 4: Master the Concept: What is a ROS message?
The obiwan.py and obiwan_pub.py create similar messages. However, the message created by obiwan.py is NOT a ROS message because:
It didn’t use a recognized ROS message type.
It was not sent over a ROS channel (topic).
obiwan_pub.pypublishes a message on the /help_msg topic while obiwan_subsubscribes to that topic and therefore gets the message.
In summary,
ROS nodes communicate with one another through messages.
They communicate over channels called topics.
There are different types of messages for different tasks. For example, a node controlling a drone will tell the drone to take off using a ROS message type Empty over a topic that might be something like /drone/takeoff.
Extra: when you send a message to a robot via the command line, you are a n*** …or at least behaving like one 🙂 .
If you are interested in how this works in detail or even you want to create your own messages, please check our ROS Basics In 5 Days (Python) course!