From 3dc17ff4554c5e4e59980839a1ca945aa99f5acf Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Thu, 4 Jun 2026 02:27:08 -0400 Subject: [PATCH] fix(commander): acknowledge DO_REPOSITION by mode, not UNSUPPORTED (#27563) DO_REPOSITION with the CHANGE_MODE flag clear (param2=0) is valid per the MAVLink spec -- "reposition without forcing a mode change" -- but Commander acked UNSUPPORTED for it while the navigator still applied the setpoint whenever armed, so an armed vehicle in Hold moved despite the UNSUPPORTED ack. Commander now acks by mode: CHANGE_MODE set switches to Hold (or stays in Course) and acks ACCEPTED/TEMPORARILY_REJECTED; param2=0 in Hold acks ACCEPTED; otherwise DENIED. It never returns UNSUPPORTED. The navigator now only applies the reposition setpoint when armed and either CHANGE_MODE was requested or it is already in Hold, so it no longer leaves a stale setpoint that a later switch into Hold could execute -- realizing the intent of the 2023 change (bec0d83), which only ever touched the ack. QGC and MAVSDK always set CHANGE_MODE, so their behavior is unchanged. Signed-off-by: Jacob Dahl --- src/modules/commander/Commander.cpp | 17 ++++++++++------- src/modules/navigator/navigator_main.cpp | 9 ++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/modules/commander/Commander.cpp b/src/modules/commander/Commander.cpp index 36569946b2d..39ffa526e77 100644 --- a/src/modules/commander/Commander.cpp +++ b/src/modules/commander/Commander.cpp @@ -868,14 +868,9 @@ Commander::handle_command(const vehicle_command_s &cmd) // to not require navigator and command to receive / process // the data at the exact same time. - const uint32_t change_mode_flags = uint32_t(cmd.param2); - const bool mode_switch_not_requested = (change_mode_flags & 1) == 0; - const bool unsupported_bits_set = (change_mode_flags & ~1) != 0; + const bool change_mode_requested = (uint32_t(cmd.param2) & 1) != 0; - if (mode_switch_not_requested || unsupported_bits_set) { - answer_command(cmd, vehicle_command_ack_s::VEHICLE_CMD_RESULT_UNSUPPORTED); - - } else { + if (change_mode_requested) { // If already in course mode, stay in course mode (navigator handles any altitude update) // Only switch to loiter if a specific lat/lon target is given, or we are not in course mode. const bool has_position_target = PX4_ISFINITE(cmd.param5) && PX4_ISFINITE(cmd.param6); @@ -891,6 +886,14 @@ Commander::handle_command(const vehicle_command_s &cmd) printRejectMode(target_state); cmd_result = vehicle_command_ack_s::VEHICLE_CMD_RESULT_TEMPORARILY_REJECTED; } + + } else if (_vehicle_status.nav_state == vehicle_status_s::NAVIGATION_STATE_AUTO_LOITER) { + // No mode switch requested: reposition the current hold setpoint in place. + cmd_result = vehicle_command_ack_s::VEHICLE_CMD_RESULT_ACCEPTED; + + } else { + // Supported, but no mode switch requested and not in hold: nothing to act on. + cmd_result = vehicle_command_ack_s::VEHICLE_CMD_RESULT_DENIED; } } break; diff --git a/src/modules/navigator/navigator_main.cpp b/src/modules/navigator/navigator_main.cpp index 5410cd9553b..0bb44140b6f 100644 --- a/src/modules/navigator/navigator_main.cpp +++ b/src/modules/navigator/navigator_main.cpp @@ -276,9 +276,12 @@ void Navigator::run() publish_vehicle_command_ack(cmd, vehicle_command_ack_s::VEHICLE_CMD_RESULT_ACCEPTED); } else if (cmd.command == vehicle_command_s::VEHICLE_CMD_DO_REPOSITION - && _vstatus.arming_state == vehicle_status_s::ARMING_STATE_ARMED) { - // only update the reposition setpoint if armed, as it otherwise won't get executed until the vehicle switches to loiter, - // which can lead to dangerous and unexpected behaviors (see loiter.cpp, there is an if(armed) in there too) + && _vstatus.arming_state == vehicle_status_s::ARMING_STATE_ARMED + && (((uint32_t)cmd.param2 & 1) != 0 + || _vstatus.nav_state == vehicle_status_s::NAVIGATION_STATE_AUTO_LOITER)) { + // Only apply the reposition setpoint when armed and either a mode switch into Hold was requested + // (CHANGE_MODE flag) or we're already in Hold. Otherwise a later switch into Hold could execute a + // stale setpoint (loiter.cpp applies it within a 500ms window). // Wait for vehicle_status before handling the next command, otherwise the setpoint could be overwritten _wait_for_vehicle_status_timestamp = hrt_absolute_time();