From 6177cbbfe20e234340daf05e5a3812c13b9572b4 Mon Sep 17 00:00:00 2001 From: Zach Flathers Date: Sat, 17 Jun 2023 10:34:08 -0700 Subject: [PATCH] Plane: Add is_landing/is_taking_off scripting support --- ArduPlane/Plane.cpp | 28 ++++++++++++++++++++++++++++ ArduPlane/Plane.h | 3 +++ ArduPlane/mode.h | 13 +++++++++++++ ArduPlane/mode_auto.cpp | 22 ++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/ArduPlane/Plane.cpp b/ArduPlane/Plane.cpp index dff3166b02..ab13b35f38 100644 --- a/ArduPlane/Plane.cpp +++ b/ArduPlane/Plane.cpp @@ -32,3 +32,31 @@ Plane::Plane(void) Plane plane; AP_Vehicle& vehicle = plane; + +#if AP_SCRIPTING_ENABLED + +// returns true if vehicle is landing. Only used by Lua scripts +bool Plane::is_landing() const +{ + #if HAL_QUADPLANE_ENABLED + + return plane.quadplane.in_vtol_land_descent(); + + #endif // HAL_QUADPLANE_ENABLED + + return control_mode->is_landing(); +} + +// returns true if vehicle is taking off. Only used by Lua scripts +bool Plane::is_taking_off() const +{ + #if HAL_QUADPLANE_ENABLED + + return plane.quadplane.in_vtol_takeoff(); + + #endif // HAL_QUADPLANE_ENABLED + + return control_mode->is_taking_off(); +} + +#endif // AP_SCRIPTING_ENABLED diff --git a/ArduPlane/Plane.h b/ArduPlane/Plane.h index 504ea7b840..d26500571b 100644 --- a/ArduPlane/Plane.h +++ b/ArduPlane/Plane.h @@ -523,6 +523,7 @@ private: float rudder_offset_pct; bool run_yaw_rate_controller; } nav_scripting; + #endif struct { @@ -1245,6 +1246,8 @@ public: // allow for landing descent rate to be overridden by a script, may be -ve to climb bool set_land_descent_rate(float descent_rate) override; + bool is_landing() const override; + bool is_taking_off() const override; #endif // AP_SCRIPTING_ENABLED }; diff --git a/ArduPlane/mode.h b/ArduPlane/mode.h index a4cb940cd1..1e2701dd7d 100644 --- a/ArduPlane/mode.h +++ b/ArduPlane/mode.h @@ -128,6 +128,13 @@ public: // handle a guided target request from GCS virtual bool handle_guided_request(Location target_loc) { return false; } + // true if is landing + virtual bool is_landing() const { return false; } + + // true if is taking + virtual bool is_taking_off() const { return false; } + + protected: // subclasses override this to perform checks before entering the mode @@ -209,6 +216,10 @@ public: bool mode_allows_autotuning() const override { return true; } + bool is_landing() const override; + + bool is_taking_off() const override; + protected: bool _enter() override; @@ -745,6 +756,8 @@ public: bool does_auto_throttle() const override { return true; } + bool is_taking_off() const override { return true; } + // var_info for holding parameter information static const struct AP_Param::GroupInfo var_info[]; diff --git a/ArduPlane/mode_auto.cpp b/ArduPlane/mode_auto.cpp index 548dd0b261..cbbf2fe452 100644 --- a/ArduPlane/mode_auto.cpp +++ b/ArduPlane/mode_auto.cpp @@ -159,3 +159,25 @@ bool ModeAuto::_pre_arm_checks(size_t buflen, char *buffer) const // Note that this bypasses the base class checks return true; } + +bool ModeAuto::is_landing() const +{ + switch(plane.mission.get_current_nav_cmd().id) { + case MAV_CMD_NAV_LAND: + case MAV_CMD_NAV_VTOL_LAND: + case MAV_CMD_DO_VTOL_TRANSITION: + return true; + } + return false; +} + +bool ModeAuto::is_taking_off() const +{ + switch(plane.mission.get_current_nav_cmd().id) { + case MAV_CMD_NAV_TAKEOFF: + case MAV_CMD_NAV_VTOL_TAKEOFF: + case MAV_CMD_DO_VTOL_TRANSITION: + return true; + } + return false; +}