123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- #include "AP_AutoTune.h"
- #include <AP_Common/AP_Common.h>
- #include <AP_HAL/AP_HAL.h>
- #include <AP_Logger/AP_Logger.h>
- #include <AP_Math/AP_Math.h>
- extern const AP_HAL::HAL& hal;
- #define AUTOTUNE_SAVE_PERIOD 10000U
- #define AUTOTUNE_OVERSHOOT_TIME 100
- #define AUTOTUNE_UNDERSHOOT_TIME 200
- #define AUTOTUNE_INCREASE_STEP 5
- #define AUTOTUNE_DECREASE_STEP 8
- #define AUTOTUNE_MAX_P 5.0f
- #define AUTOTUNE_MIN_P 0.3f
- #define AUTOTUNE_MAX_TAU 0.7f
- #define AUTOTUNE_MIN_TAU 0.2f
- #define AUTOTUNE_MIN_IMAX 2000
- #define AUTOTUNE_MAX_IMAX 4000
- AP_AutoTune::AP_AutoTune(ATGains &_gains, ATType _type,
- const AP_Vehicle::FixedWing &parms) :
- running(false),
- current(_gains),
- type(_type),
- aparm(parms),
- saturated_surfaces(false)
- {}
- #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
- #include <stdio.h>
- # define Debug(fmt, args ...) do {::printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
- #else
- # define Debug(fmt, args ...)
- #endif
- static const struct {
- float tau;
- float Dratio;
- float rmax;
- } tuning_table[] = {
- { 0.70f, 0.050f, 20 },
- { 0.65f, 0.055f, 30 },
- { 0.60f, 0.060f, 40 },
- { 0.55f, 0.065f, 50 },
- { 0.50f, 0.070f, 60 },
- { 0.45f, 0.075f, 75 },
- { 0.40f, 0.080f, 90 },
- { 0.30f, 0.085f, 120 },
- { 0.20f, 0.090f, 160 },
- { 0.10f, 0.095f, 210 },
- { 0.05f, 0.100f, 300 },
- };
- void AP_AutoTune::start(void)
- {
- running = true;
- state = DEMAND_UNSATURATED;
- uint32_t now = AP_HAL::millis();
- state_enter_ms = now;
- last_save_ms = now;
- last_save = current;
- restore = current;
- uint8_t level = aparm.autotune_level;
- if (level > ARRAY_SIZE(tuning_table)) {
- level = ARRAY_SIZE(tuning_table);
- }
- if (level < 1) {
- level = 1;
- }
- current.rmax.set(tuning_table[level-1].rmax);
-
- current.D.set(tuning_table[level-1].Dratio * current.P);
- current.tau.set(tuning_table[level-1].tau);
- current.imax = constrain_float(current.imax, AUTOTUNE_MIN_IMAX, AUTOTUNE_MAX_IMAX);
-
- current.I = 0.5f * current.D / current.tau;
- next_save = current;
- Debug("START P -> %.3f\n", current.P.get());
- }
- void AP_AutoTune::stop(void)
- {
- if (running) {
- running = false;
- save_gains(restore);
- }
- }
- void AP_AutoTune::update(float desired_rate, float achieved_rate, float servo_out)
- {
- if (!running) {
- return;
- }
- check_save();
-
- ATState new_state;
- float abs_desired_rate = fabsf(desired_rate);
- uint32_t now = AP_HAL::millis();
- if (fabsf(servo_out) >= 45) {
-
-
-
- saturated_surfaces = true;
- }
- if (abs_desired_rate < 0.8f * current.rmax) {
-
- new_state = DEMAND_UNSATURATED;
- } else if (fabsf(achieved_rate) > abs_desired_rate) {
- new_state = desired_rate > 0 ? DEMAND_OVER_POS : DEMAND_OVER_NEG;
- } else {
- new_state = desired_rate > 0 ? DEMAND_UNDER_POS : DEMAND_UNDER_NEG;
- }
- if (new_state != state) {
- check_state_exit(now - state_enter_ms);
- state = new_state;
- state_enter_ms = now;
- saturated_surfaces = false;
- }
- if (state != DEMAND_UNSATURATED) {
- write_log(servo_out, desired_rate, achieved_rate);
- }
- }
- void AP_AutoTune::check_state_exit(uint32_t state_time_ms)
- {
- switch (state) {
- case DEMAND_UNSATURATED:
- break;
- case DEMAND_UNDER_POS:
- case DEMAND_UNDER_NEG:
-
-
- if (state_time_ms >= AUTOTUNE_UNDERSHOOT_TIME && !saturated_surfaces) {
- current.P.set(current.P * (100+AUTOTUNE_INCREASE_STEP) * 0.01f);
- if (current.P > AUTOTUNE_MAX_P) {
- current.P = AUTOTUNE_MAX_P;
- }
- Debug("UNDER P -> %.3f\n", current.P.get());
- }
- current.D.set(tuning_table[aparm.autotune_level-1].Dratio * current.P);
- break;
- case DEMAND_OVER_POS:
- case DEMAND_OVER_NEG:
- if (state_time_ms >= AUTOTUNE_OVERSHOOT_TIME) {
- current.P.set(current.P * (100-AUTOTUNE_DECREASE_STEP) * 0.01f);
- if (current.P < AUTOTUNE_MIN_P) {
- current.P = AUTOTUNE_MIN_P;
- }
- Debug("OVER P -> %.3f\n", current.P.get());
- }
- current.D.set(tuning_table[aparm.autotune_level-1].Dratio * current.P);
- break;
- }
- }
- void AP_AutoTune::check_save(void)
- {
- if (AP_HAL::millis() - last_save_ms < AUTOTUNE_SAVE_PERIOD) {
- return;
- }
-
-
-
-
- ATGains tmp = current;
- save_gains(next_save);
- Debug("SAVE P -> %.3f\n", current.P.get());
-
- current = tmp;
-
- restore = next_save;
-
- next_save = current;
- last_save_ms = AP_HAL::millis();
- }
- void AP_AutoTune::log_param_change(float v, const char *suffix)
- {
- AP_Logger *logger = AP_Logger::get_singleton();
- if (!logger->logging_started()) {
- return;
- }
- char key[AP_MAX_NAME_SIZE+1];
- if (type == AUTOTUNE_ROLL) {
- strncpy(key, "RLL2SRV_", 9);
- strncpy(&key[8], suffix, AP_MAX_NAME_SIZE-8);
- } else {
- strncpy(key, "PTCH2SRV_", 10);
- strncpy(&key[9], suffix, AP_MAX_NAME_SIZE-9);
- }
- key[AP_MAX_NAME_SIZE] = 0;
- logger->Write_Parameter(key, v);
- }
- void AP_AutoTune::save_float_if_changed(AP_Float &v, float value, const char *suffix)
- {
- float old_value = v.get();
- v.set(value);
- if (value <= 0 || fabsf((value-old_value)/value) > 0.001f) {
- v.save();
- log_param_change(v.get(), suffix);
- }
- }
- void AP_AutoTune::save_int16_if_changed(AP_Int16 &v, int16_t value, const char *suffix)
- {
- int16_t old_value = v.get();
- v.set(value);
- if (old_value != v.get()) {
- v.save();
- log_param_change(v.get(), suffix);
- }
- }
- void AP_AutoTune::save_gains(const ATGains &v)
- {
- current = last_save;
- save_float_if_changed(current.tau, v.tau, "TCONST");
- save_float_if_changed(current.P, v.P, "P");
- save_float_if_changed(current.I, v.I, "I");
- save_float_if_changed(current.D, v.D, "D");
- save_int16_if_changed(current.rmax, v.rmax, "RMAX");
- save_int16_if_changed(current.imax, v.imax, "IMAX");
- last_save = current;
- }
- void AP_AutoTune::write_log(float servo, float demanded, float achieved)
- {
- AP_Logger *logger = AP_Logger::get_singleton();
- if (!logger->logging_started()) {
- return;
- }
- struct log_ATRP pkt = {
- LOG_PACKET_HEADER_INIT(LOG_ATRP_MSG),
- time_us : AP_HAL::micros64(),
- type : static_cast<uint8_t>(type),
- state : (uint8_t)state,
- servo : (int16_t)(servo*100),
- demanded : demanded,
- achieved : achieved,
- P : current.P.get()
- };
- logger->WriteBlock(&pkt, sizeof(pkt));
- }
|