123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #pragma once
- #include <AP_HAL/AP_HAL.h>
- #include <AC_AttitudeControl/AC_AttitudeControl_Multi.h>
- #include <AC_AttitudeControl/AC_PosControl.h>
- class AC_AutoTune {
- public:
-
- AC_AutoTune();
-
- virtual void run();
-
- void save_tuning_gains();
-
- void stop();
-
- void reset() {
- mode = UNINITIALISED;
- axes_completed = 0;
- }
-
- static const struct AP_Param::GroupInfo var_info[];
- protected:
-
- virtual bool init(void) = 0;
-
- virtual float get_pilot_desired_climb_rate_cms(void) const = 0;
-
- virtual void get_pilot_desired_rp_yrate_cd(float &roll_cd, float &pitch_cd, float &yaw_rate_cds) = 0;
-
- virtual void init_z_limits() = 0;
-
- virtual void log_pids() = 0;
-
-
- virtual bool start(void);
-
- virtual bool position_ok();
- enum at_event {
- EVENT_AUTOTUNE_INITIALISED = 0,
- EVENT_AUTOTUNE_OFF = 1,
- EVENT_AUTOTUNE_RESTART = 2,
- EVENT_AUTOTUNE_SUCCESS = 3,
- EVENT_AUTOTUNE_FAILED = 4,
- EVENT_AUTOTUNE_REACHED_LIMIT = 5,
- EVENT_AUTOTUNE_PILOT_TESTING = 6,
- EVENT_AUTOTUNE_SAVEDGAINS = 7
- };
-
- virtual void Log_Write_Event(enum at_event id) = 0;
-
- bool init_internals(bool use_poshold,
- AC_AttitudeControl_Multi *attitude_control,
- AC_PosControl *pos_control,
- AP_AHRS_View *ahrs_view,
- AP_InertialNav *inertial_nav);
- private:
- void control_attitude();
- void backup_gains_and_initialise();
- void load_orig_gains();
- void load_tuned_gains();
- void load_intra_test_gains();
- void load_twitch_gains();
- void update_gcs(uint8_t message_id);
- bool roll_enabled();
- bool pitch_enabled();
- bool yaw_enabled();
- void twitching_test_rate(float rate, float rate_target, float &meas_rate_min, float &meas_rate_max);
- void twitching_abort_rate(float angle, float rate, float angle_max, float meas_rate_min);
- void twitching_test_angle(float angle, float rate, float angle_target, float &meas_angle_min, float &meas_angle_max, float &meas_rate_min, float &meas_rate_max);
- void twitching_measure_acceleration(float &rate_of_change, float rate_measurement, float &rate_measurement_max);
- void updating_rate_d_up(float &tune_d, float tune_d_min, float tune_d_max, float tune_d_step_ratio, float &tune_p, float tune_p_min, float tune_p_max, float tune_p_step_ratio, float rate_target, float meas_rate_min, float meas_rate_max);
- void updating_rate_d_down(float &tune_d, float tune_d_min, float tune_d_step_ratio, float &tune_p, float tune_p_min, float tune_p_max, float tune_p_step_ratio, float rate_target, float meas_rate_min, float meas_rate_max);
- void updating_rate_p_up_d_down(float &tune_d, float tune_d_min, float tune_d_step_ratio, float &tune_p, float tune_p_min, float tune_p_max, float tune_p_step_ratio, float rate_target, float meas_rate_min, float meas_rate_max);
- void updating_angle_p_down(float &tune_p, float tune_p_min, float tune_p_step_ratio, float angle_target, float meas_angle_max, float meas_rate_min, float meas_rate_max);
- void updating_angle_p_up(float &tune_p, float tune_p_max, float tune_p_step_ratio, float angle_target, float meas_angle_max, float meas_rate_min, float meas_rate_max);
- void get_poshold_attitude(float &roll_cd, float &pitch_cd, float &yaw_cd);
- void Log_Write_AutoTune(uint8_t axis, uint8_t tune_step, float meas_target, float meas_min, float meas_max, float new_gain_rp, float new_gain_rd, float new_gain_sp, float new_ddt);
- void Log_Write_AutoTuneDetails(float angle_cd, float rate_cds);
- void send_step_string();
- const char *level_issue_string() const;
- const char * type_string() const;
- void announce_state_to_gcs();
- void do_gcs_announcements();
- enum struct LevelIssue {
- NONE,
- ANGLE_ROLL,
- ANGLE_PITCH,
- ANGLE_YAW,
- RATE_ROLL,
- RATE_PITCH,
- RATE_YAW,
- };
- bool check_level(const enum LevelIssue issue, const float current, const float maximum);
- bool currently_level();
-
- enum TuneMode {
- UNINITIALISED = 0,
- TUNING = 1,
- SUCCESS = 2,
- FAILED = 3,
- };
-
- enum StepType {
- WAITING_FOR_LEVEL = 0,
- TWITCHING = 1,
- UPDATE_GAINS = 2
- };
-
- enum AxisType {
- ROLL = 0,
- PITCH = 1,
- YAW = 2,
- };
-
- enum TuneType {
- RD_UP = 0,
- RD_DOWN = 1,
- RP_UP = 2,
- SP_DOWN = 3,
- SP_UP = 4
- };
-
- enum GainType {
- GAIN_ORIGINAL = 0,
- GAIN_TWITCH = 1,
- GAIN_INTRA_TEST = 2,
- GAIN_TUNED = 3,
- };
- enum GainType current_gain_type;
- void load_gains(enum GainType gain_type);
- TuneMode mode : 2;
- bool pilot_override : 1;
- AxisType axis : 2;
- bool positive_direction : 1;
- StepType step : 2;
- TuneType tune_type : 3;
- bool ignore_next : 1;
- bool twitch_first_iter : 1;
- bool use_poshold : 1;
- bool have_position : 1;
- Vector3f start_position;
- uint8_t axes_completed;
-
- uint32_t override_time;
- float test_rate_min;
- float test_rate_max;
- float test_angle_min;
- float test_angle_max;
- uint32_t step_start_time_ms;
- uint32_t level_start_time_ms;
- uint32_t step_time_limit_ms;
- int8_t counter;
- float target_rate, start_rate;
- float target_angle, start_angle;
- float desired_yaw_cd;
- float rate_max, test_accel_max;
- float step_scaler;
- float abort_angle;
- LowPassFilterFloat rotation_rate_filt;
-
- float orig_roll_rp, orig_roll_ri, orig_roll_rd, orig_roll_rff, orig_roll_sp, orig_roll_accel;
- float orig_pitch_rp, orig_pitch_ri, orig_pitch_rd, orig_pitch_rff, orig_pitch_sp, orig_pitch_accel;
- float orig_yaw_rp, orig_yaw_ri, orig_yaw_rd, orig_yaw_rff, orig_yaw_rLPF, orig_yaw_sp, orig_yaw_accel;
- bool orig_bf_feedforward;
-
- float tune_roll_rp, tune_roll_rd, tune_roll_sp, tune_roll_accel;
- float tune_pitch_rp, tune_pitch_rd, tune_pitch_sp, tune_pitch_accel;
- float tune_yaw_rp, tune_yaw_rLPF, tune_yaw_sp, tune_yaw_accel;
- uint32_t announce_time;
- float lean_angle;
- float rotation_rate;
- float roll_cd, pitch_cd;
- uint32_t last_pilot_override_warning;
- struct {
- LevelIssue issue{LevelIssue::NONE};
- float maximum;
- float current;
- } level_problem;
- AP_Int8 axis_bitmask;
- AP_Float aggressiveness;
- AP_Float min_d;
-
- AC_AttitudeControl_Multi *attitude_control;
- AC_PosControl *pos_control;
- AP_AHRS_View *ahrs_view;
- AP_InertialNav *inertial_nav;
- AP_Motors *motors;
- };
|