PX4 development - building, flashing and debugging without debugger (Arduino developers style)

 This is a short tutorial that will explain how to build PX4 Autopilot, flash and debug it at the PX4 FMU (Flight Management Unit) without a debugger. Everything in this tutorial is tested and reproduced at Kubuntu 22.04 LTS (It works the same for Ubuntu as well).

Preparation

Let's start with cloning PX4 Autopilot

> git clone https://github.com/PX4/PX4-Autopilot.git

After the repository is cloned make sure you initialize all submodules. You would like to keep using this GIT commands when you jump between branches in PX4, otherwise, you can experience issues like missing modules while building.

> cd PX4-Autopilot
>
git submodule sync
> git submodule update --init --recursive


And that is it we are almost ready to build our PX4 firmware. You need to know what board (FMU) you possess. Let's guess you have Pixracer. Pixracer is fmu-v4 board.

PX4 fmu-v4 board source tree

Figure 1 - px4_fmu-v4 board source tree

Boards under the name px4 are created by PX4 developers and they have generic names like px4_fmu-v4 which is later recognized as the brand name Pixracer and for example px4_fmu-v5 as Pixhawk

For manufacturers is more straightforward. For example holybro_kakuteh7 is board where the company is Holybro that produce board with name KakuteH7.

Before we can build Firmware we need to install all dependencies for PX4. Since most developers for PX4 are users of Ubuntu there is a script that will install it all for us.

> ./Tools/setup/ubuntu.sh


If everything went OK it will ask you for

Relogin or reboot computer before attempting to build NuttX targets



Buidling

Now we can build Firmware for Pixracer from PX4-Autopilot folder with running

> make px4_fmu-v4

This will maybe be fixed for you but at the time of writing this, while building I've run up into build issue:
AttributeError: module 'collections' has no attribute 'MutableSequence'


This is because my version of Kubuntu has Python 3.10 where MutableSequence was removed from collections in favor of collections.abc

In order to fix that we need to replace all collections with collections.abc inside file:


PX4-Autopilot/src/drivers/uavcan/libuavcan/libuavcan/dsdl_compiler/pyuavcan/uavcan/transport.py

Once the build is completed we will get 3 different files with the names px4_fmu-v4_default and extensions .elf, .bin, and .px4 each of these could be used for flashing PX4 firmware. In this tutorial, we are focusing on flashing without a debugger so we will use px4_fmu-v4_default.px4 in combination with QGroundControl.


Figure 2 - build output

 

Flashing

Download QGroundControl.AppImage from this page https://docs.qgroundcontrol.com/master/en/getting_started/download_and_install.html

To flash PX4 Firmware over QGroundControl power supply from the battery needs to be disconnected.

Start QGroundControl, select Vehicle Setup -> Flash


flashing PX4 firmware over QGroundControl
Figure 3 - flashing PX4 firmware over QGroundControl

 
Connect Pixracer to USB cable. On the right side, it will be the option to select custom firmware that we recently built.

QGroundControl flashing
Figure 4 - QGroundControl flashing a custom firmware file menu


Figure 5 - QGroundControl successful flashed a custom firmware file


Debuging

For debugging without a debugger two major functionality should be used. One is using P4X_INFO and the other is dmesg.

PX4_INFO is a print macro that will print the name of the current module and the text that is provided. If debugging outside of the modules printf can also be used.

For Pixracer dmesg is not enabled so it can be enabled by adding the CONFIG_SYSTEMCMDS_DMESG=y line here https://github.com/PX4/PX4-Autopilot/blob/master/boards/px4/fmu-v4/default.px4board and adding #define BOARD_ENABLE_CONSOLE_BUFFER here https://github.com/PX4/PX4-Autopilot/blob/master/boards/px4/fmu-v4/src/board_config.h#L95

We are ready to do a small test. Let's add this two-line with your favorite editor in Dshot driver.

PX4_INFO("This is an example of how to use PX4_INFO for debugging. Print ret = %d", ret);
printf("This is an example of how to use printf for debugging. Print ret = %d\r\n", ret);


 

Figure 6 - PX4_INFO and printf examples

 

For users who are using a serial port those lines could be visible directly on the NuttShell (NSH) console output, in our case we need to check it with QGroundControl and this is where we will use dmesg command. If you write dmesg command in MAVLink Console you will get messages that are printed and buffered into the NSH console. 


Figure 7 - examples printed in NuttShell (MAVLink Console)

 

Conclusion

This type of debugging is very useful for people who are just starting into Embedded software development when a debugger cannot be connected (for example while flying) or is not available. It is also useful when an issue happens sporadically and it is hard to catch it while the debugger is connected, then printing additional information into the console buffer when an issue happened can help to solve it..

For faster debugging, it is recommended to use a direct connection to NSH output with USB to serial adapter and debugger connected over SWD.

How to setup Eclipse IDE to use J-Link debugger see here:
PX4 development - building, flashing and debugging with debugger (like a PRO)



If you find this useful you can consider to buy me a coffee

Buy Me A Coffee

Comments

Popular posts from this blog

AUTOSAR and CRC calculation

Flashing/debugging/running code at external memory in the memory-mapped mode

Debugging EK-TM4C123GXL with Visual Studio Code on Linux