Custom flight mode using SRV_Channels::set_output_pwm_chan() not working

I created a new flight mode and am trying to directly send a PWM command to the throttle and aileron channels using the lines:

SRV_Channels::set_output_pwm_chan(SRV_Channel::k_aileron, 1900)
and
SRV_Channels::set_output_pwm_chan(SRV_Channel::k_throttle, 1300)

but it doesn’t seem to be doing anything.

I am testing this on the ground, and I know that the code is entering the ::update() function of the new flight mode because I’ve added a hal.console->printf("updating") for debugging purposes and am watching the serial monitor.

FWIW, I have all arming checks disabled, and also one thing I can’t figure out is that when I’m in this new mode, I can’t seem to move the throttle to spin up the motors but my control surfaces can move (throttle not working with both plane.throttle_allows_nudging = false and = true).

Here’s how my flight mode code looks like:

#include "Plane.h"
#include "mode.h"

ModeTest::ModeTest() {
	// Do nothing
}

bool ModeTest::_enter() {
    plane.throttle_allows_nudging = false;
	hal.console->printf("Entered\n");

    return true;
}

void ModeTest::update() {

	SRV_Channels::set_output_pwm_chan(SRV_Channel::k_aileron, 1900);  
	SRV_Channels::set_output_pwm_chan(SRV_Channel::k_throttle, 1300); 
	hal.console->printf("updating\n");
}

void ModeTest::_exit() {
	// Do nothing
}

Try using set output scaled, discord is a good place for code advice,

Hey Peter, that sort of worked!

I am able to spin up the motors now using set_output_scaled(SRV_Channel::k_throttle, 10); (for 10% throttle). But I still can’t seem to move the control surfaces.

From adding some hal.console->printf() statements in manual mode, it looks like the scaled output ranges for the k_aileron, k_elevator, and k_rudder range from -4500, 4500. I then tried these lines:

SRV_Channels::set_output_scaled(SRV_Channel::k_aileron, 4500); 
SRV_Channels::set_output_scaled(SRV_Channel::k_rudder, -4500); 
SRV_Channels::set_output_scaled(SRV_Channel::k_elevator, 4000);

But it doesn’t seem to move the servos at all (also tried ranges from [-1, 1] (normalized ranges), [1000, 2000] (pwm ranges), and [0, 100] (percentage ranges)).

I also still can’t figure out why in my new flight mode I am able to manually move the servos but not the throttle. FWIW, it also looks like when I manually move the aileron, the rudder servo also moves, which I believe implies that it’s performing a coordinated turn. I’m not exactly sure how I enabled coordinated turns for this flight mode.

And thank you for the Discord suggestion, will do that for future code questions.

To answer one of my questions for anyone reading this thread:

I also still can’t figure out why in my new flight mode I am able to manually move the servos but not the throttle. FWIW, it also looks like when I manually move the aileron, the rudder servo also moves, which I believe implies that it’s performing a coordinated turn. I’m not exactly sure how I enabled coordinated turns for this flight mode.

There is a conditional in servos.cpp that looks at the flight mode and dictates if the servos should behave as a manual passthrough or if the servos should be controlled by the autopilot:

if (control_mode == &mode_manual) {
        set_servos_manual_passthrough();
} else {
        set_servos_controlled();
}

hey bro! I guess u may put these codes in the wrong place. I put these codes in servo.app and succeed -4500 4500 is available