2018-12-12 17:55:52 -04:00
|
|
|
#include "AC_AutoTune.h"
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
#include <AP_Scheduler/AP_Scheduler.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* autotune support for multicopters
|
|
|
|
*
|
|
|
|
* Instructions:
|
|
|
|
* 1) Set up one flight mode switch position to be AltHold.
|
|
|
|
* 2) Set the Ch7 Opt or Ch8 Opt to AutoTune to allow you to turn the auto tuning on/off with the ch7 or ch8 switch.
|
|
|
|
* 3) Ensure the ch7 or ch8 switch is in the LOW position.
|
|
|
|
* 4) Wait for a calm day and go to a large open area.
|
|
|
|
* 5) Take off and put the vehicle into AltHold mode at a comfortable altitude.
|
|
|
|
* 6) Set the ch7/ch8 switch to the HIGH position to engage auto tuning:
|
|
|
|
* a) You will see it twitch about 20 degrees left and right for a few minutes, then it will repeat forward and back.
|
|
|
|
* b) Use the roll and pitch stick at any time to reposition the copter if it drifts away (it will use the original PID gains during repositioning and between tests).
|
|
|
|
* When you release the sticks it will continue auto tuning where it left off.
|
|
|
|
* c) Move the ch7/ch8 switch into the LOW position at any time to abandon the autotuning and return to the origin PIDs.
|
|
|
|
* d) Make sure that you do not have any trim set on your transmitter or the autotune may not get the signal that the sticks are centered.
|
|
|
|
* 7) When the tune completes the vehicle will change back to the original PID gains.
|
|
|
|
* 8) Put the ch7/ch8 switch into the LOW position then back to the HIGH position to test the tuned PID gains.
|
|
|
|
* 9) Put the ch7/ch8 switch into the LOW position to fly using the original PID gains.
|
|
|
|
* 10) If you are happy with the autotuned PID gains, leave the ch7/ch8 switch in the HIGH position, land and disarm to save the PIDs permanently.
|
|
|
|
* If you DO NOT like the new PIDS, switch ch7/ch8 LOW to return to the original PIDs. The gains will not be saved when you disarm
|
|
|
|
*
|
|
|
|
* What it's doing during each "twitch":
|
|
|
|
* a) invokes 90 deg/sec rate request
|
|
|
|
* b) records maximum "forward" roll rate and bounce back rate
|
|
|
|
* c) when copter reaches 20 degrees or 1 second has passed, it commands level
|
|
|
|
* d) tries to keep max rotation rate between 80% ~ 100% of requested rate (90deg/sec) by adjusting rate P
|
|
|
|
* e) increases rate D until the bounce back becomes greater than 10% of requested rate (90deg/sec)
|
|
|
|
* f) decreases rate D until the bounce back becomes less than 10% of requested rate (90deg/sec)
|
|
|
|
* g) increases rate P until the max rotate rate becomes greater than the request rate (90deg/sec)
|
|
|
|
* h) invokes a 20deg angle request on roll or pitch
|
|
|
|
* i) increases stab P until the maximum angle becomes greater than 110% of the requested angle (20deg)
|
|
|
|
* j) decreases stab P by 25%
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define AUTOTUNE_AXIS_BITMASK_ROLL 1
|
|
|
|
#define AUTOTUNE_AXIS_BITMASK_PITCH 2
|
|
|
|
#define AUTOTUNE_AXIS_BITMASK_YAW 4
|
|
|
|
|
|
|
|
#define AUTOTUNE_PILOT_OVERRIDE_TIMEOUT_MS 500 // restart tuning if pilot has left sticks in middle for 2 seconds
|
2018-12-13 01:01:34 -04:00
|
|
|
#define AUTOTUNE_TESTING_STEP_TIMEOUT_MS 1000U // timeout for tuning mode's testing step
|
2021-01-31 21:07:12 -04:00
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_ArduPlane)
|
|
|
|
# define AUTOTUNE_LEVEL_ANGLE_CD 500 // angle which qualifies as level (Plane uses more relaxed 5deg)
|
|
|
|
# define AUTOTUNE_LEVEL_RATE_RP_CD 1000 // rate which qualifies as level for roll and pitch (Plane uses more relaxed 10deg/sec)
|
|
|
|
#else
|
|
|
|
# define AUTOTUNE_LEVEL_ANGLE_CD 250 // angle which qualifies as level
|
|
|
|
# define AUTOTUNE_LEVEL_RATE_RP_CD 500 // rate which qualifies as level for roll and pitch
|
|
|
|
#endif
|
2018-12-12 17:55:52 -04:00
|
|
|
#define AUTOTUNE_LEVEL_RATE_Y_CD 750 // rate which qualifies as level for yaw
|
2018-12-20 09:57:30 -04:00
|
|
|
#define AUTOTUNE_REQUIRED_LEVEL_TIME_MS 500 // time we require the aircraft to be level
|
2021-01-31 22:41:27 -04:00
|
|
|
#define AUTOTUNE_LEVEL_TIMEOUT_MS 2000 // time out for level (relaxes criteria)
|
|
|
|
#define AUTOTUNE_LEVEL_WARNING_INTERVAL_MS 5000 // level failure warning messages sent at this interval to users
|
2018-12-12 17:55:52 -04:00
|
|
|
#define AUTOTUNE_RD_STEP 0.05f // minimum increment when increasing/decreasing Rate D term
|
|
|
|
#define AUTOTUNE_RP_STEP 0.05f // minimum increment when increasing/decreasing Rate P term
|
|
|
|
#define AUTOTUNE_SP_STEP 0.05f // minimum increment when increasing/decreasing Stab P term
|
|
|
|
#define AUTOTUNE_PI_RATIO_FOR_TESTING 0.1f // I is set 10x smaller than P during testing
|
|
|
|
#define AUTOTUNE_PI_RATIO_FINAL 1.0f // I is set 1x P after testing
|
|
|
|
#define AUTOTUNE_YAW_PI_RATIO_FINAL 0.1f // I is set 1x P after testing
|
|
|
|
#define AUTOTUNE_RD_MAX 0.200f // maximum Rate D value
|
|
|
|
#define AUTOTUNE_RLPF_MIN 1.0f // minimum Rate Yaw filter value
|
|
|
|
#define AUTOTUNE_RLPF_MAX 5.0f // maximum Rate Yaw filter value
|
|
|
|
#define AUTOTUNE_RP_MIN 0.01f // minimum Rate P value
|
|
|
|
#define AUTOTUNE_RP_MAX 2.0f // maximum Rate P value
|
|
|
|
#define AUTOTUNE_SP_MAX 20.0f // maximum Stab P value
|
|
|
|
#define AUTOTUNE_SP_MIN 0.5f // maximum Stab P value
|
|
|
|
#define AUTOTUNE_RP_ACCEL_MIN 4000.0f // Minimum acceleration for Roll and Pitch
|
|
|
|
#define AUTOTUNE_Y_ACCEL_MIN 1000.0f // Minimum acceleration for Yaw
|
|
|
|
#define AUTOTUNE_Y_FILT_FREQ 10.0f // Autotune filter frequency when testing Yaw
|
|
|
|
#define AUTOTUNE_SUCCESS_COUNT 4 // The number of successful iterations we need to freeze at current gains
|
|
|
|
#define AUTOTUNE_D_UP_DOWN_MARGIN 0.2f // The margin below the target that we tune D in
|
|
|
|
#define AUTOTUNE_RD_BACKOFF 1.0f // Rate D gains are reduced to 50% of their maximum value discovered during tuning
|
|
|
|
#define AUTOTUNE_RP_BACKOFF 1.0f // Rate P gains are reduced to 97.5% of their maximum value discovered during tuning
|
|
|
|
#define AUTOTUNE_SP_BACKOFF 0.9f // Stab P gains are reduced to 90% of their maximum value discovered during tuning
|
|
|
|
#define AUTOTUNE_ACCEL_RP_BACKOFF 1.0f // back off from maximum acceleration
|
|
|
|
#define AUTOTUNE_ACCEL_Y_BACKOFF 1.0f // back off from maximum acceleration
|
|
|
|
|
|
|
|
// roll and pitch axes
|
|
|
|
#define AUTOTUNE_TARGET_ANGLE_RLLPIT_CD 2000 // target angle during TESTING_RATE step that will cause us to move to next step
|
|
|
|
#define AUTOTUNE_TARGET_RATE_RLLPIT_CDS 18000 // target roll/pitch rate during AUTOTUNE_STEP_TWITCHING step
|
|
|
|
#define AUTOTUNE_TARGET_MIN_ANGLE_RLLPIT_CD 1000 // minimum target angle during TESTING_RATE step that will cause us to move to next step
|
|
|
|
#define AUTOTUNE_TARGET_MIN_RATE_RLLPIT_CDS 4500 // target roll/pitch rate during AUTOTUNE_STEP_TWITCHING step
|
|
|
|
|
|
|
|
// yaw axis
|
|
|
|
#define AUTOTUNE_TARGET_ANGLE_YAW_CD 3000 // target angle during TESTING_RATE step that will cause us to move to next step
|
|
|
|
#define AUTOTUNE_TARGET_RATE_YAW_CDS 9000 // target yaw rate during AUTOTUNE_STEP_TWITCHING step
|
|
|
|
#define AUTOTUNE_TARGET_MIN_ANGLE_YAW_CD 500 // minimum target angle during TESTING_RATE step that will cause us to move to next step
|
|
|
|
#define AUTOTUNE_TARGET_MIN_RATE_YAW_CDS 1500 // minimum target yaw rate during AUTOTUNE_STEP_TWITCHING step
|
|
|
|
|
|
|
|
// Auto Tune message ids for ground station
|
|
|
|
#define AUTOTUNE_MESSAGE_STARTED 0
|
|
|
|
#define AUTOTUNE_MESSAGE_STOPPED 1
|
|
|
|
#define AUTOTUNE_MESSAGE_SUCCESS 2
|
|
|
|
#define AUTOTUNE_MESSAGE_FAILED 3
|
|
|
|
#define AUTOTUNE_MESSAGE_SAVED_GAINS 4
|
2020-06-24 18:15:38 -03:00
|
|
|
#define AUTOTUNE_MESSAGE_TESTING 5
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
#define AUTOTUNE_ANNOUNCE_INTERVAL_MS 2000
|
|
|
|
|
|
|
|
// second table of user settable parameters for quadplanes, this
|
|
|
|
// allows us to go beyond the 64 parameter limit
|
|
|
|
const AP_Param::GroupInfo AC_AutoTune::var_info[] = {
|
|
|
|
// @Param: AXES
|
|
|
|
// @DisplayName: Autotune axis bitmask
|
|
|
|
// @Description: 1-byte bitmap of axes to autotune
|
|
|
|
// @Bitmask: 0:Roll,1:Pitch,2:Yaw
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("AXES", 1, AC_AutoTune, axis_bitmask, 7), // AUTOTUNE_AXIS_BITMASK_DEFAULT
|
|
|
|
|
|
|
|
// @Param: AGGR
|
|
|
|
// @DisplayName: Autotune aggressiveness
|
|
|
|
// @Description: Autotune aggressiveness. Defines the bounce back used to detect size of the D term.
|
|
|
|
// @Range: 0.05 0.10
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("AGGR", 2, AC_AutoTune, aggressiveness, 0.1f),
|
|
|
|
|
|
|
|
// @Param: MIN_D
|
|
|
|
// @DisplayName: AutoTune minimum D
|
|
|
|
// @Description: Defines the minimum D gain
|
|
|
|
// @Range: 0.001 0.006
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("MIN_D", 3, AC_AutoTune, min_d, 0.001f),
|
|
|
|
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
AC_AutoTune::AC_AutoTune()
|
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// autotune_init - should be called when autotune mode is selected
|
|
|
|
bool AC_AutoTune::init_internals(bool _use_poshold,
|
|
|
|
AC_AttitudeControl_Multi *_attitude_control,
|
|
|
|
AC_PosControl *_pos_control,
|
|
|
|
AP_AHRS_View *_ahrs_view,
|
|
|
|
AP_InertialNav *_inertial_nav)
|
|
|
|
{
|
|
|
|
use_poshold = _use_poshold;
|
|
|
|
attitude_control = _attitude_control;
|
|
|
|
pos_control = _pos_control;
|
|
|
|
ahrs_view = _ahrs_view;
|
|
|
|
inertial_nav = _inertial_nav;
|
2019-02-10 14:34:31 -04:00
|
|
|
motors = AP_Motors::get_singleton();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2021-04-29 23:29:34 -03:00
|
|
|
// exit immediately if motor are not armed
|
|
|
|
if ((motors == nullptr) || !motors->armed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise position controller
|
|
|
|
init_position_controller();
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
switch (mode) {
|
|
|
|
case FAILED:
|
|
|
|
// fall through to restart the tuning
|
|
|
|
FALLTHROUGH;
|
|
|
|
|
|
|
|
case UNINITIALISED:
|
|
|
|
// autotune has never been run
|
2021-04-29 23:29:34 -03:00
|
|
|
// so store current gains as original gains
|
|
|
|
backup_gains_and_initialise();
|
|
|
|
// advance mode to tuning
|
|
|
|
mode = TUNING;
|
|
|
|
// send message to ground station that we've started tuning
|
|
|
|
update_gcs(AUTOTUNE_MESSAGE_STARTED);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TUNING:
|
2021-04-29 23:29:34 -03:00
|
|
|
// we are restarting tuning so restart where we left off
|
|
|
|
// reset gains to tuning-start gains (i.e. low I term)
|
|
|
|
load_gains(GAIN_INTRA_TEST);
|
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_RESTART);
|
|
|
|
update_gcs(AUTOTUNE_MESSAGE_STARTED);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SUCCESS:
|
2021-04-29 23:29:34 -03:00
|
|
|
// we have completed a tune and the pilot wishes to test the new gains
|
2018-12-21 21:53:39 -04:00
|
|
|
load_gains(GAIN_TUNED);
|
2020-06-24 18:15:38 -03:00
|
|
|
update_gcs(AUTOTUNE_MESSAGE_TESTING);
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_PILOT_TESTING);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
have_position = false;
|
|
|
|
|
2021-04-29 23:29:34 -03:00
|
|
|
return true;
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop - should be called when the ch7/ch8 switch is switched OFF
|
|
|
|
void AC_AutoTune::stop()
|
|
|
|
{
|
|
|
|
// set gains to their original values
|
2018-12-21 21:53:39 -04:00
|
|
|
load_gains(GAIN_ORIGINAL);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
// re-enable angle-to-rate request limits
|
|
|
|
attitude_control->use_sqrt_controller(true);
|
|
|
|
|
|
|
|
update_gcs(AUTOTUNE_MESSAGE_STOPPED);
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_OFF);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
// Note: we leave the mode as it was so that we know how the autotune ended
|
|
|
|
// we expect the caller will change the flight mode back to the flight mode indicated by the flight mode switch
|
|
|
|
}
|
|
|
|
|
2021-04-29 23:29:34 -03:00
|
|
|
// initialise position controller
|
|
|
|
bool AC_AutoTune::init_position_controller(void)
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
2021-05-03 22:42:26 -03:00
|
|
|
// initialize vertical maximum speeds and acceleration
|
2018-12-12 17:55:52 -04:00
|
|
|
init_z_limits();
|
|
|
|
|
2021-05-19 11:09:48 -03:00
|
|
|
// initialise the vertical position controller
|
2021-05-03 22:42:26 -03:00
|
|
|
pos_control->init_z_controller();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *AC_AutoTune::level_issue_string() const
|
|
|
|
{
|
|
|
|
switch (level_problem.issue) {
|
2019-02-28 06:38:39 -04:00
|
|
|
case LevelIssue::NONE:
|
2018-12-12 17:55:52 -04:00
|
|
|
return "None";
|
2019-02-28 06:38:39 -04:00
|
|
|
case LevelIssue::ANGLE_ROLL:
|
2018-12-12 17:55:52 -04:00
|
|
|
return "Angle(R)";
|
2019-02-28 06:38:39 -04:00
|
|
|
case LevelIssue::ANGLE_PITCH:
|
2018-12-12 17:55:52 -04:00
|
|
|
return "Angle(P)";
|
2019-02-28 06:38:39 -04:00
|
|
|
case LevelIssue::ANGLE_YAW:
|
2018-12-12 17:55:52 -04:00
|
|
|
return "Angle(Y)";
|
2019-02-28 06:38:39 -04:00
|
|
|
case LevelIssue::RATE_ROLL:
|
2018-12-12 17:55:52 -04:00
|
|
|
return "Rate(R)";
|
2019-02-28 06:38:39 -04:00
|
|
|
case LevelIssue::RATE_PITCH:
|
2018-12-12 17:55:52 -04:00
|
|
|
return "Rate(P)";
|
2019-02-28 06:38:39 -04:00
|
|
|
case LevelIssue::RATE_YAW:
|
2018-12-12 17:55:52 -04:00
|
|
|
return "Rate(Y)";
|
|
|
|
}
|
|
|
|
return "Bug";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AC_AutoTune::send_step_string()
|
|
|
|
{
|
|
|
|
if (pilot_override) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: Paused: Pilot Override Active");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (step) {
|
|
|
|
case WAITING_FOR_LEVEL:
|
2021-01-31 22:14:01 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: Leveling (%s %4.1f > %4.1f)", level_issue_string(), (double)(level_problem.current*0.01f), (double)(level_problem.maximum*0.01f));
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
|
|
|
case UPDATE_GAINS:
|
2021-01-31 22:14:01 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: Updating Gains");
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
|
|
|
case TWITCHING:
|
2021-01-31 22:14:01 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: Twitching");
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: unknown step");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *AC_AutoTune::type_string() const
|
|
|
|
{
|
|
|
|
switch (tune_type) {
|
|
|
|
case RD_UP:
|
|
|
|
return "Rate D Up";
|
|
|
|
case RD_DOWN:
|
|
|
|
return "Rate D Down";
|
|
|
|
case RP_UP:
|
|
|
|
return "Rate P Up";
|
|
|
|
case SP_DOWN:
|
|
|
|
return "Angle P Down";
|
|
|
|
case SP_UP:
|
|
|
|
return "Angle P Up";
|
|
|
|
}
|
|
|
|
return "Bug";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AC_AutoTune::do_gcs_announcements()
|
|
|
|
{
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - announce_time < AUTOTUNE_ANNOUNCE_INTERVAL_MS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float tune_rp = 0.0f;
|
|
|
|
float tune_rd = 0.0f;
|
|
|
|
float tune_sp = 0.0f;
|
|
|
|
float tune_accel = 0.0f;
|
|
|
|
char axis_char = '?';
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
tune_rp = tune_roll_rp;
|
|
|
|
tune_rd = tune_roll_rd;
|
|
|
|
tune_sp = tune_roll_sp;
|
|
|
|
tune_accel = tune_roll_accel;
|
|
|
|
axis_char = 'R';
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
tune_rp = tune_pitch_rp;
|
|
|
|
tune_rd = tune_pitch_rd;
|
|
|
|
tune_sp = tune_pitch_sp;
|
|
|
|
tune_accel = tune_pitch_accel;
|
|
|
|
axis_char = 'P';
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
tune_rp = tune_yaw_rp;
|
|
|
|
tune_rd = tune_yaw_rLPF;
|
|
|
|
tune_sp = tune_yaw_sp;
|
|
|
|
tune_accel = tune_yaw_accel;
|
|
|
|
axis_char = 'Y';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: (%c) %s", axis_char, type_string());
|
|
|
|
send_step_string();
|
|
|
|
if (!is_zero(lean_angle)) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: lean=%f target=%f", (double)lean_angle, (double)target_angle);
|
|
|
|
}
|
|
|
|
if (!is_zero(rotation_rate)) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: rotation=%f target=%f", (double)(rotation_rate*0.01f), (double)(target_rate*0.01f));
|
|
|
|
}
|
|
|
|
switch (tune_type) {
|
|
|
|
case RD_UP:
|
|
|
|
case RD_DOWN:
|
|
|
|
case RP_UP:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: p=%f d=%f", (double)tune_rp, (double)tune_rd);
|
|
|
|
break;
|
|
|
|
case SP_DOWN:
|
|
|
|
case SP_UP:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: p=%f accel=%f", (double)tune_sp, (double)tune_accel);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: success %u/%u", counter, AUTOTUNE_SUCCESS_COUNT);
|
|
|
|
|
|
|
|
announce_time = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// run - runs the autotune flight mode
|
|
|
|
// should be called at 100hz or more
|
|
|
|
void AC_AutoTune::run()
|
|
|
|
{
|
|
|
|
// initialize vertical speeds and acceleration
|
|
|
|
init_z_limits();
|
|
|
|
|
|
|
|
// if not auto armed or motor interlock not enabled set throttle to zero and exit immediately
|
|
|
|
// this should not actually be possible because of the init() checks
|
2018-12-17 21:49:29 -04:00
|
|
|
if (!motors->armed() || !motors->get_interlock()) {
|
2019-04-09 09:08:04 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
|
2018-12-28 02:34:34 -04:00
|
|
|
attitude_control->set_throttle_out(0.0f, true, 0.0f);
|
2021-05-03 22:42:26 -03:00
|
|
|
pos_control->relax_z_controller(0.0f);
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 07:08:39 -04:00
|
|
|
float target_roll_cd, target_pitch_cd, target_yaw_rate_cds;
|
2018-12-12 17:55:52 -04:00
|
|
|
get_pilot_desired_rp_yrate_cd(target_roll_cd, target_pitch_cd, target_yaw_rate_cds);
|
|
|
|
|
|
|
|
// get pilot desired climb rate
|
2019-02-28 06:34:54 -04:00
|
|
|
const float target_climb_rate_cms = get_pilot_desired_climb_rate_cms();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2019-02-28 07:08:39 -04:00
|
|
|
const bool zero_rp_input = is_zero(target_roll_cd) && is_zero(target_pitch_cd);
|
2019-02-28 07:44:22 -04:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
2019-02-28 07:08:39 -04:00
|
|
|
if (!zero_rp_input || !is_zero(target_yaw_rate_cds) || !is_zero(target_climb_rate_cms)) {
|
2018-12-12 17:55:52 -04:00
|
|
|
if (!pilot_override) {
|
|
|
|
pilot_override = true;
|
|
|
|
// set gains to their original values
|
2018-12-21 21:53:39 -04:00
|
|
|
load_gains(GAIN_ORIGINAL);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->use_sqrt_controller(true);
|
|
|
|
}
|
|
|
|
// reset pilot override time
|
2019-02-28 07:44:22 -04:00
|
|
|
override_time = now;
|
2018-12-12 17:55:52 -04:00
|
|
|
if (!zero_rp_input) {
|
|
|
|
// only reset position on roll or pitch input
|
|
|
|
have_position = false;
|
|
|
|
}
|
|
|
|
} else if (pilot_override) {
|
|
|
|
// check if we should resume tuning after pilot's override
|
2018-12-21 06:55:15 -04:00
|
|
|
if (now - override_time > AUTOTUNE_PILOT_OVERRIDE_TIMEOUT_MS) {
|
2018-12-12 17:55:52 -04:00
|
|
|
pilot_override = false; // turn off pilot override
|
|
|
|
// set gains to their intra-test values (which are very close to the original gains)
|
2018-12-21 21:53:39 -04:00
|
|
|
// load_gains(GAIN_INTRA_TEST); //I think we should be keeping the originals here to let the I term settle quickly
|
2018-12-12 17:55:52 -04:00
|
|
|
step = WAITING_FOR_LEVEL; // set tuning step back from beginning
|
2018-12-21 06:55:15 -04:00
|
|
|
step_start_time_ms = now;
|
|
|
|
level_start_time_ms = now;
|
2018-12-12 17:55:52 -04:00
|
|
|
desired_yaw_cd = ahrs_view->yaw_sensor;
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 07:44:22 -04:00
|
|
|
if (pilot_override) {
|
|
|
|
if (now - last_pilot_override_warning > 1000) {
|
2021-01-31 22:14:01 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: pilot overrides active");
|
2019-02-28 07:44:22 -04:00
|
|
|
last_pilot_override_warning = now;
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
if (zero_rp_input) {
|
|
|
|
// pilot input on throttle and yaw will still use position hold if enabled
|
|
|
|
get_poshold_attitude(target_roll_cd, target_pitch_cd, desired_yaw_cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set motors to full range
|
2019-04-09 09:08:04 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
// if pilot override call attitude controller
|
|
|
|
if (pilot_override || mode != TUNING) {
|
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll_cd, target_pitch_cd, target_yaw_rate_cds);
|
|
|
|
} else {
|
|
|
|
// somehow get attitude requests from autotuning
|
|
|
|
control_attitude();
|
|
|
|
// tell the user what's going on
|
|
|
|
do_gcs_announcements();
|
|
|
|
}
|
|
|
|
|
|
|
|
// call position controller
|
2021-08-31 01:18:26 -03:00
|
|
|
pos_control->set_pos_target_z_from_climb_rate_cm(target_climb_rate_cms);
|
2018-12-12 17:55:52 -04:00
|
|
|
pos_control->update_z_controller();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-28 06:38:39 -04:00
|
|
|
bool AC_AutoTune::check_level(const LevelIssue issue, const float current, const float maximum)
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
|
|
|
if (current > maximum) {
|
|
|
|
level_problem.current = current;
|
|
|
|
level_problem.maximum = maximum;
|
|
|
|
level_problem.issue = issue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AC_AutoTune::currently_level()
|
|
|
|
{
|
2018-12-21 20:20:45 -04:00
|
|
|
float threshold_mul = 1.0;
|
|
|
|
|
2021-01-31 22:41:27 -04:00
|
|
|
uint32_t now_ms = AP_HAL::millis();
|
|
|
|
if (now_ms - level_start_time_ms > AUTOTUNE_LEVEL_TIMEOUT_MS) {
|
2018-12-21 20:20:45 -04:00
|
|
|
// after a long wait we use looser threshold, to allow tuning
|
|
|
|
// with poor initial gains
|
|
|
|
threshold_mul *= 2;
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:41:27 -04:00
|
|
|
// display warning if vehicle fails to level
|
|
|
|
if ((now_ms - level_start_time_ms > AUTOTUNE_LEVEL_WARNING_INTERVAL_MS) &&
|
|
|
|
(now_ms - level_fail_warning_time_ms > AUTOTUNE_LEVEL_WARNING_INTERVAL_MS)) {
|
2021-04-28 23:49:22 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "AutoTune: failing to level, please tune manually");
|
2021-01-31 22:41:27 -04:00
|
|
|
level_fail_warning_time_ms = now_ms;
|
|
|
|
}
|
|
|
|
|
2019-02-28 06:38:39 -04:00
|
|
|
if (!check_level(LevelIssue::ANGLE_ROLL,
|
2019-09-11 00:04:22 -03:00
|
|
|
fabsf(ahrs_view->roll_sensor - roll_cd),
|
2018-12-21 20:20:45 -04:00
|
|
|
threshold_mul*AUTOTUNE_LEVEL_ANGLE_CD)) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-28 06:38:39 -04:00
|
|
|
if (!check_level(LevelIssue::ANGLE_PITCH,
|
2019-09-11 00:04:22 -03:00
|
|
|
fabsf(ahrs_view->pitch_sensor - pitch_cd),
|
2018-12-21 20:20:45 -04:00
|
|
|
threshold_mul*AUTOTUNE_LEVEL_ANGLE_CD)) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-28 06:38:39 -04:00
|
|
|
if (!check_level(LevelIssue::ANGLE_YAW,
|
2018-12-12 17:55:52 -04:00
|
|
|
fabsf(wrap_180_cd(ahrs_view->yaw_sensor - desired_yaw_cd)),
|
2018-12-21 20:20:45 -04:00
|
|
|
threshold_mul*AUTOTUNE_LEVEL_ANGLE_CD)) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-28 06:38:39 -04:00
|
|
|
if (!check_level(LevelIssue::RATE_ROLL,
|
2018-12-12 17:55:52 -04:00
|
|
|
(ToDeg(ahrs_view->get_gyro().x) * 100.0f),
|
2018-12-21 20:20:45 -04:00
|
|
|
threshold_mul*AUTOTUNE_LEVEL_RATE_RP_CD)) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-28 06:38:39 -04:00
|
|
|
if (!check_level(LevelIssue::RATE_PITCH,
|
2018-12-12 17:55:52 -04:00
|
|
|
(ToDeg(ahrs_view->get_gyro().y) * 100.0f),
|
2018-12-21 20:20:45 -04:00
|
|
|
threshold_mul*AUTOTUNE_LEVEL_RATE_RP_CD)) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-28 06:38:39 -04:00
|
|
|
if (!check_level(LevelIssue::RATE_YAW,
|
2018-12-12 17:55:52 -04:00
|
|
|
(ToDeg(ahrs_view->get_gyro().z) * 100.0f),
|
2018-12-21 20:20:45 -04:00
|
|
|
threshold_mul*AUTOTUNE_LEVEL_RATE_Y_CD)) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attitude_controller - sets attitude control targets during tuning
|
|
|
|
void AC_AutoTune::control_attitude()
|
|
|
|
{
|
|
|
|
rotation_rate = 0.0f; // rotation rate in radians/second
|
|
|
|
lean_angle = 0.0f;
|
|
|
|
const float direction_sign = positive_direction ? 1.0f : -1.0f;
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
|
|
|
// check tuning step
|
|
|
|
switch (step) {
|
|
|
|
|
|
|
|
case WAITING_FOR_LEVEL: {
|
|
|
|
// Note: we should be using intra-test gains (which are very close to the original gains but have lower I)
|
|
|
|
// re-enable rate limits
|
|
|
|
attitude_control->use_sqrt_controller(true);
|
|
|
|
|
|
|
|
get_poshold_attitude(roll_cd, pitch_cd, desired_yaw_cd);
|
|
|
|
|
|
|
|
// hold level attitude
|
|
|
|
attitude_control->input_euler_angle_roll_pitch_yaw(roll_cd, pitch_cd, desired_yaw_cd, true);
|
|
|
|
|
|
|
|
// hold the copter level for 0.5 seconds before we begin a twitch
|
|
|
|
// reset counter if we are no longer level
|
|
|
|
if (!currently_level()) {
|
2018-12-20 09:57:30 -04:00
|
|
|
step_start_time_ms = now;
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we have been level for a sufficient amount of time (0.5 seconds) move onto tuning step
|
2018-12-21 20:20:45 -04:00
|
|
|
if (now - step_start_time_ms > AUTOTUNE_REQUIRED_LEVEL_TIME_MS) {
|
2018-12-12 17:55:52 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "AutoTune: Twitch");
|
|
|
|
// initiate variables for next step
|
|
|
|
step = TWITCHING;
|
2018-12-20 09:57:30 -04:00
|
|
|
step_start_time_ms = now;
|
2018-12-13 01:01:34 -04:00
|
|
|
step_time_limit_ms = AUTOTUNE_TESTING_STEP_TIMEOUT_MS;
|
2018-12-12 17:55:52 -04:00
|
|
|
twitch_first_iter = true;
|
|
|
|
test_rate_max = 0.0f;
|
|
|
|
test_rate_min = 0.0f;
|
|
|
|
test_angle_max = 0.0f;
|
|
|
|
test_angle_min = 0.0f;
|
|
|
|
rotation_rate_filt.reset(0.0f);
|
|
|
|
rate_max = 0.0f;
|
|
|
|
// set gains to their to-be-tested values
|
2018-12-21 21:53:39 -04:00
|
|
|
load_gains(GAIN_TWITCH);
|
|
|
|
} else {
|
|
|
|
// when waiting for level we use the intra-test gains
|
|
|
|
load_gains(GAIN_INTRA_TEST);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
2018-12-21 08:40:58 -04:00
|
|
|
float target_max_rate;
|
2018-12-12 17:55:52 -04:00
|
|
|
switch (axis) {
|
2021-12-19 22:38:01 -04:00
|
|
|
case ROLL: {
|
2018-12-21 08:40:58 -04:00
|
|
|
target_max_rate = MAX(AUTOTUNE_TARGET_MIN_RATE_RLLPIT_CDS, step_scaler*AUTOTUNE_TARGET_RATE_RLLPIT_CDS);
|
2021-12-19 22:38:01 -04:00
|
|
|
const float roll_rate_max_cds = degrees(attitude_control->get_ang_vel_roll_max_rads()) * 100;
|
|
|
|
if (is_positive(roll_rate_max_cds)) {
|
|
|
|
target_max_rate = MIN(target_max_rate, roll_rate_max_cds);
|
|
|
|
}
|
2018-12-21 08:40:58 -04:00
|
|
|
target_rate = constrain_float(ToDeg(attitude_control->max_rate_step_bf_roll())*100.0f, AUTOTUNE_TARGET_MIN_RATE_RLLPIT_CDS, target_max_rate);
|
2018-12-12 17:55:52 -04:00
|
|
|
target_angle = constrain_float(ToDeg(attitude_control->max_angle_step_bf_roll())*100.0f, AUTOTUNE_TARGET_MIN_ANGLE_RLLPIT_CD, AUTOTUNE_TARGET_ANGLE_RLLPIT_CD);
|
2018-12-21 08:40:58 -04:00
|
|
|
abort_angle = AUTOTUNE_TARGET_ANGLE_RLLPIT_CD;
|
2018-12-12 17:55:52 -04:00
|
|
|
start_rate = ToDeg(ahrs_view->get_gyro().x) * 100.0f;
|
|
|
|
start_angle = ahrs_view->roll_sensor;
|
2019-06-27 06:34:57 -03:00
|
|
|
rotation_rate_filt.set_cutoff_frequency(attitude_control->get_rate_roll_pid().filt_D_hz()*2.0f);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
2021-12-19 22:38:01 -04:00
|
|
|
}
|
|
|
|
case PITCH: {
|
2018-12-21 08:40:58 -04:00
|
|
|
target_max_rate = MAX(AUTOTUNE_TARGET_MIN_RATE_RLLPIT_CDS, step_scaler*AUTOTUNE_TARGET_RATE_RLLPIT_CDS);
|
2021-12-19 22:38:01 -04:00
|
|
|
const float pitch_rate_max_cds = degrees(attitude_control->get_ang_vel_pitch_max_rads()) * 100;
|
|
|
|
if (is_positive(pitch_rate_max_cds)) {
|
|
|
|
target_max_rate = MIN(target_max_rate, pitch_rate_max_cds);
|
|
|
|
}
|
2018-12-21 08:40:58 -04:00
|
|
|
target_rate = constrain_float(ToDeg(attitude_control->max_rate_step_bf_pitch())*100.0f, AUTOTUNE_TARGET_MIN_RATE_RLLPIT_CDS, target_max_rate);
|
2018-12-12 17:55:52 -04:00
|
|
|
target_angle = constrain_float(ToDeg(attitude_control->max_angle_step_bf_pitch())*100.0f, AUTOTUNE_TARGET_MIN_ANGLE_RLLPIT_CD, AUTOTUNE_TARGET_ANGLE_RLLPIT_CD);
|
2018-12-21 08:40:58 -04:00
|
|
|
abort_angle = AUTOTUNE_TARGET_ANGLE_RLLPIT_CD;
|
2018-12-12 17:55:52 -04:00
|
|
|
start_rate = ToDeg(ahrs_view->get_gyro().y) * 100.0f;
|
|
|
|
start_angle = ahrs_view->pitch_sensor;
|
2019-06-27 06:34:57 -03:00
|
|
|
rotation_rate_filt.set_cutoff_frequency(attitude_control->get_rate_pitch_pid().filt_D_hz()*2.0f);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
2021-12-19 22:38:01 -04:00
|
|
|
}
|
|
|
|
case YAW: {
|
2018-12-21 08:40:58 -04:00
|
|
|
target_max_rate = MAX(AUTOTUNE_TARGET_MIN_RATE_RLLPIT_CDS, step_scaler*AUTOTUNE_TARGET_RATE_YAW_CDS);
|
2021-12-19 22:38:01 -04:00
|
|
|
const float yaw_rate_max_cds = degrees(attitude_control->get_ang_vel_yaw_max_rads()) * 100;
|
|
|
|
if (is_positive(yaw_rate_max_cds)) {
|
|
|
|
target_max_rate = MIN(target_max_rate, yaw_rate_max_cds);
|
|
|
|
}
|
2018-12-21 08:40:58 -04:00
|
|
|
target_rate = constrain_float(ToDeg(attitude_control->max_rate_step_bf_yaw()*0.75f)*100.0f, AUTOTUNE_TARGET_MIN_RATE_YAW_CDS, target_max_rate);
|
2018-12-12 17:55:52 -04:00
|
|
|
target_angle = constrain_float(ToDeg(attitude_control->max_angle_step_bf_yaw()*0.75f)*100.0f, AUTOTUNE_TARGET_MIN_ANGLE_YAW_CD, AUTOTUNE_TARGET_ANGLE_YAW_CD);
|
2018-12-21 08:40:58 -04:00
|
|
|
abort_angle = AUTOTUNE_TARGET_ANGLE_YAW_CD;
|
2018-12-12 17:55:52 -04:00
|
|
|
start_rate = ToDeg(ahrs_view->get_gyro().z) * 100.0f;
|
|
|
|
start_angle = ahrs_view->yaw_sensor;
|
|
|
|
rotation_rate_filt.set_cutoff_frequency(AUTOTUNE_Y_FILT_FREQ);
|
|
|
|
break;
|
|
|
|
}
|
2021-12-19 22:38:01 -04:00
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
if ((tune_type == SP_DOWN) || (tune_type == SP_UP)) {
|
|
|
|
rotation_rate_filt.reset(start_rate);
|
|
|
|
} else {
|
|
|
|
rotation_rate_filt.reset(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TWITCHING: {
|
|
|
|
// Run the twitching step
|
2018-12-21 22:26:16 -04:00
|
|
|
load_gains(GAIN_TWITCH);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
// disable rate limits
|
|
|
|
attitude_control->use_sqrt_controller(false);
|
|
|
|
// hold current attitude
|
|
|
|
attitude_control->input_rate_bf_roll_pitch_yaw(0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
if ((tune_type == SP_DOWN) || (tune_type == SP_UP)) {
|
|
|
|
// step angle targets on first iteration
|
|
|
|
if (twitch_first_iter) {
|
|
|
|
twitch_first_iter = false;
|
|
|
|
// Testing increasing stabilize P gain so will set lean angle target
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
// request roll to 20deg
|
|
|
|
attitude_control->input_angle_step_bf_roll_pitch_yaw(direction_sign * target_angle, 0.0f, 0.0f);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
// request pitch to 20deg
|
|
|
|
attitude_control->input_angle_step_bf_roll_pitch_yaw(0.0f, direction_sign * target_angle, 0.0f);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
// request pitch to 20deg
|
|
|
|
attitude_control->input_angle_step_bf_roll_pitch_yaw(0.0f, 0.0f, direction_sign * target_angle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Testing rate P and D gains so will set body-frame rate targets.
|
|
|
|
// Rate controller will use existing body-frame rates and convert to motor outputs
|
|
|
|
// for all axes except the one we override here.
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
// override body-frame roll rate
|
|
|
|
attitude_control->rate_bf_roll_target(direction_sign * target_rate + start_rate);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
// override body-frame pitch rate
|
|
|
|
attitude_control->rate_bf_pitch_target(direction_sign * target_rate + start_rate);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
// override body-frame yaw rate
|
|
|
|
attitude_control->rate_bf_yaw_target(direction_sign * target_rate + start_rate);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture this iterations rotation rate and lean angle
|
|
|
|
float gyro_reading = 0;
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
gyro_reading = ahrs_view->get_gyro().x;
|
|
|
|
lean_angle = direction_sign * (ahrs_view->roll_sensor - (int32_t)start_angle);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
gyro_reading = ahrs_view->get_gyro().y;
|
|
|
|
lean_angle = direction_sign * (ahrs_view->pitch_sensor - (int32_t)start_angle);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
gyro_reading = ahrs_view->get_gyro().z;
|
|
|
|
lean_angle = direction_sign * wrap_180_cd(ahrs_view->yaw_sensor-(int32_t)start_angle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add filter to measurements
|
|
|
|
float filter_value;
|
|
|
|
switch (tune_type) {
|
|
|
|
case SP_DOWN:
|
|
|
|
case SP_UP:
|
|
|
|
filter_value = direction_sign * (ToDeg(gyro_reading) * 100.0f);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
filter_value = direction_sign * (ToDeg(gyro_reading) * 100.0f - start_rate);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rotation_rate = rotation_rate_filt.apply(filter_value,
|
|
|
|
AP::scheduler().get_loop_period_s());
|
|
|
|
|
|
|
|
switch (tune_type) {
|
|
|
|
case RD_UP:
|
|
|
|
case RD_DOWN:
|
|
|
|
twitching_test_rate(rotation_rate, target_rate, test_rate_min, test_rate_max);
|
|
|
|
twitching_measure_acceleration(test_accel_max, rotation_rate, rate_max);
|
2018-12-21 08:40:58 -04:00
|
|
|
twitching_abort_rate(lean_angle, rotation_rate, abort_angle, test_rate_min);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
case RP_UP:
|
|
|
|
twitching_test_rate(rotation_rate, target_rate*(1+0.5f*aggressiveness), test_rate_min, test_rate_max);
|
|
|
|
twitching_measure_acceleration(test_accel_max, rotation_rate, rate_max);
|
2018-12-21 08:40:58 -04:00
|
|
|
twitching_abort_rate(lean_angle, rotation_rate, abort_angle, test_rate_min);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
case SP_DOWN:
|
|
|
|
case SP_UP:
|
|
|
|
twitching_test_angle(lean_angle, rotation_rate, target_angle*(1+0.5f*aggressiveness), test_angle_min, test_angle_max, test_rate_min, test_rate_max);
|
|
|
|
twitching_measure_acceleration(test_accel_max, rotation_rate - direction_sign * start_rate, rate_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:10:15 -04:00
|
|
|
// Check for failure causing reverse response
|
|
|
|
if (lean_angle <= -AUTOTUNE_TARGET_MIN_ANGLE_RLLPIT_CD) {
|
|
|
|
step = WAITING_FOR_LEVEL;
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// log this iterations lean angle and rotation rate
|
|
|
|
Log_Write_AutoTuneDetails(lean_angle, rotation_rate);
|
2021-01-09 04:09:12 -04:00
|
|
|
ahrs_view->Write_Rate(*motors, *attitude_control, *pos_control);
|
2018-12-21 22:26:16 -04:00
|
|
|
log_pids();
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UPDATE_GAINS:
|
|
|
|
|
|
|
|
// re-enable rate limits
|
|
|
|
attitude_control->use_sqrt_controller(true);
|
|
|
|
|
|
|
|
// log the latest gains
|
|
|
|
if ((tune_type == SP_DOWN) || (tune_type == SP_UP)) {
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
Log_Write_AutoTune(axis, tune_type, target_angle, test_angle_min, test_angle_max, tune_roll_rp, tune_roll_rd, tune_roll_sp, test_accel_max);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
Log_Write_AutoTune(axis, tune_type, target_angle, test_angle_min, test_angle_max, tune_pitch_rp, tune_pitch_rd, tune_pitch_sp, test_accel_max);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
Log_Write_AutoTune(axis, tune_type, target_angle, test_angle_min, test_angle_max, tune_yaw_rp, tune_yaw_rLPF, tune_yaw_sp, test_accel_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
Log_Write_AutoTune(axis, tune_type, target_rate, test_rate_min, test_rate_max, tune_roll_rp, tune_roll_rd, tune_roll_sp, test_accel_max);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
Log_Write_AutoTune(axis, tune_type, target_rate, test_rate_min, test_rate_max, tune_pitch_rp, tune_pitch_rd, tune_pitch_sp, test_accel_max);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
Log_Write_AutoTune(axis, tune_type, target_rate, test_rate_min, test_rate_max, tune_yaw_rp, tune_yaw_rLPF, tune_yaw_sp, test_accel_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check results after mini-step to increase rate D gain
|
|
|
|
switch (tune_type) {
|
|
|
|
case RD_UP:
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
updating_rate_d_up(tune_roll_rd, min_d, AUTOTUNE_RD_MAX, AUTOTUNE_RD_STEP, tune_roll_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
updating_rate_d_up(tune_pitch_rd, min_d, AUTOTUNE_RD_MAX, AUTOTUNE_RD_STEP, tune_pitch_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
updating_rate_d_up(tune_yaw_rLPF, AUTOTUNE_RLPF_MIN, AUTOTUNE_RLPF_MAX, AUTOTUNE_RD_STEP, tune_yaw_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Check results after mini-step to decrease rate D gain
|
|
|
|
case RD_DOWN:
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
updating_rate_d_down(tune_roll_rd, min_d, AUTOTUNE_RD_STEP, tune_roll_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
updating_rate_d_down(tune_pitch_rd, min_d, AUTOTUNE_RD_STEP, tune_pitch_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
updating_rate_d_down(tune_yaw_rLPF, AUTOTUNE_RLPF_MIN, AUTOTUNE_RD_STEP, tune_yaw_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Check results after mini-step to increase rate P gain
|
|
|
|
case RP_UP:
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
updating_rate_p_up_d_down(tune_roll_rd, min_d, AUTOTUNE_RD_STEP, tune_roll_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
updating_rate_p_up_d_down(tune_pitch_rd, min_d, AUTOTUNE_RD_STEP, tune_pitch_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
updating_rate_p_up_d_down(tune_yaw_rLPF, AUTOTUNE_RLPF_MIN, AUTOTUNE_RD_STEP, tune_yaw_rp, AUTOTUNE_RP_MIN, AUTOTUNE_RP_MAX, AUTOTUNE_RP_STEP, target_rate, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Check results after mini-step to increase stabilize P gain
|
|
|
|
case SP_DOWN:
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
updating_angle_p_down(tune_roll_sp, AUTOTUNE_SP_MIN, AUTOTUNE_SP_STEP, target_angle, test_angle_max, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
updating_angle_p_down(tune_pitch_sp, AUTOTUNE_SP_MIN, AUTOTUNE_SP_STEP, target_angle, test_angle_max, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
updating_angle_p_down(tune_yaw_sp, AUTOTUNE_SP_MIN, AUTOTUNE_SP_STEP, target_angle, test_angle_max, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Check results after mini-step to increase stabilize P gain
|
|
|
|
case SP_UP:
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
updating_angle_p_up(tune_roll_sp, AUTOTUNE_SP_MAX, AUTOTUNE_SP_STEP, target_angle, test_angle_max, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
updating_angle_p_up(tune_pitch_sp, AUTOTUNE_SP_MAX, AUTOTUNE_SP_STEP, target_angle, test_angle_max, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
updating_angle_p_up(tune_yaw_sp, AUTOTUNE_SP_MAX, AUTOTUNE_SP_STEP, target_angle, test_angle_max, test_rate_min, test_rate_max);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've complete this step, finalize pids and move to next step
|
|
|
|
if (counter >= AUTOTUNE_SUCCESS_COUNT) {
|
|
|
|
|
|
|
|
// reset counter
|
|
|
|
counter = 0;
|
|
|
|
|
2018-12-21 20:03:18 -04:00
|
|
|
// reset scaling factor
|
2019-11-30 00:10:15 -04:00
|
|
|
step_scaler = 1.0f;
|
2018-12-21 20:03:18 -04:00
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// move to the next tuning type
|
|
|
|
switch (tune_type) {
|
|
|
|
case RD_UP:
|
|
|
|
tune_type = TuneType(tune_type + 1);
|
|
|
|
break;
|
|
|
|
case RD_DOWN:
|
|
|
|
tune_type = TuneType(tune_type + 1);
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
tune_roll_rd = MAX(min_d, tune_roll_rd * AUTOTUNE_RD_BACKOFF);
|
|
|
|
tune_roll_rp = MAX(AUTOTUNE_RP_MIN, tune_roll_rp * AUTOTUNE_RD_BACKOFF);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
tune_pitch_rd = MAX(min_d, tune_pitch_rd * AUTOTUNE_RD_BACKOFF);
|
|
|
|
tune_pitch_rp = MAX(AUTOTUNE_RP_MIN, tune_pitch_rp * AUTOTUNE_RD_BACKOFF);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
tune_yaw_rLPF = MAX(AUTOTUNE_RLPF_MIN, tune_yaw_rLPF * AUTOTUNE_RD_BACKOFF);
|
|
|
|
tune_yaw_rp = MAX(AUTOTUNE_RP_MIN, tune_yaw_rp * AUTOTUNE_RD_BACKOFF);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RP_UP:
|
|
|
|
tune_type = TuneType(tune_type + 1);
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
tune_roll_rp = MAX(AUTOTUNE_RP_MIN, tune_roll_rp * AUTOTUNE_RP_BACKOFF);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
tune_pitch_rp = MAX(AUTOTUNE_RP_MIN, tune_pitch_rp * AUTOTUNE_RP_BACKOFF);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
tune_yaw_rp = MAX(AUTOTUNE_RP_MIN, tune_yaw_rp * AUTOTUNE_RP_BACKOFF);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SP_DOWN:
|
|
|
|
tune_type = TuneType(tune_type + 1);
|
|
|
|
break;
|
|
|
|
case SP_UP:
|
|
|
|
// we've reached the end of a D-up-down PI-up-down tune type cycle
|
|
|
|
tune_type = RD_UP;
|
|
|
|
|
|
|
|
// advance to the next axis
|
|
|
|
bool complete = false;
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
2018-12-14 02:00:51 -04:00
|
|
|
axes_completed |= AUTOTUNE_AXIS_BITMASK_ROLL;
|
2018-12-12 17:55:52 -04:00
|
|
|
tune_roll_sp = MAX(AUTOTUNE_SP_MIN, tune_roll_sp * AUTOTUNE_SP_BACKOFF);
|
|
|
|
tune_roll_accel = MAX(AUTOTUNE_RP_ACCEL_MIN, test_accel_max * AUTOTUNE_ACCEL_RP_BACKOFF);
|
|
|
|
if (pitch_enabled()) {
|
|
|
|
axis = PITCH;
|
|
|
|
} else if (yaw_enabled()) {
|
|
|
|
axis = YAW;
|
|
|
|
} else {
|
|
|
|
complete = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PITCH:
|
2018-12-14 02:00:51 -04:00
|
|
|
axes_completed |= AUTOTUNE_AXIS_BITMASK_PITCH;
|
2018-12-12 17:55:52 -04:00
|
|
|
tune_pitch_sp = MAX(AUTOTUNE_SP_MIN, tune_pitch_sp * AUTOTUNE_SP_BACKOFF);
|
|
|
|
tune_pitch_accel = MAX(AUTOTUNE_RP_ACCEL_MIN, test_accel_max * AUTOTUNE_ACCEL_RP_BACKOFF);
|
|
|
|
if (yaw_enabled()) {
|
|
|
|
axis = YAW;
|
|
|
|
} else {
|
|
|
|
complete = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YAW:
|
2018-12-14 02:00:51 -04:00
|
|
|
axes_completed |= AUTOTUNE_AXIS_BITMASK_YAW;
|
2018-12-12 17:55:52 -04:00
|
|
|
tune_yaw_sp = MAX(AUTOTUNE_SP_MIN, tune_yaw_sp * AUTOTUNE_SP_BACKOFF);
|
|
|
|
tune_yaw_accel = MAX(AUTOTUNE_Y_ACCEL_MIN, test_accel_max * AUTOTUNE_ACCEL_Y_BACKOFF);
|
|
|
|
complete = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we've just completed all axes we have successfully completed the autotune
|
|
|
|
// change to TESTING mode to allow user to fly with new gains
|
|
|
|
if (complete) {
|
|
|
|
mode = SUCCESS;
|
|
|
|
update_gcs(AUTOTUNE_MESSAGE_SUCCESS);
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_SUCCESS);
|
2018-12-12 17:55:52 -04:00
|
|
|
AP_Notify::events.autotune_complete = true;
|
|
|
|
} else {
|
|
|
|
AP_Notify::events.autotune_next_axis = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse direction
|
|
|
|
positive_direction = !positive_direction;
|
|
|
|
|
|
|
|
if (axis == YAW) {
|
|
|
|
attitude_control->input_euler_angle_roll_pitch_yaw(0.0f, 0.0f, ahrs_view->yaw_sensor, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set gains to their intra-test values (which are very close to the original gains)
|
2018-12-21 21:53:39 -04:00
|
|
|
load_gains(GAIN_INTRA_TEST);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
// reset testing step
|
|
|
|
step = WAITING_FOR_LEVEL;
|
2018-12-20 09:57:30 -04:00
|
|
|
step_start_time_ms = now;
|
2018-12-21 06:55:15 -04:00
|
|
|
level_start_time_ms = step_start_time_ms;
|
|
|
|
step_time_limit_ms = AUTOTUNE_REQUIRED_LEVEL_TIME_MS;
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// backup_gains_and_initialise - store current gains as originals
|
|
|
|
// called before tuning starts to backup original gains
|
|
|
|
void AC_AutoTune::backup_gains_and_initialise()
|
|
|
|
{
|
|
|
|
// initialise state because this is our first time
|
|
|
|
if (roll_enabled()) {
|
|
|
|
axis = ROLL;
|
|
|
|
} else if (pitch_enabled()) {
|
|
|
|
axis = PITCH;
|
|
|
|
} else if (yaw_enabled()) {
|
|
|
|
axis = YAW;
|
|
|
|
}
|
2018-12-18 03:53:19 -04:00
|
|
|
// no axes are complete
|
|
|
|
axes_completed = 0;
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
positive_direction = false;
|
|
|
|
step = WAITING_FOR_LEVEL;
|
2018-12-20 09:57:30 -04:00
|
|
|
step_start_time_ms = AP_HAL::millis();
|
2018-12-21 06:55:15 -04:00
|
|
|
level_start_time_ms = step_start_time_ms;
|
2018-12-12 17:55:52 -04:00
|
|
|
tune_type = RD_UP;
|
2019-11-30 00:10:15 -04:00
|
|
|
step_scaler = 1.0f;
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
desired_yaw_cd = ahrs_view->yaw_sensor;
|
|
|
|
|
|
|
|
aggressiveness = constrain_float(aggressiveness, 0.05f, 0.2f);
|
|
|
|
|
|
|
|
orig_bf_feedforward = attitude_control->get_bf_feedforward();
|
|
|
|
|
|
|
|
// backup original pids and initialise tuned pid values
|
|
|
|
orig_roll_rp = attitude_control->get_rate_roll_pid().kP();
|
|
|
|
orig_roll_ri = attitude_control->get_rate_roll_pid().kI();
|
|
|
|
orig_roll_rd = attitude_control->get_rate_roll_pid().kD();
|
2018-12-21 04:21:59 -04:00
|
|
|
orig_roll_rff = attitude_control->get_rate_roll_pid().ff();
|
2020-06-24 18:15:38 -03:00
|
|
|
orig_roll_fltt = attitude_control->get_rate_roll_pid().filt_T_hz();
|
2021-11-05 17:53:11 -03:00
|
|
|
orig_roll_smax = attitude_control->get_rate_roll_pid().slew_limit();
|
2018-12-12 17:55:52 -04:00
|
|
|
orig_roll_sp = attitude_control->get_angle_roll_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
orig_roll_accel = attitude_control->get_accel_roll_max_cdss();
|
2018-12-12 17:55:52 -04:00
|
|
|
tune_roll_rp = attitude_control->get_rate_roll_pid().kP();
|
|
|
|
tune_roll_rd = attitude_control->get_rate_roll_pid().kD();
|
|
|
|
tune_roll_sp = attitude_control->get_angle_roll_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
tune_roll_accel = attitude_control->get_accel_roll_max_cdss();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
orig_pitch_rp = attitude_control->get_rate_pitch_pid().kP();
|
|
|
|
orig_pitch_ri = attitude_control->get_rate_pitch_pid().kI();
|
|
|
|
orig_pitch_rd = attitude_control->get_rate_pitch_pid().kD();
|
2018-12-21 04:21:59 -04:00
|
|
|
orig_pitch_rff = attitude_control->get_rate_pitch_pid().ff();
|
2020-06-24 18:15:38 -03:00
|
|
|
orig_pitch_fltt = attitude_control->get_rate_pitch_pid().filt_T_hz();
|
2021-11-05 17:53:11 -03:00
|
|
|
orig_pitch_smax = attitude_control->get_rate_pitch_pid().slew_limit();
|
2018-12-12 17:55:52 -04:00
|
|
|
orig_pitch_sp = attitude_control->get_angle_pitch_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
orig_pitch_accel = attitude_control->get_accel_pitch_max_cdss();
|
2018-12-12 17:55:52 -04:00
|
|
|
tune_pitch_rp = attitude_control->get_rate_pitch_pid().kP();
|
|
|
|
tune_pitch_rd = attitude_control->get_rate_pitch_pid().kD();
|
|
|
|
tune_pitch_sp = attitude_control->get_angle_pitch_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
tune_pitch_accel = attitude_control->get_accel_pitch_max_cdss();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
|
|
|
orig_yaw_rp = attitude_control->get_rate_yaw_pid().kP();
|
|
|
|
orig_yaw_ri = attitude_control->get_rate_yaw_pid().kI();
|
|
|
|
orig_yaw_rd = attitude_control->get_rate_yaw_pid().kD();
|
2018-12-21 04:21:59 -04:00
|
|
|
orig_yaw_rff = attitude_control->get_rate_yaw_pid().ff();
|
2020-06-24 18:15:38 -03:00
|
|
|
orig_yaw_fltt = attitude_control->get_rate_yaw_pid().filt_T_hz();
|
2021-11-05 17:53:11 -03:00
|
|
|
orig_yaw_smax = attitude_control->get_rate_yaw_pid().slew_limit();
|
2019-06-27 06:34:57 -03:00
|
|
|
orig_yaw_rLPF = attitude_control->get_rate_yaw_pid().filt_E_hz();
|
2021-09-06 06:45:45 -03:00
|
|
|
orig_yaw_accel = attitude_control->get_accel_yaw_max_cdss();
|
2018-12-12 17:55:52 -04:00
|
|
|
orig_yaw_sp = attitude_control->get_angle_yaw_p().kP();
|
|
|
|
tune_yaw_rp = attitude_control->get_rate_yaw_pid().kP();
|
2019-06-27 06:34:57 -03:00
|
|
|
tune_yaw_rLPF = attitude_control->get_rate_yaw_pid().filt_E_hz();
|
2018-12-12 17:55:52 -04:00
|
|
|
tune_yaw_sp = attitude_control->get_angle_yaw_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
tune_yaw_accel = attitude_control->get_accel_yaw_max_cdss();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_INITIALISED);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// load_orig_gains - set gains to their original values
|
|
|
|
// called by stop and failed functions
|
|
|
|
void AC_AutoTune::load_orig_gains()
|
|
|
|
{
|
|
|
|
attitude_control->bf_feedforward(orig_bf_feedforward);
|
|
|
|
if (roll_enabled()) {
|
|
|
|
if (!is_zero(orig_roll_rp)) {
|
|
|
|
attitude_control->get_rate_roll_pid().kP(orig_roll_rp);
|
|
|
|
attitude_control->get_rate_roll_pid().kI(orig_roll_ri);
|
|
|
|
attitude_control->get_rate_roll_pid().kD(orig_roll_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_roll_pid().ff(orig_roll_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_roll_pid().filt_T_hz(orig_roll_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_roll_pid().slew_limit(orig_roll_smax);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_roll_p().kP(orig_roll_sp);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->set_accel_roll_max_cdss(orig_roll_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pitch_enabled()) {
|
|
|
|
if (!is_zero(orig_pitch_rp)) {
|
|
|
|
attitude_control->get_rate_pitch_pid().kP(orig_pitch_rp);
|
|
|
|
attitude_control->get_rate_pitch_pid().kI(orig_pitch_ri);
|
|
|
|
attitude_control->get_rate_pitch_pid().kD(orig_pitch_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_pitch_pid().ff(orig_pitch_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().filt_T_hz(orig_pitch_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().slew_limit(orig_pitch_smax);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_pitch_p().kP(orig_pitch_sp);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->set_accel_pitch_max_cdss(orig_pitch_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (yaw_enabled()) {
|
|
|
|
if (!is_zero(orig_yaw_rp)) {
|
|
|
|
attitude_control->get_rate_yaw_pid().kP(orig_yaw_rp);
|
|
|
|
attitude_control->get_rate_yaw_pid().kI(orig_yaw_ri);
|
|
|
|
attitude_control->get_rate_yaw_pid().kD(orig_yaw_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_yaw_pid().ff(orig_yaw_rff);
|
2019-06-27 06:34:57 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_E_hz(orig_yaw_rLPF);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_T_hz(orig_yaw_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().slew_limit(orig_yaw_smax);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_yaw_p().kP(orig_yaw_sp);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->set_accel_yaw_max_cdss(orig_yaw_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load_tuned_gains - load tuned gains
|
|
|
|
void AC_AutoTune::load_tuned_gains()
|
|
|
|
{
|
|
|
|
if (!attitude_control->get_bf_feedforward()) {
|
|
|
|
attitude_control->bf_feedforward(true);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->set_accel_roll_max_cdss(0.0f);
|
|
|
|
attitude_control->set_accel_pitch_max_cdss(0.0f);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
if (roll_enabled()) {
|
|
|
|
if (!is_zero(tune_roll_rp)) {
|
|
|
|
attitude_control->get_rate_roll_pid().kP(tune_roll_rp);
|
|
|
|
attitude_control->get_rate_roll_pid().kI(tune_roll_rp*AUTOTUNE_PI_RATIO_FINAL);
|
|
|
|
attitude_control->get_rate_roll_pid().kD(tune_roll_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_roll_pid().ff(orig_roll_rff);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_roll_p().kP(tune_roll_sp);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->set_accel_roll_max_cdss(tune_roll_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pitch_enabled()) {
|
|
|
|
if (!is_zero(tune_pitch_rp)) {
|
|
|
|
attitude_control->get_rate_pitch_pid().kP(tune_pitch_rp);
|
|
|
|
attitude_control->get_rate_pitch_pid().kI(tune_pitch_rp*AUTOTUNE_PI_RATIO_FINAL);
|
|
|
|
attitude_control->get_rate_pitch_pid().kD(tune_pitch_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_pitch_pid().ff(orig_pitch_rff);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_pitch_p().kP(tune_pitch_sp);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->set_accel_pitch_max_cdss(tune_pitch_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (yaw_enabled()) {
|
|
|
|
if (!is_zero(tune_yaw_rp)) {
|
|
|
|
attitude_control->get_rate_yaw_pid().kP(tune_yaw_rp);
|
|
|
|
attitude_control->get_rate_yaw_pid().kI(tune_yaw_rp*AUTOTUNE_YAW_PI_RATIO_FINAL);
|
|
|
|
attitude_control->get_rate_yaw_pid().kD(0.0f);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_yaw_pid().ff(orig_yaw_rff);
|
2019-06-27 06:34:57 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_E_hz(tune_yaw_rLPF);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_yaw_p().kP(tune_yaw_sp);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->set_accel_yaw_max_cdss(tune_yaw_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load_intra_test_gains - gains used between tests
|
|
|
|
// called during testing mode's update-gains step to set gains ahead of return-to-level step
|
|
|
|
void AC_AutoTune::load_intra_test_gains()
|
|
|
|
{
|
|
|
|
// we are restarting tuning so reset gains to tuning-start gains (i.e. low I term)
|
|
|
|
// sanity check the gains
|
|
|
|
attitude_control->bf_feedforward(true);
|
|
|
|
if (roll_enabled()) {
|
|
|
|
attitude_control->get_rate_roll_pid().kP(orig_roll_rp);
|
|
|
|
attitude_control->get_rate_roll_pid().kI(orig_roll_rp*AUTOTUNE_PI_RATIO_FOR_TESTING);
|
|
|
|
attitude_control->get_rate_roll_pid().kD(orig_roll_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_roll_pid().ff(orig_roll_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_roll_pid().filt_T_hz(orig_roll_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_roll_pid().slew_limit(orig_roll_smax);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_roll_p().kP(orig_roll_sp);
|
|
|
|
}
|
|
|
|
if (pitch_enabled()) {
|
|
|
|
attitude_control->get_rate_pitch_pid().kP(orig_pitch_rp);
|
|
|
|
attitude_control->get_rate_pitch_pid().kI(orig_pitch_rp*AUTOTUNE_PI_RATIO_FOR_TESTING);
|
|
|
|
attitude_control->get_rate_pitch_pid().kD(orig_pitch_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_pitch_pid().ff(orig_pitch_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().filt_T_hz(orig_pitch_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().slew_limit(orig_pitch_smax);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_pitch_p().kP(orig_pitch_sp);
|
|
|
|
}
|
|
|
|
if (yaw_enabled()) {
|
|
|
|
attitude_control->get_rate_yaw_pid().kP(orig_yaw_rp);
|
|
|
|
attitude_control->get_rate_yaw_pid().kI(orig_yaw_rp*AUTOTUNE_PI_RATIO_FOR_TESTING);
|
|
|
|
attitude_control->get_rate_yaw_pid().kD(orig_yaw_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_yaw_pid().ff(orig_yaw_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_T_hz(orig_yaw_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().slew_limit(orig_yaw_smax);
|
2019-06-27 06:34:57 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_E_hz(orig_yaw_rLPF);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_yaw_p().kP(orig_yaw_sp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load_twitch_gains - load the to-be-tested gains for a single axis
|
|
|
|
// called by control_attitude() just before it beings testing a gain (i.e. just before it twitches)
|
|
|
|
void AC_AutoTune::load_twitch_gains()
|
|
|
|
{
|
|
|
|
switch (axis) {
|
|
|
|
case ROLL:
|
|
|
|
attitude_control->get_rate_roll_pid().kP(tune_roll_rp);
|
|
|
|
attitude_control->get_rate_roll_pid().kI(tune_roll_rp*0.01f);
|
|
|
|
attitude_control->get_rate_roll_pid().kD(tune_roll_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_roll_pid().ff(0.0f);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_roll_pid().filt_T_hz(0.0f);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_roll_pid().slew_limit(0.0f);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_roll_p().kP(tune_roll_sp);
|
|
|
|
break;
|
|
|
|
case PITCH:
|
|
|
|
attitude_control->get_rate_pitch_pid().kP(tune_pitch_rp);
|
|
|
|
attitude_control->get_rate_pitch_pid().kI(tune_pitch_rp*0.01f);
|
|
|
|
attitude_control->get_rate_pitch_pid().kD(tune_pitch_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_pitch_pid().ff(0.0f);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().filt_T_hz(0.0f);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().slew_limit(0.0f);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_pitch_p().kP(tune_pitch_sp);
|
|
|
|
break;
|
|
|
|
case YAW:
|
|
|
|
attitude_control->get_rate_yaw_pid().kP(tune_yaw_rp);
|
|
|
|
attitude_control->get_rate_yaw_pid().kI(tune_yaw_rp*0.01f);
|
|
|
|
attitude_control->get_rate_yaw_pid().kD(0.0f);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_yaw_pid().ff(0.0f);
|
2019-06-27 06:34:57 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_E_hz(tune_yaw_rLPF);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_T_hz(0.0f);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().slew_limit(0.0f);
|
2018-12-12 17:55:52 -04:00
|
|
|
attitude_control->get_angle_yaw_p().kP(tune_yaw_sp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 21:53:39 -04:00
|
|
|
/*
|
|
|
|
load a specified set of gains
|
|
|
|
*/
|
|
|
|
void AC_AutoTune::load_gains(enum GainType gain_type)
|
|
|
|
{
|
|
|
|
switch (gain_type) {
|
|
|
|
case GAIN_ORIGINAL:
|
|
|
|
load_orig_gains();
|
|
|
|
break;
|
|
|
|
case GAIN_INTRA_TEST:
|
|
|
|
load_intra_test_gains();
|
|
|
|
break;
|
|
|
|
case GAIN_TWITCH:
|
|
|
|
load_twitch_gains();
|
|
|
|
break;
|
|
|
|
case GAIN_TUNED:
|
|
|
|
load_tuned_gains();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// save_tuning_gains - save the final tuned gains for each axis
|
|
|
|
// save discovered gains to eeprom if autotuner is enabled (i.e. switch is in the high position)
|
|
|
|
void AC_AutoTune::save_tuning_gains()
|
|
|
|
{
|
2018-12-14 02:00:51 -04:00
|
|
|
// see if we successfully completed tuning of at least one axis
|
|
|
|
if (axes_completed == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
if (!attitude_control->get_bf_feedforward()) {
|
|
|
|
attitude_control->bf_feedforward_save(true);
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->save_accel_roll_max_cdss(0.0f);
|
|
|
|
attitude_control->save_accel_pitch_max_cdss(0.0f);
|
2018-12-14 02:00:51 -04:00
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// sanity check the rate P values
|
|
|
|
if ((axes_completed & AUTOTUNE_AXIS_BITMASK_ROLL) && roll_enabled() && !is_zero(tune_roll_rp)) {
|
|
|
|
// rate roll gains
|
|
|
|
attitude_control->get_rate_roll_pid().kP(tune_roll_rp);
|
|
|
|
attitude_control->get_rate_roll_pid().kI(tune_roll_rp*AUTOTUNE_PI_RATIO_FINAL);
|
|
|
|
attitude_control->get_rate_roll_pid().kD(tune_roll_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_roll_pid().ff(orig_roll_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_roll_pid().filt_T_hz(orig_roll_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_roll_pid().slew_limit(orig_roll_smax);
|
2018-12-14 02:00:51 -04:00
|
|
|
attitude_control->get_rate_roll_pid().save_gains();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// stabilize roll
|
|
|
|
attitude_control->get_angle_roll_p().kP(tune_roll_sp);
|
|
|
|
attitude_control->get_angle_roll_p().save_gains();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// acceleration roll
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->save_accel_roll_max_cdss(tune_roll_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// resave pids to originals in case the autotune is run again
|
|
|
|
orig_roll_rp = attitude_control->get_rate_roll_pid().kP();
|
|
|
|
orig_roll_ri = attitude_control->get_rate_roll_pid().kI();
|
|
|
|
orig_roll_rd = attitude_control->get_rate_roll_pid().kD();
|
2018-12-21 04:21:59 -04:00
|
|
|
orig_roll_rff = attitude_control->get_rate_roll_pid().ff();
|
2018-12-14 02:00:51 -04:00
|
|
|
orig_roll_sp = attitude_control->get_angle_roll_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
orig_roll_accel = attitude_control->get_accel_roll_max_cdss();
|
2018-12-14 02:00:51 -04:00
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
if ((axes_completed & AUTOTUNE_AXIS_BITMASK_PITCH) && pitch_enabled() && !is_zero(tune_pitch_rp)) {
|
|
|
|
// rate pitch gains
|
|
|
|
attitude_control->get_rate_pitch_pid().kP(tune_pitch_rp);
|
|
|
|
attitude_control->get_rate_pitch_pid().kI(tune_pitch_rp*AUTOTUNE_PI_RATIO_FINAL);
|
|
|
|
attitude_control->get_rate_pitch_pid().kD(tune_pitch_rd);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_pitch_pid().ff(orig_pitch_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().filt_T_hz(orig_pitch_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_pitch_pid().slew_limit(orig_pitch_smax);
|
2018-12-14 02:00:51 -04:00
|
|
|
attitude_control->get_rate_pitch_pid().save_gains();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// stabilize pitch
|
|
|
|
attitude_control->get_angle_pitch_p().kP(tune_pitch_sp);
|
|
|
|
attitude_control->get_angle_pitch_p().save_gains();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// acceleration pitch
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->save_accel_pitch_max_cdss(tune_pitch_accel);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// resave pids to originals in case the autotune is run again
|
|
|
|
orig_pitch_rp = attitude_control->get_rate_pitch_pid().kP();
|
|
|
|
orig_pitch_ri = attitude_control->get_rate_pitch_pid().kI();
|
|
|
|
orig_pitch_rd = attitude_control->get_rate_pitch_pid().kD();
|
2018-12-21 04:21:59 -04:00
|
|
|
orig_pitch_rff = attitude_control->get_rate_pitch_pid().ff();
|
2018-12-14 02:00:51 -04:00
|
|
|
orig_pitch_sp = attitude_control->get_angle_pitch_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
orig_pitch_accel = attitude_control->get_accel_pitch_max_cdss();
|
2018-12-14 02:00:51 -04:00
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
if ((axes_completed & AUTOTUNE_AXIS_BITMASK_YAW) && yaw_enabled() && !is_zero(tune_yaw_rp)) {
|
|
|
|
// rate yaw gains
|
|
|
|
attitude_control->get_rate_yaw_pid().kP(tune_yaw_rp);
|
|
|
|
attitude_control->get_rate_yaw_pid().kI(tune_yaw_rp*AUTOTUNE_YAW_PI_RATIO_FINAL);
|
|
|
|
attitude_control->get_rate_yaw_pid().kD(0.0f);
|
2018-12-21 04:21:59 -04:00
|
|
|
attitude_control->get_rate_yaw_pid().ff(orig_yaw_rff);
|
2020-06-24 18:15:38 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_T_hz(orig_yaw_fltt);
|
2021-11-05 17:53:11 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().slew_limit(orig_yaw_smax);
|
2019-06-27 06:34:57 -03:00
|
|
|
attitude_control->get_rate_yaw_pid().filt_E_hz(tune_yaw_rLPF);
|
2018-12-14 02:00:51 -04:00
|
|
|
attitude_control->get_rate_yaw_pid().save_gains();
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2018-12-14 02:00:51 -04:00
|
|
|
// stabilize yaw
|
|
|
|
attitude_control->get_angle_yaw_p().kP(tune_yaw_sp);
|
|
|
|
attitude_control->get_angle_yaw_p().save_gains();
|
|
|
|
|
|
|
|
// acceleration yaw
|
2021-09-06 06:45:45 -03:00
|
|
|
attitude_control->save_accel_yaw_max_cdss(tune_yaw_accel);
|
2018-12-14 02:00:51 -04:00
|
|
|
|
|
|
|
// resave pids to originals in case the autotune is run again
|
|
|
|
orig_yaw_rp = attitude_control->get_rate_yaw_pid().kP();
|
|
|
|
orig_yaw_ri = attitude_control->get_rate_yaw_pid().kI();
|
|
|
|
orig_yaw_rd = attitude_control->get_rate_yaw_pid().kD();
|
2018-12-21 04:21:59 -04:00
|
|
|
orig_yaw_rff = attitude_control->get_rate_yaw_pid().ff();
|
2019-06-27 06:34:57 -03:00
|
|
|
orig_yaw_rLPF = attitude_control->get_rate_yaw_pid().filt_E_hz();
|
2018-12-14 02:00:51 -04:00
|
|
|
orig_yaw_sp = attitude_control->get_angle_yaw_p().kP();
|
2021-09-06 06:45:45 -03:00
|
|
|
orig_yaw_accel = attitude_control->get_accel_yaw_max_cdss();
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
2018-12-14 02:00:51 -04:00
|
|
|
|
|
|
|
// update GCS and log save gains event
|
|
|
|
update_gcs(AUTOTUNE_MESSAGE_SAVED_GAINS);
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_SAVEDGAINS);
|
2018-12-14 02:00:51 -04:00
|
|
|
|
2019-05-01 09:15:56 -03:00
|
|
|
reset();
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update_gcs - send message to ground station
|
2021-02-01 12:26:25 -04:00
|
|
|
void AC_AutoTune::update_gcs(uint8_t message_id) const
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
|
|
|
switch (message_id) {
|
|
|
|
case AUTOTUNE_MESSAGE_STARTED:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"AutoTune: Started");
|
|
|
|
break;
|
|
|
|
case AUTOTUNE_MESSAGE_STOPPED:
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"AutoTune: Stopped");
|
|
|
|
break;
|
|
|
|
case AUTOTUNE_MESSAGE_SUCCESS:
|
|
|
|
gcs().send_text(MAV_SEVERITY_NOTICE,"AutoTune: Success");
|
|
|
|
break;
|
|
|
|
case AUTOTUNE_MESSAGE_FAILED:
|
|
|
|
gcs().send_text(MAV_SEVERITY_NOTICE,"AutoTune: Failed");
|
|
|
|
break;
|
2020-06-24 18:15:38 -03:00
|
|
|
case AUTOTUNE_MESSAGE_TESTING:
|
|
|
|
gcs().send_text(MAV_SEVERITY_NOTICE,"AutoTune: Pilot Testing");
|
|
|
|
break;
|
2018-12-12 17:55:52 -04:00
|
|
|
case AUTOTUNE_MESSAGE_SAVED_GAINS:
|
2018-12-14 02:00:51 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_NOTICE,"AutoTune: Saved gains for %s%s%s",
|
|
|
|
(axes_completed&AUTOTUNE_AXIS_BITMASK_ROLL)?"Roll ":"",
|
|
|
|
(axes_completed&AUTOTUNE_AXIS_BITMASK_PITCH)?"Pitch ":"",
|
|
|
|
(axes_completed&AUTOTUNE_AXIS_BITMASK_YAW)?"Yaw":"");
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// axis helper functions
|
|
|
|
inline bool AC_AutoTune::roll_enabled()
|
|
|
|
{
|
|
|
|
return axis_bitmask & AUTOTUNE_AXIS_BITMASK_ROLL;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool AC_AutoTune::pitch_enabled()
|
|
|
|
{
|
|
|
|
return axis_bitmask & AUTOTUNE_AXIS_BITMASK_PITCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool AC_AutoTune::yaw_enabled()
|
|
|
|
{
|
|
|
|
return axis_bitmask & AUTOTUNE_AXIS_BITMASK_YAW;
|
|
|
|
}
|
|
|
|
|
|
|
|
// twitching_test_rate - twitching tests
|
|
|
|
// update min and max and test for end conditions
|
|
|
|
void AC_AutoTune::twitching_test_rate(float rate, float rate_target_max, float &meas_rate_min, float &meas_rate_max)
|
|
|
|
{
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
|
|
|
// capture maximum rate
|
|
|
|
if (rate > meas_rate_max) {
|
|
|
|
// the measurement is continuing to increase without stopping
|
|
|
|
meas_rate_max = rate;
|
|
|
|
meas_rate_min = rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture minimum measurement after the measurement has peaked (aka "bounce back")
|
|
|
|
if ((rate < meas_rate_min) && (meas_rate_max > rate_target_max * 0.5f)) {
|
|
|
|
// the measurement is bouncing back
|
|
|
|
meas_rate_min = rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate early stopping time based on the time it takes to get to 75%
|
|
|
|
if (meas_rate_max < rate_target_max * 0.75f) {
|
|
|
|
// the measurement not reached the 75% threshold yet
|
2018-12-21 06:55:15 -04:00
|
|
|
step_time_limit_ms = (now - step_start_time_ms) * 3;
|
2018-12-13 01:01:34 -04:00
|
|
|
step_time_limit_ms = MIN(step_time_limit_ms, AUTOTUNE_TESTING_STEP_TIMEOUT_MS);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (meas_rate_max > rate_target_max) {
|
|
|
|
// the measured rate has passed the maximum target rate
|
|
|
|
step = UPDATE_GAINS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meas_rate_max-meas_rate_min > meas_rate_max*aggressiveness) {
|
|
|
|
// the measurement has passed 50% of the maximum rate and bounce back is larger than the threshold
|
|
|
|
step = UPDATE_GAINS;
|
|
|
|
}
|
|
|
|
|
2018-12-20 09:57:30 -04:00
|
|
|
if (now - step_start_time_ms >= step_time_limit_ms) {
|
2018-12-12 17:55:52 -04:00
|
|
|
// we have passed the maximum stop time
|
|
|
|
step = UPDATE_GAINS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 08:40:58 -04:00
|
|
|
// twitching_test_rate - twitching tests
|
|
|
|
// update min and max and test for end conditions
|
|
|
|
void AC_AutoTune::twitching_abort_rate(float angle, float rate, float angle_max, float meas_rate_min)
|
|
|
|
{
|
|
|
|
if (angle >= angle_max) {
|
2019-11-30 00:10:15 -04:00
|
|
|
if (is_equal(rate, meas_rate_min) && step_scaler > 0.5f) {
|
2018-12-21 08:40:58 -04:00
|
|
|
// we have reached the angle limit before completing the measurement of maximum and minimum
|
|
|
|
// reduce the maximum target rate
|
|
|
|
step_scaler *= 0.9f;
|
|
|
|
// ignore result and start test again
|
|
|
|
step = WAITING_FOR_LEVEL;
|
2018-12-21 19:40:31 -04:00
|
|
|
} else {
|
|
|
|
step = UPDATE_GAINS;
|
2018-12-21 08:40:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// twitching_test_angle - twitching tests
|
|
|
|
// update min and max and test for end conditions
|
|
|
|
void AC_AutoTune::twitching_test_angle(float angle, float rate, float angle_target_max, float &meas_angle_min, float &meas_angle_max, float &meas_rate_min, float &meas_rate_max)
|
|
|
|
{
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
|
|
|
// capture maximum angle
|
|
|
|
if (angle > meas_angle_max) {
|
|
|
|
// the angle still increasing
|
|
|
|
meas_angle_max = angle;
|
|
|
|
meas_angle_min = angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture minimum angle after we have reached a reasonable maximum angle
|
|
|
|
if ((angle < meas_angle_min) && (meas_angle_max > angle_target_max * 0.5f)) {
|
|
|
|
// the measurement is bouncing back
|
|
|
|
meas_angle_min = angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture maximum rate
|
|
|
|
if (rate > meas_rate_max) {
|
|
|
|
// the measurement is still increasing
|
|
|
|
meas_rate_max = rate;
|
|
|
|
meas_rate_min = rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture minimum rate after we have reached maximum rate
|
|
|
|
if (rate < meas_rate_min) {
|
|
|
|
// the measurement is still decreasing
|
|
|
|
meas_rate_min = rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate early stopping time based on the time it takes to get to 75%
|
|
|
|
if (meas_angle_max < angle_target_max * 0.75f) {
|
|
|
|
// the measurement not reached the 75% threshold yet
|
2018-12-21 06:55:15 -04:00
|
|
|
step_time_limit_ms = (now - step_start_time_ms) * 3;
|
2018-12-13 01:01:34 -04:00
|
|
|
step_time_limit_ms = MIN(step_time_limit_ms, AUTOTUNE_TESTING_STEP_TIMEOUT_MS);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (meas_angle_max > angle_target_max) {
|
|
|
|
// the measurement has passed the maximum angle
|
|
|
|
step = UPDATE_GAINS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meas_angle_max-meas_angle_min > meas_angle_max*aggressiveness) {
|
|
|
|
// the measurement has passed 50% of the maximum angle and bounce back is larger than the threshold
|
|
|
|
step = UPDATE_GAINS;
|
|
|
|
}
|
|
|
|
|
2018-12-20 09:57:30 -04:00
|
|
|
if (now - step_start_time_ms >= step_time_limit_ms) {
|
2018-12-12 17:55:52 -04:00
|
|
|
// we have passed the maximum stop time
|
|
|
|
step = UPDATE_GAINS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// twitching_measure_acceleration - measure rate of change of measurement
|
2021-02-01 12:26:25 -04:00
|
|
|
void AC_AutoTune::twitching_measure_acceleration(float &rate_of_change, float rate_measurement, float &rate_measurement_max) const
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
|
|
|
if (rate_measurement_max < rate_measurement) {
|
|
|
|
rate_measurement_max = rate_measurement;
|
2018-12-20 09:57:30 -04:00
|
|
|
rate_of_change = (1000.0f*rate_measurement_max)/(AP_HAL::millis() - step_start_time_ms);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updating_rate_d_up - increase D and adjust P to optimize the D term for a little bounce back
|
|
|
|
// optimize D term while keeping the maximum just below the target by adjusting P
|
|
|
|
void AC_AutoTune::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)
|
|
|
|
{
|
|
|
|
if (meas_rate_max > rate_target) {
|
|
|
|
// if maximum measurement was higher than target
|
|
|
|
// reduce P gain (which should reduce maximum)
|
|
|
|
tune_p -= tune_p*tune_p_step_ratio;
|
|
|
|
if (tune_p < tune_p_min) {
|
|
|
|
// P gain is at minimum so start reducing D
|
|
|
|
tune_p = tune_p_min;
|
|
|
|
tune_d -= tune_d*tune_d_step_ratio;
|
|
|
|
if (tune_d <= tune_d_min) {
|
|
|
|
// We have reached minimum D gain so stop tuning
|
|
|
|
tune_d = tune_d_min;
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((meas_rate_max < rate_target*(1.0f-AUTOTUNE_D_UP_DOWN_MARGIN)) && (tune_p <= tune_p_max)) {
|
|
|
|
// we have not achieved a high enough maximum to get a good measurement of bounce back.
|
|
|
|
// increase P gain (which should increase maximum)
|
|
|
|
tune_p += tune_p*tune_p_step_ratio;
|
|
|
|
if (tune_p >= tune_p_max) {
|
|
|
|
tune_p = tune_p_max;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we have a good measurement of bounce back
|
|
|
|
if (meas_rate_max-meas_rate_min > meas_rate_max*aggressiveness) {
|
|
|
|
// ignore the next result unless it is the same as this one
|
|
|
|
ignore_next = true;
|
|
|
|
// bounce back is bigger than our threshold so increment the success counter
|
|
|
|
counter++;
|
|
|
|
} else {
|
|
|
|
if (ignore_next == false) {
|
|
|
|
// bounce back is smaller than our threshold so decrement the success counter
|
|
|
|
if (counter > 0) {
|
|
|
|
counter--;
|
|
|
|
}
|
|
|
|
// increase D gain (which should increase bounce back)
|
|
|
|
tune_d += tune_d*tune_d_step_ratio*2.0f;
|
|
|
|
// stop tuning if we hit maximum D
|
|
|
|
if (tune_d >= tune_d_max) {
|
|
|
|
tune_d = tune_d_max;
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ignore_next = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updating_rate_d_down - decrease D and adjust P to optimize the D term for no bounce back
|
|
|
|
// optimize D term while keeping the maximum just below the target by adjusting P
|
|
|
|
void AC_AutoTune::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)
|
|
|
|
{
|
|
|
|
if (meas_rate_max > rate_target) {
|
|
|
|
// if maximum measurement was higher than target
|
|
|
|
// reduce P gain (which should reduce maximum)
|
|
|
|
tune_p -= tune_p*tune_p_step_ratio;
|
|
|
|
if (tune_p < tune_p_min) {
|
|
|
|
// P gain is at minimum so start reducing D gain
|
|
|
|
tune_p = tune_p_min;
|
|
|
|
tune_d -= tune_d*tune_d_step_ratio;
|
|
|
|
if (tune_d <= tune_d_min) {
|
|
|
|
// We have reached minimum D so stop tuning
|
|
|
|
tune_d = tune_d_min;
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((meas_rate_max < rate_target*(1.0f-AUTOTUNE_D_UP_DOWN_MARGIN)) && (tune_p <= tune_p_max)) {
|
|
|
|
// we have not achieved a high enough maximum to get a good measurement of bounce back.
|
|
|
|
// increase P gain (which should increase maximum)
|
|
|
|
tune_p += tune_p*tune_p_step_ratio;
|
|
|
|
if (tune_p >= tune_p_max) {
|
|
|
|
tune_p = tune_p_max;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we have a good measurement of bounce back
|
|
|
|
if (meas_rate_max-meas_rate_min < meas_rate_max*aggressiveness) {
|
|
|
|
if (ignore_next == false) {
|
|
|
|
// bounce back is less than our threshold so increment the success counter
|
|
|
|
counter++;
|
|
|
|
} else {
|
|
|
|
ignore_next = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ignore the next result unless it is the same as this one
|
|
|
|
ignore_next = true;
|
|
|
|
// bounce back is larger than our threshold so decrement the success counter
|
|
|
|
if (counter > 0) {
|
|
|
|
counter--;
|
|
|
|
}
|
|
|
|
// decrease D gain (which should decrease bounce back)
|
|
|
|
tune_d -= tune_d*tune_d_step_ratio;
|
|
|
|
// stop tuning if we hit minimum D
|
|
|
|
if (tune_d <= tune_d_min) {
|
|
|
|
tune_d = tune_d_min;
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updating_rate_p_up_d_down - increase P to ensure the target is reached while checking bounce back isn't increasing
|
|
|
|
// P is increased until we achieve our target within a reasonable time while reducing D if bounce back increases above the threshold
|
|
|
|
void AC_AutoTune::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)
|
|
|
|
{
|
|
|
|
if (meas_rate_max > rate_target*(1+0.5f*aggressiveness)) {
|
|
|
|
// ignore the next result unless it is the same as this one
|
|
|
|
ignore_next = true;
|
|
|
|
// if maximum measurement was greater than target so increment the success counter
|
|
|
|
counter++;
|
|
|
|
} else if ((meas_rate_max < rate_target) && (meas_rate_max > rate_target*(1.0f-AUTOTUNE_D_UP_DOWN_MARGIN)) && (meas_rate_max-meas_rate_min > meas_rate_max*aggressiveness) && (tune_d > tune_d_min)) {
|
|
|
|
// if bounce back was larger than the threshold so decrement the success counter
|
|
|
|
if (counter > 0) {
|
|
|
|
counter--;
|
|
|
|
}
|
|
|
|
// decrease D gain (which should decrease bounce back)
|
|
|
|
tune_d -= tune_d*tune_d_step_ratio;
|
|
|
|
// do not decrease the D term past the minimum
|
|
|
|
if (tune_d <= tune_d_min) {
|
|
|
|
tune_d = tune_d_min;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
// decrease P gain to match D gain reduction
|
|
|
|
tune_p -= tune_p*tune_p_step_ratio;
|
|
|
|
// do not decrease the P term past the minimum
|
|
|
|
if (tune_p <= tune_p_min) {
|
|
|
|
tune_p = tune_p_min;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
// cancel change in direction
|
|
|
|
positive_direction = !positive_direction;
|
|
|
|
} else {
|
|
|
|
if (ignore_next == false) {
|
|
|
|
// if maximum measurement was lower than target so decrement the success counter
|
|
|
|
if (counter > 0) {
|
|
|
|
counter--;
|
|
|
|
}
|
|
|
|
// increase P gain (which should increase the maximum)
|
|
|
|
tune_p += tune_p*tune_p_step_ratio;
|
|
|
|
// stop tuning if we hit maximum P
|
|
|
|
if (tune_p >= tune_p_max) {
|
|
|
|
tune_p = tune_p_max;
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ignore_next = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updating_angle_p_down - decrease P until we don't reach the target before time out
|
|
|
|
// P is decreased to ensure we are not overshooting the target
|
|
|
|
void AC_AutoTune::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)
|
|
|
|
{
|
|
|
|
if (meas_angle_max < angle_target*(1+0.5f*aggressiveness)) {
|
|
|
|
if (ignore_next == false) {
|
|
|
|
// if maximum measurement was lower than target so increment the success counter
|
|
|
|
counter++;
|
|
|
|
} else {
|
|
|
|
ignore_next = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ignore the next result unless it is the same as this one
|
|
|
|
ignore_next = true;
|
|
|
|
// if maximum measurement was higher than target so decrement the success counter
|
|
|
|
if (counter > 0) {
|
|
|
|
counter--;
|
|
|
|
}
|
|
|
|
// decrease P gain (which should decrease the maximum)
|
|
|
|
tune_p -= tune_p*tune_p_step_ratio;
|
|
|
|
// stop tuning if we hit maximum P
|
|
|
|
if (tune_p <= tune_p_min) {
|
|
|
|
tune_p = tune_p_min;
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updating_angle_p_up - increase P to ensure the target is reached
|
|
|
|
// P is increased until we achieve our target within a reasonable time
|
|
|
|
void AC_AutoTune::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)
|
|
|
|
{
|
|
|
|
if ((meas_angle_max > angle_target*(1+0.5f*aggressiveness)) ||
|
|
|
|
((meas_angle_max > angle_target) && (meas_rate_min < -meas_rate_max*aggressiveness))) {
|
|
|
|
// ignore the next result unless it is the same as this one
|
|
|
|
ignore_next = true;
|
|
|
|
// if maximum measurement was greater than target so increment the success counter
|
|
|
|
counter++;
|
|
|
|
} else {
|
|
|
|
if (ignore_next == false) {
|
|
|
|
// if maximum measurement was lower than target so decrement the success counter
|
|
|
|
if (counter > 0) {
|
|
|
|
counter--;
|
|
|
|
}
|
|
|
|
// increase P gain (which should increase the maximum)
|
|
|
|
tune_p += tune_p*tune_p_step_ratio;
|
|
|
|
// stop tuning if we hit maximum P
|
|
|
|
if (tune_p >= tune_p_max) {
|
|
|
|
tune_p = tune_p_max;
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
2019-11-01 19:07:25 -03:00
|
|
|
AP::logger().Write_Event(LogEvent::AUTOTUNE_REACHED_LIMIT);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ignore_next = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
check if we have a good position estimate
|
|
|
|
*/
|
|
|
|
bool AC_AutoTune::position_ok(void)
|
|
|
|
{
|
|
|
|
if (!AP::ahrs().have_inertial_nav()) {
|
|
|
|
// do not allow navigation with dcm position
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// with EKF use filter status and ekf check
|
|
|
|
nav_filter_status filt_status = inertial_nav->get_filter_status();
|
|
|
|
|
|
|
|
// require a good absolute position and EKF must not be in const_pos_mode
|
|
|
|
return (filt_status.flags.horiz_pos_abs && !filt_status.flags.const_pos_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get attitude for slow position hold in autotune mode
|
2019-02-28 07:08:39 -04:00
|
|
|
void AC_AutoTune::get_poshold_attitude(float &roll_cd_out, float &pitch_cd_out, float &yaw_cd_out)
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
|
|
|
roll_cd_out = pitch_cd_out = 0;
|
|
|
|
|
|
|
|
if (!use_poshold) {
|
|
|
|
// we are not trying to hold position
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do we know where we are? If not then don't do poshold
|
|
|
|
if (!position_ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_position) {
|
|
|
|
have_position = true;
|
2021-10-20 05:32:20 -03:00
|
|
|
start_position = inertial_nav->get_position_neu_cm();
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// don't go past 10 degrees, as autotune result would deteriorate too much
|
|
|
|
const float angle_max_cd = 1000;
|
|
|
|
|
|
|
|
// hit the 10 degree limit at 20 meters position error
|
|
|
|
const float dist_limit_cm = 2000;
|
|
|
|
|
|
|
|
// we only start adjusting yaw if we are more than 5m from the
|
|
|
|
// target position. That corresponds to a lean angle of 2.5 degrees
|
|
|
|
const float yaw_dist_limit_cm = 500;
|
|
|
|
|
2021-10-20 05:32:20 -03:00
|
|
|
Vector3f pdiff = inertial_nav->get_position_neu_cm() - start_position;
|
2018-12-12 17:55:52 -04:00
|
|
|
pdiff.z = 0;
|
|
|
|
float dist_cm = pdiff.length();
|
|
|
|
if (dist_cm < 10) {
|
|
|
|
// don't do anything within 10cm
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
very simple linear controller
|
|
|
|
*/
|
|
|
|
float scaling = constrain_float(angle_max_cd * dist_cm / dist_limit_cm, 0, angle_max_cd);
|
|
|
|
Vector2f angle_ne(pdiff.x, pdiff.y);
|
|
|
|
angle_ne *= scaling / dist_cm;
|
|
|
|
|
|
|
|
// rotate into body frame
|
|
|
|
pitch_cd_out = angle_ne.x * ahrs_view->cos_yaw() + angle_ne.y * ahrs_view->sin_yaw();
|
|
|
|
roll_cd_out = angle_ne.x * ahrs_view->sin_yaw() - angle_ne.y * ahrs_view->cos_yaw();
|
|
|
|
|
|
|
|
if (dist_cm < yaw_dist_limit_cm) {
|
|
|
|
// no yaw adjustment
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
also point so that twitching occurs perpendicular to the wind,
|
|
|
|
if we have drifted more than yaw_dist_limit_cm from the desired
|
|
|
|
position. This ensures that autotune doesn't have to deal with
|
|
|
|
more than 2.5 degrees of attitude on the axis it is tuning
|
|
|
|
*/
|
|
|
|
float target_yaw_cd = degrees(atan2f(pdiff.y, pdiff.x)) * 100;
|
|
|
|
if (axis == PITCH) {
|
|
|
|
// for roll and yaw tuning we point along the wind, for pitch
|
|
|
|
// we point across the wind
|
|
|
|
target_yaw_cd += 9000;
|
|
|
|
}
|
|
|
|
// go to the nearest 180 degree mark, with 5 degree slop to prevent oscillation
|
|
|
|
if (fabsf(yaw_cd_out - target_yaw_cd) > 9500) {
|
|
|
|
target_yaw_cd += 18000;
|
|
|
|
}
|
|
|
|
|
|
|
|
yaw_cd_out = target_yaw_cd;
|
|
|
|
}
|
|
|
|
|
2020-03-20 02:18:53 -03:00
|
|
|
// @LoggerMessage: ATUN
|
|
|
|
// @Description: Copter/QuadPlane AutoTune
|
|
|
|
// @Vehicles: Copter, Plane
|
2020-04-07 04:33:57 -03:00
|
|
|
// @Field: TimeUS: Time since system startup
|
2020-03-20 23:43:51 -03:00
|
|
|
// @Field: Axis: which axis is currently being tuned
|
2020-03-20 02:18:53 -03:00
|
|
|
// @Field: TuneStep: step in autotune process
|
|
|
|
// @Field: Targ: target angle or rate, depending on tuning step
|
|
|
|
// @Field: Min: measured minimum target angle or rate
|
|
|
|
// @Field: Max: measured maximum target angle or rate
|
|
|
|
// @Field: RP: new rate gain P term
|
|
|
|
// @Field: RD: new rate gain D term
|
|
|
|
// @Field: SP: new angle P term
|
|
|
|
// @Field: ddt: maximum measured twitching acceleration
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// Write an Autotune data packet
|
|
|
|
void AC_AutoTune::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)
|
|
|
|
{
|
2019-01-18 00:24:08 -04:00
|
|
|
AP::logger().Write(
|
2018-12-12 17:55:52 -04:00
|
|
|
"ATUN",
|
|
|
|
"TimeUS,Axis,TuneStep,Targ,Min,Max,RP,RD,SP,ddt",
|
|
|
|
"s--ddd---o",
|
2019-03-25 07:21:10 -03:00
|
|
|
"F--000---0",
|
2018-12-12 17:55:52 -04:00
|
|
|
"QBBfffffff",
|
|
|
|
AP_HAL::micros64(),
|
|
|
|
axis,
|
|
|
|
tune_step,
|
2018-12-21 22:23:06 -04:00
|
|
|
meas_target*0.01f,
|
|
|
|
meas_min*0.01f,
|
|
|
|
meas_max*0.01f,
|
2018-12-12 17:55:52 -04:00
|
|
|
new_gain_rp,
|
|
|
|
new_gain_rd,
|
|
|
|
new_gain_sp,
|
|
|
|
new_ddt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write an Autotune data packet
|
|
|
|
void AC_AutoTune::Log_Write_AutoTuneDetails(float angle_cd, float rate_cds)
|
|
|
|
{
|
2020-04-06 23:54:08 -03:00
|
|
|
// @LoggerMessage: ATDE
|
|
|
|
// @Description: AutoTune data packet
|
|
|
|
// @Field: TimeUS: Time since system startup
|
|
|
|
// @Field: Angle: current angle
|
|
|
|
// @Field: Rate: current angular rate
|
2021-08-17 06:57:41 -03:00
|
|
|
AP::logger().WriteStreaming(
|
2018-12-12 17:55:52 -04:00
|
|
|
"ATDE",
|
|
|
|
"TimeUS,Angle,Rate",
|
|
|
|
"sdk",
|
2019-03-25 07:21:10 -03:00
|
|
|
"F00",
|
2018-12-12 17:55:52 -04:00
|
|
|
"Qff",
|
|
|
|
AP_HAL::micros64(),
|
2018-12-21 22:23:06 -04:00
|
|
|
angle_cd*0.01f,
|
|
|
|
rate_cds*0.01f);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|