From d995af218457e5b170189948e8e664fcb07ce72b Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Tue, 17 Nov 2026 14:00:37 +1300 Subject: [PATCH] fix(commander/mavlink): DO_SET_HOME improvements (#29786) * fix(commander): accept only 0 or 1 as DO_SET_HOME param1 MAV_CMD_DO_SET_HOME param1 is a MAV_BOOL: 1 uses the current position, 0 the specified location, and any other value is invalid. Any value above 0.5 was taken as "use current", so an invalid value silently set home to the current position instead of being denied. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Julian Oes * fix(mavlink): honour the COMMAND_INT frame for DO_SET_HOME The frame of a COMMAND_INT was only used to scale x/y and then dropped, and Commander takes the DO_SET_HOME altitude as AMSL. A relative altitude, as sent by MAVProxy, therefore set home to that many metres above mean sea level, and local frames gave meaningless coordinates. A global altitude is passed on as before, a relative altitude is converted to AMSL using the current home altitude, and other frames are rejected with MAV_RESULT_COMMAND_UNSUPPORTED_MAV_FRAME. The frame does not matter when the current position is used. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Julian Oes * fix(commander): allow DO_SET_HOME with COM_HOME_EN disabled COM_HOME_EN is documented as setting home automatically, but disabling it also denied MAV_CMD_DO_SET_HOME, leaving the vehicle without any way to get a home. Disabling it now only stops home from being set automatically, and home can still be set manually. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Julian Oes --------- Signed-off-by: Julian Oes --- src/modules/commander/Commander.cpp | 13 ++++----- src/modules/commander/commander_params.yaml | 2 ++ src/modules/mavlink/mavlink_receiver.cpp | 29 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/modules/commander/Commander.cpp b/src/modules/commander/Commander.cpp index 0aece4d0b2e..2b4a97257dc 100644 --- a/src/modules/commander/Commander.cpp +++ b/src/modules/commander/Commander.cpp @@ -1155,8 +1155,13 @@ Commander::handle_command(const vehicle_command_s &cmd) break; case vehicle_command_s::VEHICLE_CMD_DO_SET_HOME: { - if (_param_com_home_en.get()) { - bool use_current = cmd.param1 > 0.5f; + // COM_HOME_EN only disables setting home automatically, it can still be set manually + if (!(fabsf(cmd.param1) < FLT_EPSILON) && !(fabsf(cmd.param1 - 1.f) < FLT_EPSILON)) { + // param1 is a MAV_BOOL: 1 uses the current position, 0 the specified location + cmd_result = vehicle_command_ack_s::VEHICLE_CMD_RESULT_DENIED; + + } else { + const bool use_current = fabsf(cmd.param1 - 1.f) < FLT_EPSILON; if (use_current) { /* use current position */ @@ -1192,10 +1197,6 @@ Commander::handle_command(const vehicle_command_s &cmd) cmd_result = vehicle_command_ack_s::VEHICLE_CMD_RESULT_DENIED; } } - - } else { - // COM_HOME_EN disabled - cmd_result = vehicle_command_ack_s::VEHICLE_CMD_RESULT_DENIED; } } break; diff --git a/src/modules/commander/commander_params.yaml b/src/modules/commander/commander_params.yaml index f61427ef05a..7ff0cbf87af 100644 --- a/src/modules/commander/commander_params.yaml +++ b/src/modules/commander/commander_params.yaml @@ -44,6 +44,8 @@ parameters: long: |- Set home position automatically if possible. + If disabled, home is only set manually with MAV_CMD_DO_SET_HOME. + During missions, the latitude/longitude of the home position is locked and will not reset during intermediate landings. It will only update once the mission is complete or landed outside of a mission. However, the altitude is still being adjusted to correct for GNSS vertical drift in the first 2 minutes after takeoff. diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index 31421ce0d90..8a939c31ebc 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -717,6 +717,35 @@ MavlinkReceiver::handle_message_command_int(mavlink_message_t *msg) vcmd.confirmation = false; vcmd.from_external = true; + // The frame is not passed on to Commander, which takes the DO_SET_HOME altitude as AMSL. + // The frame only matters if the location is given (param1 MAV_BOOL_FALSE), not when the + // current position is used (MAV_BOOL_TRUE). Other param1 values are denied by Commander. + if (cmd_mavlink.command == MAV_CMD_DO_SET_HOME && fabsf(cmd_mavlink.param1) < FLT_EPSILON + && evaluate_target_ok(vcmd.command, vcmd.target_system, vcmd.target_component)) { + if (cmd_mavlink.frame == MAV_FRAME_GLOBAL || cmd_mavlink.frame == MAV_FRAME_GLOBAL_INT) { + // already AMSL + + } else if (cmd_mavlink.frame == MAV_FRAME_GLOBAL_RELATIVE_ALT + || cmd_mavlink.frame == MAV_FRAME_GLOBAL_RELATIVE_ALT_INT) { + home_position_s home_position{}; + _home_position_sub.copy(&home_position); + + if (!home_position.valid_alt) { + // the altitude is relative to a home that is not set yet + acknowledge(msg->sysid, msg->compid, cmd_mavlink.command, + vehicle_command_ack_s::VEHICLE_CMD_RESULT_TEMPORARILY_REJECTED); + return; + } + + vcmd.param7 = home_position.alt + cmd_mavlink.z; + + } else { + acknowledge(msg->sysid, msg->compid, cmd_mavlink.command, + vehicle_command_ack_s::VEHICLE_CMD_RESULT_UNSUPPORTED_MAV_FRAME); + return; + } + } + handle_message_command_both(msg, vcmd); }