2014-04-12 01:11:33 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
The strategy for roll/pitch autotune is to give the user a AUTOTUNE
|
|
|
|
flight mode which behaves just like FBWA, but does automatic
|
|
|
|
tuning.
|
|
|
|
*/
|
|
|
|
|
2015-10-26 09:01:52 -03:00
|
|
|
#include "AP_AutoTune.h"
|
|
|
|
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
2015-10-26 09:01:52 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2019-06-27 00:03:58 -03:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2022-04-08 06:45:01 -03:00
|
|
|
#include <AC_PID/AC_PID.h>
|
2022-09-29 20:10:40 -03:00
|
|
|
#include <AP_Scheduler/AP_Scheduler.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2024-08-30 05:29:29 -03:00
|
|
|
#include <AP_InertialSensor/AP_InertialSensor.h>
|
2014-04-12 01:11:33 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2021-07-10 21:32:08 -03:00
|
|
|
// step size for changing FF gains, percentage
|
2021-03-31 20:42:19 -03:00
|
|
|
#define AUTOTUNE_INCREASE_FF_STEP 12
|
|
|
|
#define AUTOTUNE_DECREASE_FF_STEP 15
|
2014-04-13 02:05:52 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
// limits on IMAX
|
|
|
|
#define AUTOTUNE_MIN_IMAX 0.4
|
|
|
|
#define AUTOTUNE_MAX_IMAX 0.9
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
// ratio of I to P
|
|
|
|
#define AUTOTUNE_I_RATIO 0.75
|
2014-04-13 02:05:52 -03:00
|
|
|
|
2021-04-04 21:25:05 -03:00
|
|
|
// time constant of rate trim loop
|
|
|
|
#define TRIM_TCONST 1.0f
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
// constructor
|
2014-04-15 17:58:02 -03:00
|
|
|
AP_AutoTune::AP_AutoTune(ATGains &_gains, ATType _type,
|
2022-09-29 20:10:40 -03:00
|
|
|
const AP_FixedWing &parms,
|
2021-02-27 05:56:41 -04:00
|
|
|
AC_PID &_rpid) :
|
2014-04-12 05:21:50 -03:00
|
|
|
current(_gains),
|
2021-02-27 05:56:41 -04:00
|
|
|
rpid(_rpid),
|
2014-04-12 05:21:50 -03:00
|
|
|
type(_type),
|
2014-04-13 09:11:57 -03:00
|
|
|
aparm(parms),
|
2021-03-31 20:42:19 -03:00
|
|
|
ff_filter(2)
|
2014-04-12 05:21:50 -03:00
|
|
|
{}
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2015-05-04 03:15:49 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2014-04-12 01:11:33 -03:00
|
|
|
#include <stdio.h>
|
2014-04-12 05:21:50 -03:00
|
|
|
# define Debug(fmt, args ...) do {::printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
|
2014-04-12 01:11:33 -03:00
|
|
|
#else
|
2021-11-25 01:23:42 -04:00
|
|
|
# define Debug(fmt, args ...)
|
2014-04-12 01:11:33 -03:00
|
|
|
#endif
|
|
|
|
|
2014-04-13 09:11:57 -03:00
|
|
|
/*
|
|
|
|
auto-tuning table. This table gives the starting values for key
|
|
|
|
tuning parameters based on a user chosen AUTOTUNE_LEVEL parameter
|
|
|
|
from 1 to 10. Level 1 is a very soft tune. Level 10 is a very
|
2016-05-12 13:54:45 -03:00
|
|
|
aggressive tune.
|
2021-04-04 06:50:15 -03:00
|
|
|
Level 0 means use the existing RMAX and TCONST parameters
|
2014-04-13 09:11:57 -03:00
|
|
|
*/
|
|
|
|
static const struct {
|
|
|
|
float tau;
|
|
|
|
float rmax;
|
2015-10-25 14:03:46 -03:00
|
|
|
} tuning_table[] = {
|
2021-03-31 20:42:19 -03:00
|
|
|
{ 1.00, 20 }, // level 1
|
|
|
|
{ 0.90, 30 }, // level 2
|
|
|
|
{ 0.80, 40 }, // level 3
|
|
|
|
{ 0.70, 50 }, // level 4
|
|
|
|
{ 0.60, 60 }, // level 5
|
|
|
|
{ 0.50, 75 }, // level 6
|
2021-04-04 21:23:53 -03:00
|
|
|
{ 0.30, 90 }, // level 7
|
|
|
|
{ 0.2, 120 }, // level 8
|
|
|
|
{ 0.15, 160 }, // level 9
|
|
|
|
{ 0.1, 210 }, // level 10
|
|
|
|
{ 0.1, 300 }, // (yes, it goes to 11)
|
2014-04-13 09:11:57 -03:00
|
|
|
};
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
/*
|
|
|
|
start an autotune session
|
|
|
|
*/
|
|
|
|
void AP_AutoTune::start(void)
|
|
|
|
{
|
|
|
|
running = true;
|
2021-03-31 20:42:19 -03:00
|
|
|
state = ATState::IDLE;
|
2014-04-15 17:58:02 -03:00
|
|
|
|
2021-10-09 07:25:59 -03:00
|
|
|
current = restore = last_save = get_gains();
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2021-04-01 23:31:45 -03:00
|
|
|
// do first update of rmax and tau now
|
|
|
|
update_rmax();
|
|
|
|
|
2021-07-10 21:32:08 -03:00
|
|
|
dt = AP::scheduler().get_loop_period_s();
|
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
rpid.kIMAX().set(constrain_float(rpid.kIMAX(), AUTOTUNE_MIN_IMAX, AUTOTUNE_MAX_IMAX));
|
2014-04-13 02:05:52 -03:00
|
|
|
|
2021-04-12 23:37:37 -03:00
|
|
|
// use 0.75Hz filters on the actuator, rate and target to reduce impact of noise
|
2021-04-08 02:57:42 -03:00
|
|
|
actuator_filter.set_cutoff_frequency(AP::scheduler().get_loop_rate_hz(), 0.75);
|
|
|
|
rate_filter.set_cutoff_frequency(AP::scheduler().get_loop_rate_hz(), 0.75);
|
2021-03-31 20:42:19 -03:00
|
|
|
|
2021-04-12 23:37:37 -03:00
|
|
|
// target filter is a bit broader
|
|
|
|
target_filter.set_cutoff_frequency(AP::scheduler().get_loop_rate_hz(), 4);
|
|
|
|
|
2021-04-03 20:15:20 -03:00
|
|
|
ff_filter.reset();
|
|
|
|
actuator_filter.reset();
|
|
|
|
rate_filter.reset();
|
2021-07-10 21:32:08 -03:00
|
|
|
D_limit = 0;
|
|
|
|
P_limit = 0;
|
|
|
|
ff_count = 0;
|
|
|
|
D_set_ms = 0;
|
|
|
|
P_set_ms = 0;
|
|
|
|
done_count = 0;
|
2021-04-03 20:15:20 -03:00
|
|
|
|
2021-04-04 06:50:15 -03:00
|
|
|
if (!is_positive(rpid.slew_limit())) {
|
|
|
|
// we must have a slew limit, default to 150 deg/s
|
|
|
|
rpid.slew_limit().set_and_save(150);
|
|
|
|
}
|
|
|
|
|
2021-04-08 03:36:32 -03:00
|
|
|
if (current.FF < 0.01) {
|
|
|
|
// don't allow for zero FF
|
|
|
|
current.FF = 0.01;
|
|
|
|
rpid.ff().set(current.FF);
|
|
|
|
}
|
|
|
|
|
2021-02-27 05:56:41 -04:00
|
|
|
Debug("START FF -> %.3f\n", rpid.ff().get());
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
called when we change state to see if we should change gains
|
|
|
|
*/
|
|
|
|
void AP_AutoTune::stop(void)
|
|
|
|
{
|
2014-08-14 03:03:23 -03:00
|
|
|
if (running) {
|
|
|
|
running = false;
|
2021-10-09 07:25:59 -03:00
|
|
|
if (is_positive(D_limit) && is_positive(P_limit)) {
|
|
|
|
save_gains();
|
|
|
|
} else {
|
|
|
|
restore_gains();
|
2021-07-10 21:32:08 -03:00
|
|
|
}
|
2014-08-14 03:03:23 -03:00
|
|
|
}
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
|
|
|
|
2021-11-25 04:41:55 -04:00
|
|
|
const char *AP_AutoTune::axis_string(void) const
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AUTOTUNE_ROLL:
|
|
|
|
return "Roll";
|
|
|
|
case AUTOTUNE_PITCH:
|
|
|
|
return "Pitch";
|
|
|
|
case AUTOTUNE_YAW:
|
|
|
|
return "Yaw";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
/*
|
|
|
|
one update cycle of the autotuner
|
|
|
|
*/
|
2022-03-03 23:29:47 -04:00
|
|
|
void AP_AutoTune::update(AP_PIDInfo &pinfo, float scaler, float angle_err_deg)
|
2014-04-12 01:11:33 -03:00
|
|
|
{
|
|
|
|
if (!running) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-09 07:25:59 -03:00
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
// see what state we are in
|
2021-03-31 20:42:19 -03:00
|
|
|
ATState new_state = state;
|
2021-04-12 23:37:37 -03:00
|
|
|
const float desired_rate = target_filter.apply(pinfo.target);
|
2021-04-02 20:54:31 -03:00
|
|
|
|
2021-04-02 22:00:44 -03:00
|
|
|
// filter actuator without I term so we can take ratios without
|
|
|
|
// accounting for trim offsets. We first need to include the I and
|
|
|
|
// clip to 45 degrees to get the right value of the real surface
|
2023-11-12 02:34:27 -04:00
|
|
|
const float clipped_actuator = constrain_float(pinfo.FF + pinfo.P + pinfo.D + pinfo.DFF + pinfo.I, -45, 45) - pinfo.I;
|
2021-04-02 22:00:44 -03:00
|
|
|
const float actuator = actuator_filter.apply(clipped_actuator);
|
2021-03-31 20:42:19 -03:00
|
|
|
const float actual_rate = rate_filter.apply(pinfo.actual);
|
|
|
|
|
|
|
|
max_actuator = MAX(max_actuator, actuator);
|
|
|
|
min_actuator = MIN(min_actuator, actuator);
|
|
|
|
max_rate = MAX(max_rate, actual_rate);
|
|
|
|
min_rate = MIN(min_rate, actual_rate);
|
|
|
|
max_target = MAX(max_target, desired_rate);
|
|
|
|
min_target = MIN(min_target, desired_rate);
|
|
|
|
max_P = MAX(max_P, fabsf(pinfo.P));
|
|
|
|
max_D = MAX(max_D, fabsf(pinfo.D));
|
|
|
|
min_Dmod = MIN(min_Dmod, pinfo.Dmod);
|
2021-04-11 21:13:16 -03:00
|
|
|
max_Dmod = MAX(max_Dmod, pinfo.Dmod);
|
2021-07-10 21:32:08 -03:00
|
|
|
|
|
|
|
// update the P and D slew rates, using P and D values from before Dmod was applied
|
|
|
|
const float slew_limit_scale = 45.0 / degrees(1);
|
|
|
|
slew_limit_max = rpid.slew_limit();
|
|
|
|
slew_limit_tau = 1.0;
|
|
|
|
slew_limiter_P.modifier((pinfo.P/pinfo.Dmod)*slew_limit_scale, dt);
|
|
|
|
slew_limiter_D.modifier((pinfo.D/pinfo.Dmod)*slew_limit_scale, dt);
|
|
|
|
|
|
|
|
// remember maximum slew rates for this cycle
|
|
|
|
max_SRate_P = MAX(max_SRate_P, slew_limiter_P.get_slew_rate());
|
|
|
|
max_SRate_D = MAX(max_SRate_D, slew_limiter_D.get_slew_rate());
|
2021-03-31 20:42:19 -03:00
|
|
|
|
2021-11-25 04:41:55 -04:00
|
|
|
float att_limit_deg = 0;
|
|
|
|
switch (type) {
|
|
|
|
case AUTOTUNE_ROLL:
|
2024-01-18 21:58:28 -04:00
|
|
|
att_limit_deg = aparm.roll_limit;
|
2021-11-25 04:41:55 -04:00
|
|
|
break;
|
|
|
|
case AUTOTUNE_PITCH:
|
2024-01-18 21:50:35 -04:00
|
|
|
att_limit_deg = MIN(abs(aparm.pitch_limit_max*100),abs(aparm.pitch_limit_min*100))*0.01;
|
2021-11-25 04:41:55 -04:00
|
|
|
break;
|
|
|
|
case AUTOTUNE_YAW:
|
|
|
|
// arbitrary value for yaw angle
|
|
|
|
att_limit_deg = 20;
|
|
|
|
break;
|
2021-03-31 20:42:19 -03:00
|
|
|
}
|
2021-04-02 20:54:31 -03:00
|
|
|
|
2021-11-25 04:41:55 -04:00
|
|
|
|
2021-04-02 20:54:31 -03:00
|
|
|
// thresholds for when we consider an event to start and end
|
2021-07-28 17:33:46 -03:00
|
|
|
const float rate_threshold1 = 0.4 * MIN(att_limit_deg / current.tau.get(), current.rmax_pos);
|
2021-03-31 20:42:19 -03:00
|
|
|
const float rate_threshold2 = 0.25 * rate_threshold1;
|
2021-04-12 23:37:37 -03:00
|
|
|
bool in_att_demand = fabsf(angle_err_deg) >= 0.3 * att_limit_deg;
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
switch (state) {
|
|
|
|
case ATState::IDLE:
|
2021-04-12 23:37:37 -03:00
|
|
|
if (desired_rate > rate_threshold1 && in_att_demand) {
|
2021-03-31 20:42:19 -03:00
|
|
|
new_state = ATState::DEMAND_POS;
|
2021-04-12 23:37:37 -03:00
|
|
|
} else if (desired_rate < -rate_threshold1 && in_att_demand) {
|
2021-03-31 20:42:19 -03:00
|
|
|
new_state = ATState::DEMAND_NEG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATState::DEMAND_POS:
|
|
|
|
if (desired_rate < rate_threshold2) {
|
|
|
|
new_state = ATState::IDLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATState::DEMAND_NEG:
|
|
|
|
if (desired_rate > -rate_threshold2) {
|
|
|
|
new_state = ATState::IDLE;
|
|
|
|
}
|
|
|
|
break;
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:13:16 -03:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
2023-07-13 21:58:06 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2021-04-12 05:48:36 -03:00
|
|
|
if (now - last_log_ms >= 40) {
|
|
|
|
// log at 25Hz
|
2024-08-30 23:50:11 -03:00
|
|
|
const struct log_ATRP pkt {
|
2021-04-12 05:48:36 -03:00
|
|
|
LOG_PACKET_HEADER_INIT(LOG_ATRP_MSG),
|
|
|
|
time_us : AP_HAL::micros64(),
|
|
|
|
type : uint8_t(type),
|
|
|
|
state: uint8_t(new_state),
|
|
|
|
actuator : actuator,
|
2021-07-10 21:32:08 -03:00
|
|
|
P_slew : max_SRate_P,
|
|
|
|
D_slew : max_SRate_D,
|
2021-04-13 22:34:04 -03:00
|
|
|
FF_single: FF_single,
|
2021-04-12 05:48:36 -03:00
|
|
|
FF: current.FF,
|
|
|
|
P: current.P,
|
|
|
|
I: current.I,
|
|
|
|
D: current.D,
|
|
|
|
action: uint8_t(action),
|
|
|
|
rmax: float(current.rmax_pos.get()),
|
2021-04-12 23:37:37 -03:00
|
|
|
tau: current.tau.get()
|
2021-04-12 05:48:36 -03:00
|
|
|
};
|
|
|
|
AP::logger().WriteBlock(&pkt, sizeof(pkt));
|
|
|
|
last_log_ms = now;
|
|
|
|
}
|
2023-07-13 21:58:06 -03:00
|
|
|
#endif
|
2021-04-12 05:48:36 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
if (new_state == state) {
|
2021-04-11 21:13:16 -03:00
|
|
|
if (state == ATState::IDLE &&
|
|
|
|
now - state_enter_ms > 500 &&
|
|
|
|
max_Dmod < 0.9) {
|
|
|
|
// we've been oscillating while idle, reduce P or D
|
2021-07-10 21:32:08 -03:00
|
|
|
const float slew_sum = max_SRate_P + max_SRate_D;
|
|
|
|
const float gain_mul = 0.5;
|
|
|
|
current.P *= linear_interpolate(gain_mul, 1.0,
|
|
|
|
max_SRate_P,
|
|
|
|
slew_sum, 0);
|
|
|
|
current.D *= linear_interpolate(gain_mul, 1.0,
|
|
|
|
max_SRate_D,
|
|
|
|
slew_sum, 0);
|
2021-04-11 21:13:16 -03:00
|
|
|
rpid.kP().set(current.P);
|
|
|
|
rpid.kD().set(current.D);
|
|
|
|
action = Action::IDLE_LOWER_PD;
|
2021-07-10 21:32:08 -03:00
|
|
|
P_limit = MIN(P_limit, current.P);
|
|
|
|
D_limit = MIN(D_limit, current.D);
|
2021-04-11 21:13:16 -03:00
|
|
|
state_change(state);
|
|
|
|
}
|
2021-03-31 20:42:19 -03:00
|
|
|
return;
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
2021-03-31 20:42:19 -03:00
|
|
|
|
|
|
|
if (new_state != ATState::IDLE) {
|
|
|
|
// starting an event
|
|
|
|
min_actuator = max_actuator = min_rate = max_rate = 0;
|
2014-04-12 01:11:33 -03:00
|
|
|
state_enter_ms = now;
|
2021-03-31 20:42:19 -03:00
|
|
|
state = new_state;
|
|
|
|
return;
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
2021-03-31 20:42:19 -03:00
|
|
|
|
|
|
|
if ((state == ATState::DEMAND_POS && max_rate < 0.01 * current.rmax_pos) ||
|
|
|
|
(state == ATState::DEMAND_NEG && min_rate > -0.01 * current.rmax_neg)) {
|
|
|
|
// we didn't get enough rate
|
|
|
|
action = Action::LOW_RATE;
|
2021-04-11 21:13:16 -03:00
|
|
|
state_change(ATState::IDLE);
|
2021-03-31 20:42:19 -03:00
|
|
|
return;
|
2014-04-12 05:21:50 -03:00
|
|
|
}
|
2014-04-12 01:11:33 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
if (now - state_enter_ms < 100) {
|
|
|
|
// not long enough sample
|
|
|
|
action = Action::SHORT;
|
2021-04-11 21:13:16 -03:00
|
|
|
state_change(ATState::IDLE);
|
2021-03-31 20:42:19 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've finished an event. calculate the single-event FF value
|
|
|
|
if (state == ATState::DEMAND_POS) {
|
2021-04-01 22:54:34 -03:00
|
|
|
FF_single = max_actuator / (max_rate * scaler);
|
2021-03-31 20:42:19 -03:00
|
|
|
} else {
|
2021-04-01 22:54:34 -03:00
|
|
|
FF_single = min_actuator / (min_rate * scaler);
|
2021-03-31 20:42:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// apply median filter
|
2021-04-01 22:54:34 -03:00
|
|
|
float FF = ff_filter.apply(FF_single);
|
2021-07-10 21:32:08 -03:00
|
|
|
ff_count++;
|
2021-03-31 20:42:19 -03:00
|
|
|
|
|
|
|
const float old_FF = rpid.ff();
|
|
|
|
|
|
|
|
// limit size of change in FF
|
|
|
|
FF = constrain_float(FF,
|
|
|
|
old_FF*(1-AUTOTUNE_DECREASE_FF_STEP*0.01),
|
|
|
|
old_FF*(1+AUTOTUNE_INCREASE_FF_STEP*0.01));
|
|
|
|
|
|
|
|
// adjust P and D
|
|
|
|
float D = rpid.kD();
|
|
|
|
float P = rpid.kP();
|
|
|
|
|
2021-07-10 21:32:08 -03:00
|
|
|
if (ff_count == 1) {
|
|
|
|
// apply minimum D and P values
|
|
|
|
D = MAX(D, 0.0005);
|
|
|
|
P = MAX(P, 0.01);
|
|
|
|
} else if (ff_count == 4) {
|
|
|
|
// we got a good ff estimate, halve P ready to start raising D
|
|
|
|
P *= 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if the slew limiter kicked in
|
|
|
|
if (min_Dmod < 1.0 && !is_positive(D_limit)) {
|
|
|
|
// oscillation, without D_limit set
|
|
|
|
if (max_P > 0.5 * max_D) {
|
|
|
|
// lower P and D to get us to a non-oscillating state
|
|
|
|
P *= 0.35;
|
|
|
|
D *= 0.75;
|
|
|
|
action = Action::LOWER_PD;
|
2021-03-31 20:42:19 -03:00
|
|
|
} else {
|
2021-07-10 21:32:08 -03:00
|
|
|
// set D limit to 30% of current D, remember D limit and start to work on P
|
|
|
|
D *= 0.3;
|
|
|
|
D_limit = D;
|
|
|
|
D_set_ms = now;
|
|
|
|
action = Action::LOWER_D;
|
2021-11-25 04:41:55 -04:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "%sD: %.4f", axis_string(), D_limit);
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
2021-07-10 21:32:08 -03:00
|
|
|
} else if (min_Dmod < 1.0) {
|
|
|
|
// oscillation, with D_limit set
|
|
|
|
if (now - D_set_ms > 2000) {
|
|
|
|
// leave 2s for Dmod to settle after lowering D
|
|
|
|
if (max_D > 0.8 * max_P) {
|
|
|
|
// lower D limit some more
|
|
|
|
D *= 0.35;
|
|
|
|
D_limit = D;
|
|
|
|
D_set_ms = now;
|
|
|
|
action = Action::LOWER_D;
|
2021-11-25 04:41:55 -04:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "%sD: %.4f", axis_string(), D_limit);
|
2021-07-10 21:32:08 -03:00
|
|
|
done_count = 0;
|
2021-11-24 04:37:11 -04:00
|
|
|
} else if (now - P_set_ms > 2500) {
|
|
|
|
if (is_positive(P_limit)) {
|
|
|
|
// if we've already got a P estimate then don't
|
|
|
|
// reduce as quickly, stopping small spikes at the
|
|
|
|
// later part of the tune from giving us a very
|
|
|
|
// low P gain
|
|
|
|
P *= 0.7;
|
|
|
|
} else {
|
|
|
|
P *= 0.35;
|
|
|
|
}
|
2021-07-10 21:32:08 -03:00
|
|
|
P_limit = P;
|
|
|
|
P_set_ms = now;
|
|
|
|
action = Action::LOWER_P;
|
|
|
|
done_count = 0;
|
2021-11-25 04:41:55 -04:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "%sP: %.4f", axis_string(), P_limit);
|
2021-07-10 21:32:08 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (ff_count < 4) {
|
|
|
|
// we don't have a good FF estimate yet, keep going
|
2021-11-25 01:23:42 -04:00
|
|
|
|
2021-07-10 21:32:08 -03:00
|
|
|
} else if (!is_positive(D_limit)) {
|
|
|
|
/* we haven't detected D oscillation yet, keep raising D */
|
|
|
|
D *= 1.3;
|
|
|
|
action = Action::RAISE_D;
|
|
|
|
} else if (!is_positive(P_limit)) {
|
|
|
|
/* not oscillating, increase P gain */
|
|
|
|
P *= 1.3;
|
2021-03-31 20:42:19 -03:00
|
|
|
action = Action::RAISE_PD;
|
2021-07-10 21:32:08 -03:00
|
|
|
} else {
|
|
|
|
// after getting P_limit we consider the tune done when we
|
|
|
|
// have done 3 cycles without reducing P
|
|
|
|
if (done_count < 3) {
|
|
|
|
if (++done_count == 3) {
|
2021-11-25 04:41:55 -04:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "%s: Finished", axis_string());
|
2021-10-09 07:25:59 -03:00
|
|
|
save_gains();
|
2021-07-10 21:32:08 -03:00
|
|
|
}
|
|
|
|
}
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
2021-03-31 20:42:19 -03:00
|
|
|
|
|
|
|
rpid.ff().set(FF);
|
|
|
|
rpid.kP().set(P);
|
|
|
|
rpid.kD().set(D);
|
2022-06-23 16:56:53 -03:00
|
|
|
if (type == AUTOTUNE_ROLL) { // for roll set I = smaller of FF or P
|
|
|
|
rpid.kI().set(MIN(P, (FF / TRIM_TCONST)));
|
|
|
|
} else { // for pitch/yaw naturally damped axes) set I usually = FF to get 1 sec I closure
|
|
|
|
rpid.kI().set(MAX(P*AUTOTUNE_I_RATIO, (FF / TRIM_TCONST)));
|
|
|
|
}
|
2021-04-04 21:25:05 -03:00
|
|
|
|
2021-07-10 21:32:08 -03:00
|
|
|
// setup filters to be suitable for time constant and gyro filter
|
2023-07-19 09:40:09 -03:00
|
|
|
// filtering T can prevent P/D oscillation being seen, so allow the
|
|
|
|
// user to switch it off
|
|
|
|
if (!has_option(DISABLE_FLTT_UPDATE)) {
|
|
|
|
rpid.filt_T_hz().set(10.0/(current.tau * 2 * M_PI));
|
|
|
|
}
|
2021-07-10 21:32:08 -03:00
|
|
|
rpid.filt_E_hz().set(0);
|
2023-07-19 09:40:09 -03:00
|
|
|
// filtering D at the same level as VTOL can allow unwanted oscillations to be seen,
|
|
|
|
// so allow the user to switch it off and select their own (usually lower) value
|
|
|
|
if (!has_option(DISABLE_FLTD_UPDATE)) {
|
|
|
|
rpid.filt_D_hz().set(AP::ins().get_gyro_filter_hz()*0.5);
|
|
|
|
}
|
2021-07-04 21:50:49 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
current.FF = FF;
|
|
|
|
current.P = P;
|
|
|
|
current.I = rpid.kI().get();
|
|
|
|
current.D = D;
|
|
|
|
|
2021-07-10 21:32:08 -03:00
|
|
|
Debug("FPID=(%.3f, %.3f, %.3f, %.3f) Dmod=%.2f\n",
|
2021-03-31 20:42:19 -03:00
|
|
|
rpid.ff().get(),
|
|
|
|
rpid.kP().get(),
|
|
|
|
rpid.kI().get(),
|
2021-07-10 21:32:08 -03:00
|
|
|
rpid.kD().get(),
|
|
|
|
min_Dmod);
|
2021-03-31 20:42:19 -03:00
|
|
|
|
2021-04-01 23:31:45 -03:00
|
|
|
// move rmax and tau towards target
|
|
|
|
update_rmax();
|
|
|
|
|
2021-04-11 21:13:16 -03:00
|
|
|
state_change(new_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
record a state change
|
|
|
|
*/
|
|
|
|
void AP_AutoTune::state_change(ATState new_state)
|
|
|
|
{
|
2021-03-31 20:42:19 -03:00
|
|
|
min_Dmod = 1;
|
2021-04-11 21:13:16 -03:00
|
|
|
max_Dmod = 0;
|
2021-07-10 21:32:08 -03:00
|
|
|
max_SRate_P = 1;
|
|
|
|
max_SRate_D = 1;
|
2021-03-31 20:42:19 -03:00
|
|
|
max_P = max_D = 0;
|
|
|
|
state = new_state;
|
2021-04-11 21:13:16 -03:00
|
|
|
state_enter_ms = AP_HAL::millis();
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-10-09 07:25:59 -03:00
|
|
|
save a float if it has changed
|
2014-04-17 04:20:40 -03:00
|
|
|
*/
|
2021-10-09 07:25:59 -03:00
|
|
|
void AP_AutoTune::save_float_if_changed(AP_Float &v, float old_value)
|
2014-04-17 04:20:40 -03:00
|
|
|
{
|
2021-10-09 07:25:59 -03:00
|
|
|
if (!is_equal(old_value, v.get())) {
|
2014-04-17 04:20:40 -03:00
|
|
|
v.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 09:22:13 -03:00
|
|
|
/*
|
2021-10-09 07:25:59 -03:00
|
|
|
save a int16_t if it has changed
|
2014-04-30 09:22:13 -03:00
|
|
|
*/
|
2021-10-09 07:25:59 -03:00
|
|
|
void AP_AutoTune::save_int16_if_changed(AP_Int16 &v, int16_t old_value)
|
2014-04-30 09:22:13 -03:00
|
|
|
{
|
|
|
|
if (old_value != v.get()) {
|
|
|
|
v.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-12 01:11:33 -03:00
|
|
|
/*
|
|
|
|
save a set of gains
|
|
|
|
*/
|
2021-10-09 07:25:59 -03:00
|
|
|
void AP_AutoTune::save_gains(void)
|
2014-04-12 01:11:33 -03:00
|
|
|
{
|
2021-10-09 07:25:59 -03:00
|
|
|
const auto &v = last_save;
|
2021-03-31 20:42:19 -03:00
|
|
|
save_float_if_changed(current.tau, v.tau);
|
|
|
|
save_int16_if_changed(current.rmax_pos, v.rmax_pos);
|
|
|
|
save_int16_if_changed(current.rmax_neg, v.rmax_neg);
|
|
|
|
save_float_if_changed(rpid.ff(), v.FF);
|
|
|
|
save_float_if_changed(rpid.kP(), v.P);
|
|
|
|
save_float_if_changed(rpid.kI(), v.I);
|
|
|
|
save_float_if_changed(rpid.kD(), v.D);
|
|
|
|
save_float_if_changed(rpid.kIMAX(), v.IMAX);
|
2021-07-04 21:50:49 -03:00
|
|
|
save_float_if_changed(rpid.filt_T_hz(), v.flt_T);
|
2021-07-10 21:32:08 -03:00
|
|
|
save_float_if_changed(rpid.filt_E_hz(), v.flt_E);
|
|
|
|
save_float_if_changed(rpid.filt_D_hz(), v.flt_D);
|
2021-10-09 07:25:59 -03:00
|
|
|
last_save = get_gains();
|
2014-04-12 01:11:33 -03:00
|
|
|
}
|
2014-04-12 05:21:50 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
/*
|
|
|
|
get gains with PID components
|
|
|
|
*/
|
2021-10-09 07:25:59 -03:00
|
|
|
AP_AutoTune::ATGains AP_AutoTune::get_gains(void)
|
2014-04-12 05:21:50 -03:00
|
|
|
{
|
2021-10-09 07:25:59 -03:00
|
|
|
ATGains ret = current;
|
2021-03-31 20:42:19 -03:00
|
|
|
ret.FF = rpid.ff();
|
|
|
|
ret.P = rpid.kP();
|
|
|
|
ret.I = rpid.kI();
|
|
|
|
ret.D = rpid.kD();
|
|
|
|
ret.IMAX = rpid.kIMAX();
|
2021-07-04 21:50:49 -03:00
|
|
|
ret.flt_T = rpid.filt_T_hz();
|
2021-07-10 21:32:08 -03:00
|
|
|
ret.flt_E = rpid.filt_E_hz();
|
|
|
|
ret.flt_D = rpid.filt_D_hz();
|
2021-03-31 20:42:19 -03:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-12 05:21:50 -03:00
|
|
|
|
2021-03-31 20:42:19 -03:00
|
|
|
/*
|
|
|
|
set gains with PID components
|
|
|
|
*/
|
2021-10-09 07:25:59 -03:00
|
|
|
void AP_AutoTune::restore_gains(void)
|
2021-03-31 20:42:19 -03:00
|
|
|
{
|
2021-10-09 07:25:59 -03:00
|
|
|
current = restore;
|
|
|
|
rpid.ff().set(restore.FF);
|
|
|
|
rpid.kP().set(restore.P);
|
|
|
|
rpid.kI().set(restore.I);
|
|
|
|
rpid.kD().set(restore.D);
|
|
|
|
rpid.kIMAX().set(restore.IMAX);
|
|
|
|
rpid.filt_T_hz().set(restore.flt_T);
|
|
|
|
rpid.filt_E_hz().set(restore.flt_E);
|
|
|
|
rpid.filt_D_hz().set(restore.flt_D);
|
2014-04-12 05:21:50 -03:00
|
|
|
}
|
2021-04-01 23:31:45 -03:00
|
|
|
|
2021-04-02 16:44:10 -03:00
|
|
|
/*
|
|
|
|
update RMAX and TAU parameters on each step. We move them gradually
|
|
|
|
towards the target to allow for a user going straight to a level 10
|
|
|
|
tune while starting with a poorly tuned plane
|
|
|
|
*/
|
2021-04-01 23:31:45 -03:00
|
|
|
void AP_AutoTune::update_rmax(void)
|
|
|
|
{
|
2021-04-02 23:13:23 -03:00
|
|
|
uint8_t level = constrain_int32(aparm.autotune_level, 0, ARRAY_SIZE(tuning_table));
|
2021-04-02 16:44:10 -03:00
|
|
|
|
2021-04-02 23:13:23 -03:00
|
|
|
int16_t target_rmax;
|
|
|
|
float target_tau;
|
|
|
|
|
|
|
|
if (level == 0) {
|
|
|
|
// this level means to keep current values of RMAX and TCONST
|
2023-10-28 17:51:03 -03:00
|
|
|
target_rmax = constrain_float(current.rmax_pos, 20, 720);
|
2021-04-08 03:36:32 -03:00
|
|
|
target_tau = constrain_float(current.tau, 0.1, 2);
|
2021-04-02 23:13:23 -03:00
|
|
|
} else {
|
|
|
|
target_rmax = tuning_table[level-1].rmax;
|
|
|
|
target_tau = tuning_table[level-1].tau;
|
2021-07-10 21:32:08 -03:00
|
|
|
if (type == AUTOTUNE_PITCH) {
|
|
|
|
// 50% longer time constant on pitch
|
|
|
|
target_tau *= 1.5;
|
|
|
|
}
|
2021-04-02 23:13:23 -03:00
|
|
|
}
|
2021-04-02 16:44:10 -03:00
|
|
|
|
2021-04-08 03:36:32 -03:00
|
|
|
if (level > 0 && is_positive(current.FF)) {
|
|
|
|
const float invtau = ((1.0f / target_tau) + (current.I / current.FF));
|
|
|
|
if (is_positive(invtau)) {
|
2021-05-05 21:24:50 -03:00
|
|
|
target_tau = MAX(target_tau,1.0f / invtau);
|
2021-04-08 03:36:32 -03:00
|
|
|
}
|
2021-04-04 21:25:05 -03:00
|
|
|
}
|
|
|
|
|
2021-04-01 23:31:45 -03:00
|
|
|
if (current.rmax_pos == 0) {
|
|
|
|
// conservative initial value
|
|
|
|
current.rmax_pos.set(75);
|
|
|
|
}
|
2021-04-02 16:44:10 -03:00
|
|
|
// move RMAX by 20 deg/s per step
|
2021-04-01 23:31:45 -03:00
|
|
|
current.rmax_pos.set(constrain_int32(target_rmax,
|
|
|
|
current.rmax_pos.get()-20,
|
|
|
|
current.rmax_pos.get()+20));
|
2021-04-02 23:13:23 -03:00
|
|
|
|
|
|
|
if (level != 0 || current.rmax_neg.get() == 0) {
|
|
|
|
current.rmax_neg.set(current.rmax_pos.get());
|
|
|
|
}
|
2021-04-01 23:31:45 -03:00
|
|
|
|
|
|
|
// move tau by max 15% per loop
|
|
|
|
current.tau.set(constrain_float(target_tau,
|
|
|
|
current.tau*0.85,
|
|
|
|
current.tau*1.15));
|
|
|
|
}
|