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/ArduSub/mode.h
Views: 1798
#pragma once12#include "Sub.h"3class Parameters;4class ParametersG2;56class GCS_Sub;78// Guided modes9enum GuidedSubMode {10Guided_WP,11Guided_Velocity,12Guided_PosVel,13Guided_Angle,14};1516// Auto modes17enum AutoSubMode {18Auto_WP,19Auto_CircleMoveToEdge,20Auto_Circle,21Auto_NavGuided,22Auto_Loiter,23Auto_TerrainRecover24};2526// RTL states27enum RTLState {28RTL_InitialClimb,29RTL_ReturnHome,30RTL_LoiterAtHome,31RTL_FinalDescent,32RTL_Land33};3435class Mode36{3738public:3940// Auto Pilot Modes enumeration41enum class Number : uint8_t {42STABILIZE = 0, // manual angle with manual depth/throttle43ACRO = 1, // manual body-frame angular rate with manual depth/throttle44ALT_HOLD = 2, // manual angle with automatic depth/throttle45AUTO = 3, // fully automatic waypoint control using mission commands46GUIDED = 4, // fully automatic fly to coordinate or fly at velocity/direction using GCS immediate commands47CIRCLE = 7, // automatic circular flight with automatic throttle48SURFACE = 9, // automatically return to surface, pilot maintains horizontal control49POSHOLD = 16, // automatic position hold with manual override, with automatic throttle50MANUAL = 19, // Pass-through input with no stabilization51MOTOR_DETECT = 20, // Automatically detect motors orientation52SURFTRAK = 21 // Track distance above seafloor (hold range)53// Mode number 30 reserved for "offboard" for external/lua control.54};5556// constructor57Mode(void);5859// do not allow copying60CLASS_NO_COPY(Mode);6162// child classes should override these methods63virtual bool init(bool ignore_checks) { return true; }64virtual void run() = 0;65virtual bool requires_GPS() const = 0;66virtual bool has_manual_throttle() const = 0;67virtual bool allows_arming(bool from_gcs) const = 0;68virtual bool is_autopilot() const { return false; }69virtual bool in_guided_mode() const { return false; }7071// return a string for this flightmode72virtual const char *name() const = 0;73virtual const char *name4() const = 0;7475// returns a unique number specific to this mode76virtual Mode::Number number() const = 0;7778// functions for reporting to GCS79virtual bool get_wp(Location &loc) { return false; }80virtual int32_t wp_bearing() const { return 0; }81virtual uint32_t wp_distance() const { return 0; }82virtual float crosstrack_error() const { return 0.0f; }838485// pilot input processing86void get_pilot_desired_accelerations(float &right_out, float &front_out) const;87void get_pilot_desired_angle_rates(int16_t roll_in, int16_t pitch_in, int16_t yaw_in, float &roll_out, float &pitch_out, float &yaw_out);888990protected:9192// navigation support functions93virtual void run_autopilot() {}9495// helper functions96bool is_disarmed_or_landed() const;9798// functions to control landing99// in modes that support landing100void land_run_horizontal_control();101void land_run_vertical_control(bool pause_descent = false);102103// convenience references to avoid code churn in conversion:104Parameters &g;105ParametersG2 &g2;106AP_InertialNav &inertial_nav;107AP_AHRS &ahrs;108AP_Motors6DOF &motors;109RC_Channel *&channel_roll;110RC_Channel *&channel_pitch;111RC_Channel *&channel_throttle;112RC_Channel *&channel_yaw;113RC_Channel *&channel_forward;114RC_Channel *&channel_lateral;115AC_PosControl *position_control;116AC_AttitudeControl_Sub *attitude_control;117// TODO: channels118float &G_Dt;119120public:121// Navigation Yaw control122class AutoYaw123{124125public:126// mode(): current method of determining desired yaw:127autopilot_yaw_mode mode() const128{129return (autopilot_yaw_mode)_mode;130}131void set_mode_to_default(bool rtl);132void set_mode(autopilot_yaw_mode new_mode);133autopilot_yaw_mode default_mode(bool rtl) const;134135void set_rate(float new_rate_cds);136137// set_roi(...): set a "look at" location:138void set_roi(const Location &roi_location);139140void set_fixed_yaw(float angle_deg,141float turn_rate_dps,142int8_t direction,143bool relative_angle);144145private:146147// yaw_cd(): main product of AutoYaw; the heading:148float yaw_cd();149150// rate_cds(): desired yaw rate in centidegrees/second:151float rate_cds();152153float look_ahead_yaw();154float roi_yaw();155156// auto flight mode's yaw mode157uint8_t _mode = AUTO_YAW_LOOK_AT_NEXT_WP;158159// Yaw will point at this location if mode is set to AUTO_YAW_ROI160Vector3f roi;161162// bearing from current location to the ROI163float _roi_yaw;164165// yaw used for YAW_FIXED yaw_mode166int32_t _fixed_yaw;167168// Deg/s we should turn169int16_t _fixed_yaw_slewrate;170171// heading when in yaw_look_ahead_yaw172float _look_ahead_yaw;173174// used to reduce update rate to 100hz:175uint8_t roi_yaw_counter;176177GuidedSubMode guided_mode;178179};180static AutoYaw auto_yaw;181182// pass-through functions to reduce code churn on conversion;183// these are candidates for moving into the Mode base184// class.185bool set_mode(Mode::Number mode, ModeReason reason);186GCS_Sub &gcs();187188// end pass-through functions189};190191class ModeManual : public Mode192{193194public:195// inherit constructor196using Mode::Mode;197virtual void run() override;198bool init(bool ignore_checks) override;199bool requires_GPS() const override { return false; }200bool has_manual_throttle() const override { return true; }201bool allows_arming(bool from_gcs) const override { return true; }202bool is_autopilot() const override { return false; }203204protected:205206const char *name() const override { return "MANUAL"; }207const char *name4() const override { return "MANU"; }208Mode::Number number() const override { return Mode::Number::MANUAL; }209};210211212class ModeAcro : public Mode213{214215public:216// inherit constructor217using Mode::Mode;218219virtual void run() override;220221bool init(bool ignore_checks) override;222bool requires_GPS() const override { return false; }223bool has_manual_throttle() const override { return true; }224bool allows_arming(bool from_gcs) const override { return true; }225bool is_autopilot() const override { return false; }226227protected:228229const char *name() const override { return "ACRO"; }230const char *name4() const override { return "ACRO"; }231Mode::Number number() const override { return Mode::Number::ACRO; }232};233234235class ModeStabilize : public Mode236{237238public:239// inherit constructor240using Mode::Mode;241242virtual void run() override;243244bool init(bool ignore_checks) override;245bool requires_GPS() const override { return false; }246bool has_manual_throttle() const override { return true; }247bool allows_arming(bool from_gcs) const override { return true; }248bool is_autopilot() const override { return false; }249250protected:251252const char *name() const override { return "STABILIZE"; }253const char *name4() const override { return "STAB"; }254Mode::Number number() const override { return Mode::Number::STABILIZE; }255};256257258class ModeAlthold : public Mode259{260261public:262// inherit constructor263using Mode::Mode;264265virtual void run() override;266267bool init(bool ignore_checks) override;268bool requires_GPS() const override { return false; }269bool has_manual_throttle() const override { return false; }270bool allows_arming(bool from_gcs) const override { return true; }271bool is_autopilot() const override { return false; }272void control_depth();273274protected:275276void run_pre();277void run_post();278279const char *name() const override { return "ALT_HOLD"; }280const char *name4() const override { return "ALTH"; }281Mode::Number number() const override { return Mode::Number::ALT_HOLD; }282};283284285class ModeSurftrak : public ModeAlthold286{287288public:289// constructor290ModeSurftrak();291292void run() override;293294bool init(bool ignore_checks) override;295296float get_rangefinder_target_cm() const WARN_IF_UNUSED { return rangefinder_target_cm; }297bool set_rangefinder_target_cm(float target_cm);298299protected:300301const char *name() const override { return "SURFTRAK"; }302const char *name4() const override { return "STRK"; }303Mode::Number number() const override { return Mode::Number::SURFTRAK; }304305private:306307void reset();308void control_range();309void update_surface_offset();310311float rangefinder_target_cm;312313bool pilot_in_control; // pilot is moving up/down314float pilot_control_start_z_cm; // alt when pilot took control315};316317class ModeGuided : public Mode318{319320public:321// inherit constructor322using Mode::Mode;323324virtual void run() override;325326bool init(bool ignore_checks) override;327bool requires_GPS() const override { return true; }328bool has_manual_throttle() const override { return false; }329bool allows_arming(bool from_gcs) const override { return true; }330bool is_autopilot() const override { return true; }331bool in_guided_mode() const override { return true; }332bool guided_limit_check();333void guided_limit_init_time_and_pos();334void guided_set_angle(const Quaternion &q, float climb_rate_cms, bool use_yaw_rate, float yaw_rate_rads);335void guided_set_angle(const Quaternion&, float);336void guided_limit_set(uint32_t timeout_ms, float alt_min_cm, float alt_max_cm, float horiz_max_cm);337bool guided_set_destination_posvel(const Vector3f& destination, const Vector3f& velocity);338bool guided_set_destination_posvel(const Vector3f& destination, const Vector3f& velocity, bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_yaw);339bool guided_set_destination(const Vector3f& destination);340bool guided_set_destination(const Location&);341bool guided_set_destination(const Vector3f& destination, bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_yaw);342void guided_set_velocity(const Vector3f& velocity);343void guided_set_velocity(const Vector3f& velocity, bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_yaw);344void guided_set_yaw_state(bool use_yaw, float yaw_cd, bool use_yaw_rate, float yaw_rate_cds, bool relative_angle);345float get_auto_heading();346void guided_limit_clear();347void set_auto_yaw_mode(autopilot_yaw_mode yaw_mode);348349protected:350351const char *name() const override { return "GUIDED"; }352const char *name4() const override { return "GUID"; }353Mode::Number number() const override { return Mode::Number::GUIDED; }354355autopilot_yaw_mode get_default_auto_yaw_mode(bool rtl) const;356357private:358void guided_pos_control_run();359void guided_vel_control_run();360void guided_posvel_control_run();361void guided_angle_control_run();362void guided_takeoff_run();363void guided_pos_control_start();364void guided_vel_control_start();365void guided_posvel_control_start();366void guided_angle_control_start();367};368369370371class ModeAuto : public ModeGuided372{373374public:375// inherit constructor376using ModeGuided::ModeGuided;377378virtual void run() override;379380bool init(bool ignore_checks) override;381bool requires_GPS() const override { return true; }382bool has_manual_throttle() const override { return false; }383bool allows_arming(bool from_gcs) const override { return true; }384bool is_autopilot() const override { return true; }385bool auto_loiter_start();386void auto_wp_start(const Vector3f& destination);387void auto_wp_start(const Location& dest_loc);388void auto_circle_movetoedge_start(const Location &circle_center, float radius_m, bool ccw_turn);389void auto_circle_start();390void auto_nav_guided_start();391void set_auto_yaw_roi(const Location &roi_location);392void set_auto_yaw_look_at_heading(float angle_deg, float turn_rate_dps, int8_t direction, uint8_t relative_angle);393void set_yaw_rate(float turn_rate_dps);394bool auto_terrain_recover_start();395396protected:397398const char *name() const override { return "AUTO"; }399const char *name4() const override { return "AUTO"; }400Mode::Number number() const override { return Mode::Number::AUTO; }401402private:403void auto_wp_run();404void auto_circle_run();405void auto_nav_guided_run();406void auto_loiter_run();407void auto_terrain_recover_run();408};409410411class ModePoshold : public ModeAlthold412{413414public:415// inherit constructor416using ModeAlthold::ModeAlthold;417418virtual void run() override;419420bool init(bool ignore_checks) override;421422bool requires_GPS() const override { return false; }423bool has_manual_throttle() const override { return false; }424bool allows_arming(bool from_gcs) const override { return true; }425bool is_autopilot() const override { return true; }426427protected:428429const char *name() const override { return "POSHOLD"; }430const char *name4() const override { return "POSH"; }431Mode::Number number() const override { return Mode::Number::POSHOLD; }432};433434435class ModeCircle : public Mode436{437438public:439// inherit constructor440using Mode::Mode;441442virtual void run() override;443444bool init(bool ignore_checks) override;445bool requires_GPS() const override { return true; }446bool has_manual_throttle() const override { return false; }447bool allows_arming(bool from_gcs) const override { return true; }448bool is_autopilot() const override { return true; }449450protected:451452const char *name() const override { return "CIRCLE"; }453const char *name4() const override { return "CIRC"; }454Mode::Number number() const override { return Mode::Number::CIRCLE; }455};456457class ModeSurface : public Mode458{459460public:461// inherit constructor462using Mode::Mode;463464virtual void run() override;465466bool init(bool ignore_checks) override;467bool requires_GPS() const override { return true; }468bool has_manual_throttle() const override { return false; }469bool allows_arming(bool from_gcs) const override { return true; }470bool is_autopilot() const override { return true; }471472protected:473474const char *name() const override { return "SURFACE"; }475const char *name4() const override { return "SURF"; }476Mode::Number number() const override { return Mode::Number::CIRCLE; }477};478479480class ModeMotordetect : public Mode481{482483public:484// inherit constructor485using Mode::Mode;486487virtual void run() override;488489bool init(bool ignore_checks) override;490bool requires_GPS() const override { return false; }491bool has_manual_throttle() const override { return false; }492bool allows_arming(bool from_gcs) const override { return true; }493bool is_autopilot() const override { return true; }494495protected:496497const char *name() const override { return "MOTORDETECT"; }498const char *name4() const override { return "DETE"; }499Mode::Number number() const override { return Mode::Number::MOTOR_DETECT; }500};501502503