Skip to content

PWM_output Module Documentation

Toolbox: Sensors and Actuators

PWM_output Module Appearance

Introduction

One-sentence Description: Sends specified PWM control signals to the I/O PWM output ports of PX4 hardware to drive actuators such as motors and servos; supports hardware-in-the-loop (HIL) simulation and real-flight scenarios.

This module belongs to the Sensors and Actuators interface library of the RflySim toolchain. It enables direct output control of PX4 hardware actuators by Simulink-based control algorithms and serves as a core module for connecting control outputs to underlying actuators in custom drone control algorithm development. The module supports user-defined configuration of PWM update rate and output channels, accommodating refresh frequency requirements of various actuators (e.g., electronic speed controllers (ESCs), servos), and can simultaneously control up to 8 PX4 hardware PWM output ports.

The module supports two operational modes: real-flight and hardware-in-the-loop simulation. When Publish Outputs is checked, it adapts to the RflySim platform’s HIL simulation workflow, working with CopterSim and RflySim3D to perform semi-physical simulation validation; when unchecked, it can be used directly for real-flight control of the PX4 flight controller. The module controls PWM output unlock/lock via the ARM input signal to ensure safe actuator startup. The input PWM signals are specified in microseconds (µs), conforming to the standard PWM output specification of PX4 hardware.

Port Descriptions

Input Ports (Inputs)

Port Name Data Type Dimension Description
ARM Output boolean 1×1 Unlocks/locks PWM outputs. PWM outputs are enabled when the signal is high (TRUE) and disabled when low (FALSE). When disabled, all PWM outputs are set to 900 µs (idle value) to prevent ESCs from entering timeout protection.
PWM1 uint16 1×1 PWM signal value for PX4 hardware PWM output port 1, in microseconds (µs); typical range: 1000–2000 µs
PWM2 uint16 1×1 PWM signal value for PX4 hardware PWM output port 2, in microseconds (µs); typical range: 1000–2000 µs
PWM3 uint16 1×1 PWM signal value for PX4 hardware PWM output port 3, in microseconds (µs); typical range: 1000–2000 µs
PWM4 uint16 1×1 PWM signal value for PX4 hardware PWM output port 4, in microseconds (µs); typical range: 1000–2000 µs
PWM5 uint16 1×1 PWM signal value for PX4 hardware PWM output port 5, in microseconds (µs); typical range: 1000–2000 µs
PWM6 uint16 1×1 PWM signal value for PX4 hardware PWM output port 6, in microseconds (µs); typical range: 1000–2000 µs
PWM7 uint16 1×1 PWM signal value for PX4 hardware PWM output port 7, in microseconds (µs); typical range: 1000–2000 µs
PWM8 uint16 1×1 PWM signal value for PX4 hardware PWM output port 8, in microseconds (µs); typical range: 1000–2000 µs

Note: Only PWM ports that are enabled will display corresponding input ports in the module.

Output Ports (Outputs)

This module has no output ports.

Parameter Configuration (Parameters)

The following parameters can be configured in the Mask dialog box (double-click the module to open):

Parameter Name Type Default Value Available Values / Range Description
Publish Outputs bool true true / false When checked, the module supports hardware-in-the-loop simulation; when unchecked, it supports only real-flight scenarios.
PWM update rate enum 400Hz 50Hz / 125Hz / 250Hz / 300Hz / 400Hz PWM signal refresh frequency (i.e., PWM update rate). PX4 defaults to 400 Hz.
Enable PWM1 bool true true / false Enables/disables PWM output port 1.
Enable PWM2 bool true true / false Enables/disables PWM output port 2.
Enable PWM3 bool true true / false Enables/disables PWM output port 3.
Enable PWM4 bool true true / false Enables/disables PWM output port 4.
Enable PWM5 bool false true / false Enables/disables PWM output port 5.
Enable PWM6 bool false true / false Enables/disables PWM output port 6.
Enable PWM7 bool false true / false Enables/disables PWM output port 7.
Enable PWM8 bool false true / false Enables/disables PWM output port 8.
Sample time double -1 > 0 Module sample time in seconds. Default -1 indicates inheritance from the model’s sample time settings.

Parameter Setting Notes

Publish Outputs

This parameter must be checked for hardware-in-the-loop simulation. When unchecked, the module supports only real-flight operation.

PWM update rate

The PWM update rate determines the refresh frequency of PWM signal transmission. PX4 defaults to 400 Hz. Different actuators require different update rates: ESCs typically require a higher update rate (e.g., 400 Hz) to ensure smooth control and fast response, whereas standard servos can operate with a lower update rate.

Enabling PWM Ports

Only PWM ports that are enabled will have corresponding input ports generated in the module. Unused ports can be disabled to hide their inputs; configure according to the actual number of PWM output ports used on the PX4 hardware.

ARM Output Input Description

Unlocking via a high-level ARM Output signal is a prerequisite for starting actuators such as motors. This input is typically controlled jointly by RC receiver signals and flight mode logic within the Simulink model. When ARM Output is low or during module initialization, all PWM outputs are set to 900 µs (idle value) to prevent ESCs from entering timeout protection due to lack of valid signals.

PWM Signal Value Description

The input PWM signal unit is microseconds (usec), with a typical range of 1000–2000 usec:
- 1000–1100 usec usually corresponds to minimum throttle or idle state,
- 1500 usec usually corresponds to the neutral position,
- 1900–2000 usec usually corresponds to maximum throttle.

Module Characteristics (Block Characteristics)

Characteristic Value
Supported Data Types boolean, uint16, double, single
Direct Feedthrough Yes
Sample Time Discrete
Code Generation Support Yes

Data Communication Protocol

This module communicates with the PX4 flight controller hardware via the MAVLink protocol. After enabling the Publish Outputs option, PWM output data is transmitted via UDP to the simulation flight controller interface, following the default RflySim platform HIL simulation communication rules. The data format consists of 8 channels of 16-bit unsigned PWM values plus 1 unlock status flag, encapsulated into a MAVLink message for transmission.

Module Name Description
RC_Input Reads PWM input signals from the RC receiver; can be used to obtain PWM control commands
Servo_output Outputs servo control signals; belongs to the actuator output category of PX4 hardware modules
GPS_Receiver Reads GPS positioning data; belongs to the sensor input category of the Sensors and Actuators library
IMU_Reader Reads inertial measurement unit (IMU) data; belongs to the sensor input category of the Sensors and Actuators library

Usage Example

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

[RflySim installation path]/RflySimAPIs/5.RflySimFlyCtrl/0.ApiExps/2.PSPOfficialExps/Readme.pdf

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

Notes and Common Issues

  • Initialization Sequence: This module can only be used after flashing the PX4 firmware and correctly configuring the hardware I/O mapping. Before starting the simulation, ensure that the PX4 flight controller has completed its power-on self-test. Do not connect loads until the ARM Output input is set high, to avoid the risk of unintended startup.
  • Parameter Configuration Consistency: The PWM update rate configured in this module must match the PWM output update rate configured in the PX4 flight controller firmware; otherwise, output frequency mismatch and motor control jitter may occur.
  • Input Value Range Requirements: The input PWM signal is a 16-bit unsigned integer in microseconds. For typical applications, keep the input value within the range of 1000–2000 microseconds. Values outside this reasonable range may cause ESC misidentification, servo stalling, and other issues.
  • Hardware-in-the-Loop (HIL) Simulation Configuration: To use this module in HIL simulation, the Publish Outputs parameter option must be enabled. Without enabling this option, only real-flight flight controller support is available, and PWM control outputs cannot be properly generated in the simulation environment.
  • Sampling Time Matching: The module’s sampling time must match the selected PWM update rate. For example, when selecting a 400 Hz update rate, it is recommended to set the sampling time to 0.0025 s to prevent asynchronous output refresh and ensure smooth control.
  • Unlock Logic Requirements: PWM outputs are locked by default and only produce valid control signals when the ARM Output input is high. Before starting the motors, ensure the unlock logic is correctly configured, and promptly set the ARM Output low after flight termination to re-lock the outputs.
  • Port Correspondence: The input port order of this module corresponds one-to-one with the PWM output ports of the PX4 hardware I/O. During wiring, please verify the port numbers to avoid connecting the wrong motor channels, which could lead to flight anomalies.

Change Log

  • v4.00 (2024-08-12): Initial release. Supports PX4 hardware PWM signal output, offering multi-channel selectable PWM outputs, configurable PWM update rate, and output unlock/lock control functionality. Compatible with both hardware-in-the-loop simulation and real-flight scenarios.