When To Use Python vs C++ in Robotics?

C++ and Python are probably the 2 most used languages when it comes to program robots. C++ for performance and Python for the ease of use.

Of course, there are now many other languages that you can use. For example, JavaScript. You can create some parts of your robotic application with NodeJs/JavaScript, create a GUI (Graphical User Interface) in a web browser, and so on.

In this post we’ll focus on C++ and Python, simply because they are the most used. Just look at job offers in robotics companies and you’ll see a huge amount of C++ jobs, and less (but still many) jobs in Python.

So, maybe you’re wondering whether you should use only C++, only Python, or a mix of both.

C++ is known for performance but is harder to learn, and usually it takes more time to write a C++ program than a Python program.

So, let’s see when to use which language!

Robotics, the need for speed

In robotics, you need performance. And the performance between a robot and a web server for a SaaS application is totally different. In fact it’s like comparing oranges and bananas.

When we speak about performance in robotics, we don’t care about being able to handle thousands of connections at the same time with load balancing. What we care about is the real-time performance on a hardware product.

Let’s take a very simplified architecture for a robot: let’s say you build a mobile robot. You have some motors on the wheels to control with a micro-controller, and then you have an embedded computer that needs to fit in the robot structure. This computer will communicate with the outside, run the robot application, and communicate with the micro-controller to move the robot.

Usually, the low level layer of your application will involve a driver to read/write data from/on the motor controllers.

The driver will contain a thread which will communicate with your hardware devices, usually within a control loop.

This control loop is often very simple:

  1. Read data
  2. Update internal controller to get a new command
  3. Write command

And, what you do now is simply repeating this sequence over and over again. And here comes the challenge.

For smooth trajectories, you might have to run this loop at more than 100Hz, which means that the code is executed more than 100 times every second, in addition to all the other parts of your software !

The hardware part of robotics

There is a notion of cost when building a software for a robot. You can’t just take a 3000$ laptop, develop your application, and forget about it.

If you’re serious about the development, and if you’re willing to go to a production phase, you’ll need to optimize the hardware.

Maybe you don’t need a 3000$ computer. Maybe a 50$ one is just enough for what you need. But guess what, this 50$ computer may not have GPU rendering and huge amount of cores/RAM.

If your goal is to produce 10 000 units, then each dollar that you save on one robot will make you save 10 000$ for your complete batch.

Another important point is that if you don’t respect some real-time constraints, you may damage your hardware or compromise safety of people. If the maximum allowed amount of time to execute a given command is 2 milliseconds, then you need to make sure that each command (sent at 100+ Hz) will take less than 2 milliseconds.

So, I’ve talked much about the need for speed, and for hardware optimization. But still no comparison between C++ and Python yet! The point was to make you understand some of the most important challenges you can face in robotics, so the following will be quite straightforward.

When C++ is mandatory

C++ is one of the best programming language when it comes to performance.

If you consider that Python can be 10-100 (it really depends on the software and hardware) times slower than C++, you might understand that if you want to go fast, you need C++.

So, if you’re running a control loop at 100+ Hz, and if on top of that you need real-time constraints, then go for C++.

→ OK, C++ is performing well, but it’s not the lowest level in programming. Why not going deeper ?

Well, of course you can always go deeper and try to write code in assembly. But when it comes to motion planning, image processing, and networking, good luck with that.

C++ is a great language to maximize the performance/productivity ratio in robotics.

Also, don’t forget the micro-controller part. Your embedded computer will most likely not control the motors directly. You’ll probably need to use one or multiple micro-controllers.

Usually the language to program a micro-controller is close to C/C++. Just forget about Python here. A typical micro-controller has only one core and very limited amount of flash memory, but it’s very fast and reliable if used correctly.

So, when to use Python ?

Here it depends mostly on your skills and preferences.

Anyway, you’ve just seen that you’ll certainly have to use C++ for anything low level that requires performance: micro-controllers and the driver layer in your robotics application.

Well, you could use Python for anything else that does not involve critical time constraints and does not requires too much computation power.

Python will usually allow you to develop faster. You can focus more on the application and less on the language itself.

It’s also very common in robotics and software in general, to have a C++ part of the software with a Python binding.

Take the example of a motion planning algorithm. Again, on limited hardware, motion planning could not be well suited for Python. Here, C++ might be the best option. But once you have developed the core of the library (or used an already existing one), you can create a Python binding so you can easily use it in your Python code.

OpenCV is a great example for that. The Python API is widely used, which allows people to develop prototypes and learn about image processing faster, without having to dive into the C++ code of the library.

Another big difference between Python and C++ is the fact that C++ is a compiled language, but Python is an interpreted language.

What does it mean ? You’ll spend less time compiling code, and you’ll be able to launch and test your program faster.

Speaking of testing, Python is great for this purpose in robotics. You can easily setup a basic script to test one part of your program (for example: the physical behavior of your robot).

So, nothing will ever replace C++ for robotics in the near future. You can’t bypass C++ if you want to get serious about developing robotics software.

Python will help you develop the non critical parts of your software and test your application. The biggest advantage I see for Python is the gain of development time. It’s a trade-off between performance and development speed.

If, for a specific part of your program, the use Python is not compromising the performance/safety of your application, then feel free to use Python as much as you want.

How much time do you have ?

To conclude, I’ll just ask this question: how much time do you have ? And what is your experience ?

If you’re already a C++ expert with a lot of experience, maybe you can write C++ code as fast as Python code. In that case, you could write your entire application with C++.

If you have knowledge in both C++ and Python, but are no expert yet in one or another, then what I advise you to do is to use C++ for critical parts of your program when you need performance, and stick with Python for the rest. If you find out that some Python parts are not fast enough, then optimize the code or switch this part to C++.

Final note: if you’re building a robot at an enterprise level, with production costs already in mind, then maybe going full or almost full C++ might be a good choice (performance-wise and hiring-wise)

But if you’re building a robot for a personal project, a school project, or a prototype for a startup to make a POC (Proof Of Concept), don’t waste time. Build your application as fast as you can. Build something that works. Don’t over-engineer things. You’ll optimize for performance later.

Leave a Comment