2023-12-07 20:03:15 -04:00
|
|
|
#include "AC_AutoTune_config.h"
|
|
|
|
|
|
|
|
#if AC_AUTOTUNE_ENABLED
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
#include "AC_AutoTune.h"
|
2022-03-03 23:29:45 -04:00
|
|
|
|
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2018-12-12 17:55:52 -04:00
|
|
|
#include <AP_Scheduler/AP_Scheduler.h>
|
2022-11-08 06:10:56 -04:00
|
|
|
#include <AP_Notify/AP_Notify.h>
|
2022-03-03 23:29:45 -04:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2022-11-08 06:10:56 -04:00
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2024-02-23 10:24:17 -04:00
|
|
|
#define AUTOTUNE_PILOT_OVERRIDE_TIMEOUT_MS 500 // restart tuning if pilot has left sticks in middle for 2 seconds
|
2021-01-31 21:07:12 -04:00
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_ArduPlane)
|
2024-02-23 10:24:17 -04:00
|
|
|
# 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)
|
2021-01-31 21:07:12 -04:00
|
|
|
#else
|
2024-02-23 10:24:17 -04:00
|
|
|
# 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
|
2021-01-31 21:07:12 -04:00
|
|
|
#endif
|
2024-02-23 10:24:17 -04:00
|
|
|
#define AUTOTUNE_LEVEL_RATE_Y_CD 750 // rate which qualifies as level for yaw
|
|
|
|
#define AUTOTUNE_REQUIRED_LEVEL_TIME_MS 250 // time we require the aircraft to be level before starting next test
|
|
|
|
#define AUTOTUNE_LEVEL_TIMEOUT_MS 2000 // time out for level
|
|
|
|
#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
|
|
|
|
|
|
|
AC_AutoTune::AC_AutoTune()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// autotune_init - should be called when autotune mode is selected
|
|
|
|
bool AC_AutoTune::init_internals(bool _use_poshold,
|
2020-10-19 23:51:05 -03:00
|
|
|
AC_AttitudeControl *_attitude_control,
|
2018-12-12 17:55:52 -04:00
|
|
|
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();
|
2024-02-18 23:46:37 -04:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
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-12-22 23:37:31 -04:00
|
|
|
// reset test variables for each vehicle
|
|
|
|
reset_vehicle_test_variables();
|
|
|
|
|
2021-04-29 23:29:34 -03:00
|
|
|
// we are restarting tuning so restart where we left off
|
2021-01-18 00:16:08 -04:00
|
|
|
step = WAITING_FOR_LEVEL;
|
2024-02-18 23:46:37 -04:00
|
|
|
step_start_time_ms = now;
|
|
|
|
level_start_time_ms = now;
|
2021-04-29 23:29:34 -03:00
|
|
|
// reset gains to tuning-start gains (i.e. low I term)
|
|
|
|
load_gains(GAIN_INTRA_TEST);
|
2023-07-13 21:58:04 -03:00
|
|
|
LOGGER_WRITE_EVENT(LogEvent::AUTOTUNE_RESTART);
|
2021-04-29 23:29:34 -03:00
|
|
|
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);
|
2023-07-13 21:58:04 -03:00
|
|
|
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);
|
2023-07-13 21:58:04 -03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-04 19:38:18 -03:00
|
|
|
// Autotune aux function trigger
|
|
|
|
void AC_AutoTune::do_aux_function(const RC_Channel::AuxSwitchPos ch_flag)
|
|
|
|
{
|
|
|
|
if (mode != TuneMode::SUCCESS) {
|
|
|
|
if (ch_flag == RC_Channel::AuxSwitchPos::HIGH) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_NOTICE,"AutoTune: must be complete to test gains");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(ch_flag) {
|
|
|
|
case RC_Channel::AuxSwitchPos::LOW:
|
|
|
|
// load original gains
|
|
|
|
load_gains(GainType::GAIN_ORIGINAL);
|
|
|
|
update_gcs(AUTOTUNE_MESSAGE_TESTING_END);
|
|
|
|
break;
|
|
|
|
case RC_Channel::AuxSwitchPos::MIDDLE:
|
|
|
|
// Middle position is unused for now
|
|
|
|
break;
|
|
|
|
case RC_Channel::AuxSwitchPos::HIGH:
|
|
|
|
// Load tuned gains
|
|
|
|
load_gains(GainType::GAIN_TUNED);
|
|
|
|
update_gcs(AUTOTUNE_MESSAGE_TESTING);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
have_pilot_testing_command = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possibly save gains, called on disarm
|
|
|
|
void AC_AutoTune::disarmed(const bool in_autotune_mode)
|
|
|
|
{
|
|
|
|
// True if pilot is testing tuned gains
|
|
|
|
const bool testing_tuned = have_pilot_testing_command && (loaded_gains == GainType::GAIN_TUNED);
|
|
|
|
|
|
|
|
// True if in autotune mode and no pilot testing commands have been received
|
|
|
|
const bool tune_complete_no_testing = !have_pilot_testing_command && in_autotune_mode;
|
|
|
|
|
|
|
|
if (tune_complete_no_testing || testing_tuned) {
|
|
|
|
save_tuning_gains();
|
|
|
|
} else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AC_AutoTune::send_step_string()
|
|
|
|
{
|
|
|
|
if (pilot_override) {
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "AutoTune: Paused: Pilot Override Active");
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (step) {
|
|
|
|
case WAITING_FOR_LEVEL:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "AutoTune: Leveling");
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
|
|
|
case UPDATE_GAINS:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "AutoTune: Updating Gains");
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
2024-07-21 10:11:41 -03:00
|
|
|
case ABORT:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "AutoTune: Aborting Test");
|
2024-07-21 10:11:41 -03:00
|
|
|
return;
|
2020-10-19 23:51:05 -03:00
|
|
|
case TESTING:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "AutoTune: Testing");
|
2018-12-12 17:55:52 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "AutoTune: unknown step");
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2020-10-19 23:51:05 -03:00
|
|
|
case RFF_UP:
|
|
|
|
return "Rate FF Up";
|
2018-12-12 17:55:52 -04:00
|
|
|
case SP_UP:
|
|
|
|
return "Angle P Up";
|
|
|
|
case SP_DOWN:
|
2020-10-19 23:51:05 -03:00
|
|
|
return "Angle P Down";
|
|
|
|
case MAX_GAINS:
|
|
|
|
return "Find Max Gains";
|
2022-05-08 00:28:50 -03:00
|
|
|
case TUNE_CHECK:
|
|
|
|
return "Check Tune Frequency Response";
|
2020-10-19 23:51:05 -03:00
|
|
|
case TUNE_COMPLETE:
|
|
|
|
return "Tune Complete";
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
2022-02-03 17:16:49 -04:00
|
|
|
return "";
|
2022-01-30 00:06:07 -04:00
|
|
|
// this should never happen
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
2022-02-03 17:24:23 -04:00
|
|
|
// return current axis string
|
|
|
|
const char *AC_AutoTune::axis_string() const
|
|
|
|
{
|
|
|
|
switch (axis) {
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::ROLL:
|
2022-02-03 17:24:23 -04:00
|
|
|
return "Roll";
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::PITCH:
|
2022-02-03 17:24:23 -04:00
|
|
|
return "Pitch";
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::YAW:
|
2023-03-06 05:39:47 -04:00
|
|
|
return "Yaw(E)";
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::YAW_D:
|
2023-03-06 05:39:47 -04:00
|
|
|
return "Yaw(D)";
|
2022-02-03 17:24:23 -04:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// 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
|
|
|
|
2021-10-02 00:07:31 -03:00
|
|
|
const bool zero_rp_input = is_zero(target_roll_cd) && is_zero(target_pitch_cd);
|
2021-01-18 00:16:08 -04:00
|
|
|
|
2019-02-28 07:44:22 -04:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
2022-11-12 15:04:46 -04:00
|
|
|
|
|
|
|
if (mode != SUCCESS) {
|
|
|
|
if (!zero_rp_input || !is_zero(target_yaw_rate_cds) || !is_zero(target_climb_rate_cms)) {
|
|
|
|
if (!pilot_override) {
|
|
|
|
pilot_override = true;
|
|
|
|
// set gains to their original values
|
|
|
|
load_gains(GAIN_ORIGINAL);
|
|
|
|
attitude_control->use_sqrt_controller(true);
|
|
|
|
}
|
|
|
|
// reset pilot override time
|
|
|
|
override_time = now;
|
|
|
|
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
|
|
|
|
if (now - override_time > AUTOTUNE_PILOT_OVERRIDE_TIMEOUT_MS) {
|
|
|
|
pilot_override = false; // turn off pilot override
|
|
|
|
// set gains to their intra-test values (which are very close to the original gains)
|
|
|
|
// load_gains(GAIN_INTRA_TEST); //I think we should be keeping the originals here to let the I term settle quickly
|
|
|
|
step = WAITING_FOR_LEVEL; // set tuning step back from beginning
|
|
|
|
step_start_time_ms = now;
|
|
|
|
level_start_time_ms = now;
|
|
|
|
desired_yaw_cd = ahrs_view->yaw_sensor;
|
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
2019-02-28 07:44:22 -04:00
|
|
|
if (pilot_override) {
|
|
|
|
if (now - last_pilot_override_warning > 1000) {
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "AutoTune: pilot overrides active");
|
2019-02-28 07:44:22 -04:00
|
|
|
last_pilot_override_warning = now;
|
|
|
|
}
|
|
|
|
}
|
2021-10-02 00:07:31 -03:00
|
|
|
if (zero_rp_input) {
|
2018-12-12 17:55:52 -04:00
|
|
|
// 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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:28:20 -04:00
|
|
|
// return true if vehicle is close to level
|
2018-12-12 17:55:52 -04:00
|
|
|
bool AC_AutoTune::currently_level()
|
|
|
|
{
|
2024-02-18 23:46:37 -04:00
|
|
|
// abort AutoTune if we pass 2 * AUTOTUNE_LEVEL_TIMEOUT_MS
|
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
2024-04-15 04:33:49 -03:00
|
|
|
if (now_ms - level_start_time_ms > 3 * AUTOTUNE_LEVEL_TIMEOUT_MS) {
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "AutoTune: Failed to level, please tune manually");
|
2024-02-18 23:46:37 -04:00
|
|
|
mode = FAILED;
|
|
|
|
LOGGER_WRITE_EVENT(LogEvent::AUTOTUNE_FAILED);
|
2018-12-21 20:20:45 -04:00
|
|
|
}
|
|
|
|
|
2024-02-18 23:46:37 -04:00
|
|
|
// slew threshold to ensure sufficient settling time for aircraft unable to obtain small thresholds
|
|
|
|
// relax threshold if we pass AUTOTUNE_LEVEL_TIMEOUT_MS
|
|
|
|
const float threshold_mul = constrain_float((float)(now_ms - level_start_time_ms) / (float)AUTOTUNE_LEVEL_TIMEOUT_MS, 0.0, 2.0);
|
2021-01-31 22:41:27 -04:00
|
|
|
|
2024-02-18 23:46:37 -04:00
|
|
|
if (fabsf(ahrs_view->roll_sensor - roll_cd) > threshold_mul * AUTOTUNE_LEVEL_ANGLE_CD) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-18 23:46:37 -04:00
|
|
|
if (fabsf(ahrs_view->pitch_sensor - pitch_cd) > threshold_mul * AUTOTUNE_LEVEL_ANGLE_CD) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-18 23:46:37 -04:00
|
|
|
if (fabsf(wrap_180_cd(ahrs_view->yaw_sensor - desired_yaw_cd)) > threshold_mul * AUTOTUNE_LEVEL_ANGLE_CD) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-18 23:46:37 -04:00
|
|
|
if ((ToDeg(ahrs_view->get_gyro().x) * 100.0f) > threshold_mul * AUTOTUNE_LEVEL_RATE_RP_CD) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-18 23:46:37 -04:00
|
|
|
if ((ToDeg(ahrs_view->get_gyro().y) * 100.0f) > threshold_mul * AUTOTUNE_LEVEL_RATE_RP_CD) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-18 23:46:37 -04:00
|
|
|
if ((ToDeg(ahrs_view->get_gyro().z) * 100.0f) > threshold_mul * AUTOTUNE_LEVEL_RATE_Y_CD) {
|
2018-12-12 17:55:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:28:20 -04:00
|
|
|
// main state machine to level vehicle, perform a test and update gains
|
|
|
|
// directly updates attitude controller with targets
|
2018-12-12 17:55:52 -04:00
|
|
|
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: {
|
2020-10-19 23:51:05 -03:00
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// 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
|
|
|
// initiate variables for next step
|
2020-10-19 23:51:05 -03:00
|
|
|
step = TESTING;
|
2018-12-20 09:57:30 -04:00
|
|
|
step_start_time_ms = now;
|
2021-12-28 00:23:19 -04:00
|
|
|
step_time_limit_ms = get_testing_step_timeout_ms();
|
2020-10-19 23:51:05 -03:00
|
|
|
// set gains to their to-be-tested values
|
|
|
|
load_gains(GAIN_TEST);
|
2018-12-21 21:53:39 -04:00
|
|
|
} else {
|
|
|
|
// when waiting for level we use the intra-test gains
|
|
|
|
load_gains(GAIN_INTRA_TEST);
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:38:04 -04:00
|
|
|
// Initialize test-specific variables
|
2018-12-12 17:55:52 -04:00
|
|
|
switch (axis) {
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::ROLL:
|
2018-12-12 17:55:52 -04:00
|
|
|
start_rate = ToDeg(ahrs_view->get_gyro().x) * 100.0f;
|
|
|
|
start_angle = ahrs_view->roll_sensor;
|
|
|
|
break;
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::PITCH:
|
2018-12-12 17:55:52 -04:00
|
|
|
start_rate = ToDeg(ahrs_view->get_gyro().y) * 100.0f;
|
|
|
|
start_angle = ahrs_view->pitch_sensor;
|
|
|
|
break;
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::YAW:
|
|
|
|
case AxisType::YAW_D:
|
2018-12-12 17:55:52 -04:00
|
|
|
start_rate = ToDeg(ahrs_view->get_gyro().z) * 100.0f;
|
|
|
|
start_angle = ahrs_view->yaw_sensor;
|
|
|
|
break;
|
|
|
|
}
|
2020-10-19 23:51:05 -03:00
|
|
|
|
|
|
|
// tests must be initialized last as some rely on variables above
|
|
|
|
test_init();
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-19 23:51:05 -03:00
|
|
|
case TESTING: {
|
2018-12-12 17:55:52 -04:00
|
|
|
// Run the twitching step
|
2020-10-19 23:51:05 -03:00
|
|
|
load_gains(GAIN_TEST);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2020-10-19 23:51:05 -03:00
|
|
|
// run the test
|
|
|
|
test_run(axis, direction_sign);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2019-11-30 00:10:15 -04:00
|
|
|
// Check for failure causing reverse response
|
2024-02-29 02:42:39 -04:00
|
|
|
if (lean_angle <= -angle_lim_neg_rpy_cd()) {
|
2019-11-30 00:10:15 -04:00
|
|
|
step = WAITING_FOR_LEVEL;
|
2024-02-18 23:35:35 -04:00
|
|
|
positive_direction = twitch_reverse_direction();
|
2024-02-18 23:46:37 -04:00
|
|
|
step_start_time_ms = now;
|
|
|
|
level_start_time_ms = now;
|
2019-11-30 00:10:15 -04:00
|
|
|
}
|
|
|
|
|
2021-09-14 00:35:12 -03:00
|
|
|
// protect from roll over
|
2024-02-29 02:42:39 -04:00
|
|
|
if (attitude_control->lean_angle_deg() * 100 > angle_lim_max_rp_cd()) {
|
2021-09-14 00:35:12 -03:00
|
|
|
step = WAITING_FOR_LEVEL;
|
2024-02-18 23:35:35 -04:00
|
|
|
positive_direction = twitch_reverse_direction();
|
2024-02-18 23:46:37 -04:00
|
|
|
step_start_time_ms = now;
|
|
|
|
level_start_time_ms = now;
|
2021-09-14 00:35:12 -03:00
|
|
|
}
|
|
|
|
|
2023-07-13 21:58:04 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2018-12-12 17:55:52 -04:00
|
|
|
// log this iterations lean angle and rotation rate
|
2020-10-19 23:51:05 -03:00
|
|
|
Log_AutoTuneDetails();
|
2024-08-28 17:20:00 -03:00
|
|
|
attitude_control->Write_Rate(*pos_control);
|
2018-12-21 22:26:16 -04:00
|
|
|
log_pids();
|
2023-07-13 21:58:04 -03:00
|
|
|
#endif
|
2024-02-18 23:35:35 -04:00
|
|
|
|
2024-07-25 01:07:26 -03:00
|
|
|
if (axis == AxisType::YAW || axis == AxisType::YAW_D) {
|
2024-02-18 23:35:35 -04:00
|
|
|
desired_yaw_cd = ahrs_view->yaw_sensor;
|
|
|
|
}
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UPDATE_GAINS:
|
|
|
|
|
|
|
|
// re-enable rate limits
|
|
|
|
attitude_control->use_sqrt_controller(true);
|
|
|
|
|
2023-07-13 21:58:04 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2018-12-12 17:55:52 -04:00
|
|
|
// log the latest gains
|
2020-10-19 23:51:05 -03:00
|
|
|
Log_AutoTune();
|
2023-07-13 21:58:04 -03:00
|
|
|
#endif
|
2020-10-19 23:51:05 -03:00
|
|
|
|
2022-01-07 14:37:02 -04:00
|
|
|
// Announce tune type test results
|
|
|
|
// must be done before updating method because this method changes parameters for next test
|
|
|
|
do_post_test_gcs_announcements();
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
switch (tune_type) {
|
2020-10-19 23:51:05 -03:00
|
|
|
// Check results after mini-step to increase rate D gain
|
2018-12-12 17:55:52 -04:00
|
|
|
case RD_UP:
|
2020-10-19 23:51:05 -03:00
|
|
|
updating_rate_d_up_all(axis);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
// Check results after mini-step to decrease rate D gain
|
|
|
|
case RD_DOWN:
|
2020-10-19 23:51:05 -03:00
|
|
|
updating_rate_d_down_all(axis);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
// Check results after mini-step to increase rate P gain
|
|
|
|
case RP_UP:
|
2020-10-19 23:51:05 -03:00
|
|
|
updating_rate_p_up_all(axis);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
// Check results after mini-step to increase stabilize P gain
|
|
|
|
case SP_DOWN:
|
2020-10-19 23:51:05 -03:00
|
|
|
updating_angle_p_down_all(axis);
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
// Check results after mini-step to increase stabilize P gain
|
|
|
|
case SP_UP:
|
2020-10-19 23:51:05 -03:00
|
|
|
updating_angle_p_up_all(axis);
|
|
|
|
break;
|
|
|
|
case RFF_UP:
|
|
|
|
updating_rate_ff_up_all(axis);
|
|
|
|
break;
|
|
|
|
case MAX_GAINS:
|
|
|
|
updating_max_gains_all(axis);
|
|
|
|
break;
|
2022-05-08 00:28:50 -03:00
|
|
|
case TUNE_CHECK:
|
|
|
|
counter = AUTOTUNE_SUCCESS_COUNT;
|
|
|
|
FALLTHROUGH;
|
2020-10-19 23:51:05 -03:00
|
|
|
case TUNE_COMPLETE:
|
2018-12-12 17:55:52 -04:00
|
|
|
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
|
|
|
|
2020-10-19 23:51:05 -03:00
|
|
|
|
2021-12-27 00:53:50 -04:00
|
|
|
// set gains for post tune before moving to the next tuning type
|
|
|
|
set_gains_post_tune(axis);
|
2020-10-19 23:51:05 -03:00
|
|
|
|
|
|
|
// increment the tune type to the next one in tune sequence
|
2021-12-25 23:43:10 -04:00
|
|
|
next_tune_type(tune_type, false);
|
2020-10-19 23:51:05 -03:00
|
|
|
|
|
|
|
if (tune_type == TUNE_COMPLETE) {
|
2018-12-12 17:55:52 -04:00
|
|
|
// we've reached the end of a D-up-down PI-up-down tune type cycle
|
2021-12-25 23:43:10 -04:00
|
|
|
next_tune_type(tune_type, true);
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2022-02-04 19:29:23 -04:00
|
|
|
report_final_gains(axis);
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// advance to the next axis
|
|
|
|
bool complete = false;
|
|
|
|
switch (axis) {
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::ROLL:
|
2018-12-14 02:00:51 -04:00
|
|
|
axes_completed |= AUTOTUNE_AXIS_BITMASK_ROLL;
|
2018-12-12 17:55:52 -04:00
|
|
|
if (pitch_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::PITCH;
|
2018-12-12 17:55:52 -04:00
|
|
|
} else if (yaw_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::YAW;
|
2023-03-06 05:39:47 -04:00
|
|
|
} else if (yaw_d_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::YAW_D;
|
2018-12-12 17:55:52 -04:00
|
|
|
} else {
|
|
|
|
complete = true;
|
|
|
|
}
|
|
|
|
break;
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::PITCH:
|
2018-12-14 02:00:51 -04:00
|
|
|
axes_completed |= AUTOTUNE_AXIS_BITMASK_PITCH;
|
2018-12-12 17:55:52 -04:00
|
|
|
if (yaw_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::YAW;
|
2023-03-06 05:39:47 -04:00
|
|
|
} else if (yaw_d_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::YAW_D;
|
2018-12-12 17:55:52 -04:00
|
|
|
} else {
|
|
|
|
complete = true;
|
|
|
|
}
|
|
|
|
break;
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::YAW:
|
2018-12-14 02:00:51 -04:00
|
|
|
axes_completed |= AUTOTUNE_AXIS_BITMASK_YAW;
|
2023-03-06 05:39:47 -04:00
|
|
|
if (yaw_d_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::YAW_D;
|
2023-03-06 05:39:47 -04:00
|
|
|
} else {
|
|
|
|
complete = true;
|
|
|
|
}
|
|
|
|
break;
|
2024-07-25 01:07:26 -03:00
|
|
|
case AxisType::YAW_D:
|
2023-03-06 05:39:47 -04:00
|
|
|
axes_completed |= AUTOTUNE_AXIS_BITMASK_YAW_D;
|
2018-12-12 17:55:52 -04:00
|
|
|
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);
|
2023-07-13 21:58:04 -03:00
|
|
|
LOGGER_WRITE_EVENT(LogEvent::AUTOTUNE_SUCCESS);
|
2018-12-12 17:55:52 -04:00
|
|
|
AP_Notify::events.autotune_complete = true;
|
2024-08-04 19:38:18 -03:00
|
|
|
|
|
|
|
// Return to original gains for landing
|
|
|
|
load_gains(GainType::GAIN_ORIGINAL);
|
2018-12-12 17:55:52 -04:00
|
|
|
} else {
|
|
|
|
AP_Notify::events.autotune_next_axis = true;
|
2022-01-07 14:37:02 -04:00
|
|
|
reset_update_gain_variables();
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-21 10:11:41 -03:00
|
|
|
FALLTHROUGH;
|
2018-12-12 17:55:52 -04:00
|
|
|
|
2024-07-21 10:11:41 -03:00
|
|
|
case ABORT:
|
2024-07-25 01:07:26 -03:00
|
|
|
if (axis == AxisType::YAW || axis == AxisType::YAW_D) {
|
2024-07-21 08:28:24 -03:00
|
|
|
// todo: check to make sure we need this
|
2018-12-12 17:55:52 -04:00
|
|
|
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;
|
2024-02-18 23:35:35 -04:00
|
|
|
positive_direction = twitch_reverse_direction();
|
2018-12-20 09:57:30 -04:00
|
|
|
step_start_time_ms = now;
|
2024-02-18 23:46:37 -04:00
|
|
|
level_start_time_ms = now;
|
2018-12-21 06:55:15 -04:00
|
|
|
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()
|
|
|
|
{
|
2024-02-18 23:46:37 -04:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
// initialise state because this is our first time
|
|
|
|
if (roll_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::ROLL;
|
2018-12-12 17:55:52 -04:00
|
|
|
} else if (pitch_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::PITCH;
|
2018-12-12 17:55:52 -04:00
|
|
|
} else if (yaw_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::YAW;
|
2023-03-06 05:39:47 -04:00
|
|
|
} else if (yaw_d_enabled()) {
|
2024-07-25 01:07:26 -03:00
|
|
|
axis = AxisType::YAW_D;
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
2018-12-18 03:53:19 -04:00
|
|
|
// no axes are complete
|
|
|
|
axes_completed = 0;
|
|
|
|
|
2022-01-07 14:37:02 -04:00
|
|
|
// reset update gain variables for each vehicle
|
|
|
|
reset_update_gain_variables();
|
|
|
|
|
2020-10-19 23:51:05 -03:00
|
|
|
// start at the beginning of tune sequence
|
2021-12-25 23:43:10 -04:00
|
|
|
next_tune_type(tune_type, true);
|
2020-10-19 23:51:05 -03:00
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
step = WAITING_FOR_LEVEL;
|
2024-02-18 23:46:37 -04:00
|
|
|
positive_direction = false;
|
|
|
|
step_start_time_ms = now;
|
|
|
|
level_start_time_ms = now;
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-21 21:53:39 -04:00
|
|
|
/*
|
|
|
|
load a specified set of gains
|
|
|
|
*/
|
|
|
|
void AC_AutoTune::load_gains(enum GainType gain_type)
|
|
|
|
{
|
2024-08-04 19:38:18 -03:00
|
|
|
if (loaded_gains == gain_type) {
|
|
|
|
// Loaded gains are already of correct type
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loaded_gains = gain_type;
|
|
|
|
|
2018-12-21 21:53:39 -04:00
|
|
|
switch (gain_type) {
|
|
|
|
case GAIN_ORIGINAL:
|
|
|
|
load_orig_gains();
|
|
|
|
break;
|
|
|
|
case GAIN_INTRA_TEST:
|
|
|
|
load_intra_test_gains();
|
|
|
|
break;
|
2020-10-19 23:51:05 -03:00
|
|
|
case GAIN_TEST:
|
|
|
|
load_test_gains();
|
2018-12-21 21:53:39 -04:00
|
|
|
break;
|
|
|
|
case GAIN_TUNED:
|
|
|
|
load_tuned_gains();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO,"AutoTune: Started");
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
case AUTOTUNE_MESSAGE_STOPPED:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO,"AutoTune: Stopped");
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
case AUTOTUNE_MESSAGE_SUCCESS:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_NOTICE,"AutoTune: Success");
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
|
|
|
case AUTOTUNE_MESSAGE_FAILED:
|
2024-08-07 00:17:19 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_NOTICE,"AutoTune: Failed");
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
2020-06-24 18:15:38 -03:00
|
|
|
case AUTOTUNE_MESSAGE_TESTING:
|
2018-12-12 17:55:52 -04:00
|
|
|
case AUTOTUNE_MESSAGE_SAVED_GAINS:
|
2024-08-04 19:38:18 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_NOTICE,"AutoTune: %s gains for %s%s%s%s",
|
|
|
|
(message_id == AUTOTUNE_MESSAGE_SAVED_GAINS) ? "Saved" : "Pilot Testing",
|
2018-12-14 02:00:51 -04:00
|
|
|
(axes_completed&AUTOTUNE_AXIS_BITMASK_ROLL)?"Roll ":"",
|
|
|
|
(axes_completed&AUTOTUNE_AXIS_BITMASK_PITCH)?"Pitch ":"",
|
2023-03-06 05:39:47 -04:00
|
|
|
(axes_completed&AUTOTUNE_AXIS_BITMASK_YAW)?"Yaw(E)":"",
|
|
|
|
(axes_completed&AUTOTUNE_AXIS_BITMASK_YAW_D)?"Yaw(D)":"");
|
2018-12-12 17:55:52 -04:00
|
|
|
break;
|
2024-08-04 19:38:18 -03:00
|
|
|
case AUTOTUNE_MESSAGE_TESTING_END:
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_NOTICE,"AutoTune: original gains restored");
|
|
|
|
break;
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// axis helper functions
|
2020-10-19 23:51:05 -03:00
|
|
|
bool AC_AutoTune::roll_enabled() const
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
2021-12-27 00:53:50 -04:00
|
|
|
return get_axis_bitmask() & AUTOTUNE_AXIS_BITMASK_ROLL;
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:51:05 -03:00
|
|
|
bool AC_AutoTune::pitch_enabled() const
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
2021-12-27 00:53:50 -04:00
|
|
|
return get_axis_bitmask() & AUTOTUNE_AXIS_BITMASK_PITCH;
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:51:05 -03:00
|
|
|
bool AC_AutoTune::yaw_enabled() const
|
2018-12-12 17:55:52 -04:00
|
|
|
{
|
2021-12-27 00:53:50 -04:00
|
|
|
return get_axis_bitmask() & AUTOTUNE_AXIS_BITMASK_YAW;
|
2018-12-12 17:55:52 -04:00
|
|
|
}
|
|
|
|
|
2023-03-06 05:39:47 -04:00
|
|
|
bool AC_AutoTune::yaw_d_enabled() const
|
|
|
|
{
|
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_Heli)
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return get_axis_bitmask() & AUTOTUNE_AXIS_BITMASK_YAW_D;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:55:52 -04:00
|
|
|
/*
|
|
|
|
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;
|
2024-07-25 01:07:26 -03:00
|
|
|
if (axis == AxisType::PITCH) {
|
2018-12-12 17:55:52 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2021-12-25 23:43:10 -04:00
|
|
|
|
|
|
|
// get the next tune type
|
|
|
|
void AC_AutoTune::next_tune_type(TuneType &curr_tune_type, bool reset)
|
|
|
|
{
|
|
|
|
if (reset) {
|
|
|
|
set_tune_sequence();
|
|
|
|
tune_seq_curr = 0;
|
2022-01-21 14:44:36 -04:00
|
|
|
} else if (curr_tune_type == TUNE_COMPLETE) {
|
|
|
|
// leave tune_type as TUNE_COMPLETE to initiate next axis or exit autotune
|
|
|
|
return;
|
2021-12-25 23:43:10 -04:00
|
|
|
} else {
|
|
|
|
tune_seq_curr++;
|
|
|
|
}
|
2022-01-21 14:44:36 -04:00
|
|
|
|
2021-12-25 23:43:10 -04:00
|
|
|
curr_tune_type = tune_seq[tune_seq_curr];
|
|
|
|
}
|
|
|
|
|
2023-12-07 20:03:15 -04:00
|
|
|
#endif // AC_AUTOTUNE_ENABLED
|