Main Content

Getting Started with PWM Blocks for PX4 Autopilots

This example shows you how to use the PX4 PWM Output block to generate signals on the PWM pins of a Pixhawk Series controller, and verify the PWM values.

Introduction

UAV Toolbox Support Package for PX4® Autopilots enables you to generate PWM signals on the PWM outputs of the Pixhawk Series controller which are connected to ESC and thus drive the motors.

In this example, you will learn how to create and run a Simulink® model to generate signals on the PWM outputs.

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)

  • Safety switch (if required by the Pixhawk Series flight controller)

  • Cathode Ray Oscilloscope (CRO)

Model

You will use Simulink to create PWM signals on the PWM outputs. For verification purposes, you can connect the PWM output pins of the Pixhawk Series hardware to a CRO to see the pulse width.

You will be creating the below px4demo_PWM model for generating PWM signals:

Task 1 - Configure the Simulink Model for PWM

1. From the MATLAB® toolstrip, select HOME > New > Simulink Model to open the Simulink Start Page. Click Blank Model to launch a new Simulink model.

2. In the model window, from the Simulation tab select Library Browser to open the Simulink Library Browser. Click the UAV Toolbox Support Package for PX4 Autopilots tab (you can also type px4lib at the MATLAB command prompt).

3. Drag a PX4 PWM Output block to the model. Double-click the block to open the Block Parameters dialog box and to change the block properties.

4. For generating PWM signals for the Main PWM channels, select /dev/pwm_output0 as the PWM device. For the AUX channels, select /dev/pwm_output1.

5. Select the channels for which you want to generate the PWM signals. A Pixhawk Series controller can have a maximum of 8 PWM channels for Main/AUX.

Note: It is not necessary that all 8 PWM channels will be present on the Pixhawk® hardware. For example, Pixracer has only 6 Main PWM channels and Pixhawk 1 has 6 AUX channels. Ensure that the channels that you select on the PX4 PWM Output Block Parameters dialog box exist on the Pixhawk board.

6. Click Apply and then OK to close the dialog box.

7. From the Simulink > Sources tab in the Library Browser, drag and drop three Constant blocks into the model.

8. Connect the output port of the first Constant block to the Arm input of the PX4 PWM Output block.

9. Double-click this Constant block (which is the Arm input of the PWM block), set the value of the Constant block as 1, and set the Output data type as boolean.

10. Connect the output port of the second Constant block to the Set failsafe input of the PWM block.

11. Double-click this Constant block (which is connected to Set failsafe input), set the value of the Constant block as 0, and set the Output data type as boolean.

12. Connect the output port of the third Constant block to selected PWM channels input of the PWM block (for example, Ch1, Ch2, and so on).

Note: To select the required PWM channels as inputs, double-click the PX4 PWM Output block. In the Block Parameters dialog box, ensure that you select all the channels that belong to the same channel group. If you need to connect to PWM channels that are in different groups, select all channels in all the desired groups. Only one PX4 PWM Output block per channel category (Main or AUX) is allowed in the entire Simulink model.

Tip: To identify the channel groups in the Pixhawk Series flight controller board, use QGroundControl application on your host computer. For details, see PX4 PWM Output (refer to the sub-topic: Parameters > PWM Channels).

13. Double-click this Constant block (which is connected to the PWM channel inputs), set the value of the Constant block as 1400, and set the Output data type as uint16.

Task 2 - Read the PWM block Using uORB Blocks

In this task, you will configure the model created in Task 1 to read the written PWM values, using the uORB topic actuator_outputs.

Note: The PWM values are published to the uORB topic actuator_outputs for the Main channels only. Also, the Pixhawk Series flight controller must have a px4io processor to read the actuator_outputs topic values. For example, Pixracer does not have the px4io processor, and does not read the actuator_outputs values.

1. From the UAV Toolbox Support Package for PX4 Autopilots tab in Library Browser, drag a PX4 uORB Read block to the model. Double-click the block.

2. Click Select next to the Topic to subscribe to field, and select actuator_outputs.msg from the pop-up window. Click OK to close the dialog box.

3. From the Simulink > Signal Routing tab in the Library Browser, drag a Bus Selector block to the model.

4. Connect the Msg output of the PX4 uORB Read block to the input port of the Bus Selector block.

5. Double-click the Bus Selector block. Add the output signal to the block output. Then, remove signal1 and signal2 from the list of output signals.

Note: If the output signal is not listed as an element of the input bus, close the dialog box. To ensure that the bus information is propagated, on the Modeling tab, click Update Model. Then, reopen the dialog box.

6. From the Simulink > Sinks tab in the Library Browser, drag a Display block to the model. Connect the output of the Bus Selector block to the Display block.

Your completed Simulink model should look like this:

Task 3 - Configure the PWM Frequency and PWM Limits

In this task, you will configure and run your model.

1. Connect the Pixhawk Series controller to the host computer using micro-USB cable.

2. Open Configuration Parameters dialog box, and set the Target Hardware as the Pixhawk Series controller you have selected during Hardware Setup screens.

3. In the Target Hardware Resources section, enter the serial port of the host computer to which the Pixhawk Series controller is connected, in the Serial port for firmware upload field. Click Apply.

4. In the PWM Frequency pane, set the frequency for the PWM signals as 400 Hz for the Main PWM Channels.

Note: Limit for setting PWM signal frequency is 50-400 Hz.

5. You can also set the Disarmed and Failsafe values for the Main and AUX PWM channels, in the Main PWM or AUX PWM panes.

6. Click Apply and then OK to close the Configuration Parameters dialog box.

Task 4 - Connect the Pixhawk Series Controller and Generate PWM

1. Connect the PWM channels of the Pixhawk Series controller for which you want to generate PWM signals to a CRO (these are the channels you selected in Task 1).

2. If your Pixhawk Series controller has a safety switch, connect the safety switch to the board.

3. In the Simulation tab, specify the stop time for parameter tuning simulation. The default value for the Stop Time parameter is 10.0 seconds. To run the model for an indefinite period, enter inf.

4. Run the model using one of the following options.

  • Monitor & Tune: To run the model for signal monitoring and parameter tuning, on the Hardware tab, in the Mode section, select Run on board and then click Monitor & Tune to start signal monitoring and parameter tuning.

Wait for the code generation to be completed. Whenever the dialog box appears instructing you to reconnect the flight controller to the serial port, click OK and then reconnect the PX4 Autopilot on the host computer.

The lower left corner of the model window displays status while Simulink prepares, downloads, and runs the model on the hardware.

  • Connected IO: To run this model in the Connected I/O mode, on the Hardware tab, in the Mode section, select Connected IO and then click Run with IO.

If the Connected IO firmware is not deployed on the hardware, then the dialog box instructing you to reconnect the flight controller to the serial port appears otherwise the dialog box is not displayed. If the dialog box appears, click OK and then reconnect the PX4 Autopilot on the host computer.

5. Observe that the PWM waveforms are not yet appearing in the CRO.

6. Press the safety switch and keep it pressed for more than 3 seconds. Release it after 3 seconds.

Note: The PWM values are published to the uORB topic actuator_outputs for the Main channels only. Also, the Pixhawk Series flight controller must have a px4io processor to read the actuator_outputs topic values (for example, Pixracer does not have the px4io processor, and does not read the actuator_outputs values).

Now you should be able to see the PWM waveform of frequency 400 Hz with an ON time value of 1400us (as entered in the Constant block), on the CRO.

The ON-time value written to the PWM block is published to the actuators_output topic, and also appears in the Display block in the model.

7. Double click on the Constant block connected to the Channel inputs of the PWM blocks. Change the value to 1600 and click OK. On the CRO, you should see a waveform with an ON time value of 1600us.

The ON-time value written to the PWM block is published to the actuators_output topic, and also appears in the Display block in the model.

8. Disarm the PWM signals by changing the value of Constant block connect to Arm input of PWM block to 0. The pulse width of the PWM waveforms will change to the disarmed values set in the Model Configuration Parameters. You can arm the PWM again by setting the value as 1.

9. You can set the failsafe mode by setting the value of Constant block connect to Set failsafe input as 1. The pulse width of the PWM waveforms will change to the failsafe values set in the Model Configuration Parameters. You can remove the failsafe mode by setting the value as 0. If the Force terminate failsafe mode option is selected in PWM Block Parameters dialog box, the failsafe mode becomes permanent once Set failsafe is set.