Main Content

Transmit and Receive Data Using PX4 CAN Blocks

This example shows to use UAV Toolbox Support Package for PX4 Autopilots to transmit and receive data from the CAN network using the specified CAN device.

Introduction

In this example, the Transmitter side of the model uses PX4 CAN Transmit blocks to transmit data. The Receiver side of the model uses PX4 CAN Receive blocks to receive data.

Prerequisites

Required Hardware

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

  • Supported PX4 Autopilot (this example uses Pixhawk 4)

Configure Model, Transmit Data, and Receive Data

In this example, Sine wave of data type double is sent as a CAN message. Byte Pack block is used to convert signals of data types to a uint8 vector output. Message ID:100 is transmitted to CAN Transmit block in the Transmitter Side of the model. In the Receiver side of the model, Message ID:100 is received by the CAN Receive block. The Byte Unpack block is used to convert a vector of uint8 data type to signals of data types double.

Similarly, a constant value is sent as a CAN message. Message ID:200 is transmitted to CAN Transmit block in the Transmitter Side of the model and in the Receiver side of the model, Message ID:200 is received by the CAN Receive block.

Note: To avoid conflict with PX4 UAVCAN, disable UAVCAN before working with PX4 CAN blocks. To disable, set the PX4 parameter UAVCAN_ENABLE to 0. For more information, see Finding/Updating Parameters.

Perform these steps to configure the model, transmit data, and receive data.

1. Open MATLAB.

2. Open the example MATLAB project by executing this command at the MATLAB command prompt:

open_system('px4demo_CAN')

3. Configure the model.

Note: Steps to configure the model is not required in the pre-configured model. Perform these steps if you have changed the hardware or not using the pre-configured model.

  • Go to Modeling > Model Settings to open the Configuration Parameters dialog box.

  • Click Hardware Implementation and select the required PX4 hardware from the list in Hardware board parameter.

  • Expand Target hardware resources for that board.

  • Go to CAN tab.

  • Set the Baud rate to 500000.

  • Set the Test mode to Loopback. This enables loopback testing of CAN messages and allows you to send and receive CAN messages without connecting to the hardware.

  • CAN port is set to CAN1. Only CAN1 is supported for CAN port.

4. Click Monitor & Tune from Run on Hardware section of Hardware tab in the Simulink Toolstrip. Wait for the Simulation to start. The Display block displays the data being received in the target hardware.

Other Things to Try

In this example, the identifier types used are Standard ID for both transmit and receive communications. Try using this example with Extended ID as the identifier type for one communication.