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.

ROS Python or ROS C++ image 1

ROS C++ code

 

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.

 

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 to create a lot of code.
  • To understand 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 2

Programming the Aibo robot in ROS Python

 

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 example of 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. Finally, you can check this post about 5 methods to learn ROS).

ROS Python or ROS C++ image 3

PR2 robot ROS programming C++ and Python using the ROS Development Studio

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

Related Courses

Python for Robotics a full fundamental Python course designed for Robotics!

Python for Robotics Course

ROS for Beginners (Python)

ROS Basics C++ Course Cover - ROS Online Courses - Robot Ignite Academy

ROS for Beginners (C++)

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?