Path: blob/master/libraries/AC_AutoTune/AC_AutoTune_Multi.h
9662 views
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14/*15Multirotor implementation of AutoTune. Based on the original ArduCopter16autotune code by Leonard Hall.17*/1819#pragma once2021#include "AC_AutoTune_config.h"2223#if AC_AUTOTUNE_ENABLED2425#include "AC_AutoTune.h"2627class AC_AutoTune_Multi : public AC_AutoTune28{29public:30// Constructor31AC_AutoTune_Multi();3233// Saves tuned gains to EEPROM on disarm34void save_tuning_gains() override;3536// Parameter group for AP_Param registration37static const struct AP_Param::GroupInfo var_info[];3839protected:40//41// Gain management and initialization42//4344// Backs up original gains and resets tuning state45void backup_gains_and_initialise() override;4647// Loads original pre-tune gains48void load_orig_gains() override;4950// Loads gains from the last successful autotune session51void load_tuned_gains() override;5253// Loads conservative gains used between twitch tests54void load_intra_test_gains() override;5556// Loads current test gains before executing a twitch57void load_test_gains() override;5859// Resets vehicle-specific test state (not used in multirotor)60void reset_vehicle_test_variables() override {};6162// Resets vehicle-specific gain tracking state (not used in multirotor)63void reset_update_gain_variables() override {};6465// Maximum/Minimum target angles for the current axis (in centidegrees)66float target_angle_max_rp_cd() const override;67float target_angle_max_y_cd() const override;68float target_angle_min_rp_cd() const override;69float target_angle_min_y_cd() const override;7071// Absolute and negative angle abort limits for twitch safety (in centidegrees)72float angle_lim_max_rp_cd() const override;73float angle_lim_neg_rpy_cd() const override;7475// Prepares state and targets for a new twitch test76void test_init() override;7778// Executes one twitch step for the specified axis and direction79void test_run(AxisType test_axis, const float dir_sign) override;8081// Sends regular status messages to the ground station82void do_gcs_announcements() override;8384// (Unused) Placeholder for post-test announcements85void do_post_test_gcs_announcements() override {};8687// Reports final tuned gains to the ground station88void report_final_gains(AxisType test_axis) const override;8990// Update functions for each TuneType91void updating_rate_p_up_all(AxisType test_axis) override;92void updating_rate_d_up_all(AxisType test_axis) override;93void updating_rate_d_down_all(AxisType test_axis) override;94void updating_angle_p_up_all(AxisType test_axis) override;95void updating_angle_p_down_all(AxisType test_axis) override;9697// These tune types are not used in multirotors98void updating_rate_ff_up_all(AxisType) override {99INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);100}101void updating_max_gains_all(AxisType) override {102INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);103}104105// Applies gain margin (backoff) at the end of a TuneType106void set_tuning_gains_with_backoff(AxisType test_axis) override;107108// reverse the direction of the next test109bool reverse_test_direction() override { return !positive_direction; }110111#if HAL_LOGGING_ENABLED112void Log_AutoTune() override;113void Log_AutoTuneDetails() override;114void Log_AutoTuneSweep() override {115INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);116}117118void Log_Write_AutoTune(AxisType axis, TuneType tune_step,119float meas_target, float meas_min, float meas_max,120float new_gain_rp, float new_gain_rd, float new_gain_sp, float new_ddt);121void Log_Write_AutoTuneDetails(float angle_cd, float rate_cds);122#endif123124// Defines the sequence of tuning steps (P, D, etc.)125void set_tune_sequence() override {126tune_seq[0] = TuneType::RATE_D_UP;127tune_seq[1] = TuneType::RATE_D_DOWN;128tune_seq[2] = TuneType::RATE_P_UP;129tune_seq[3] = TuneType::ANGLE_P_DOWN;130tune_seq[4] = TuneType::ANGLE_P_UP;131tune_seq[5] = TuneType::TUNE_COMPLETE;132}133134// Axis tuning mask accessor135uint8_t get_axis_bitmask() const override { return axis_bitmask; }136137// Timeout for a single twitch test138uint32_t get_testing_step_timeout_ms() const override;139140private:141// Helpers for twitch-based test monitoring142143void twitching_test_rate(float angle, float rate, float rate_target,144float &meas_rate_min, float &meas_rate_max, float &meas_angle_min);145146void twitching_abort_rate(float angle, float rate, float angle_max,147float meas_rate_min, float angle_min);148149void twitching_test_angle(float angle, float rate, float angle_target,150float &meas_angle_min, float &meas_angle_max,151float &meas_rate_min, float &meas_rate_max);152153// Calculates average acceleration during twitch154void twitching_measure_acceleration(float &accel_average, float rate, float &rate_max) const;155156// Gain update routines157158// Increases D and adjusts P to produce controlled bounce-back behavior.159// Seeks to bring the response peak just below the target rate while inducing160// a small overshoot (bounce) for evaluating D influence.161void updating_rate_d_up(float &tune_d, float tune_d_min, float tune_d_max, float tune_d_step_ratio,162float &tune_p, float tune_p_min, float tune_p_max, float tune_p_step_ratio,163float rate_target, float meas_rate_min, float meas_rate_max);164165// Decreases D and adjusts P to eliminate bounce-back in the response.166// Aims to achieve a clean response just below the target rate with no overshoot.167void updating_rate_d_down(float &tune_d, float tune_d_min, float tune_d_step_ratio,168float &tune_p, float tune_p_min, float tune_p_max, float tune_p_step_ratio,169float rate_target, float meas_rate_min, float meas_rate_max);170171// Increases P to ensure the target rate is reached quickly, while reducing D172// if bounce-back exceeds the aggressiveness threshold.173// Fails if D hits minimum and clean response is not achievable (optional).174void updating_rate_p_up_d_down(float &tune_d, float tune_d_min, float tune_d_step_ratio,175float &tune_p, float tune_p_min, float tune_p_max, float tune_p_step_ratio,176float rate_target, float meas_rate_min, float meas_rate_max,177bool fail_min_d = true);178179// Decreases angle P gain to reduce overshoot until the target is no longer reached before timeout.180// Used to back off from aggressive behavior in angle control.181void updating_angle_p_down(float &tune_p, float tune_p_min, float tune_p_step_ratio,182float angle_target, float meas_angle_max,183float meas_rate_min, float meas_rate_max);184185// Increases angle P gain to ensure the target is reached within a reasonable time.186// Stops increasing once target response is consistently achieved without overshoot.187void updating_angle_p_up(float &tune_p, float tune_p_max, float tune_p_step_ratio,188float angle_target, float meas_angle_max,189float meas_rate_min, float meas_rate_max);190191// Formats and sends gain reports192void report_axis_gains(const char* axis_string, float rate_P, float rate_I,193float rate_D, float angle_P, float max_accel_radss) const;194195// Parameters196AP_Int8 axis_bitmask; // Axis enable mask197AP_Float aggressiveness; // Target overshoot ratio (D tuning sensitivity)198AP_Float min_d; // Minimum allowed D gain199AP_Float gain_backoff; // Fraction by which tuned P and D gains are reduced after each AutoTune stage (rate loop, then angle loop) to provide additional stability margin200bool ignore_next; // Skip next result (used for rate overshoot handling)201202// Measurement and target values for each test step203float target_angle;204float target_rate;205float angle_abort;206207float test_rate_min;208float test_rate_max;209float test_angle_min;210float test_angle_max;211212float accel_measure_rate_max;213};214215#endif // AC_AUTOTUNE_ENABLED216217218