123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #pragma once
- #include <AP_Math/AP_Math.h>
- #include <AP_Math/vectorN.h>
- #define ACCEL_CAL_MAX_NUM_PARAMS 9
- #define ACCEL_CAL_TOLERANCE 0.1
- #define MAX_ITERATIONS 50
- enum accel_cal_status_t {
- ACCEL_CAL_NOT_STARTED=0,
- ACCEL_CAL_WAITING_FOR_ORIENTATION=1,
- ACCEL_CAL_COLLECTING_SAMPLE=2,
- ACCEL_CAL_SUCCESS=3,
- ACCEL_CAL_FAILED=4
- };
- enum accel_cal_fit_type_t {
- ACCEL_CAL_AXIS_ALIGNED_ELLIPSOID=0,
- ACCEL_CAL_ELLIPSOID=1
- };
- class AccelCalibrator {
- public:
- AccelCalibrator();
-
- void start(enum accel_cal_fit_type_t fit_type = ACCEL_CAL_AXIS_ALIGNED_ELLIPSOID, uint8_t num_samples = 6, float sample_time = 0.5f);
- void start(enum accel_cal_fit_type_t fit_type, uint8_t num_samples, float sample_time, Vector3f offset, Vector3f diag, Vector3f offdiag);
-
-
- void clear();
-
- bool running();
-
- void collect_sample();
-
- void check_for_timeout();
-
- void get_calibration(Vector3f& offset) const;
- void get_calibration(Vector3f& offset, Vector3f& diag) const;
- void get_calibration(Vector3f& offset, Vector3f& diag, Vector3f& offdiag) const;
-
- void new_sample(const Vector3f& delta_velocity, float dt);
-
-
- bool get_sample(uint8_t i, Vector3f& s) const;
-
-
- bool get_sample_corrected(uint8_t i, Vector3f& s) const;
-
- void set_tolerance(float tolerance) { _conf_tolerance = tolerance; }
-
- enum accel_cal_status_t get_status() const { return _status; }
-
- uint8_t get_num_samples_collected() const { return _samples_collected; }
-
- float get_fitness() const { return _fitness; }
- struct param_t {
- Vector3f offset;
- Vector3f diag;
- Vector3f offdiag;
- };
- private:
- struct AccelSample {
- Vector3f delta_velocity;
- float delta_time;
- };
- typedef VectorN<float, ACCEL_CAL_MAX_NUM_PARAMS> VectorP;
- union param_u {
- struct param_t s;
- VectorN<float, ACCEL_CAL_MAX_NUM_PARAMS> a;
- param_u() : a{}
- {
- static_assert(sizeof(*this) == sizeof(struct param_t),
- "Invalid union members: sizes do not match");
- }
- };
-
- uint8_t _conf_num_samples;
- float _conf_sample_time;
- enum accel_cal_fit_type_t _conf_fit_type;
- float _conf_tolerance;
-
- accel_cal_status_t _status;
- struct AccelSample* _sample_buffer;
- uint8_t _samples_collected;
- union param_u _param;
- float _fitness;
- uint32_t _last_samp_frag_collected_ms;
- float _min_sample_dist;
-
-
- bool accept_sample(const Vector3f& sample);
-
- void reset_state();
-
- void set_status(enum accel_cal_status_t);
-
- bool accept_result() const;
-
- uint8_t get_num_params() const;
-
- float calc_residual(const Vector3f& sample, const struct param_t& params) const;
- float calc_mean_squared_residuals() const;
- float calc_mean_squared_residuals(const struct param_t& params) const;
- void calc_jacob(const Vector3f& sample, const struct param_t& params, VectorP& ret) const;
- void run_fit(uint8_t max_iterations, float& fitness);
- };
|