This example shows how to use Embedded Coder Support Package for PX4 Autopilots to send and receive serial data with a Pixhawk Series flight controller.
This example uses a simple protocol to send a data request from the host computer to the Pixhawk Series flight controller, and receives the requested data from the flight controller.
The Embedded Coder Support Package for PX4 Autopilots contain a Serial Receive and a Serial Transmit block that helps you to receive and send serial data over UART or USART port on the Pixhawk Series flight controller.
To view the blocks, enter
px4lib at the MATLAB prompt.
In this example, we use the pre-configured
PX4 Serial Transmit and Receive model, along with a custom script
getAccelerometerData.m to send and receive serial data from the host computer. This model uses the Serial Transmit and Serial Receive blocks to exchange data.
PX4 Serial Transmit and Receive model: In this model, the Pixhawk Series flight controller sends the accelerometer data to the host computer using Serial USB cable. The data is sent only upon receipt of a request from the host computer (which runs the custom script, getAccelerometerData.m).
getAccelerometerData.m: Using this script, the host computer requests the Pixhawk Series flight controller to send its accelerometer data, and it displays the accelerometer values upon receiving them. Run the following command in MATLAB to open the file:
In this example, you will learn how to:
Create and deploy a Simulink model that can send and receive serial data.
Create a simple protocol to exchange serial data.
If you are new to Simulink, watch the Simulink Quick Start video.
Perform the initial setup and configuration tasks of the support package using Hardware Setup screens.
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)
1. Connect your Pixhawk board to the host computer using the USB cable.
2. Open the
PX4 Serial Transmit and Receive model. This model is configured to use the PX4 Pixhawk Series boards.
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, open the Build options pane, and enter the serial port of the host computer to which the Pixhawk Series flight controller is connected, in the Serial port for firmware upload field.
5. From the Groups list under Target hardware resources, select /dev/ttyACM0.
6. To know the mapping between the serial ports mentioned on the board, click View port map.
7. Set the Baud rate to 115200, Parity to None and Stop bits to 1.
8. Click Apply, and then OK to close the dialog box.
In the Simulink model, double-click the following blocks and verify that the parameter values specified are the same as shown below:
Serial Recieve block in the main model
Header block (Constant) block in the Send Header subsystem
Serial Transmit block in the Send Header subsystem
*Serial Transmit block in the Send Accel Data subsystem
Note: The Header (Constant block inside Send Header Subsystem) must be set to [5 5] of unit8 data type. This is because the getAccelerometerData.m script, which receives the data from PX4 Serial Transmit and Receive model, expects the header to be
Note: The Sample time parameter specified in Header (Constant block inside Send Header Subsystem) and in the PX4 Accelerometer block (inside Send Accel Data Subsystem) must be set to -1.
This model uses a simple protocol to exchange data between the hardware and host computer.
The getAccelerometerData.m script in MATLAB sends a request to the Pixhawk Series flight controller. The request data sent from the host computer is in the form of HEADER_HOST+DATA_REQUEST.
The PX4 Serial Transmit and Receive model running on the Pixhawk hardware receives the above data and parses it to get the DATA_REQUEST by stripping the HEADER_HOST. Upon validation that request is genuine, the Pixhawk hardware sends the accelerometer data in the form of HEADER_PIXHAWK+ACCEL_DATA back to the host computer.
The getAccelerometerData.m executing on MATLAB receives the above data and parses it to get the ACCEL_DATA by stripping the HEADER_PIXHAWK.
1. Open the PX4 Serial Transmit and Receive model.
2. In the Hardware tab, click Build, Deploy & Start.
The lower left corner of the model window displays the status while Simulink prepares, downloads, and runs the model on the hardware.
3. Open the
getAccelerometerData.m in MATLAB. Ensure that the comport defined in Line 14 is the same as the serial port that you entered in the Build options pane (of the Configuration Parameters dialog box), as mentioned in Task 1, Step 4 above.
4. Execute the getAccelerometerData.m script in MATLAB to view the accelerometer values. For example, the accelerometer values may look like the following:
Accelerometer data (x | y | z) in m/s^2: 0.12 | 0.03 | 9.74
5. Change the orientation of the Pixhawk hardware board and repeat Step 4 to see the updated accelerometer values.
Enhance the example by using a Header-Terminator protocol to exchange data between Pixhawk hardware and host computer.
PX4 Serial Transmit and Receive model to exchange data over other serial ports of the Pixhawk Series controller.
Create a Simulink model that uses Serial Receive and Serial Send blocks from Instrument Control Toolbox to exchange serial data between host computer and Pixhawk Series flight controller.