From dcb9b712bb12697c246effd66365354f03340b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 15 Aug 2022 09:07:47 +0200 Subject: [PATCH] refactor commander: move vehicle control mode to ModeUtil --- src/modules/commander/Commander.cpp | 139 +------------- src/modules/commander/Commander.hpp | 2 - .../HealthAndArmingChecks/Common.cpp | 2 +- src/modules/commander/ModeUtil/CMakeLists.txt | 1 + .../commander/ModeUtil/control_mode.cpp | 178 ++++++++++++++++++ .../commander/ModeUtil/control_mode.hpp | 48 +++++ .../commander/ModeUtil/mode_requirements.cpp | 5 + .../commander/ModeUtil/mode_requirements.hpp | 5 + 8 files changed, 241 insertions(+), 139 deletions(-) create mode 100644 src/modules/commander/ModeUtil/control_mode.cpp create mode 100644 src/modules/commander/ModeUtil/control_mode.hpp diff --git a/src/modules/commander/Commander.cpp b/src/modules/commander/Commander.cpp index 48a0d605f9..34ef8b8348 100644 --- a/src/modules/commander/Commander.cpp +++ b/src/modules/commander/Commander.cpp @@ -50,6 +50,7 @@ #include "esc_calibration.h" #include "px4_custom_mode.h" #include "state_machine_helper.h" +#include "ModeUtil/control_mode.hpp" /* PX4 headers */ #include @@ -2869,146 +2870,12 @@ void Commander::update_control_mode() { _vehicle_control_mode = {}; - - /* set vehicle_control_mode according to set_navigation_state */ - _vehicle_control_mode.flag_armed = _arm_state_machine.isArmed(); - - switch (_vehicle_status.nav_state) { - case vehicle_status_s::NAVIGATION_STATE_MANUAL: - _vehicle_control_mode.flag_control_manual_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = stabilization_required(); - _vehicle_control_mode.flag_control_attitude_enabled = stabilization_required(); - break; - - case vehicle_status_s::NAVIGATION_STATE_STAB: - _vehicle_control_mode.flag_control_manual_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - break; - - case vehicle_status_s::NAVIGATION_STATE_ALTCTL: - _vehicle_control_mode.flag_control_manual_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - _vehicle_control_mode.flag_control_altitude_enabled = true; - _vehicle_control_mode.flag_control_climb_rate_enabled = true; - break; - - case vehicle_status_s::NAVIGATION_STATE_POSCTL: - _vehicle_control_mode.flag_control_manual_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - _vehicle_control_mode.flag_control_altitude_enabled = true; - _vehicle_control_mode.flag_control_climb_rate_enabled = true; - _vehicle_control_mode.flag_control_position_enabled = true; - _vehicle_control_mode.flag_control_velocity_enabled = true; - break; - - case vehicle_status_s::NAVIGATION_STATE_AUTO_RTL: - case vehicle_status_s::NAVIGATION_STATE_AUTO_LAND: - case vehicle_status_s::NAVIGATION_STATE_AUTO_PRECLAND: - case vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION: - case vehicle_status_s::NAVIGATION_STATE_AUTO_LOITER: - case vehicle_status_s::NAVIGATION_STATE_AUTO_TAKEOFF: - case vehicle_status_s::NAVIGATION_STATE_AUTO_VTOL_TAKEOFF: - _vehicle_control_mode.flag_control_auto_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - _vehicle_control_mode.flag_control_altitude_enabled = true; - _vehicle_control_mode.flag_control_climb_rate_enabled = true; - _vehicle_control_mode.flag_control_position_enabled = true; - _vehicle_control_mode.flag_control_velocity_enabled = true; - break; - - case vehicle_status_s::NAVIGATION_STATE_ACRO: - _vehicle_control_mode.flag_control_manual_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - break; - - case vehicle_status_s::NAVIGATION_STATE_DESCEND: - _vehicle_control_mode.flag_control_auto_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - _vehicle_control_mode.flag_control_climb_rate_enabled = true; - break; - - case vehicle_status_s::NAVIGATION_STATE_TERMINATION: - /* disable all controllers on termination */ - _vehicle_control_mode.flag_control_termination_enabled = true; - break; - - case vehicle_status_s::NAVIGATION_STATE_OFFBOARD: - _vehicle_control_mode.flag_control_offboard_enabled = true; - - if (_offboard_control_mode_sub.get().position) { - _vehicle_control_mode.flag_control_position_enabled = true; - _vehicle_control_mode.flag_control_velocity_enabled = true; - _vehicle_control_mode.flag_control_altitude_enabled = true; - _vehicle_control_mode.flag_control_climb_rate_enabled = true; - _vehicle_control_mode.flag_control_acceleration_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - - } else if (_offboard_control_mode_sub.get().velocity) { - _vehicle_control_mode.flag_control_velocity_enabled = true; - _vehicle_control_mode.flag_control_altitude_enabled = true; - _vehicle_control_mode.flag_control_climb_rate_enabled = true; - _vehicle_control_mode.flag_control_acceleration_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - - } else if (_offboard_control_mode_sub.get().acceleration) { - _vehicle_control_mode.flag_control_acceleration_enabled = true; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - - } else if (_offboard_control_mode_sub.get().attitude) { - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - - } else if (_offboard_control_mode_sub.get().body_rate) { - _vehicle_control_mode.flag_control_rates_enabled = true; - } - - break; - - case vehicle_status_s::NAVIGATION_STATE_AUTO_FOLLOW_TARGET: - - // Follow Target supports RC adjustment, so disable auto control mode to disable - // the Flight Task from exiting itself when RC stick movement is detected. - case vehicle_status_s::NAVIGATION_STATE_ORBIT: - _vehicle_control_mode.flag_control_manual_enabled = false; - _vehicle_control_mode.flag_control_auto_enabled = false; - _vehicle_control_mode.flag_control_rates_enabled = true; - _vehicle_control_mode.flag_control_attitude_enabled = true; - _vehicle_control_mode.flag_control_altitude_enabled = true; - _vehicle_control_mode.flag_control_climb_rate_enabled = true; - _vehicle_control_mode.flag_control_position_enabled = true; - _vehicle_control_mode.flag_control_velocity_enabled = true; - break; - - default: - break; - } - - _vehicle_control_mode.flag_multicopter_position_control_enabled = - (_vehicle_status.vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROTARY_WING) - && (_vehicle_control_mode.flag_control_altitude_enabled - || _vehicle_control_mode.flag_control_climb_rate_enabled - || _vehicle_control_mode.flag_control_position_enabled - || _vehicle_control_mode.flag_control_velocity_enabled - || _vehicle_control_mode.flag_control_acceleration_enabled); - + mode_util::getVehicleControlMode(_arm_state_machine.isArmed(), _vehicle_status.nav_state, + _vehicle_status.vehicle_type, _offboard_control_mode_sub.get(), _vehicle_control_mode); _vehicle_control_mode.timestamp = hrt_absolute_time(); _vehicle_control_mode_pub.publish(_vehicle_control_mode); } -bool -Commander::stabilization_required() -{ - return _vehicle_status.vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROTARY_WING; -} - void Commander::print_reject_mode(uint8_t main_state) { diff --git a/src/modules/commander/Commander.hpp b/src/modules/commander/Commander.hpp index d8c84aef72..faa42e2e47 100644 --- a/src/modules/commander/Commander.hpp +++ b/src/modules/commander/Commander.hpp @@ -152,8 +152,6 @@ private: bool shutdown_if_allowed(); - bool stabilization_required(); - void send_parachute_command(); void checkWindSpeedThresholds(); diff --git a/src/modules/commander/HealthAndArmingChecks/Common.cpp b/src/modules/commander/HealthAndArmingChecks/Common.cpp index d236020620..6378d6773b 100644 --- a/src/modules/commander/HealthAndArmingChecks/Common.cpp +++ b/src/modules/commander/HealthAndArmingChecks/Common.cpp @@ -177,7 +177,7 @@ void Report::reset() void Report::prepare(uint8_t vehicle_type) { // Get mode requirements before running any checks (in particular the mode checks require them) - getModeRequirements(vehicle_type, _status_flags); + mode_util::getModeRequirements(vehicle_type, _status_flags); } NavModes Report::getModeGroup(uint8_t nav_state) const diff --git a/src/modules/commander/ModeUtil/CMakeLists.txt b/src/modules/commander/ModeUtil/CMakeLists.txt index 82afff9f02..dab3d3ba2e 100644 --- a/src/modules/commander/ModeUtil/CMakeLists.txt +++ b/src/modules/commander/ModeUtil/CMakeLists.txt @@ -32,5 +32,6 @@ ############################################################################ px4_add_library(mode_util + control_mode.cpp mode_requirements.cpp ) diff --git a/src/modules/commander/ModeUtil/control_mode.cpp b/src/modules/commander/ModeUtil/control_mode.cpp new file mode 100644 index 0000000000..0a07c2989d --- /dev/null +++ b/src/modules/commander/ModeUtil/control_mode.cpp @@ -0,0 +1,178 @@ +/**************************************************************************** + * + * Copyright (c) 2022 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include "control_mode.hpp" +#include + +namespace mode_util +{ + +static bool stabilization_required(uint8_t vehicle_type) +{ + return vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROTARY_WING; +} + +void getVehicleControlMode(bool armed, uint8_t nav_state, uint8_t vehicle_type, + const offboard_control_mode_s &offboard_control_mode, + vehicle_control_mode_s &vehicle_control_mode) +{ + vehicle_control_mode.flag_armed = armed; + + switch (nav_state) { + case vehicle_status_s::NAVIGATION_STATE_MANUAL: + vehicle_control_mode.flag_control_manual_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = stabilization_required(vehicle_type); + vehicle_control_mode.flag_control_attitude_enabled = stabilization_required(vehicle_type); + break; + + case vehicle_status_s::NAVIGATION_STATE_STAB: + vehicle_control_mode.flag_control_manual_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + break; + + case vehicle_status_s::NAVIGATION_STATE_ALTCTL: + vehicle_control_mode.flag_control_manual_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + vehicle_control_mode.flag_control_altitude_enabled = true; + vehicle_control_mode.flag_control_climb_rate_enabled = true; + break; + + case vehicle_status_s::NAVIGATION_STATE_POSCTL: + vehicle_control_mode.flag_control_manual_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + vehicle_control_mode.flag_control_altitude_enabled = true; + vehicle_control_mode.flag_control_climb_rate_enabled = true; + vehicle_control_mode.flag_control_position_enabled = true; + vehicle_control_mode.flag_control_velocity_enabled = true; + break; + + case vehicle_status_s::NAVIGATION_STATE_AUTO_RTL: + case vehicle_status_s::NAVIGATION_STATE_AUTO_LAND: + case vehicle_status_s::NAVIGATION_STATE_AUTO_PRECLAND: + case vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION: + case vehicle_status_s::NAVIGATION_STATE_AUTO_LOITER: + case vehicle_status_s::NAVIGATION_STATE_AUTO_TAKEOFF: + case vehicle_status_s::NAVIGATION_STATE_AUTO_VTOL_TAKEOFF: + vehicle_control_mode.flag_control_auto_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + vehicle_control_mode.flag_control_altitude_enabled = true; + vehicle_control_mode.flag_control_climb_rate_enabled = true; + vehicle_control_mode.flag_control_position_enabled = true; + vehicle_control_mode.flag_control_velocity_enabled = true; + break; + + case vehicle_status_s::NAVIGATION_STATE_ACRO: + vehicle_control_mode.flag_control_manual_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + break; + + case vehicle_status_s::NAVIGATION_STATE_DESCEND: + vehicle_control_mode.flag_control_auto_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + vehicle_control_mode.flag_control_climb_rate_enabled = true; + break; + + case vehicle_status_s::NAVIGATION_STATE_TERMINATION: + /* disable all controllers on termination */ + vehicle_control_mode.flag_control_termination_enabled = true; + break; + + case vehicle_status_s::NAVIGATION_STATE_OFFBOARD: + vehicle_control_mode.flag_control_offboard_enabled = true; + + if (offboard_control_mode.position) { + vehicle_control_mode.flag_control_position_enabled = true; + vehicle_control_mode.flag_control_velocity_enabled = true; + vehicle_control_mode.flag_control_altitude_enabled = true; + vehicle_control_mode.flag_control_climb_rate_enabled = true; + vehicle_control_mode.flag_control_acceleration_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + + } else if (offboard_control_mode.velocity) { + vehicle_control_mode.flag_control_velocity_enabled = true; + vehicle_control_mode.flag_control_altitude_enabled = true; + vehicle_control_mode.flag_control_climb_rate_enabled = true; + vehicle_control_mode.flag_control_acceleration_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + + } else if (offboard_control_mode.acceleration) { + vehicle_control_mode.flag_control_acceleration_enabled = true; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + + } else if (offboard_control_mode.attitude) { + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + + } else if (offboard_control_mode.body_rate) { + vehicle_control_mode.flag_control_rates_enabled = true; + } + + break; + + case vehicle_status_s::NAVIGATION_STATE_AUTO_FOLLOW_TARGET: + + // Follow Target supports RC adjustment, so disable auto control mode to disable + // the Flight Task from exiting itself when RC stick movement is detected. + case vehicle_status_s::NAVIGATION_STATE_ORBIT: + vehicle_control_mode.flag_control_manual_enabled = false; + vehicle_control_mode.flag_control_auto_enabled = false; + vehicle_control_mode.flag_control_rates_enabled = true; + vehicle_control_mode.flag_control_attitude_enabled = true; + vehicle_control_mode.flag_control_altitude_enabled = true; + vehicle_control_mode.flag_control_climb_rate_enabled = true; + vehicle_control_mode.flag_control_position_enabled = true; + vehicle_control_mode.flag_control_velocity_enabled = true; + break; + + default: + break; + } + + vehicle_control_mode.flag_multicopter_position_control_enabled = + (vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROTARY_WING) + && (vehicle_control_mode.flag_control_altitude_enabled + || vehicle_control_mode.flag_control_climb_rate_enabled + || vehicle_control_mode.flag_control_position_enabled + || vehicle_control_mode.flag_control_velocity_enabled + || vehicle_control_mode.flag_control_acceleration_enabled); +} + +} // namespace mode_util diff --git a/src/modules/commander/ModeUtil/control_mode.hpp b/src/modules/commander/ModeUtil/control_mode.hpp new file mode 100644 index 0000000000..9b2375424e --- /dev/null +++ b/src/modules/commander/ModeUtil/control_mode.hpp @@ -0,0 +1,48 @@ +/**************************************************************************** + * + * Copyright (c) 2022 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include +#include + +#include + +namespace mode_util +{ + +void getVehicleControlMode(bool armed, uint8_t nav_state, uint8_t vehicle_type, + const offboard_control_mode_s &offboard_control_mode, + vehicle_control_mode_s &vehicle_control_mode); + +} // namespace mode_util diff --git a/src/modules/commander/ModeUtil/mode_requirements.cpp b/src/modules/commander/ModeUtil/mode_requirements.cpp index 3cbe06a9b7..21eeea76e9 100644 --- a/src/modules/commander/ModeUtil/mode_requirements.cpp +++ b/src/modules/commander/ModeUtil/mode_requirements.cpp @@ -34,6 +34,9 @@ #include "mode_requirements.hpp" #include +namespace mode_util +{ + static inline void setRequirement(uint8_t nav_state, uint32_t &mode_requirement) { mode_requirement |= 1u << nav_state; @@ -159,3 +162,5 @@ void getModeRequirements(uint8_t vehicle_type, vehicle_status_flags_s &flags) static_assert(vehicle_status_s::NAVIGATION_STATE_MAX == 23, "update mode requirements"); } + +} // namespace mode_util diff --git a/src/modules/commander/ModeUtil/mode_requirements.hpp b/src/modules/commander/ModeUtil/mode_requirements.hpp index 64246e8697..4edd1c9285 100644 --- a/src/modules/commander/ModeUtil/mode_requirements.hpp +++ b/src/modules/commander/ModeUtil/mode_requirements.hpp @@ -35,6 +35,9 @@ #include +namespace mode_util +{ + /** * Get per-mode requirements * @param vehicle_type one of vehicle_status_s::VEHICLE_TYPE_* @@ -42,3 +45,5 @@ */ void getModeRequirements(uint8_t vehicle_type, vehicle_status_flags_s &flags); + +} // namespace mode_util