2016-02-17 21:25:34 -04:00
|
|
|
#pragma once
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2019-06-27 00:03:58 -03:00
|
|
|
#include <AP_Logger/LogStructure.h>
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
2019-06-27 00:03:58 -03:00
|
|
|
#include <AP_Vehicle/AP_Vehicle.h>
|
2021-02-27 05:56:41 -04:00
|
|
|
#include <AC_PID/AC_PID.h>
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
class AP_AutoTune {
|
|
|
|
public:
|
|
|
|
struct ATGains {
|
|
|
|
AP_Float tau;
|
2021-03-31 20:42:19 -03:00
|
|
|
AP_Int16 rmax_pos;
|
|
|
|
AP_Int16 rmax_neg;
|
2021-07-04 21:50:49 -03:00
|
|
|
float FF, P, I, D, IMAX, flt_T;
|
2014-04-12 01:11:33 -03:00
|
|
|
};
|
|
|
|
|
2014-04-12 05:21:50 -03:00
|
|
|
enum ATType {
|
|
|
|
AUTOTUNE_ROLL = 0,
|
|
|
|
AUTOTUNE_PITCH = 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;
|
|
|
|
float desired_rate;
|
|
|
|
float actual_rate;
|
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
|
2021-02-27 05:56:41 -04:00
|
|
|
AP_AutoTune(ATGains &_gains, ATType type, const AP_Vehicle::FixedWing &parms, 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
|
2021-04-12 23:37:37 -03:00
|
|
|
void update(AP_Logger::PID_Info &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;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// the current gains
|
|
|
|
ATGains ¤t;
|
2021-02-27 05:56:41 -04:00
|
|
|
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;
|
|
|
|
|
2014-04-13 09:11:57 -03:00
|
|
|
const AP_Vehicle::FixedWing &aparm;
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
// values to restore if we leave autotune mode
|
|
|
|
ATGains restore;
|
|
|
|
|
2014-04-13 02:35:52 -03:00
|
|
|
// values we last saved
|
|
|
|
ATGains last_save;
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
// values to save on the next save event
|
|
|
|
ATGains next_save;
|
|
|
|
|
|
|
|
// time when we last saved
|
2021-03-31 20:42:19 -03:00
|
|
|
uint32_t last_save_ms;
|
|
|
|
|
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,
|
|
|
|
DEMAND_NEG};
|
|
|
|
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,
|
|
|
|
IDLE_LOWER_PD};
|
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_save(void);
|
|
|
|
void check_state_exit(uint32_t state_time_ms);
|
|
|
|
void save_gains(const ATGains &v);
|
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-03-31 20:42:19 -03:00
|
|
|
|
|
|
|
// get gains with PID components
|
|
|
|
ATGains get_gains(const ATGains ¤t);
|
|
|
|
void set_gains(const ATGains &v);
|
|
|
|
|
2021-04-01 23:31:45 -03:00
|
|
|
// update rmax and tau towards target
|
|
|
|
void update_rmax();
|
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
// 5 point mode filter for FF estimate
|
|
|
|
ModeFilterFloat_Size5 ff_filter;
|
|
|
|
|
|
|
|
LowPassFilterFloat actuator_filter;
|
|
|
|
LowPassFilterFloat rate_filter;
|
2021-04-12 23:37:37 -03:00
|
|
|
LowPassFilterFloat target_filter;
|
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-04-04 06:50:15 -03:00
|
|
|
float max_SRate;
|
2021-04-01 22:54:34 -03:00
|
|
|
float FF_single;
|
2014-04-12 01:11:33 -03:00
|
|
|
};
|