Set different PWM rates and ranges for MAIN1-4 and MAIN5-8 during system boot

I am programming a Pixhawk MINI, the 8 PMW outputs from pixhawk MINI are all MAIN outputs.

I want to set the PWM rate of the first 4 MAIN outputs to be 400Hz for ESCs, and the PWM rate of the last 4 MAIN outputs to be 50Hz for servo motors. I have written the following startup script


if [ $AUTOCNF == yes ]
	param set NAV_ACC_RAD 2.0

	param set RTL_RETURN_ALT 30.0
	param set RTL_DESCEND_ALT 10.0
	param set RTL_LAND_DELAY 0

set MIXER kly_delta_x3

param set PWM_MAX 1950
param set PWM_MIN 1075
set PWM_OUT 1234
set PWM_RATE 400

param set PWM_MAX 2200
param set PWM_MIN 800
set PWM_OUT 5678
set PWM_RATE 50

However, this startup script gives me 50Hz pwm rate for all 8 outputs. I have been changing this scripts in all different ways but could not set them at different PWM rates and ranges.

I am wondering what does “set PWM_OUT 1234” do exactly? Should I put the settings for 1234 before this line or after this line for them to take effect?

Please suggest what is the correct way to set different PWM rates and ranges for MAIN 1234 and 5678 in startup script.

Thank you!

Hi @hankyang94 ,

The variables PWM_OUT and PWM_RATE are used in rc.interface which is executed after this startup script. Your second block of code then just overwrites the first one and the parameters applied will be:

PWM_MAX 2200
PWM_OUT 5678

Basically, there are two possibles rates:

  1. the default rate: 50Hz
  2. the alternative rate: defined by PWM_RATE

And each group of outputs can be set to the default rate or the alternative rate. A group is defined by the Timer that controls a set of outputs (i.e: on the Pixhawk4 (fmuv5), the groups for FMU are [1 2 3 4], [5 6] and [7 8], check groups using pwm info). By setting a (or several) groups(s) using pwm rate, to a certain rate, it will set all the other groups to the default rate.

Let’s take a few examples (try in the Mavlink console) :
pwm rate -c 1234 -r 400 // sets outputs 1,2,3 and 4 to 400Hz and 5,6,7 and 8 to 50Hz
pwm rate -c 56 -r 400 // sets outputs 5 and 6 to 400Hz and 1,2,3,4,7 and 8 to 50Hz
pwm rate -c 123478 -r 300 // sets outputs 1,2,3,4,7 and 8 to 300Hz and 5 and 6 to 50Hz
pwm rate -c 12 -r 400// ERROR, not a complete group

Since in rc.interface the rates are set using pwm rate -c ${PWM_OUT} -r ${PWM_RATE}, you should use:

param set PWM_RATE 400
set PWM_OUT 1234

Note: “param set” sets a parameter in the autopilot, “set” sets a variable used in rcS, rc.interface, … and “set <var_name> p:<param_name>” is used to set the variable with the parameter value.

About PWM_MIN and PWM_MAX, rc.interface applies PWM_MIN/MAX to the PWM_OUT defined. You can’t apply a specific min and max to a specific channel from the startup script. To handle this, there are two solutions:

  1. Add support by creating a new parameter per channel (like PWM_MAIN/AUX_DISx/FAILx). This will require to create 32 new parameters.
  2. Hack in your rc.interface file and add a few pwm min/max -c <channel(s)> -p <value> commands.

Hope it helped

Hi @bresch, thanks for your help. I hacked my rc.interface file and it worked well.

However, I do want to know how should I go for the first solution, i.e. create new parameters for each channel? Could you explain in more details?

Thank you very much!

To add the min and max parameters, you should basically do that.

But as it will add 32 new parameters I’m not sure if we want to have that… @dagar What is your opinion?

I think all of this needs to be broken out as parameters. It’s just far too inaccessible for most users in these init scripts.

@dagar So I guess this feature will be incorporated into the master branch of PX4 soon?

@hankyang94 You can add the parameters and open a PR or I can take a few minutes next week to do that.

@hankyang94 I just added the parameters here.

1 Like