Timed actions within the run() function of a new flight mode

Hello everyone,

I made a ballistic launch flight mode and it’s “working” as in my first tests indicate it is at least willing to do what I tell it, I think. When I shake my copter it begins to try to spin the motors to try to stabilize. I would like it to stop doing that after lets say 6 seconds OR even better after I flip an AUX switch and then resets back to checking for ballistic launch - kind of a show-proof-of-concept-mode.
This code works

if(launchDetected)     {
        motors->armed(true);
        motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
        attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(0.0f, 0.0f, 0.0f);
        attitude_control->set_throttle_out(0.1f, false, g.throttle_filt);
}

So in other words I am looking for a way to execute a certain function within the run() function of a flight mode for only a certain amount of time. Trying AP_HAL::millis() and hal.scheduler->delay(300); both didn’t work for me.

Any help is appreciated. :smiley:

What exactly went wrong with this? It should work if your logics behind it were right.

2 Likes

Inside my flight mode definition in mode.h I privately declare the variable
static uint32_t now_ms;
In the run() function in my mode_launch.cpp once a launch has been detected the variable is initialized with
now_ms = AP_HAL::millis()
Later I want to use a logical check if 2000 ms have elapsed since launch
if(launchDetected && AP_HAL::millis() - now_ms >= 2000)
However it never enters the loop. I think I am misunderstanding the values AP_HAL::millis() outputs and how to work with them then.

Have you tried without static declaration? Not sure about needing it…

You can use SITL simulation and add some callings to GCS_SEND_TEXT function to check if you are not entering in this loop. If it is being called by run(), it should enter (at least if you are not returning from it earlier by any condition).

1 Like