2016-02-17 21:25:34 -04:00
|
|
|
#pragma once
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2019-06-27 00:03:58 -03:00
|
|
|
#include <AP_Logger/LogStructure.h>
|
2022-09-29 20:10:40 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include <AP_Vehicle/AP_FixedWing.h>
|
2022-04-08 06:45:01 -03:00
|
|
|
#include <Filter/SlewLimiter.h>
|
2022-09-29 20:10:40 -03:00
|
|
|
|
2022-03-03 23:29:47 -04:00
|
|
|
#include <Filter/ModeFilter.h>
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2021-11-25 01:23:42 -04:00
|
|
|
class AP_AutoTune
|
|
|
|
{
|
2014-04-12 01:11:33 -03:00
|
|
|
public:
|
|
|
|
struct ATGains {
|
|
|
|
AP_Float tau;
|
2021-03-31 20:42:19 -03:00
|
|
|
AP_Int16 rmax_pos;
|
|
|
|
AP_Int16 rmax_neg;
|
2021-07-10 21:32:08 -03:00
|
|
|
float FF, P, I, D, IMAX;
|
|
|
|
float flt_T, flt_E, flt_D;
|
2014-04-12 01:11:33 -03:00
|
|
|
};
|
|
|
|
|
2014-04-12 05:21:50 -03:00
|
|
|
enum ATType {
|
|
|
|
AUTOTUNE_ROLL = 0,
|
2021-11-25 04:41:55 -04:00
|
|
|
AUTOTUNE_PITCH = 1,
|
|
|
|
AUTOTUNE_YAW = 2,
|
2014-04-12 05:21:50 -03:00
|
|
|
};
|
|
|
|
|
2023-07-19 09:40:09 -03:00
|
|
|
enum Options {
|
|
|
|
DISABLE_FLTD_UPDATE = 0,
|
|
|
|
DISABLE_FLTT_UPDATE = 1
|
|
|
|
};
|
|
|
|
|
2014-04-30 08:21:42 -03:00
|
|
|
struct PACKED log_ATRP {
|
|
|
|
LOG_PACKET_HEADER;
|
2015-04-30 00:23:08 -03:00
|
|
|
uint64_t time_us;
|
2021-04-12 01:43:34 -03:00
|
|
|
uint8_t type;
|
|
|
|
uint8_t state;
|
|
|
|
float actuator;
|
2021-07-10 21:32:08 -03:00
|
|
|
float P_slew;
|
|
|
|
float D_slew;
|
2021-04-13 22:34:04 -03:00
|
|
|
float FF_single;
|
2021-04-12 01:43:34 -03:00
|
|
|
float FF;
|
|
|
|
float P;
|
|
|
|
float I;
|
|
|
|
float D;
|
|
|
|
uint8_t action;
|
|
|
|
float rmax;
|
|
|
|
float tau;
|
2014-04-30 08:21:42 -03:00
|
|
|
};
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
// constructor
|
2022-09-29 20:10:40 -03:00
|
|
|
AP_AutoTune(ATGains &_gains, ATType type, const AP_FixedWing &parms, class AC_PID &rpid);
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
// called when autotune mode is entered
|
|
|
|
void start(void);
|
|
|
|
|
|
|
|
// called to stop autotune and restore gains when user leaves
|
|
|
|
// autotune
|
|
|
|
void stop(void);
|
|
|
|
|
|
|
|
// update called whenever autotune mode is active. This is
|
2021-03-31 20:42:19 -03:00
|
|
|
// called at the main loop rate
|
2022-04-08 06:45:01 -03:00
|
|
|
void update(struct AP_PIDInfo &pid_info, float scaler, float angle_err_deg);
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
// are we running?
|
2021-03-31 20:42:19 -03:00
|
|
|
bool running;
|
2021-11-25 01:23:42 -04:00
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
private:
|
|
|
|
// the current gains
|
|
|
|
ATGains ¤t;
|
2022-04-08 06:45:01 -03:00
|
|
|
class AC_PID &rpid;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2014-04-12 05:21:50 -03:00
|
|
|
// what type of autotune is this
|
|
|
|
ATType type;
|
|
|
|
|
2022-09-29 20:10:40 -03:00
|
|
|
const AP_FixedWing &aparm;
|
2014-04-13 09:11:57 -03:00
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
// values to restore if we leave autotune mode
|
2021-10-09 07:25:59 -03:00
|
|
|
ATGains restore;
|
|
|
|
ATGains last_save;
|
2021-03-31 20:42:19 -03:00
|
|
|
|
2021-04-12 05:48:36 -03:00
|
|
|
// last logging time
|
|
|
|
uint32_t last_log_ms;
|
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
// the demanded/achieved state
|
|
|
|
enum class ATState {IDLE,
|
|
|
|
DEMAND_POS,
|
2021-11-25 01:23:42 -04:00
|
|
|
DEMAND_NEG
|
|
|
|
};
|
2021-03-31 20:42:19 -03:00
|
|
|
ATState state;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
// the demanded/achieved state
|
2021-03-31 20:42:19 -03:00
|
|
|
enum class Action {NONE,
|
|
|
|
LOW_RATE,
|
|
|
|
SHORT,
|
|
|
|
RAISE_PD,
|
2021-04-11 21:13:16 -03:00
|
|
|
LOWER_PD,
|
2021-07-10 21:32:08 -03:00
|
|
|
IDLE_LOWER_PD,
|
|
|
|
RAISE_D,
|
|
|
|
RAISE_P,
|
|
|
|
LOWER_D,
|
2021-11-25 01:23:42 -04:00
|
|
|
LOWER_P
|
|
|
|
};
|
2021-03-31 20:42:19 -03:00
|
|
|
Action action;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
// when we entered the current state
|
2021-03-31 20:42:19 -03:00
|
|
|
uint32_t state_enter_ms;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
void check_state_exit(uint32_t state_time_ms);
|
2021-10-09 07:25:59 -03:00
|
|
|
void save_gains(void);
|
2014-04-12 05:21:50 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
void save_float_if_changed(AP_Float &v, float value);
|
|
|
|
void save_int16_if_changed(AP_Int16 &v, int16_t value);
|
2021-04-11 21:13:16 -03:00
|
|
|
void state_change(ATState newstate);
|
2021-11-25 04:41:55 -04:00
|
|
|
const char *axis_string(void) const;
|
2021-03-31 20:42:19 -03:00
|
|
|
|
|
|
|
// get gains with PID components
|
2021-10-09 07:25:59 -03:00
|
|
|
ATGains get_gains(void);
|
|
|
|
void restore_gains(void);
|
2021-03-31 20:42:19 -03:00
|
|
|
|
2021-04-01 23:31:45 -03:00
|
|
|
// update rmax and tau towards target
|
|
|
|
void update_rmax();
|
|
|
|
|
2023-07-19 09:40:09 -03:00
|
|
|
bool has_option(Options option) {
|
|
|
|
return (aparm.autotune_options.get() & uint32_t(1<<uint32_t(option))) != 0;
|
|
|
|
}
|
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
// 5 point mode filter for FF estimate
|
|
|
|
ModeFilterFloat_Size5 ff_filter;
|
|
|
|
|
2024-07-27 09:12:50 -03:00
|
|
|
LowPassFilterConstDtFloat actuator_filter;
|
|
|
|
LowPassFilterConstDtFloat rate_filter;
|
|
|
|
LowPassFilterConstDtFloat target_filter;
|
2021-03-31 20:42:19 -03:00
|
|
|
|
2021-07-10 21:32:08 -03:00
|
|
|
// separate slew limiters for P and D
|
|
|
|
float slew_limit_max, slew_limit_tau;
|
|
|
|
SlewLimiter slew_limiter_P{slew_limit_max, slew_limit_tau};
|
|
|
|
SlewLimiter slew_limiter_D{slew_limit_max, slew_limit_tau};
|
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
float max_actuator;
|
|
|
|
float min_actuator;
|
|
|
|
float max_rate;
|
|
|
|
float min_rate;
|
|
|
|
float max_target;
|
|
|
|
float min_target;
|
|
|
|
float max_P;
|
|
|
|
float max_D;
|
|
|
|
float min_Dmod;
|
2021-04-11 21:13:16 -03:00
|
|
|
float max_Dmod;
|
2021-07-10 21:32:08 -03:00
|
|
|
float max_SRate_P;
|
|
|
|
float max_SRate_D;
|
2021-04-01 22:54:34 -03:00
|
|
|
float FF_single;
|
2021-07-10 21:32:08 -03:00
|
|
|
uint16_t ff_count;
|
|
|
|
float dt;
|
|
|
|
float D_limit;
|
|
|
|
float P_limit;
|
|
|
|
uint32_t D_set_ms;
|
|
|
|
uint32_t P_set_ms;
|
|
|
|
uint8_t done_count;
|
2014-04-12 01:11:33 -03:00
|
|
|
};
|