Log Signals to SD Card on PX4 Flight Controller

This example shows how to log the signals to the micro-SD card connected to the Pixhawk series flight controller.

The example uses a pre-defined Simulink model that contains the PX4 SD Card Logging block connected to a uORB Read block. You can reuse the same PX4 SD Card Logging block in your own Simulink model to log signals.

Prerequisites

Required Hardware

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

  • Pixhawk Series flight controller

  • Micro USB type-B cable

  • Micro-SD card (already used during the initial Hardware Setup)

  • Micro-SD card reader

Task 1 - Create the log folder in SD card

In this task, you will prepare the required folder structure in the SD card that will be used to log the signals.

1. Remove the micro-SD card from the PX4 flight controller

2. Connect the micro-SD card to the host computer using a micro-SD card reader.

3. Create the log folder. For example, if you want to log data to the folder /fs/microsd/log, create a folder log in the SD card root.

4. Reinsert the SD card back into the PX4 flight controller.

Task 2 - Configure, build and deploy an example model to the target

In this task, you will open the pre-defined Simulink model that contains the PX4 SD Card Logging block, and deploy the same to the PX4 flight controller.

1. Open the example model, px4_SDcard_logging, from the MATLAB command prompt.

open_system('px4_SDCard_logging');

In the example model, the PX4 SD Card Logger block is used to log signals. This block accepts signals from the uORB sensor_accel message, and accelaration along the three axis (x,y and z) are logged to the specific folder on the SD card in .bin format.

The example model uses a Bus Selector block that helps you to select the signals, and then provides the same as the input to the PX4 SD Card Logger block (using a Mux block between the two).

Note: Ensure that the signals to the data input of the PX4 SD Card Logger block are of the same data type.

The example model also uses a Pulse Generator block and a Data Type Convertor block to feed a Boolean signal to the en port of the PX4 SD Card Logger block. The signal will be logged only when the Boolean enable is high. This provides more flexibility with the time duration during which you want to log a signal. If you want to log signals at specific time durations, generate a corresponding boolean signal and feed it to the en port.

You can restrict the maximum number of data points to be logged using the Maximum Record Count parameter in the PX4 SD Card Logger block mask.

If the Cache in Memory parameter in the block is not selected, the data is written to the SD card at every timestep. If this parameter is selected, data is cached in the memory and is written to SD card only when enable goes low or when the number of data points logged reaches the Maximum Record Count specified. If you are logging multiple signals for larger duration, it is recommended to keep the Cache in Memory as unchecked to avoid memory issues.

2. Double-click the PX4 SD Card Logger block, and ensure that the file path for the Log Folder parameter is same as the path of the folder created in Task 1.

Tip: The Log File Name parameter defines the filename of the log file that you will see in the folder. For this example model, you need not change this filename (accel_data).

3. In the Modeling tab, click Model Settings.

4. In the Configuration Parameters dialog box, navigate to the Hardware Implementation pane:

  • Set the Hardware board to the same Pixhawk series controller that you selected during Hardware Setup screens.

  • In the Target Hardware Resources section, set the Build options to Build, load and run to automatically download the generated binary file on to the connected PX4 flight controller.

  • Enter the serial port of the host computer to which the PX4 flight controller is connected, in the Serial port for firmware upload field.

5. Navigate to Solver pane and select the option Treat each discrete rate as a separate task. Click OK.

6. In the Hardware tab, click Build, Deploy & Start. The model is deployed to the PX4 flight controller.

Task 3 - Read and Visualize the Logged Data from the SD Card

In this task, you will learn about MATLAB commands that help you to read and visualize the logged signal data from the SD card.

1. Disconnect the USB cable connected between the host computer and the PX4 flight controller.

2. Remove the micro-SD card from the PX4 flight controller.

3. Connect the micro-SD card to the host computer using a micro-SD card reader.

4. Copy the log file from the log folder (for example, accel_data.bin) to the current folder in MATLAB.

Note: The filename is accel_data.bin because it corresponds to the value for the Log File Name parameter that you defined in the block mask.

5. To read the data, run the following command in MATLAB:

[datapoints,timestamp,numberofpoints]=px4_read_binary_file('accel_data.bin');

6. To visualize the data as a 2-D Plot in MATLAB, run the following commands in MATLAB:

figure,plot(timestamp,datapoints,'*');
legend('x_{accel}','y_{accel}','z_{accel}');
xlabel('PX4 timestamp in \mu s');
ylabel('Acceleration in m/s');

Because the board is kept horizontally on the table, you can see that only the acceleration along -z axis (acceleration due to gravity(g)) is dominant. Since the en is provided as a periodic pulse, you can see that the signal is also logged at periodic intervals.

Task 4 - Use the SD Card Logger block in your Simulink models

In this task, you will learn how to log signals for any Simulink model that you develop using Embedded Coder Support Package for PX4 Autopilots.

1. Open the Simulink model that contains the signals you want to log to the micro-SD card on the Pixhawk Series flight controller.

2. Open the example model, px4_SDcard_logging. You can also open the model from the MATLAB command prompt - enter the command: open_system('px4_SDCard_logging').

3. From the px4_SDcard_logging model, copy the PX4 SD Card Logger block.

4. Paste the PX4 SD Card Logger to your Simulink model that you opened in Step 1.

5. Connect the signal you want to log to the input data port of PX4 SD Card Logger block.

Note: One PX4 SD Card Logger block can be used to log signals of the same data type only. If you want to log signals of different data types, create multiple copies of PX4 SD Card Logger block and use each of them for individual data types.

6. Double-click the PX4 SD Card Logger block, and ensure the following:

  • The file path that you specify for the Log Folder parameter is same as the path of the folder created in Task 1.

  • The Log File Name is unique within the Log Folder (unique filenames are entered for each PX4 SD Card Logger block)

7. Connect the en input of PX4 SD Card Logger block to any logic (boolean) that can enable/disable the logging of data.

Note: The maximum data points to be logged is defined using the Max record count parameter of the PX4 SD Card Logger block. If Cache in Memory option is not selected, the data is written to the SD card at every timestep. If the option is selected, data is cached in the memory and is written to SD card only when enable goes low or when the number of data points logged reaches the Maximum Record Count specified.

8. After you complete the connections for signals that need to be logged, save the model.

9. Perform the configuration and build actions, and deploy the model (as explained in Step 4 to Step 6 of Task 2).

10. Analyze and visualize the logged signal data using the same techniques as explained in Task 3.