Skip to content

UDP_20100_PX4SilRecv Module Documentation

Toolbox: RflySim Model

UDP_20100_PX4SilRecv Module Appearance

Introduction

One-sentence Description: Listens on UDP ports in the series starting from 20101, receives and parses flight controller state data output by PX4 software-in-the-loop (SITL) simulation.

This module belongs to the RflySim Model interface library and is designed for state data exchange between Simulink algorithms and the PX4 flight controller in PX4 SITL simulation scenarios. It is primarily used in conjunction with the PX4 SITL simulation workflow and external custom control algorithm development. Based on the configured CopterID, the module automatically maps to the corresponding UDP port starting from 20101, receives complete flight controller state data computed by PX4 and sent by CopterSim—including raw GPS information, position, velocity, attitude, etc.—and outputs them as a structured data format for use by custom control algorithms within Simulink.

This module is commonly used in external controller development and validation scenarios based on the RflySim toolchain. Developers can leverage this module to obtain full-state information output by the PX4 flight controller, combine it with custom control logic to generate control commands, thereby enabling simulation validation of extended control algorithms. It supports receiving state data for specific drones in multi-vehicle simulation scenarios.

Port Descriptions

Input Ports (Inputs)

This module has no input ports.

Output Ports (Outputs)

Port Name Data Type Dimension Description
time_boot_ms double 1×1 Timestamp of the message, in milliseconds
copterID double 1×1 Drone ID, numbered starting from 1
GpsPos double 1×3 Estimated GPS position, format: [latitude, longitude, altitude]; latitude and longitude in degrees, altitude in meters
GpsVel double 1×3 Estimated GPS velocity, in NED coordinate system, units: m/s
gpsHome double 1×3 Home point GPS position, format: [latitude, longitude, altitude]; latitude and longitude in degrees, altitude in meters
relative_alt double 1×1 Relative altitude, in meters
hdg double 1×1 Heading angle, in NED coordinate system, units: degrees, range: 0~360
satellites_visible double 1×1 Number of visible satellites
fix_type double 1×1 GPS fix type; 3 indicates 3D fix (high accuracy)
AngEular double 1×3 Estimated attitude Euler angles [roll, pitch, yaw], in radians
localPos double 1×3 Estimated local position, in NED coordinate system, units: meters
localVel double 1×3 Estimated local velocity, in NED coordinate system, units: m/s
pos_horiz_accuracy double 1×1 GPS horizontal positioning accuracy, in meters
pos_vert_accuracy double 1×1 GPS vertical positioning accuracy, in meters

Note: Reserved fields in the original output structure are not exposed by default; they can be accessed via custom configuration if needed [to be confirmed].

Parameter Configuration (Parameters)

The following parameters can be configured in the Mask dialog box opened by double-clicking the module:

Parameter Name Type Default Value Optional Values/Range Description
CopterID int 1 1~255 Drone ID, used to identify the specific drone whose data is to be listened to
GPS Orin latLon (degree) double vector [31.0, 121.0] Any valid latitude/longitude Latitude and longitude of the GPS origin in the simulation environment, format: [latitude, longitude], units: degrees
GPS Orin Altitude (m) double 10.0 Any valid altitude Altitude of the GPS origin in the simulation environment, units: meters
Sample Time (s) double 0.01 >0 Module sampling time in Simulink simulation, units: seconds

Parameter Setting Descriptions

CopterID

This module is a single-drone listening module. CopterID determines the UDP port corresponding to the drone to be monitored. The port number is calculated as 20100 + 2 * CopterID; for example, for ID 1, the listening port is 20102.

GPS Orin latLon (degree)

This parameter defines the latitude and longitude of the GPS origin in the simulation environment, used to convert the ENU local coordinate system to the WGS84 global latitude/longitude coordinate system. It must match the simulation environment configuration.

GPS Orin Altitude (m)

This parameter defines the altitude (elevation) of the GPS origin in the simulation environment, used for altitude conversion calculations. It must match the simulation environment configuration.

Sample Time (s)

This parameter specifies the sampling period of the module in Simulink simulation, in seconds. Since the PX4 flight controller typically updates state data at 100 Hz, a value of 0.01 s is recommended.

Module Characteristics (Block Characteristics)

Characteristic Value
Supported Data Types double, single, uint32_t, int32_t, float
Direct Feedthrough No
Sample Time Discrete
Code Generation Support Yes

Data Communication Protocol

This module communicates via UDP, listening on ports following the rule: port number = 20100 + CopterID + 1, i.e., ports in the 20101++2 series. Data is encapsulated using a custom structure format:

  1. Overall received data encapsulation format:

    ``c typedef struct _netDataShortShort { uint32_t TargetType tg; // Target port identifier int len; // Total length of the transmitted structure, fixed at 120 char payload[112]; // Valid data payload, storing theoutHILStateData` structure }netDataShortShort;

  2. Load data format outHILStateData:

struct outHILStateData{
    uint32_t time_boot_ms; // Message timestamp, unit: ms
    uint32_t copterID;     // Aircraft ID, numbered starting from 1
    int32_t GpsPos[3];     // GPS estimated position: latitude/longitude in degrees * 1e7, altitude in meters * 1e3 (positive upward)
    int32_t GpsVel[3];     // GPS estimated velocity, NED frame, unit: m/s * 1e2 (i.e., cm/s)
    int32_t gpsHome[3];    // Home position GPS coordinates, same units as GpsPos
    int32_t relative_alt;  // Relative altitude, same unit as GpsPos altitude
    int32_t hdg;           // Heading angle, NED frame, unit: deg * 1000, range 0–360
    int32_t satellites_visible; // Number of visible satellites
    int32_t fix_type;      // GPS fix type; 3 indicates 3D fix (high accuracy)
    int32_t resrveInit;    // Reserved integer field
    float AngEular[3];     // Estimated Euler angles, unit: rad
    float localPos[3];     // Estimated local position, NED frame, unit: m
    float localVel[3];     // Estimated local velocity, NED frame, unit: m/s
    float pos_horiz_accuracy;   // GPS horizontal positioning accuracy, unit: m
    float pos_vert_accuracy;    // GPS vertical positioning accuracy, unit: m
    float resrveFloat;     // Reserved floating-point field
}
Module Name Description
UDP_20100_PX4ControlSend Sends external control commands to the PX4 SITL flight controller via UDP ports starting from 20100 (port = 20100 + 2 * series_index)
UDP_RflySim_Send Sends aircraft state data to the RflySim visualization interface
UDP_RflySim_Recv Receives user interactive control commands from the RflySim visualization interface

Usage Example

For relevant usage examples, please refer to the following path:

[RflySim Installation Path]/RflySimAPIs/4.RflySimModel/0.ApiExps/3.ExtCtrlAPI/1.ExtCtrlAPI-UDP20100/Readme.pdf

Please refer to Readme.pdf in the above path for complete example descriptions and operational steps.

Notes and Common Issues

  • Initialization Sequence: PX4 SITL simulation or RflySim platform tasks must be started before launching the Simulink simulation containing this module; otherwise, the module will fail to listen to UDP port data, causing all outputs to remain at initial zero values.
  • Port Mapping Rule: This module listens on port 20101 + 2*(CopterID-1). In multi-copter simulation scenarios, assign a unique CopterID to each aircraft to prevent UDP data listening conflicts.
  • Coordinate System Consistency: The position and velocity data output by this module conform to the NED coordinate system convention. If your development uses the ENU coordinate system, you must perform coordinate transformation on the output data manually.
  • GPS Data Units: Raw GPS position, GPS velocity, relative altitude, and heading angle in the output are scaled integer values: latitude/longitude in deg*1e7, altitude in mm, velocity in cm/s, and heading in deg*1e-3. Physical quantities must be recovered using the corresponding scaling factors during use.
  • Sample Time Matching: The module’s Sample Time parameter should match the PX4 flight controller’s state update period (typically 250 Hz, i.e., 0.004 s). Setting it too large causes delayed data updates; setting it too small results in unnecessary repeated data reads.
  • Firewall Interception: If the module fails to receive data after startup, check whether the system firewall is blocking MATLAB/Simulink’s UDP network access permissions. Granting access will allow normal listening.
  • Origin Parameter Consistency: The GPS Orin latLon and GPS Orin Altitude configured in the module must exactly match the takeoff origin latitude, longitude, and altitude set in the RflySim visualization interface; otherwise, the local NED position calculation will be incorrect.

Change Log

  • v4.1.0 (2024-01-15): Initial release of the UDP_20100_PX4SilRecv module, supporting listening on UDP ports starting from 20101 (port = 20101 + 2 * series_index) to read PX4 SITL flight controller state data, and outputting a complete flight controller state structure.