APM:Libraries
CompassCalibrator.h
Go to the documentation of this file.
1 #include <AP_Math/AP_Math.h>
2 
3 #define COMPASS_CAL_NUM_SPHERE_PARAMS 4
4 #define COMPASS_CAL_NUM_ELLIPSOID_PARAMS 9
5 #define COMPASS_CAL_NUM_SAMPLES 300
6 
7 //RMS tolerance
8 #define COMPASS_CAL_DEFAULT_TOLERANCE 5.0f
9 
17 };
18 
20 public:
21  typedef uint8_t completion_mask_t[10];
22 
24 
25  void start(bool retry, float delay, uint16_t offset_max);
26  void clear();
27 
28  void update(bool &failure);
29  void new_sample(const Vector3f &sample);
30 
31  bool check_for_timeout();
32 
33  bool running() const;
34 
35  void set_tolerance(float tolerance) { _tolerance = tolerance; }
36 
37  void get_calibration(Vector3f &offsets, Vector3f &diagonals, Vector3f &offdiagonals);
38 
39  float get_completion_percent() const;
40  completion_mask_t& get_completion_mask();
41  enum compass_cal_status_t get_status() const { return _status; }
42  float get_fitness() const { return sqrtf(_fitness); }
43  uint8_t get_attempt() const { return _attempt; }
44 
45 private:
46  class param_t {
47  public:
48  float* get_sphere_params() {
49  return &radius;
50  }
51 
53  return &offset.x;
54  }
55 
56  float radius;
60  };
61 
62  class CompassSample {
63  public:
64  Vector3f get() const;
65  void set(const Vector3f &in);
66  private:
67  int16_t x;
68  int16_t y;
69  int16_t z;
70  };
71 
72 
73 
75 
76  // timeout watchdog state
77  uint32_t _last_sample_ms;
78 
79  // behavioral state
81  uint32_t _start_time_ms;
82  bool _retry;
83  float _tolerance;
84  uint8_t _attempt;
85  uint16_t _offset_max;
86 
87  completion_mask_t _completion_mask;
88 
89  //fit state
91  uint16_t _fit_step;
93  float _fitness; // mean squared residuals
98  uint16_t _samples_thinned;
99 
100  bool set_status(compass_cal_status_t status);
101 
102  // returns true if sample should be added to buffer
103  bool accept_sample(const Vector3f &sample);
104  bool accept_sample(const CompassSample &sample);
105 
106  // returns true if fit is acceptable
107  bool fit_acceptable();
108 
109  void reset_state();
110  void initialize_fit();
111 
112  bool fitting() const;
113 
114  // thins out samples between step one and step two
115  void thin_samples();
116 
117  float calc_residual(const Vector3f& sample, const param_t& params) const;
118  float calc_mean_squared_residuals(const param_t& params) const;
119  float calc_mean_squared_residuals() const;
120 
121  void calc_initial_offset();
122  void calc_sphere_jacob(const Vector3f& sample, const param_t& params, float* ret) const;
123  void run_sphere_fit();
124 
125  void calc_ellipsoid_jacob(const Vector3f& sample, const param_t& params, float* ret) const;
126  void run_ellipsoid_fit();
127 
134  void update_completion_mask(const Vector3f& v);
138  void update_completion_mask();
139 };
float get_completion_percent() const
void start(bool retry, float delay, uint16_t offset_max)
void new_sample(const Vector3f &sample)
void set_tolerance(float tolerance)
enum compass_cal_status_t _status
float calc_residual(const Vector3f &sample, const param_t &params) const
uint8_t get_attempt() const
void calc_ellipsoid_jacob(const Vector3f &sample, const param_t &params, float *ret) const
uint8_t completion_mask_t[10]
completion_mask_t _completion_mask
completion_mask_t & get_completion_mask()
void delay(uint32_t ms)
Definition: system.cpp:91
void calc_sphere_jacob(const Vector3f &sample, const param_t &params, float *ret) const
compass_cal_status_t
void update(bool &failure)
float get_fitness() const
class param_t _params
void get_calibration(Vector3f &offsets, Vector3f &diagonals, Vector3f &offdiagonals)
enum compass_cal_status_t get_status() const
bool set_status(compass_cal_status_t status)
bool accept_sample(const Vector3f &sample)
float v
Definition: Printf.cpp:15
float calc_mean_squared_residuals() const
CompassSample * _sample_buffer
T x
Definition: vector3.h:67