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 state.hpp 00034 * 00035 * \author MAV'RIC Team 00036 * \author Nicolas Dousse 00037 * 00038 * \brief Holds the current status of the MAV 00039 * 00040 ******************************************************************************/ 00041 00042 00043 #ifndef STATE_HPP_ 00044 #define STATE_HPP_ 00045 00046 #include "cstdint" 00047 #include <cstdbool> 00048 00049 #include "status/mav_modes.hpp" 00050 #include "communication/mavlink_stream.hpp" 00051 #include "drivers/battery.hpp" 00052 #include "communication/mavlink_message_handler.hpp" 00053 00054 00058 class State 00059 { 00060 friend class State_machine; 00061 public: 00062 00068 struct conf_t 00069 { 00070 Mav_mode mav_mode; 00071 mav_state_t mav_state; 00072 00073 Mav_mode::custom_mode_t mav_mode_custom; 00074 00075 int32_t simulation_mode; 00076 uint8_t autopilot_type; 00077 uint8_t autopilot_name; 00078 00079 uint32_t sensor_present; 00080 uint32_t sensor_enabled; 00081 uint32_t sensor_health; 00082 00083 bool out_of_safety_geofence; 00084 bool out_of_emergency_geofence; 00085 00086 bool reset_position; 00087 00088 double last_heartbeat_msg; 00089 double max_lost_connection; 00090 00091 uint32_t msg_count; 00092 00093 bool connection_lost; 00094 bool first_connection_set; 00095 }; 00096 00097 00105 State(Mavlink_stream& mavlink_stream_, Battery& battery, conf_t config = default_config()); 00106 00107 00113 void switch_to_active_mode(mav_state_t* mav_state_); 00114 00115 00119 void connection_status(); 00120 00121 00134 bool set_armed(bool arming); 00135 00136 00142 inline bool is_armed() const {return mav_mode_.is_armed();}; 00143 00144 00150 inline bool is_manual() const {return mav_mode_.is_manual();}; 00151 00152 00158 inline bool is_hil() const {return mav_mode_.is_hil();}; 00159 00160 00166 inline bool is_stabilize() const {return mav_mode_.is_stabilize();}; 00167 00168 00174 inline bool is_guided() const {return mav_mode_.is_guided();}; 00175 00176 00182 inline bool is_auto() const {return mav_mode_.is_auto();}; 00183 00189 inline bool is_custom() const {return mav_mode_.is_custom();}; 00190 00191 00197 inline bool is_test() const {return mav_mode_.is_test();}; 00198 00199 00205 inline Mav_mode mav_mode() const {return mav_mode_;}; 00206 00207 00213 static inline conf_t default_config(); 00214 00220 static inline conf_t wing_default_config(); 00221 00222 friend bool state_telemetry_set_mode(State* state, Mav_mode mav_mode); 00223 friend mav_result_t state_telemetry_send_autopilot_capabilities(State* state, const mavlink_command_long_t* packet); 00224 00225 // TODO: 00226 // All this should be private 00227 00228 mav_state_t mav_state_; 00229 Mav_mode mav_mode_; 00230 Mav_mode::custom_mode_t mav_mode_custom; 00231 00232 00233 uint8_t autopilot_type; 00234 uint8_t autopilot_name; 00235 00236 uint32_t sensor_present; 00237 uint32_t sensor_enabled; 00238 uint32_t sensor_health; 00239 00240 bool out_of_safety_geofence; 00241 bool out_of_emergency_geofence; 00242 00243 bool reset_position; 00244 00245 double last_heartbeat_msg; 00246 double max_lost_connection; 00247 00248 uint32_t msg_count; 00249 00250 bool connection_lost; 00251 bool first_connection_set; 00252 00253 Battery& battery_; 00254 00255 private: 00256 Mavlink_stream& mavlink_stream_; 00257 }; 00258 00259 00260 State::conf_t State::default_config() 00261 { 00262 conf_t conf = {}; 00263 00264 conf.mav_mode = Mav_mode::ATTITUDE; 00265 conf.mav_state = MAV_STATE_BOOT; 00266 conf.simulation_mode = false; 00267 conf.autopilot_type = MAV_TYPE_QUADROTOR; 00268 conf.autopilot_name = MAV_AUTOPILOT_GENERIC; // TODO: add MAV_AUTOPILOT_MAVRIC to std mavlink and support waypoint commands in QGC for this autopilot type 00269 conf.sensor_present = 0b1111110000100111; 00270 conf.sensor_enabled = 0b1111110000100111; 00271 conf.sensor_health = 0b1111110000100111; 00272 conf.max_lost_connection = 60.0f; 00273 00274 return conf; 00275 } 00276 00277 00278 State::conf_t State::wing_default_config() 00279 { 00280 conf_t conf = {}; 00281 00282 conf.mav_mode = Mav_mode::ATTITUDE; 00283 conf.mav_state = MAV_STATE_BOOT; 00284 conf.simulation_mode = false; 00285 conf.autopilot_type = MAV_TYPE_FIXED_WING; 00286 conf.autopilot_name = MAV_AUTOPILOT_GENERIC; // TODO: add MAV_AUTOPILOT_MAVRIC to std mavlink and support waypoint commands in QGC for this autopilot type 00287 conf.sensor_present = 0b1111110000100111; 00288 conf.sensor_enabled = 0b1111110000100111; 00289 conf.sensor_health = 0b1111110000100111; 00290 conf.max_lost_connection = 60.0f; 00291 00292 return conf; 00293 } 00294 00295 #endif //STATE_HPP_