MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file mav_mode.hpp 00034 * 00035 * \author MAV'RIC Team 00036 * \author Julien Lecoeur 00037 * 00038 * \brief Place where the mav modes and flags are defined 00039 * 00040 ******************************************************************************/ 00041 00042 00043 #ifndef MAV_MODE_H 00044 #define MAV_MODE_H 00045 00046 #include <cstdint> 00047 #include <cstdbool> 00048 00049 #include "communication/mavlink_stream.hpp" 00050 #include "util/print_util.hpp" 00051 00052 00053 typedef enum MAV_STATE mav_state_t; 00054 00055 class Mav_mode 00056 { 00057 public: 00058 typedef uint8_t mode_bits_t; 00059 typedef uint8_t bitmask_t; 00060 typedef uint32_t custom_mode_t; 00061 00062 00063 /* 00064 * \brief enum for defining custom mode 00065 */ 00066 typedef enum 00067 { 00068 CUSTOM_BASE_MODE = 0, 00069 CUST_CRITICAL_CLIMB_TO_SAFE_ALT = 1, 00070 CUST_CRITICAL_FLY_TO_HOME_WP = 2, 00071 CUST_CRITICAL_LAND = 4, 00072 00073 CUST_DESCENT_TO_SMALL_ALTITUDE = 8, 00074 CUST_DESCENT_TO_GND = 16, 00075 00076 CUST_COLLISION_AVOIDANCE = 32, 00077 00078 CUST_BATTERY_LOW = 64, 00079 CUST_FENCE_1 = 128, 00080 CUST_FENCE_2 = 256, 00081 CUST_HEARTBEAT_LOST = 512, 00082 CUST_REMOTE_LOST = 1024, 00083 CUST_GPS_BAD = 2048 00084 } custom_mode_list_t; 00085 00086 00087 /* Control modes (predefined mode taking into account manual, stabilize, guided, and auto flag) */ 00088 enum ctrl_mode_t 00089 { 00090 RATE = MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, 00091 ATTITUDE = MAV_MODE_FLAG_MANUAL_INPUT_ENABLED + MAV_MODE_FLAG_STABILIZE_ENABLED, 00092 VELOCITY = MAV_MODE_FLAG_MANUAL_INPUT_ENABLED + MAV_MODE_FLAG_STABILIZE_ENABLED + MAV_MODE_FLAG_GUIDED_ENABLED, 00093 POSITION_HOLD = MAV_MODE_FLAG_STABILIZE_ENABLED + MAV_MODE_FLAG_GUIDED_ENABLED, 00094 AUTO = MAV_MODE_FLAG_STABILIZE_ENABLED + MAV_MODE_FLAG_GUIDED_ENABLED + MAV_MODE_FLAG_AUTO_ENABLED 00095 }; 00096 00097 /* describes which flags are used for ctrl_mode */ 00098 static const bitmask_t CTRL_MODE_BITFIELD = 0b01011100; 00099 00100 Mav_mode() : bits_(0){}; 00101 00102 Mav_mode(const mode_bits_t& bits) : bits_(bits){}; 00103 00104 Mav_mode(const Mav_mode& mav_mode) : bits_(mav_mode.bits_){}; 00105 00111 inline mode_bits_t bits() const {return bits_;}; 00112 00120 inline const uint8_t* bits_ptr() const {return static_cast<const uint8_t*>(&bits_);}; 00121 00122 00128 inline bool is_armed() const {return is_flag_set(MAV_MODE_FLAG_SAFETY_ARMED);}; 00129 00135 inline bool is_manual() const {return is_flag_set(MAV_MODE_FLAG_MANUAL_INPUT_ENABLED);}; 00136 00142 inline bool is_hil() const {return is_flag_set(MAV_MODE_FLAG_HIL_ENABLED);}; 00143 00149 inline bool is_stabilize() const {return is_flag_set(MAV_MODE_FLAG_STABILIZE_ENABLED);}; 00150 00156 inline bool is_guided() const {return is_flag_set(MAV_MODE_FLAG_GUIDED_ENABLED);}; 00157 00163 inline bool is_auto() const {return is_flag_set(MAV_MODE_FLAG_AUTO_ENABLED);}; 00164 00170 inline bool is_test() const {return is_flag_set(MAV_MODE_FLAG_TEST_ENABLED);}; 00171 00177 inline bool is_custom() const {return is_flag_set(MAV_MODE_FLAG_CUSTOM_MODE_ENABLED);}; 00178 00184 inline ctrl_mode_t ctrl_mode() const {return (ctrl_mode_t)(bits_ & CTRL_MODE_BITFIELD);}; 00185 00192 inline void set_armed_flag(bool value) { set_flag(MAV_MODE_FLAG_SAFETY_ARMED, value);}; 00193 00200 inline void set_manual_flag(bool value) { set_flag(MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, value);}; 00201 00208 inline void set_hil_flag(bool value) { set_flag(MAV_MODE_FLAG_HIL_ENABLED, value);}; 00209 00216 inline void set_stabilize_flag(bool value) { set_flag(MAV_MODE_FLAG_STABILIZE_ENABLED, value);}; 00217 00224 inline void set_guided_flag(bool value) { set_flag(MAV_MODE_FLAG_GUIDED_ENABLED, value);}; 00225 00232 inline void set_auto_flag(bool value) { set_flag(MAV_MODE_FLAG_AUTO_ENABLED, value);}; 00233 00240 inline void set_test_flag(bool value) { set_flag(MAV_MODE_FLAG_TEST_ENABLED, value);}; 00241 00248 inline void set_custom_flag(bool value) { set_flag(MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, value);}; 00249 00255 inline void set_ctrl_mode(ctrl_mode_t ctrl_mode) {bits_ = (bits_ & ~CTRL_MODE_BITFIELD) + (ctrl_mode & CTRL_MODE_BITFIELD);}; 00256 00262 bool operator == (const Mav_mode& mav_mode) const {return equal(mav_mode);}; 00263 00264 private: 00265 mode_bits_t bits_; 00266 00274 inline bool is_flag_set(MAV_MODE_FLAG flag) const {return ((bits_ & flag) == flag);}; 00275 00282 inline void set_flag(MAV_MODE_FLAG flag, bool value) { bits_ = value ? bits_ | flag : bits_ & ~flag;}; 00283 00289 inline bool equal(Mav_mode mav_mode) const {return bits_ == mav_mode.bits_;}; 00290 00297 inline void set_mode(mode_bits_t bits) {bits_ = bits;}; 00298 00299 }; 00300 00301 00305 typedef enum 00306 { 00307 ARM_ACTION_DISARMING = -1, 00308 ARM_ACTION_NONE = 0, 00309 ARM_ACTION_ARMING = 1, 00310 } arm_action_t; 00311 00312 00313 #endif //MAV_MODE_H