Why we are using C++ over MicroPython for our CanSat

Arca avatar
In categories

During the research phase for our CanSat, we saw that a lot of teams using a Raspberry Pi Pico opted to use MicroPython or CircuitPython for their CanSats. In fact, the CanSat UK organisation recommends (or at least implies) using Python. However, we think that it is better to use C++, assuming you are confident with low-level languages. Here’s why.

I will start by saying that C++ is not for everybody, especially on an embedded system. If you have no prior programming knowledge and not a lot of time on your hands then choosing Python is the way to go. However if you have used another programming language (Scratch does not count :/) and are familiar with programming concepts such as Object Oriented Programming (OOP) then do give it a try! However, don’t get over-confident before you learn how to use pointers, classes and structs properly. If you have no prior experience with the language, I would recommend learning it on a computer where it is easy to debug and troubleshoot, then the Pico. Speaking of debugging, buy (or make) yourself a Pico debugger. It will make your life 100x easier.

c++
C++ self-confidence over time, from lbrandy.com

Another drawback is that you will often need to create/implement your own libraries for specific sensors which can be time-consuming. Often, you will find many libraries for Python but not for C++, which can be frustrating. However, if you enjoy programming and writing fast code that runs very close to the hardware, then C++ may be for you. Which leads us onto the first point:

C++ is Fast

C++ is faster than Micro/CircuitPython. There’s no getting around that. C++ is a compiled language, Python is interpreted which means that in C++, the code is pre-prepared and turned into CPU instructions whereas in Python the interpreter figures it out on-the-fly. In fact, we made a simple benchmark test to show that.

We created an algorithm that increments a score continuously, without delay, for exactly 1 second. Afterwards, the number is checked and the score is printed. The source code is available on our GitHub repository here and these are the results we measured on our Pico:

LanguageScore
MicroPython79968
CircuitPython53654
C/C++3676472

And here is a nice graph visualising the data:

As you can see, C/C++ is 46x faster than MicroPython and 69x faster than CircuitPython.

C++ is Efficient

C++ is efficient in multiple ways. Firstly, if the code runs faster, it takes less time to run, therefore it uses less power when running. Unfortunately, we do not have enough testing equipment to test that but you probably get the point.

Secondly, C/C++ has pointers. If you are unaware of what a pointer is, it is an integer variable that contains the memory address of another variable. Seems pointless (pun intended) at first, doesn’t it? However, I would argue that they are one of the most powerful features of the language and allow you to greatly reduce the memory footprint of the code. This is extremely useful as the Pico only has 264 kilobytes of RAM.

In the following code, the string str is copied when being passed into the function:

string str = "Hello world!";

void doSomething(string str) {
    ...
}
...
doSomething(str);

When a pointer is used, the same variable is used inside the function and is not duplicated, therefore using less memory:

string str = "Hello world!";

void doSomething(string* str) {
    ...
}
...
doSomething(&str);

C++ is Compiled

As I have said before, C++ is compiled whilst Python is interpreted. This means that in C/C++, 95% of all errors in the code are caught during compilation whereas in Python most errors are runtime errors. As you probably don’t want your CanSat failing halfway through launch because of a ValueError, C++ is better at runtime reliability and if you are going to use Python, you will need to test your code even more thoroughly. Don’t get me wrong, you can still get runtime errors in C++; they are simply a lot less common than Python.

Conclusion

I hope this helped you decide which programming language to use for your CanSat. As I have said before, for most novice programmers I would recommend Python as it is a lot simpler to work with, however if you have some prior experience with programming then I would highly recommend considering C++.

Published under:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *