From 7e79a130533958e7a06500c85407483ac297095d Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Wed, 17 Jan 2024 20:18:18 +0000 Subject: [PATCH] Plane: ask mode if battery voltage compensation should be done --- ArduPlane/mode.cpp | 32 ++++++++++++++++++++++++++++++++ ArduPlane/mode.h | 3 +++ ArduPlane/servos.cpp | 3 ++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/ArduPlane/mode.cpp b/ArduPlane/mode.cpp index 2d97c9cb1a..ec10b69631 100644 --- a/ArduPlane/mode.cpp +++ b/ArduPlane/mode.cpp @@ -290,3 +290,35 @@ bool Mode::use_throttle_limits() const return true; } + +// true if voltage correction should be applied to throttle +bool Mode::use_battery_compensation() const +{ +#if AP_SCRIPTING_ENABLED + if (plane.nav_scripting_active()) { + return false; + } +#endif + + if (this == &plane.mode_stabilize || + this == &plane.mode_training || + this == &plane.mode_acro || + this == &plane.mode_fbwa || + this == &plane.mode_autotune) { + // a manual throttle mode + return false; + } + + if (is_guided_mode() && plane.guided_throttle_passthru) { + // manual pass through of throttle while in GUIDED + return false; + } + +#if HAL_QUADPLANE_ENABLED + if (quadplane.in_vtol_mode()) { + return false; + } +#endif + + return true; +} diff --git a/ArduPlane/mode.h b/ArduPlane/mode.h index 1de286f866..d2a4d3d11e 100644 --- a/ArduPlane/mode.h +++ b/ArduPlane/mode.h @@ -138,6 +138,9 @@ public: // true if throttle min/max limits should be applied bool use_throttle_limits() const; + // true if voltage correction should be applied to throttle + bool use_battery_compensation() const; + protected: // subclasses override this to perform checks before entering the mode diff --git a/ArduPlane/servos.cpp b/ArduPlane/servos.cpp index c04cc8dbb3..08b102acd9 100644 --- a/ArduPlane/servos.cpp +++ b/ArduPlane/servos.cpp @@ -614,8 +614,9 @@ void Plane::set_throttle(void) } SRV_Channels::set_output_scaled(SRV_Channel::k_throttle, fwd_thr); #endif // HAL_QUADPLANE_ENABLED + } - } else { + if (control_mode->use_battery_compensation()) { // Apply voltage compensation to throttle output from flight mode const float throttle = g2.fwd_batt_cmp.apply_throttle(SRV_Channels::get_output_scaled(SRV_Channel::k_throttle)); SRV_Channels::set_output_scaled(SRV_Channel::k_throttle, throttle);