Learn ROS with C++  or Python?

Learn ROS with C++ or Python?

This is a question that I get asked all the time:

Do you recommend me to learn ROS with C++  or Python?

As you may know, you can create ROS programs mainly in two programming languages: Python and C++. There are other languages available like Swift, Lisp or others, but those are not fully supported. So for the rest of the article, we will consider that only Python and C++ are the available ones for a newcomer. Mostly, whatever you can do in ROS with C++, you can do it also with Python. Furthermore, you can have C++ ROS programs (ROS programs are called nodes) talking to other Python ROS nodes in the same robot. Since the nodes communicate using standard ROS messages, the actual language implementation of the nodes is not affecting their communication. That is the beauty of ROS.

If that is the way of working of ROS, then, why not to let everybody program in the language they want? Well, because programming in one language or another has its consequences, especially when you are learning ROS. Let’s have a look at them.

Programming ROS with Python

Pros of programming ROS in Python

  • Is faster to build a prototype. You can create a working demo of your node very fast if you use Python, because the language takes care of a lot of things by itself so the programmer doesn’t have to bother.
  • You don’t need to compile, and spend endless hours trying to catch a hidden bug. Even if you can have bugs in Python, the nature of them are a lot easier and faster to catch.
  • You can learn Python very fast.
  • You can make really short programs for complex things
  • The final code of your node is quite easy to read and understand what it does.
  • You can do anything with Python. Python is a very powerful language with libraries for anything you want.
  • It is easier to integrate it with web services based on Django. Since Django is based on Python, you can integrate ROS functions easily in the server calls.
  • It is easier to understand some ROS concepts if you use the Python API, because some complex concepts are hidden for the developer in the ROS Python API. For example, things like the Callback Queue are handled directly by the ROS Python API.

Cons of programming ROS in Python

  • It runs slower. Python is an interpreted language, which means that it is compiled in run time, while the program is being executed. That makes the code slower.
  • Higher chances of crashing in run time, that is, while the program is running on the robot. You can have a very silly mistake in the code that won’t be cached until the program runs that part of the code (in run time).
  • Unless you define very clear procedures, you can end with a messy code in a short time if the project grows. I mean, you need to impose some rules for developing, like indicating every class you import from which library is, force strong types on any definition of a variable, etc.

ROS Python or ROS C++ image 2

Programming the Aibo robot in ROS Python

 

Programming ROS with C++

Pros of programming ROS in C++:

  • The code runs really fast. Maybe in your project, you need some fast code.
  • By having to compile, you can catch a lot of errors during compilation time, instead of having them in run time.
  • C++ has an infinite number of libraries that allow you to do whatever you want with C++.
  • It is the language used in the robotics industry, so you need to master it if you want to work there.

Cons of programming ROS in C++:

  • C++ is a lot more complex to learn and master. A LOT
  • Just creating a small demo of something requires creating a lot of code.
  • Understanding what a C++ program does can take you a long time.
  • Debugging errors in C++ is complex and takes time.

ROS Python or ROS C++ image 1

ROS C++ code

 

A proposed ROS learning path

You: I do understand your list of pros and cons, but in the end, what should I do? I want to learn ROS… do I have to use Python or C++ for learning ROS?

Me: Well that depends on your situation. Based on your situation, some of the cons may not be a con (if you are a master of C++ the fact that C++ is difficult doesn’t affect you). So it may be convenient for you to start ROS with C++… if your goal is the industry! (because the robotics industry needs the speed of C++ for their robot programs). However, if your goal is research and academia, I would not recommend learning ROS with C++ even if you mastered it, because in academia, speed in testing hypothesis is more important than speed of execution. Hence Python would be your choice.

It all depends on your situation.

You: Yes but I need some clear examples of the learning path for the typical newcomer to ROS.

Me: OK, let me give you the typical situation.

One situation that I get all the time, especially at the University, is that students that come to learn ROS do not know neither C++ nor Python (actually, most of them not even know about Linux shell, but that is another matter). In that case, I strongly recommend start learning ROS using Python. Really, believe me, it is too much to try to learn ROS at the same time that you learn C++. I’ve seen many times. People get frustrated and complain about the difficulty of ROS. Yes, ROS is difficult but the thing is that you tried to swallow too much by learning C++ and ROS at the same time.

You: But I want to go to work for the industry. Does that mean that I’ll have to stay with Python every time that I program with ROS, so I won’t have access to the industry?

Me: No. What I’m proposing here is that you go step by step. I know you want to go fast, but that doesn’t work. Go step by step and you will actually learn faster and master both ROS for Python and for C++ (just if you want to).

You: Ok, so how do I learn step by step ROS if I know neither Python nor C++?

Me: Well, first you need to learn Python. Learn the basics of Python and how to manage classes. That is mostly what you will need from Python. You can learn it in this online free course. Once you know more or less about Python, then start learning ROS in Python. You should learn the following subjects in the following order:

  1. ROS Basic concepts
    1. Topics
    2. Messages
    3. Packages
    4. Services
    5. Actions
  2. How to debug with ROS
    1. Rviz
    2. rqt tools
  3. ROS TF
  4. ROS URDF
  5. ROS Gazebo
  6. ROS Control

(note: you can find all those subjects explained in the ROS wiki, or if you want something more interactive, you can try our ROS online academy).

At this point in time, you will already know the basics of ROS and understand what it is and how it works. Also, you would have practiced a lot writing Python code with classes. Now it is the moment to switch to C++ in case you want to program in that language (if it is not your case and you feel comfortable with Python, jump to the next step). Learn now the basics of C++, including classes and pointers (especially smart pointers, because they are used a lot in ROS). Then, you will have to revisit some basic concepts of ROS, because they are treated differently in C++.

So you need to go back again to learn the basics of ROS, but this time in C++. Study the following subjects:

  1. ROS Basic concepts
    1. Topics
    2. Messages
    3. Packages
    4. Services
    5. Actions
  2. ROS TF
  3. ROS Control

Finally, continue getting more knowledge of ROS, either in C++ or in Python. Learn the following subjects, without any specific order. Just choose the subject based on your own preferences or needs:

  • ROS Navigation
  • ROS Perception
  • ROS Manipulation
  • ROS Machine Learning
  • ROS for Industrial Robots

That’s it!

You: it sounds like a lot of work, but it makes sense.

Me: Yes it is! But nobody said that was going to be easy…

Once you have mastered most of the subjects of ROS and both Python and C++, you can start developing like a pro.

You: What does it mean like a pro?

Me: Well, for me it means that you will create nodes in either C++ or Python, depending on your requirements.

For example, while I was creating the navigation software of a human size humanoid robot, I created the localization algorithm using C++, but I created the recovery behavior when lost using state machines in Python. The localization algorithm is a complex algorithm requiring to run fast and consuming a lot of resources (because of the particles), that is why I did it in C++, but the coordination behavior did not require a fast execution, but instead, required a clear structure and easy reading to understand the logic behind.

 

Conclusion

Learning ROS with Python or C++ depends on your current situation and the reason why you want to learn ROS. If you are like the typical newcomer (no knowledge of Python nor C++), then definitely, you should start learning ROS with Python and then decide whether it is convenient for you to move to C++.

Let me know about your experiences in the comments below. Did you learn ROS using C++ or using Python? Why? Would you have done differently if you could go back?

Take Away

RDP 028:  Using Time of Flight Sensors With Krzysztof Zurad

RDP 028: Using Time of Flight Sensors With Krzysztof Zurad

 

In this episode, Krzysztof will explain us about using cheap, fast, small and light weight time of flight sensors on drones in outdoor environments.

We will discuss about distances that those sensors can reach, frequency of update, interference between sensors and how to combine rings of sensors. Krzysztof will explain how to get data from those sensors into ROS and how to convert to different types of ROS messages, like LaserScan or PointCloud2.

Selected quote:

“Those sensors are very light and very fast which make them perfect for sensorizing drones”

Krzysztof Zurad

Related links:

Subscribe to the podcast using any of the following methods

Or listen to it on Spotify:

RDP 027: Off-the-shelf ROS components for Robots With Roberto Guzman

RDP 027: Off-the-shelf ROS components for Robots With Roberto Guzman

 

In this episode I talk to Roberto Guzman who will tell us about how to speed up our robot building by using certified ROS components.

He also talks about how easy he feels that is to transfer results from simulation to real robot, about the ROSCON, IROS and ROS-Industrial.

Selected quote:

“One thing that I’m missing from simulation is to have a less heavy (in physics) simulator that allows the easy simulation of a fleet of, let’s say, 10 robots”

Roberto Guzman

ROS Developers podcast interview of Roberto Guzman, about ROS components for robots

ROS Developers podcast interview of Roberto Guzman, about ROS components for robots

Related links:

Subscribe to the podcast using any of the following methods

Or listen to it on Spotify:

RDP 026: Tensorflow in ROS With Martin Pecka

RDP 026: Tensorflow in ROS With Martin Pecka

 

In this episode Martin Pecka will talk about the Tensorflow ROS cpp package, a ROS package he created to simplify the use of Tensorflow within a ROS node and to provide a universal interface to ROS programs, independently on how Tensorflow was installed in the system.

Selected quote:

“The Tensorflow ROS cpp package works as a translation layer between all the ways you can get Tensorflow installed in your computer and your ROS programs”

Martin Pecka

Related links:

Subscribe to the podcast using any of the following methods

Or listen to it on Spotify:

[ROS Q&A] 148 – ROS CPP Time and Duration arithmetics build error

In this video we are going to explain about a problem trying to build a ROS C++ node using ros::Time and ros::Duration.

It is because the order of the terms in sum operation matters. You can check in the video it is simple to solve but it can be a trap if you do not pay attention to that during the development.

Step 1. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s call the project ros time error.

Step 2. Create a package

Let’s start by creating a ROS package for our code

cd ~/catkin_ws/src
catkin_create_pkg mypkg roscpp geometry_msgs 

Then we call the source file mypkg_node.cpp and put it under the mypkg/src folder with the content from the question.

// This program publishes randomly-generated velocity

// messages for turtlesim.
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>  // For geometry_msgs::Twist
#include <stdlib.h> // For rand() and RAND_MAX

int main(int argc, char **argv) {



  ros::init(argc, argv, "publish_velocity");

  ros::NodeHandle nh; // handles node


  ros::Publisher pub = nh.advertise<geometry_msgs::Twist>(
    "turtle1/cmd_vel", 1000); //links publisher to the node


  while(ros::ok()) {

    geometry_msgs::Twist msg; //Object msgs

    msg.linear.x = 0.5; 
    msg.angular.z = 0;
    msg.linear.z = 0.5;

    // Time tracking
    ros::Time beginTime = ros::Time::now();
    ros::Duration MessageTime = ros::Duration(3);
    ros::Time endTime = MessageTime + beginTime; 

    while(ros::Time::now() < endTime){
        pub.publish(msg);


        ROS_INFO_STREAM("Sending velocity command:"
          << " linear=" << msg.linear.x
          << " angular=" << msg.angular.z
           <<"linearZ="<<msg.linear.z);


        ros::Duration(0.3).sleep();

    }


  }
}

To compile the code, we need to at first uncomment the add_executable and target_link_libraries in the CMakeLists.txt. After that, we can compile it with

cd ~/catkin_ws
catkin_make

However, some error related to ROS time occurs. After checking the documentation ROS C++ Time and Duration, we found out that the order is not quite right. The following part should be changed.

...
    ros::Time endTime = beginTime +  MessageTime; 
...

If we compile again, the problem is solved.

Want to learn more?

If you are interested in using ROS with C++, please check our ROS Basics in 5 days(C++) course for more information.

 

Edit by: Tony Huang

RELATED LINKS

The original question on ROS_Answers
ROS Development Studio (ROSDS)
Robot Ignite Academy
ROS C++ Time and Duration


We love feedback!

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

Pin It on Pinterest