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>
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
class AP_AutoTune {
|
|
|
|
public:
|
|
|
|
struct ATGains {
|
|
|
|
AP_Float tau;
|
|
|
|
AP_Float P;
|
|
|
|
AP_Float I;
|
|
|
|
AP_Float D;
|
2015-05-23 00:26:17 -03:00
|
|
|
AP_Float FF;
|
2014-04-12 01:11:33 -03:00
|
|
|
AP_Int16 rmax;
|
|
|
|
AP_Int16 imax;
|
|
|
|
};
|
|
|
|
|
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;
|
2014-04-30 08:21:42 -03:00
|
|
|
uint8_t type;
|
|
|
|
uint8_t state;
|
|
|
|
int16_t servo;
|
|
|
|
float demanded;
|
|
|
|
float achieved;
|
|
|
|
float P;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
// constructor
|
2019-01-17 20:57:30 -04:00
|
|
|
AP_AutoTune(ATGains &_gains, ATType type, const AP_Vehicle::FixedWing &parms);
|
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
|
|
|
|
// typically at 50Hz
|
|
|
|
void update(float desired_rate, float achieved_rate, float servo_out);
|
|
|
|
|
|
|
|
// are we running?
|
|
|
|
bool running:1;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// the current gains
|
|
|
|
ATGains ¤t;
|
|
|
|
|
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
|
|
|
// did we saturate surfaces?
|
|
|
|
bool saturated_surfaces:1;
|
|
|
|
|
|
|
|
// 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
|
2015-06-20 00:30:32 -03:00
|
|
|
uint32_t last_save_ms = 0;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
// the demanded/achieved state
|
|
|
|
enum ATState {DEMAND_UNSATURATED,
|
|
|
|
DEMAND_UNDER_POS,
|
|
|
|
DEMAND_OVER_POS,
|
|
|
|
DEMAND_UNDER_NEG,
|
2015-06-20 00:30:32 -03:00
|
|
|
DEMAND_OVER_NEG} state = DEMAND_UNSATURATED;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
// when we entered the current state
|
2015-06-20 00:30:32 -03:00
|
|
|
uint32_t state_enter_ms = 0;
|
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
|
|
|
|
2014-04-13 02:05:52 -03:00
|
|
|
void write_log(float servo, float demanded, float achieved);
|
2014-04-17 04:20:40 -03:00
|
|
|
|
2015-10-26 08:25:44 -03:00
|
|
|
void log_param_change(float v, const char *suffix);
|
|
|
|
void save_float_if_changed(AP_Float &v, float value, const char *suffix);
|
|
|
|
void save_int16_if_changed(AP_Int16 &v, int16_t value, const char *suffix);
|
2014-04-12 01:11:33 -03:00
|
|
|
};
|