Lua: delay with scripted flight mode changing

I’m trying to switch my plane into FBWA mode during flight using the attached code (only sharing the relevant part, not the full script). My goal is to land the plane using the script, as the terrain maps in my area are quite inconsistent. To achieve this, I switch the mode to FBWA at a certain distance from the last waypoint, set the throttle to 0%, and try to maintain a pitch of approximately 1 degree.

However, even though the flight mode switch is supposed to happen before reducing the throttle to 0%, in actual flight, the plane switches to FBWA mode 3–4 seconds after the throttle has already dropped to 0. I would greatly appreciate any advice on why this delay occurs.

local landing_flag = false

function land()
    if not landing_flag then
        if check_waypoint_distance() then
            vehicle:set_mode(5)
            landing_flag = true
        end
    else
        local current_roll = math.deg(ahrs:get_roll())
        local current_pitch = math.deg(ahrs:get_pitch())

        local pitch_rate = pitch_pi.update(LANDING_PITCH, current_pitch)
        local roll_rate = roll_pi.update(0, current_roll)

        local throttle = 0

        vehicle:nav_scripting_enable(vehicle:get_mode())
        vehicle:set_target_throttle_rate_rpy(throttle, roll_rate, pitch_rate, 0)
    end

    return land, FREQUENCY
end

I would recommend using landing mission with ASML waypoint altitudes.

Hello Harper,
welcome to the forum.

I can’t work out from the LUA fragment alone why there is a delay in switching to FBWA. Perhaps you could post the complete script or a link to it? In any case, I am very interested:

I also have this problem at our airfield with the LAND function, as the terrain in front of the runway threshold is significantly higher than the runway. With a LIDAR sensor to initiate the flair precisely, the aircraft suddenly climbs before the runway threshold because Arduplane assumes it is approaching too low.
So far, my idea is to use RTK-GPS instead of LIDAR to determine the altitude on final approach.

But I also find your solution very exciting.

Rolf

The delay may simply be in telemetry reporting vs an actual delay in mode change. IIRC, flight mode is not downlinked at a very high rate by default.

Agree that the code snippet provided gives no indication of potential issue (unless the FREQUENCY variable is quite large).

Sorry for the late response.

And my apologies for opening this topic too quickly - I have investigated the logs and indeed, there is no delay in the flight mode change. The issue was that my mission had the LAND command as the final waypoint (in case my script didn’t work as expected), and it was activated before my script was initialized. I didn’t expect LAND to work this way - completely shutting off the engine - and I mistakenly thought it was caused by my script. However, my script was only initialized a few seconds later.

I’ve adjusted the initialization condition so that the plane will land by my script well before the regular LAND command triggers.

1 Like