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

Embedded Coder 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 example model for generating PWM signals:

open_system('px4demo_PWM');

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. From the model window, select View > Library Browser to open the Simulink Library Browser. Click the Embedded Coder Support Package for PX4 Autopilots tab (you can also type px4lib at the MATLAB command prompt).

3. Drag and drop a PX4 PWM Output block to the model. Double-click the block to open the block mask 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 mask exist on the Pixhawk board.

6. Click Apply and then OK to close the block mask.

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 on 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 on 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 mask, 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 on 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 Embedded Coder Support Package for PX4 Autopilots tab in Library Browser, drag and drop 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 block mask.

3. From the Simulink > Signal Routing tab in the Library Browser, drag and drop 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. Click on Modeling > Update Model to ensure that the bus information is propagated. You may get an error, Selected signal 'signal1' in the Bus Selector block 'untitled/Bus Selector' cannot be found in the input bus signal. This error is expected, and will be resolved by the next step.

6. Double-click the Bus Selector block. Select ??? signal1 and ??? signal2 in the list on the right and click Remove. Select the output signals in the list on the left, and click Select>>. Click OK.

7. From the Simulink > Sinks tab in the Library Browser, drag and drop 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.

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 Hardware tab of the Simulink toolstrip, 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, ensure that you click OK on the dialog box within 5 seconds after reconnecting the flight controller.

4. After the signal monitoring starts, you may observe that the PWM waveforms are not yet appearing in the CRO.

5. 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.

6. 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.

7. 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.

8. 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 mask, the failsafe mode becomes permanent once Set failsafe is set.