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/libraries/AC_AutoTune/AC_AutoTune.h
Views: 1798
/*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/*15support for autotune of multirotors. Based on original autotune code from ArduCopter, written by Leonard Hall16Converted to a library by Andrew Tridgell17*/18#pragma once1920#include "AC_AutoTune_config.h"2122#if AC_AUTOTUNE_ENABLED2324#include <AC_AttitudeControl/AC_AttitudeControl.h>25#include <AC_AttitudeControl/AC_PosControl.h>26#include <AP_Math/AP_Math.h>27#include <RC_Channel/RC_Channel.h>28#include "AC_AutoTune_FreqResp.h"2930#define AUTOTUNE_AXIS_BITMASK_ROLL 131#define AUTOTUNE_AXIS_BITMASK_PITCH 232#define AUTOTUNE_AXIS_BITMASK_YAW 433#define AUTOTUNE_AXIS_BITMASK_YAW_D 83435#define AUTOTUNE_SUCCESS_COUNT 4 // The number of successful iterations we need to freeze at current gains3637// Auto Tune message ids for ground station38#define AUTOTUNE_MESSAGE_STARTED 039#define AUTOTUNE_MESSAGE_STOPPED 140#define AUTOTUNE_MESSAGE_SUCCESS 241#define AUTOTUNE_MESSAGE_FAILED 342#define AUTOTUNE_MESSAGE_SAVED_GAINS 443#define AUTOTUNE_MESSAGE_TESTING 544#define AUTOTUNE_MESSAGE_TESTING_END 64546#define AUTOTUNE_ANNOUNCE_INTERVAL_MS 20004748class AC_AutoTune49{50public:51// constructor52AC_AutoTune();5354// main run loop55virtual void run();5657// Possibly save gains, called on disarm58void disarmed(const bool in_autotune_mode);5960// stop tune, reverting gains61void stop();6263// Autotune aux function trigger64void do_aux_function(const RC_Channel::AuxSwitchPos ch_flag);6566protected:6768virtual void save_tuning_gains() = 0;697071// reset Autotune so that gains are not saved again and autotune can be run again.72void reset() {73mode = UNINITIALISED;74axes_completed = 0;75have_pilot_testing_command = false;76}7778// axis that can be tuned79enum class AxisType {80ROLL = 0, // roll axis is being tuned (either angle or rate)81PITCH = 1, // pitch axis is being tuned (either angle or rate)82YAW = 2, // yaw axis is being tuned using FLTE (either angle or rate)83YAW_D = 3, // yaw axis is being tuned using D (either angle or rate)84};8586//87// methods that must be supplied by the vehicle specific subclass88//89virtual bool init(void) = 0;9091// get pilot input for desired climb rate92virtual float get_pilot_desired_climb_rate_cms(void) const = 0;9394// get pilot input for designed roll and pitch, and yaw rate95virtual void get_pilot_desired_rp_yrate_cd(float &roll_cd, float &pitch_cd, float &yaw_rate_cds) = 0;9697// init pos controller Z velocity and accel limits98virtual void init_z_limits() = 0;99100#if HAL_LOGGING_ENABLED101// log PIDs at full rate for during twitch102virtual void log_pids() = 0;103#endif104105//106// methods to load and save gains107//108109// backup original gains and prepare for start of tuning110virtual void backup_gains_and_initialise();111112// switch to use original gains113virtual void load_orig_gains() = 0;114115// switch to gains found by last successful autotune116virtual void load_tuned_gains() = 0;117118// load gains used between tests. called during testing mode's update-gains step to set gains ahead of return-to-level step119virtual void load_intra_test_gains() = 0;120121// load gains for next test. relies on axis variable being set122virtual void load_test_gains() = 0;123124// reset the test vaariables for each vehicle125virtual void reset_vehicle_test_variables() = 0;126127// reset the update gain variables for each vehicle128virtual void reset_update_gain_variables() = 0;129130// test initialization and run methods that should be overridden for each vehicle131virtual void test_init() = 0;132virtual void test_run(AxisType test_axis, const float dir_sign) = 0;133134// return true if user has enabled autotune for roll, pitch or yaw axis135bool roll_enabled() const;136bool pitch_enabled() const;137bool yaw_enabled() const;138bool yaw_d_enabled() const;139140// update gains for the rate p up tune type141virtual void updating_rate_p_up_all(AxisType test_axis)=0;142143// update gains for the rate d up tune type144virtual void updating_rate_d_up_all(AxisType test_axis)=0;145146// update gains for the rate d down tune type147virtual void updating_rate_d_down_all(AxisType test_axis)=0;148149// update gains for the angle p up tune type150virtual void updating_angle_p_up_all(AxisType test_axis)=0;151152// update gains for the angle p down tune type153virtual void updating_angle_p_down_all(AxisType test_axis)=0;154155// set gains post tune for the tune type156virtual void set_gains_post_tune(AxisType test_axis)=0;157158// reverse direction for twitch test159virtual bool twitch_reverse_direction() = 0;160161162#if HAL_LOGGING_ENABLED163virtual void Log_AutoTune() = 0;164virtual void Log_AutoTuneDetails() = 0;165virtual void Log_AutoTuneSweep() = 0;166#endif167168// internal init function, should be called from init()169bool init_internals(bool use_poshold,170AC_AttitudeControl *attitude_control,171AC_PosControl *pos_control,172AP_AHRS_View *ahrs_view,173AP_InertialNav *inertial_nav);174175// send intermittent updates to user on status of tune176virtual void do_gcs_announcements() = 0;177178// send post test updates to user179virtual void do_post_test_gcs_announcements() = 0;180181// send message with high level status (e.g. Started, Stopped)182void update_gcs(uint8_t message_id) const;183184// send lower level step status (e.g. Pilot overrides Active)185void send_step_string();186187// convert tune type to string for reporting188const char *type_string() const;189190// return current axis string191const char *axis_string() const;192193// report final gains for a given axis to GCS194virtual void report_final_gains(AxisType test_axis) const = 0;195196// Functions added for heli autotune197198// Add additional updating gain functions specific to heli199// generic method used by subclasses to update gains for the rate ff up tune type200virtual void updating_rate_ff_up_all(AxisType test_axis)=0;201202// generic method used by subclasses to update gains for the max gain tune type203virtual void updating_max_gains_all(AxisType test_axis)=0;204205// steps performed while in the tuning mode206enum StepType {207WAITING_FOR_LEVEL = 0, // autotune is waiting for vehicle to return to level before beginning the next twitch208TESTING = 1, // autotune has begun a test and is watching the resulting vehicle movement209UPDATE_GAINS = 2, // autotune has completed a test and is updating the gains based on the results210ABORT = 3 // load normal gains and return to WAITING_FOR_LEVEL211};212213// mini steps performed while in Tuning mode, Testing step214enum TuneType {215RD_UP = 0, // rate D is being tuned up216RD_DOWN = 1, // rate D is being tuned down217RP_UP = 2, // rate P is being tuned up218RFF_UP = 3, // rate FF is being tuned up219SP_DOWN = 4, // angle P is being tuned down220SP_UP = 5, // angle P is being tuned up221MAX_GAINS = 6, // max allowable stable gains are determined222TUNE_CHECK = 7, // frequency sweep with tuned gains223TUNE_COMPLETE = 8 // Reached end of tuning224};225TuneType tune_seq[6]; // holds sequence of tune_types to be performed226uint8_t tune_seq_curr; // current tune sequence step227228// get the next tune type229void next_tune_type(TuneType &curr_tune_type, bool reset);230231// Sets customizable tune sequence for the vehicle232virtual void set_tune_sequence() = 0;233234// get_axis_bitmask accessor235virtual uint8_t get_axis_bitmask() const = 0;236237// get_testing_step_timeout_ms accessor238virtual uint32_t get_testing_step_timeout_ms() const = 0;239240// get attitude for slow position hold in autotune mode241void get_poshold_attitude(float &roll_cd, float &pitch_cd, float &yaw_cd);242243// type of gains to load244enum GainType {245GAIN_ORIGINAL = 0,246GAIN_TEST = 1,247GAIN_INTRA_TEST = 2,248GAIN_TUNED = 3,249} loaded_gains;250void load_gains(enum GainType gain_type);251252// autotune modes (high level states)253enum TuneMode {254UNINITIALISED = 0, // autotune has never been run255TUNING = 1, // autotune is testing gains256SUCCESS = 2, // tuning has completed, user is flight testing the new gains257FAILED = 3, // tuning has failed, user is flying on original gains258};259TuneMode mode; // see TuneMode for what modes are allowed260261// copies of object pointers to make code a bit clearer262AC_AttitudeControl *attitude_control;263AC_PosControl *pos_control;264AP_AHRS_View *ahrs_view;265AP_InertialNav *inertial_nav;266AP_Motors *motors;267268AxisType axis; // current axis being tuned. see AxisType enum269bool positive_direction; // false = tuning in negative direction (i.e. left for roll), true = positive direction (i.e. right for roll)270StepType step; // see StepType for what steps are performed271TuneType tune_type; // see TuneType272bool twitch_first_iter; // true on first iteration of a twitch (used to signal we must step the attitude or rate target)273uint8_t axes_completed; // bitmask of completed axes274uint32_t step_start_time_ms; // start time of current tuning step (used for timeout checks)275uint32_t step_time_limit_ms; // time limit of current autotune process276uint32_t level_start_time_ms; // start time of waiting for level277int8_t counter; // counter for tuning gains278float start_angle; // start angle279float start_rate; // start rate - parent and multi280float test_accel_max; // maximum acceleration variable281float desired_yaw_cd; // yaw heading during tune - parent and Tradheli282float step_scaler; // scaler to reduce maximum target step - parent and multi283284LowPassFilterFloat rotation_rate_filt; // filtered rotation rate in radians/second285286// backup of currently being tuned parameter values287float orig_roll_rp, orig_roll_ri, orig_roll_rd, orig_roll_rff, orig_roll_dff, orig_roll_fltt, orig_roll_smax, orig_roll_sp, orig_roll_accel, orig_roll_rate;288float orig_pitch_rp, orig_pitch_ri, orig_pitch_rd, orig_pitch_rff, orig_pitch_dff, orig_pitch_fltt, orig_pitch_smax, orig_pitch_sp, orig_pitch_accel, orig_pitch_rate;289float orig_yaw_rp, orig_yaw_ri, orig_yaw_rd, orig_yaw_rff, orig_yaw_dff, orig_yaw_fltt, orig_yaw_smax, orig_yaw_rLPF, orig_yaw_sp, orig_yaw_accel, orig_yaw_rate;290bool orig_bf_feedforward;291292// currently being tuned parameter values293float tune_roll_rp, tune_roll_rd, tune_roll_sp, tune_roll_accel;294float tune_pitch_rp, tune_pitch_rd, tune_pitch_sp, tune_pitch_accel;295float tune_yaw_rp, tune_yaw_rLPF, tune_yaw_sp, tune_yaw_accel;296float tune_roll_rff, tune_pitch_rff, tune_yaw_rd, tune_yaw_rff;297298uint32_t announce_time;299float lean_angle;300float rotation_rate;301float roll_cd, pitch_cd;302303// heli specific variables304float start_freq; //start freq for dwell test305float stop_freq; //ending freq for dwell test306307private:308// return true if we have a good position estimate309virtual bool position_ok();310311// methods subclasses must implement to specify max/min test angles:312virtual float target_angle_max_rp_cd() const = 0;313314// methods subclasses must implement to specify max/min test angles:315virtual float target_angle_max_y_cd() const = 0;316317// methods subclasses must implement to specify max/min test angles:318virtual float target_angle_min_rp_cd() const = 0;319320// methods subclasses must implement to specify max/min test angles:321virtual float target_angle_min_y_cd() const = 0;322323// methods subclasses must implement to specify max/min test angles:324virtual float angle_lim_max_rp_cd() const = 0;325326// methods subclasses must implement to specify max/min test angles:327virtual float angle_lim_neg_rpy_cd() const = 0;328329// initialise position controller330bool init_position_controller();331332// main state machine to level vehicle, perform a test and update gains333// directly updates attitude controller with targets334void control_attitude();335336// returns true if vehicle is close to level337bool currently_level();338339bool pilot_override; // true = pilot is overriding controls so we suspend tuning temporarily340bool use_poshold; // true = enable position hold341bool have_position; // true = start_position is value342Vector3f start_position; // target when holding position as an offset from EKF origin in cm in NEU frame343344// variables345uint32_t override_time; // the last time the pilot overrode the controls346347// time in ms of last pilot override warning348uint32_t last_pilot_override_warning;349350// True if we ever got a pilot testing command of tuned gains.351// If true then disarming will save if the tuned gains are currently active.352bool have_pilot_testing_command;353354};355356#endif // AC_AUTOTUNE_ENABLED357358359