AP_AHRS_DCM.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * DCM based AHRS (Attitude Heading Reference System) interface for
  16. * ArduPilot
  17. *
  18. */
  19. class AP_AHRS_DCM : public AP_AHRS {
  20. public:
  21. AP_AHRS_DCM()
  22. : AP_AHRS()
  23. , _error_rp(1.0f)
  24. , _error_yaw(1.0f)
  25. , _mag_earth(1, 0)
  26. , _imu1_weight(0.5f)
  27. {
  28. _dcm_matrix.identity();
  29. // these are experimentally derived from the simulator
  30. // with large drift levels
  31. _ki = 0.0087f;
  32. _ki_yaw = 0.01f;
  33. }
  34. /* Do not allow copies */
  35. AP_AHRS_DCM(const AP_AHRS_DCM &other) = delete;
  36. AP_AHRS_DCM &operator=(const AP_AHRS_DCM&) = delete;
  37. // return the smoothed gyro vector corrected for drift
  38. const Vector3f &get_gyro() const override {
  39. return _omega;
  40. }
  41. // return rotation matrix representing rotaton from body to earth axes
  42. const Matrix3f &get_rotation_body_to_ned() const override {
  43. return _body_dcm_matrix;
  44. }
  45. // get rotation matrix specifically from DCM backend (used for compass calibrator)
  46. const Matrix3f &get_DCM_rotation_body_to_ned(void) const override { return _body_dcm_matrix; }
  47. // return the current drift correction integrator value
  48. const Vector3f &get_gyro_drift() const override {
  49. return _omega_I;
  50. }
  51. // reset the current gyro drift estimate
  52. // should be called if gyro offsets are recalculated
  53. void reset_gyro_drift() override;
  54. // Methods
  55. void update(bool skip_ins_update=false) override;
  56. void reset(bool recover_eulers = false) override;
  57. // reset the current attitude, used on new IMU calibration
  58. void reset_attitude(const float &roll, const float &pitch, const float &yaw) override;
  59. // dead-reckoning support
  60. virtual bool get_position(struct Location &loc) const override;
  61. // status reporting
  62. float get_error_rp() const override {
  63. return _error_rp;
  64. }
  65. float get_error_yaw() const override {
  66. return _error_yaw;
  67. }
  68. // return a wind estimation vector, in m/s
  69. Vector3f wind_estimate() const override {
  70. return _wind;
  71. }
  72. void get_relative_position_D_home(float &posD) const override;
  73. // return an airspeed estimate if available. return true
  74. // if we have an estimate
  75. bool airspeed_estimate(float *airspeed_ret) const override;
  76. bool use_compass() override;
  77. bool set_home(const Location &loc) override WARN_IF_UNUSED;
  78. void estimate_wind(void);
  79. // is the AHRS subsystem healthy?
  80. bool healthy() const override;
  81. bool get_velocity_NED(Vector3f &vec) const override;
  82. private:
  83. float _ki;
  84. float _ki_yaw;
  85. // Methods
  86. void matrix_update(float _G_Dt);
  87. void normalize(void);
  88. void check_matrix(void);
  89. bool renorm(Vector3f const &a, Vector3f &result);
  90. void drift_correction(float deltat);
  91. void drift_correction_yaw(void);
  92. float yaw_error_compass();
  93. void euler_angles(void);
  94. bool have_gps(void) const;
  95. bool use_fast_gains(void) const;
  96. void load_watchdog_home();
  97. void backup_attitude(void);
  98. // primary representation of attitude of board used for all inertial calculations
  99. Matrix3f _dcm_matrix;
  100. // primary representation of attitude of flight vehicle body
  101. Matrix3f _body_dcm_matrix;
  102. Vector3f _omega_P; // accel Omega proportional correction
  103. Vector3f _omega_yaw_P; // proportional yaw correction
  104. Vector3f _omega_I; // Omega Integrator correction
  105. Vector3f _omega_I_sum;
  106. float _omega_I_sum_time;
  107. Vector3f _omega; // Corrected Gyro_Vector data
  108. // variables to cope with delaying the GA sum to match GPS lag
  109. Vector3f ra_delayed(uint8_t instance, const Vector3f &ra);
  110. Vector3f _ra_delay_buffer[INS_MAX_INSTANCES];
  111. // P term gain based on spin rate
  112. float _P_gain(float spin_rate);
  113. // P term yaw gain based on rate of change of horiz velocity
  114. float _yaw_gain(void) const;
  115. // state to support status reporting
  116. float _renorm_val_sum;
  117. uint16_t _renorm_val_count;
  118. float _error_rp;
  119. float _error_yaw;
  120. // time in millis when we last got a GPS heading
  121. uint32_t _gps_last_update;
  122. // state of accel drift correction
  123. Vector3f _ra_sum[INS_MAX_INSTANCES];
  124. Vector3f _last_velocity;
  125. float _ra_deltat;
  126. uint32_t _ra_sum_start;
  127. // the earths magnetic field
  128. float _last_declination;
  129. Vector2f _mag_earth;
  130. // whether we have GPS lock
  131. bool _have_gps_lock;
  132. // the lat/lng where we last had GPS lock
  133. int32_t _last_lat;
  134. int32_t _last_lng;
  135. // position offset from last GPS lock
  136. float _position_offset_north;
  137. float _position_offset_east;
  138. // whether we have a position estimate
  139. bool _have_position;
  140. // support for wind estimation
  141. Vector3f _last_fuse;
  142. Vector3f _last_vel;
  143. uint32_t _last_wind_time;
  144. float _last_airspeed;
  145. uint32_t _last_consistent_heading;
  146. // estimated wind in m/s
  147. Vector3f _wind;
  148. float _imu1_weight;
  149. // last time AHRS failed in milliseconds
  150. uint32_t _last_failure_ms;
  151. // time when DCM was last reset
  152. uint32_t _last_startup_ms;
  153. };