Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/ArduPlane/mode.h
Views: 1798
#pragma once12#include <AP_Param/AP_Param.h>3#include <AP_Common/Location.h>4#include <stdint.h>5#include <AP_Soaring/AP_Soaring.h>6#include <AP_ADSB/AP_ADSB.h>7#include <AP_Vehicle/ModeReason.h>8#include "quadplane.h"9#include <AP_AHRS/AP_AHRS.h>10#include <AP_Mission/AP_Mission.h>11#include "pullup.h"1213#ifndef AP_QUICKTUNE_ENABLED14#define AP_QUICKTUNE_ENABLED HAL_QUADPLANE_ENABLED15#endif1617#ifndef MODE_AUTOLAND_ENABLED18#define MODE_AUTOLAND_ENABLED 119#endif2021#include <AP_Quicktune/AP_Quicktune.h>2223class AC_PosControl;24class AC_AttitudeControl_Multi;25class AC_Loiter;26class Mode27{28public:2930/* Do not allow copies */31CLASS_NO_COPY(Mode);3233// Auto Pilot modes34// ----------------35enum Number : uint8_t {36MANUAL = 0,37CIRCLE = 1,38STABILIZE = 2,39TRAINING = 3,40ACRO = 4,41FLY_BY_WIRE_A = 5,42FLY_BY_WIRE_B = 6,43CRUISE = 7,44AUTOTUNE = 8,45AUTO = 10,46RTL = 11,47LOITER = 12,48TAKEOFF = 13,49AVOID_ADSB = 14,50GUIDED = 15,51INITIALISING = 16,52#if HAL_QUADPLANE_ENABLED53QSTABILIZE = 17,54QHOVER = 18,55QLOITER = 19,56QLAND = 20,57QRTL = 21,58#if QAUTOTUNE_ENABLED59QAUTOTUNE = 22,60#endif61QACRO = 23,62#endif63THERMAL = 24,64#if HAL_QUADPLANE_ENABLED65LOITER_ALT_QLAND = 25,66#endif67#if MODE_AUTOLAND_ENABLED68AUTOLAND = 26,69#endif7071// Mode number 30 reserved for "offboard" for external/lua control.72};7374// Constructor75Mode();7677// enter this mode, always returns true/success78bool enter();7980// perform any cleanups required:81void exit();8283// run controllers specific to this mode84virtual void run();8586// returns a unique number specific to this mode87virtual Number mode_number() const = 0;8889// returns full text name90virtual const char *name() const = 0;9192// returns a string for this flightmode, exactly 4 bytes93virtual const char *name4() const = 0;9495// returns true if the vehicle can be armed in this mode96bool pre_arm_checks(size_t buflen, char *buffer) const;9798// Reset rate and steering and TECS controllers99void reset_controllers();100101//102// methods that sub classes should override to affect movement of the vehicle in this mode103//104105// convert user input to targets, implement high level control for this mode106virtual void update() = 0;107108// true for all q modes109virtual bool is_vtol_mode() const { return false; }110virtual bool is_vtol_man_throttle() const;111virtual bool is_vtol_man_mode() const { return false; }112113// guided or adsb mode114virtual bool is_guided_mode() const { return false; }115116// true if mode can have terrain following disabled by switch117virtual bool allows_terrain_disable() const { return false; }118119// true if automatic switch to thermal mode is supported.120virtual bool does_automatic_thermal_switch() const {return false; }121122// subclasses override this if they require navigation.123virtual void navigate() { return; }124125// this allows certain flight modes to mix RC input with throttle126// depending on airspeed_nudge_cm127virtual bool allows_throttle_nudging() const { return false; }128129// true if the mode sets the vehicle destination, which controls130// whether control input is ignored with STICK_MIXING=0131virtual bool does_auto_navigation() const { return false; }132133// true if the mode sets the vehicle destination, which controls134// whether control input is ignored with STICK_MIXING=0135virtual bool does_auto_throttle() const { return false; }136137// true if the mode supports autotuning (via switch for modes other138// that AUTOTUNE itself139virtual bool mode_allows_autotuning() const { return false; }140141// method for mode specific target altitude profiles142virtual void update_target_altitude();143144// handle a guided target request from GCS145virtual bool handle_guided_request(Location target_loc) { return false; }146147// true if is landing148virtual bool is_landing() const { return false; }149150// true if is taking151virtual bool is_taking_off() const;152153// true if throttle min/max limits should be applied154virtual bool use_throttle_limits() const;155156// true if voltage correction should be applied to throttle157virtual bool use_battery_compensation() const;158159#if AP_QUICKTUNE_ENABLED160// does this mode support VTOL quicktune?161virtual bool supports_quicktune() const { return false; }162#endif163164protected:165166// subclasses override this to perform checks before entering the mode167virtual bool _enter() { return true; }168169// subclasses override this to perform any required cleanup when exiting the mode170virtual void _exit() { return; }171172// mode specific pre-arm checks173virtual bool _pre_arm_checks(size_t buflen, char *buffer) const;174175// Helper to output to both k_rudder and k_steering servo functions176void output_rudder_and_steering(float val);177178// Output pilot throttle, this is used in stabilized modes without auto throttle control179void output_pilot_throttle();180181// makes the initialiser list in the constructor manageable182uint8_t unused_integer;183184#if HAL_QUADPLANE_ENABLED185// References for convenience, used by QModes186AC_PosControl*& pos_control;187AC_AttitudeControl_Multi*& attitude_control;188AC_Loiter*& loiter_nav;189QuadPlane& quadplane;190QuadPlane::PosControlState &poscontrol;191#endif192AP_AHRS& ahrs;193};194195196class ModeAcro : public Mode197{198friend class ModeQAcro;199public:200201Mode::Number mode_number() const override { return Mode::Number::ACRO; }202const char *name() const override { return "ACRO"; }203const char *name4() const override { return "ACRO"; }204205// methods that affect movement of the vehicle in this mode206void update() override;207208void run() override;209210void stabilize();211212void stabilize_quaternion();213214protected:215216// ACRO controller state217struct {218bool locked_roll;219bool locked_pitch;220float locked_roll_err;221int32_t locked_pitch_cd;222Quaternion q;223bool roll_active_last;224bool pitch_active_last;225bool yaw_active_last;226} acro_state;227228bool _enter() override;229};230231class ModeAuto : public Mode232{233public:234friend class Plane;235236Number mode_number() const override { return Number::AUTO; }237const char *name() const override { return "AUTO"; }238const char *name4() const override { return "AUTO"; }239240bool does_automatic_thermal_switch() const override { return true; }241242// methods that affect movement of the vehicle in this mode243void update() override;244245void navigate() override;246247bool allows_throttle_nudging() const override { return true; }248249bool does_auto_navigation() const override;250251bool does_auto_throttle() const override;252253bool mode_allows_autotuning() const override { return true; }254255bool is_landing() const override;256257void do_nav_delay(const AP_Mission::Mission_Command& cmd);258bool verify_nav_delay(const AP_Mission::Mission_Command& cmd);259260bool verify_altitude_wait(const AP_Mission::Mission_Command& cmd);261262void run() override;263264#if AP_PLANE_GLIDER_PULLUP_ENABLED265bool in_pullup() const { return pullup.in_pullup(); }266#endif267268protected:269270bool _enter() override;271void _exit() override;272bool _pre_arm_checks(size_t buflen, char *buffer) const override;273274private:275276// Delay the next navigation command277struct {278uint32_t time_max_ms;279uint32_t time_start_ms;280} nav_delay;281282// wiggle state and timer for NAV_ALTITUDE_WAIT283void wiggle_servos();284struct {285uint8_t stage;286uint32_t last_ms;287} wiggle;288289#if AP_PLANE_GLIDER_PULLUP_ENABLED290GliderPullup pullup;291#endif // AP_PLANE_GLIDER_PULLUP_ENABLED292};293294295class ModeAutoTune : public Mode296{297public:298299Number mode_number() const override { return Number::AUTOTUNE; }300const char *name() const override { return "AUTOTUNE"; }301const char *name4() const override { return "ATUN"; }302303// methods that affect movement of the vehicle in this mode304void update() override;305306bool mode_allows_autotuning() const override { return true; }307308void run() override;309310protected:311312bool _enter() override;313};314315class ModeGuided : public Mode316{317public:318319Number mode_number() const override { return Number::GUIDED; }320const char *name() const override { return "GUIDED"; }321const char *name4() const override { return "GUID"; }322323// methods that affect movement of the vehicle in this mode324void update() override;325326void navigate() override;327328virtual bool is_guided_mode() const override { return true; }329330bool allows_throttle_nudging() const override { return true; }331332bool does_auto_navigation() const override { return true; }333334bool does_auto_throttle() const override { return true; }335336// handle a guided target request from GCS337bool handle_guided_request(Location target_loc) override;338339void set_radius_and_direction(const float radius, const bool direction_is_ccw);340341void update_target_altitude() override;342343protected:344345bool _enter() override;346bool _pre_arm_checks(size_t buflen, char *buffer) const override { return true; }347#if AP_QUICKTUNE_ENABLED348bool supports_quicktune() const override { return true; }349#endif350351private:352float active_radius_m;353};354355class ModeCircle: public Mode356{357public:358359Number mode_number() const override { return Number::CIRCLE; }360const char *name() const override { return "CIRCLE"; }361const char *name4() const override { return "CIRC"; }362363// methods that affect movement of the vehicle in this mode364void update() override;365366bool does_auto_navigation() const override { return true; }367368bool does_auto_throttle() const override { return true; }369370protected:371372bool _enter() override;373};374375class ModeLoiter : public Mode376{377public:378379Number mode_number() const override { return Number::LOITER; }380const char *name() const override { return "LOITER"; }381const char *name4() const override { return "LOIT"; }382383// methods that affect movement of the vehicle in this mode384void update() override;385386void navigate() override;387388bool isHeadingLinedUp(const Location loiterCenterLoc, const Location targetLoc);389bool isHeadingLinedUp_cd(const int32_t bearing_cd, const int32_t heading_cd);390bool isHeadingLinedUp_cd(const int32_t bearing_cd);391392bool allows_throttle_nudging() const override { return true; }393394bool does_auto_navigation() const override { return true; }395396bool does_auto_throttle() const override { return true; }397398bool allows_terrain_disable() const override { return true; }399400void update_target_altitude() override;401402bool mode_allows_autotuning() const override { return true; }403404protected:405406bool _enter() override;407};408409#if HAL_QUADPLANE_ENABLED410class ModeLoiterAltQLand : public ModeLoiter411{412public:413414Number mode_number() const override { return Number::LOITER_ALT_QLAND; }415const char *name() const override { return "Loiter to QLAND"; }416const char *name4() const override { return "L2QL"; }417418// handle a guided target request from GCS419bool handle_guided_request(Location target_loc) override;420421protected:422bool _enter() override;423424void navigate() override;425426private:427void switch_qland();428429};430#endif // HAL_QUADPLANE_ENABLED431432class ModeManual : public Mode433{434public:435436Number mode_number() const override { return Number::MANUAL; }437const char *name() const override { return "MANUAL"; }438const char *name4() const override { return "MANU"; }439440// methods that affect movement of the vehicle in this mode441void update() override;442443void run() override;444445// true if throttle min/max limits should be applied446bool use_throttle_limits() const override;447448// true if voltage correction should be applied to throttle449bool use_battery_compensation() const override { return false; }450451};452453454class ModeRTL : public Mode455{456public:457458Number mode_number() const override { return Number::RTL; }459const char *name() const override { return "RTL"; }460const char *name4() const override { return "RTL "; }461462// methods that affect movement of the vehicle in this mode463void update() override;464465void navigate() override;466467bool allows_throttle_nudging() const override { return true; }468469bool does_auto_navigation() const override { return true; }470471bool does_auto_throttle() const override { return true; }472473protected:474475bool _enter() override;476bool _pre_arm_checks(size_t buflen, char *buffer) const override { return false; }477478private:479480// Switch to QRTL if enabled and within radius481bool switch_QRTL();482};483484class ModeStabilize : public Mode485{486public:487488Number mode_number() const override { return Number::STABILIZE; }489const char *name() const override { return "STABILIZE"; }490const char *name4() const override { return "STAB"; }491492// methods that affect movement of the vehicle in this mode493void update() override;494495void run() override;496497private:498void stabilize_stick_mixing_direct();499500};501502class ModeTraining : public Mode503{504public:505506Number mode_number() const override { return Number::TRAINING; }507const char *name() const override { return "TRAINING"; }508const char *name4() const override { return "TRAN"; }509510// methods that affect movement of the vehicle in this mode511void update() override;512513void run() override;514515};516517class ModeInitializing : public Mode518{519public:520521Number mode_number() const override { return Number::INITIALISING; }522const char *name() const override { return "INITIALISING"; }523const char *name4() const override { return "INIT"; }524525bool _enter() override { return false; }526527// methods that affect movement of the vehicle in this mode528void update() override { }529530bool allows_throttle_nudging() const override { return true; }531532bool does_auto_throttle() const override { return true; }533534protected:535bool _pre_arm_checks(size_t buflen, char *buffer) const override { return false; }536537};538539class ModeFBWA : public Mode540{541public:542543Number mode_number() const override { return Number::FLY_BY_WIRE_A; }544const char *name() const override { return "FLY_BY_WIRE_A"; }545const char *name4() const override { return "FBWA"; }546547// methods that affect movement of the vehicle in this mode548void update() override;549550bool mode_allows_autotuning() const override { return true; }551552void run() override;553554};555556class ModeFBWB : public Mode557{558public:559560Number mode_number() const override { return Number::FLY_BY_WIRE_B; }561const char *name() const override { return "FLY_BY_WIRE_B"; }562const char *name4() const override { return "FBWB"; }563564bool allows_terrain_disable() const override { return true; }565566bool does_automatic_thermal_switch() const override { return true; }567568// methods that affect movement of the vehicle in this mode569void update() override;570571bool does_auto_throttle() const override { return true; }572573bool mode_allows_autotuning() const override { return true; }574575void update_target_altitude() override {};576577protected:578579bool _enter() override;580};581582class ModeCruise : public Mode583{584public:585586Number mode_number() const override { return Number::CRUISE; }587const char *name() const override { return "CRUISE"; }588const char *name4() const override { return "CRUS"; }589590bool allows_terrain_disable() const override { return true; }591592bool does_automatic_thermal_switch() const override { return true; }593594// methods that affect movement of the vehicle in this mode595void update() override;596597void navigate() override;598599bool get_target_heading_cd(int32_t &target_heading) const;600601bool does_auto_throttle() const override { return true; }602603void update_target_altitude() override {};604605protected:606607bool _enter() override;608609bool locked_heading;610int32_t locked_heading_cd;611uint32_t lock_timer_ms;612};613614#if HAL_ADSB_ENABLED615class ModeAvoidADSB : public Mode616{617public:618619Number mode_number() const override { return Number::AVOID_ADSB; }620const char *name() const override { return "AVOID_ADSB"; }621const char *name4() const override { return "AVOI"; }622623// methods that affect movement of the vehicle in this mode624void update() override;625626void navigate() override;627628virtual bool is_guided_mode() const override { return true; }629630bool does_auto_throttle() const override { return true; }631632protected:633634bool _enter() override;635};636#endif637638#if HAL_QUADPLANE_ENABLED639class ModeQStabilize : public Mode640{641public:642643Number mode_number() const override { return Number::QSTABILIZE; }644const char *name() const override { return "QSTABILIZE"; }645const char *name4() const override { return "QSTB"; }646647bool is_vtol_mode() const override { return true; }648bool is_vtol_man_throttle() const override { return true; }649virtual bool is_vtol_man_mode() const override { return true; }650bool allows_throttle_nudging() const override { return true; }651652// methods that affect movement of the vehicle in this mode653void update() override;654655// used as a base class for all Q modes656bool _enter() override;657658void run() override;659660protected:661private:662663void set_tailsitter_roll_pitch(const float roll_input, const float pitch_input);664void set_limited_roll_pitch(const float roll_input, const float pitch_input);665666};667668class ModeQHover : public Mode669{670public:671672Number mode_number() const override { return Number::QHOVER; }673const char *name() const override { return "QHOVER"; }674const char *name4() const override { return "QHOV"; }675676bool is_vtol_mode() const override { return true; }677virtual bool is_vtol_man_mode() const override { return true; }678679// methods that affect movement of the vehicle in this mode680void update() override;681682void run() override;683684protected:685686bool _enter() override;687#if AP_QUICKTUNE_ENABLED688bool supports_quicktune() const override { return true; }689#endif690};691692class ModeQLoiter : public Mode693{694friend class QuadPlane;695friend class ModeQLand;696friend class Plane;697698public:699700Number mode_number() const override { return Number::QLOITER; }701const char *name() const override { return "QLOITER"; }702const char *name4() const override { return "QLOT"; }703704bool is_vtol_mode() const override { return true; }705virtual bool is_vtol_man_mode() const override { return true; }706707// methods that affect movement of the vehicle in this mode708void update() override;709710void run() override;711712protected:713714bool _enter() override;715uint32_t last_target_loc_set_ms;716717#if AP_QUICKTUNE_ENABLED718bool supports_quicktune() const override { return true; }719#endif720};721722class ModeQLand : public Mode723{724public:725Number mode_number() const override { return Number::QLAND; }726const char *name() const override { return "QLAND"; }727const char *name4() const override { return "QLND"; }728729bool is_vtol_mode() const override { return true; }730731// methods that affect movement of the vehicle in this mode732void update() override;733734void run() override;735736protected:737738bool _enter() override;739bool _pre_arm_checks(size_t buflen, char *buffer) const override { return false; }740};741742class ModeQRTL : public Mode743{744public:745746Number mode_number() const override { return Number::QRTL; }747const char *name() const override { return "QRTL"; }748const char *name4() const override { return "QRTL"; }749750bool is_vtol_mode() const override { return true; }751752// methods that affect movement of the vehicle in this mode753void update() override;754755void run() override;756757bool does_auto_throttle() const override { return true; }758759void update_target_altitude() override;760761bool allows_throttle_nudging() const override;762763float get_VTOL_return_radius() const;764765protected:766767bool _enter() override;768bool _pre_arm_checks(size_t buflen, char *buffer) const override { return false; }769770private:771772enum class SubMode {773climb,774RTL,775} submode;776};777778class ModeQAcro : public Mode779{780public:781782Number mode_number() const override { return Number::QACRO; }783const char *name() const override { return "QACRO"; }784const char *name4() const override { return "QACO"; }785786bool is_vtol_mode() const override { return true; }787bool is_vtol_man_throttle() const override { return true; }788virtual bool is_vtol_man_mode() const override { return true; }789790// methods that affect movement of the vehicle in this mode791void update() override;792793void run() override;794795protected:796797bool _enter() override;798};799800#if QAUTOTUNE_ENABLED801class ModeQAutotune : public Mode802{803public:804805Number mode_number() const override { return Number::QAUTOTUNE; }806const char *name() const override { return "QAUTOTUNE"; }807const char *name4() const override { return "QATN"; }808809bool is_vtol_mode() const override { return true; }810virtual bool is_vtol_man_mode() const override { return true; }811812void run() override;813814// methods that affect movement of the vehicle in this mode815void update() override;816817protected:818819bool _enter() override;820void _exit() override;821};822#endif // QAUTOTUNE_ENABLED823824#endif // HAL_QUADPLANE_ENABLED825826class ModeTakeoff: public Mode827{828public:829ModeTakeoff();830831Number mode_number() const override { return Number::TAKEOFF; }832const char *name() const override { return "TAKEOFF"; }833const char *name4() const override { return "TKOF"; }834835// methods that affect movement of the vehicle in this mode836void update() override;837838void navigate() override;839840bool allows_throttle_nudging() const override { return true; }841842bool does_auto_navigation() const override { return true; }843844bool does_auto_throttle() const override { return true; }845846// var_info for holding parameter information847static const struct AP_Param::GroupInfo var_info[];848849AP_Int16 target_alt;850AP_Int16 level_alt;851AP_Float ground_pitch;852853protected:854AP_Int16 target_dist;855AP_Int8 level_pitch;856857bool takeoff_mode_setup;858Location start_loc;859860bool _enter() override;861862private:863864// flag that we have already called autoenable fences once in MODE TAKEOFF865bool have_autoenabled_fences;866867};868#if MODE_AUTOLAND_ENABLED869class ModeAutoLand: public Mode870{871public:872ModeAutoLand();873874Number mode_number() const override { return Number::AUTOLAND; }875const char *name() const override { return "AUTOLAND"; }876const char *name4() const override { return "ALND"; }877878// methods that affect movement of the vehicle in this mode879void update() override;880881void navigate() override;882883bool allows_throttle_nudging() const override { return true; }884885bool does_auto_navigation() const override { return true; }886887bool does_auto_throttle() const override { return true; }888889890// var_info for holding parameter information891static const struct AP_Param::GroupInfo var_info[];892893AP_Int16 final_wp_alt;894AP_Int16 final_wp_dist;895AP_Int16 landing_dir_off;896897protected:898bool _enter() override;899AP_Mission::Mission_Command cmd[3];900uint8_t stage;901};902#endif903#if HAL_SOARING_ENABLED904905class ModeThermal: public Mode906{907public:908909Number mode_number() const override { return Number::THERMAL; }910const char *name() const override { return "THERMAL"; }911const char *name4() const override { return "THML"; }912913// methods that affect movement of the vehicle in this mode914void update() override;915916// Update thermal tracking and exiting logic.917void update_soaring();918919void navigate() override;920921bool allows_throttle_nudging() const override { return true; }922923bool does_auto_navigation() const override { return true; }924925// true if we are in an auto-throttle mode, which means926// we need to run the speed/height controller927bool does_auto_throttle() const override { return true; }928929protected:930931bool exit_heading_aligned() const;932void restore_mode(const char *reason, ModeReason modereason);933934bool _enter() override;935};936937#endif938939940