AP_Compass_Backend.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. Compass driver backend class. Each supported compass sensor type
  15. needs to have an object derived from this class.
  16. */
  17. #pragma once
  18. #include "AP_Compass.h"
  19. class Compass; // forward declaration
  20. class AP_Compass_Backend
  21. {
  22. public:
  23. AP_Compass_Backend();
  24. // we declare a virtual destructor so that drivers can
  25. // override with a custom destructor if need be.
  26. virtual ~AP_Compass_Backend(void) {}
  27. // read sensor data
  28. virtual void read(void) = 0;
  29. /*
  30. device driver IDs. These are used to fill in the devtype field
  31. of the device ID, which shows up as COMPASS*ID* parameters to
  32. users. The values are chosen for compatibility with existing PX4
  33. drivers.
  34. If a change is made to a driver that would make existing
  35. calibration values invalid then this number must be changed.
  36. */
  37. enum DevTypes {
  38. DEVTYPE_HMC5883_OLD = 0x01,
  39. DEVTYPE_HMC5883 = 0x07,
  40. DEVTYPE_LSM303D = 0x02,
  41. DEVTYPE_AK8963 = 0x04,
  42. DEVTYPE_BMM150 = 0x05,
  43. DEVTYPE_LSM9DS1 = 0x06,
  44. DEVTYPE_LIS3MDL = 0x08,
  45. DEVTYPE_AK09916 = 0x09,
  46. DEVTYPE_IST8310 = 0x0A,
  47. DEVTYPE_ICM20948 = 0x0B,
  48. DEVTYPE_MMC3416 = 0x0C,
  49. DEVTYPE_QMC5883L = 0x0D,
  50. DEVTYPE_MAG3110 = 0x0E,
  51. DEVTYPE_SITL = 0x0F,
  52. DEVTYPE_IST8308 = 0x10,
  53. DEVTYPE_RM3100 = 0x11,
  54. };
  55. protected:
  56. /*
  57. * A compass measurement is expected to pass through the following functions:
  58. * 1. rotate_field - this rotates the measurement in-place from sensor frame
  59. * to body frame
  60. * 2. publish_raw_field - this provides an uncorrected point-sample for
  61. * calibration libraries
  62. * 3. correct_field - this corrects the measurement in-place for hard iron,
  63. * soft iron, motor interference, and non-orthagonality errors
  64. * 4. publish_filtered_field - legacy filtered magnetic field
  65. *
  66. * All those functions expect the mag field to be in milligauss.
  67. */
  68. void rotate_field(Vector3f &mag, uint8_t instance);
  69. void publish_raw_field(const Vector3f &mag, uint8_t instance);
  70. void correct_field(Vector3f &mag, uint8_t i);
  71. void publish_filtered_field(const Vector3f &mag, uint8_t instance);
  72. void set_last_update_usec(uint32_t last_update, uint8_t instance);
  73. void accumulate_sample(Vector3f &field, uint8_t instance,
  74. uint32_t max_samples = 10);
  75. void drain_accumulated_samples(uint8_t instance, const Vector3f *scale = NULL);
  76. // register a new compass instance with the frontend
  77. uint8_t register_compass(void) const;
  78. // set dev_id for an instance
  79. void set_dev_id(uint8_t instance, uint32_t dev_id);
  80. // save dev_id, used by SITL
  81. void save_dev_id(uint8_t instance);
  82. // set external state for an instance
  83. void set_external(uint8_t instance, bool external);
  84. // tell if instance is an external compass
  85. bool is_external(uint8_t instance);
  86. // set rotation of an instance
  87. void set_rotation(uint8_t instance, enum Rotation rotation);
  88. // get board orientation (for SITL)
  89. enum Rotation get_board_orientation(void) const;
  90. // access to frontend
  91. Compass &_compass;
  92. // semaphore for access to shared frontend data
  93. HAL_Semaphore_Recursive _sem;
  94. // Check that the compass field is valid by using a mean filter on the vector length
  95. bool field_ok(const Vector3f &field);
  96. uint32_t get_error_count() const { return _error_count; }
  97. private:
  98. void apply_corrections(Vector3f &mag, uint8_t i);
  99. // mean field length for range filter
  100. float _mean_field_length;
  101. // number of dropped samples. Not used for now, but can be usable to choose more reliable sensor
  102. uint32_t _error_count;
  103. };