Main Content

Log Simulink Signals Using PX4 ULog

This example shows how to log signals in ULog format from a Simulink model running on a Pixhawk hardware and retrieve the log files from the SD card for further analysis.

This example uses a PX4 Pixhawk 4 as the hardware board. However, you can run this example using any PX4 Pixhawk hardware board.


The UAV Toolbox Support Package for PX4 Autopilots supports logging of signals from your Simulink® model on Pixhawk hardware in the ULog format. Signal logging enables you to monitor the behavior of the signal and perform analysis of historical data.


Required Hardware

To run this example, you will need the following hardware:

Task 1: Getting Started with Logging Prebuild Custom uORB Message

In this task, you will configure a Simulink model to log few signals into the custom uORB message that builds during the Hardware Setup screens.

1. From the MATLAB toolstrip, select HOME > New > Simulink Model to to open the Simulink Start Page. Click Blank Model to launch a new Simulink model.

2. On the Simulink toolbar, select View > Library Browser to open the Simulink Library Browser. Click the UAV Toolbox Support Package for PX4 Autopilots tab (you can also type px4lib in MATLAB command window). Select PX4 Utility Blocks category.

3. Drag and drop a PX4 ULog block to the model.

4. The selected ULog Simulink block contains SL_CustomPX4uORB as the default uORB topic. This topic is configured to log 4 inputs with double and single data types. To log the desired signals, input the necessary information with appropriate data types for logging. Your entire model should look like this.

To log signals using ULog format from the illustrated Simulink model perform the steps listed from Step 2 to Step 6.

Note: In Step2, use the topic name SL_CustomPX4uORB to update the logger_topics.txt.

nsh > echo SL_CustomPX4uORB >> logger_topics.txt

Replace the example model (px4demo_ulog) in Step 3 with the above specified model. Then, follow points 2, 3, 4, and 5 to log the desired signals in your model.

Task 2: Create Custom uORB Message and Log in ULog Format

To log signals of your choice, follow the provided instructions provided below. The following image helps you in understanding the complete workflow.

Step 1 - Include Custom uORB Message in Firmware

To use this example, you must create 'SL_CustomPX4uORBVerbose' custom UORB message by using the createPX4uORBMessage MATLAB API.

Use the following command in MATLAB terminal to create the message,

>> createPX4uORBMessage ('SL_CustomPX4uORBVerbose','int16 int16_signal', 'int32 int32_signal', ...

'int64 int64_signal','uint8 uint8_signal','uint16 uint16_signal','uint32 uint32_signal', ...

'uint64 uint64_signal','float32 float32_signal','float64 float64_signal')

To access this message in Simulink, build the PX4 firmware using the Install Support for UAV Toolbox Support Package for PX4 Autopilots.

For more information on uORB messages, see PX4 User Guide.

Step 2 - Update SD Card Configuration

You can customize the list of uORB topics to log by using a text file on the SD card. This needs to be updated with the new SL_CustomPX4uORBVerbose topic. Each time a new uORB message is created, this file includes the new topic to log. This step adheres with the current PX4 Logging workflow. For more information, see PX4 Logging Workflow.

You can update the SD card configuration in one of two ways.

Use Terminal with Connected SD Card (Recommended method)

1. This method requires QGC to access the nsh console. In the QGC, navigate to Analyze Tools > MAVLink Console. If the console is not accessible, ensure to turn on the Enable MAVLink option as mentioned in Step 3, point 4.

2. Go to the etc/ folder on the SD Card and create a new logging folder. Use the following commands to create the folder, if it is not already created.

a. Access the location using the cd command.

nsh > cd fs/microsd/etc

b. Create the logging folder using the mkdir command.

nsh > mkdir logging

3. Once the logging folder is created, include a logger_topics.txt file is in this location.

  • Use the following command to include the text file logger_topics with the uORB topic SL_CustomPX4uORBVerbose mentioned in the file.

nsh > echo SL_CustomPX4uORBVerbose >> logger_topics.txt

Manual Method - with SD Card disconnected

The above mentioned steps can be manually done by removing the SD card from Pixhawk board and accessing the contents.

Note: Along with the topic_name the instance and interval for the topic can be mentioned as additional information.


Step 3 - Open Model

1. Open the px4demo_ulog model.

In the example model, the Simulink signals are connected to the PX4 ULog, which is responsible for logging the selected topic (SL_CustomPX4uORBVerbose) on the block.

This block logs the input signals as a ULog file in the SD Card. The input signals are logged as a custom uORB topic as mentioned in the block parameters.

The name and number of inports are mentioned in the custom uORB topic.

This block internally maps the connected signals to the uORB Write block and is connected to a PX4 Timestamp block that writes the time from Hardware. Here the ports are updated as per message description for the selected topic SL_CustomPX4uORBVerbose.

Note: In this example, sample time is set to 30ms, as there might be issues related to logging while using Pixracer board and sample time set to 10ms. However, there is no issue on other boards with sample time set to 10ms. For troubleshooting, see Dropouts and SD Cards.

The timestamp block PX4 Absolute Time in this model is required to write the timestamp from Pixhawk to Simulink. This timestamp information is useful to analyze the ULog data and can be used from the PX4 Utils library in the Simulink model. This Simulink model allows you to log the desired Simulink signals in a custom uORB topic, which can be analyzed later to gain insights. You can copy and paste this block to any other Simulink model using Ctrl + C and Ctrl + V keys on the keyboard.

2. Open the Model Configuration Parameters dialog box by selecting Modeling > Model Settings on the Simulink toolstrip.

3. Go to the Hardware Implementation pane and select the name of the target hardware from the Hardware board list. If you are using a hardware board other than the Pixhawk 4, change the Hardware board selection.

4. In the Configuration Parameters dialog box, go to Hardware Implementation > Target hardware resources > MAVLink, and select Enable MAVLink on /dev/ttyACM0.

5. Click OK to close the dialog box.

Deploy the Simulink model to Pixhawk Hardware

In the Simulink model, on the Hardware tab, in the Mode section, select Run on board and then click Build, Deploy & Start. The build process for the model starts and it is deployed on the selected Hardware board.

Step 4 - Start PX4 Logger

The system logger in PX4 is responsible for logging information. Since this example is not a flying demo, you need to manually switch the logger on and off. By default, the PX4 logger is configured to log when the system is armed.

1. Access the PX4 console by opening QGC and selecting Analyze Tools > MAVLink Console for logging the topics.

2. As an additional checkpoint before logging, ensure that the uORB message is being published by using listener topic_name command. For example, listener SL_CustomPX4uORBVerbose.

3. In the PX4 console, use the logger on and logger off command to log the data. To verify the status of the logging process, use the logger status command.

Note: If the logger is not started, use the logger start command to start it.

Step 5 - Retrieve the Data

Download the logged flight data to your system using the Log Download screen in QgroundControl (QGC).

1. In the QGC, navigate to Analyze Tools > Log Download.

2. Click Refresh to get the latest logged flight data and then click Download to download the files.

Step 6 - Analyze ULog Data

You can analyze the downloaded signals in MATLAB by using ulogreader, which enables you to import the data into workspace for further analysis. You can also use the Flight Log Analyzer to plot the data directly from ULog file as shown below.

For more online tools, see Flight Log Analysis.

Related Topics