AP_AutoTune.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include <AP_HAL/AP_HAL.h>
  3. #include <AP_Logger/LogStructure.h>
  4. #include <AP_Param/AP_Param.h>
  5. #include <AP_Vehicle/AP_Vehicle.h>
  6. class AP_AutoTune {
  7. public:
  8. struct ATGains {
  9. AP_Float tau;
  10. AP_Float P;
  11. AP_Float I;
  12. AP_Float D;
  13. AP_Float FF;
  14. AP_Int16 rmax;
  15. AP_Int16 imax;
  16. };
  17. enum ATType {
  18. AUTOTUNE_ROLL = 0,
  19. AUTOTUNE_PITCH = 1
  20. };
  21. struct PACKED log_ATRP {
  22. LOG_PACKET_HEADER;
  23. uint64_t time_us;
  24. uint8_t type;
  25. uint8_t state;
  26. int16_t servo;
  27. float demanded;
  28. float achieved;
  29. float P;
  30. };
  31. // constructor
  32. AP_AutoTune(ATGains &_gains, ATType type, const AP_Vehicle::FixedWing &parms);
  33. // called when autotune mode is entered
  34. void start(void);
  35. // called to stop autotune and restore gains when user leaves
  36. // autotune
  37. void stop(void);
  38. // update called whenever autotune mode is active. This is
  39. // typically at 50Hz
  40. void update(float desired_rate, float achieved_rate, float servo_out);
  41. // are we running?
  42. bool running:1;
  43. private:
  44. // the current gains
  45. ATGains &current;
  46. // what type of autotune is this
  47. ATType type;
  48. const AP_Vehicle::FixedWing &aparm;
  49. // did we saturate surfaces?
  50. bool saturated_surfaces:1;
  51. // values to restore if we leave autotune mode
  52. ATGains restore;
  53. // values we last saved
  54. ATGains last_save;
  55. // values to save on the next save event
  56. ATGains next_save;
  57. // time when we last saved
  58. uint32_t last_save_ms = 0;
  59. // the demanded/achieved state
  60. enum ATState {DEMAND_UNSATURATED,
  61. DEMAND_UNDER_POS,
  62. DEMAND_OVER_POS,
  63. DEMAND_UNDER_NEG,
  64. DEMAND_OVER_NEG} state = DEMAND_UNSATURATED;
  65. // when we entered the current state
  66. uint32_t state_enter_ms = 0;
  67. void check_save(void);
  68. void check_state_exit(uint32_t state_time_ms);
  69. void save_gains(const ATGains &v);
  70. void write_log(float servo, float demanded, float achieved);
  71. void log_param_change(float v, const char *suffix);
  72. void save_float_if_changed(AP_Float &v, float value, const char *suffix);
  73. void save_int16_if_changed(AP_Int16 &v, int16_t value, const char *suffix);
  74. };