#pragma once // Gyro and Accelerometer calibration criteria #define AP_INERTIAL_SENSOR_ACCEL_TOT_MAX_OFFSET_CHANGE 4.0f #define AP_INERTIAL_SENSOR_ACCEL_MAX_OFFSET 250.0f #define AP_INERTIAL_SENSOR_ACCEL_VIBE_FLOOR_FILT_HZ 5.0f // accel vibration floor filter hz #define AP_INERTIAL_SENSOR_ACCEL_VIBE_FILT_HZ 2.0f // accel vibration filter hz #define AP_INERTIAL_SENSOR_ACCEL_PEAK_DETECT_TIMEOUT_MS 500 // peak-hold detector timeout #include /** maximum number of INS instances available on this platform. If more than 1 then redundant sensors may be available */ #ifndef INS_MAX_INSTANCES #define INS_MAX_INSTANCES 3 #endif #define INS_MAX_BACKENDS 2*INS_MAX_INSTANCES #define INS_MAX_NOTCHES 12 #ifndef INS_VIBRATION_CHECK_INSTANCES #if HAL_MEM_CLASS >= HAL_MEM_CLASS_300 #define INS_VIBRATION_CHECK_INSTANCES INS_MAX_INSTANCES #else #define INS_VIBRATION_CHECK_INSTANCES 1 #endif #endif #define XYZ_AXIS_COUNT 3 // The maximum we need to store is gyro-rate / loop-rate, worst case ArduCopter with BMI088 is 2000/400 #define INS_MAX_GYRO_WINDOW_SAMPLES 8 #define DEFAULT_IMU_LOG_BAT_MASK 0 #ifndef HAL_INS_TEMPERATURE_CAL_ENABLE #define HAL_INS_TEMPERATURE_CAL_ENABLE !HAL_MINIMIZE_FEATURES && BOARD_FLASH_SIZE > 1024 #endif #ifndef HAL_INS_NUM_HARMONIC_NOTCH_FILTERS #define HAL_INS_NUM_HARMONIC_NOTCH_FILTERS 2 #endif #include #include #include #include #include #include #include #include #include #ifndef AP_SIM_INS_ENABLED #define AP_SIM_INS_ENABLED (CONFIG_HAL_BOARD == HAL_BOARD_SITL) #endif class AP_InertialSensor_Backend; class AuxiliaryBus; class AP_AHRS; /* forward declare AP_Logger class. We can't include logger.h because of mutual dependencies */ class AP_Logger; /* AP_InertialSensor is an abstraction for gyro and accel measurements * which are correctly aligned to the body axes and scaled to SI units. * * Gauss-Newton accel calibration routines borrowed from Rolfe Schmidt * blog post describing the method: http://chionophilous.wordpress.com/2011/10/24/accelerometer-calibration-iv-1-implementing-gauss-newton-on-an-atmega/ * original sketch available at http://rolfeschmidt.com/mathtools/skimetrics/adxl_gn_calibration.pde */ class AP_InertialSensor : AP_AccelCal_Client { friend class AP_InertialSensor_Backend; public: AP_InertialSensor(); /* Do not allow copies */ AP_InertialSensor(const AP_InertialSensor &other) = delete; AP_InertialSensor &operator=(const AP_InertialSensor&) = delete; static AP_InertialSensor *get_singleton(); enum Gyro_Calibration_Timing { GYRO_CAL_NEVER = 0, GYRO_CAL_STARTUP_ONLY = 1 }; /// Perform startup initialisation. /// /// Called to initialise the state of the IMU. /// /// Gyros will be calibrated unless INS_GYRO_CAL is zero /// /// @param style The initialisation startup style. /// void init(uint16_t sample_rate_hz); /// Register a new gyro/accel driver, allocating an instance /// number bool register_gyro(uint8_t &instance, uint16_t raw_sample_rate_hz, uint32_t id); bool register_accel(uint8_t &instance, uint16_t raw_sample_rate_hz, uint32_t id); // a function called by the main thread at the main loop rate: void periodic(); bool calibrate_trim(Vector3f &trim_rad); /// calibrating - returns true if the gyros or accels are currently being calibrated bool calibrating() const; /// calibrating - returns true if a temperature calibration is running bool temperature_cal_running() const; /// Perform cold-start initialisation for just the gyros. /// /// @note This should not be called unless ::init has previously /// been called, as ::init may perform other work /// void init_gyro(void); // get startup messages to output to the GCS bool get_output_banner(uint8_t instance_id, char* banner, uint8_t banner_len); /// Fetch the current gyro values /// /// @returns vector of rotational rates in radians/sec /// const Vector3f &get_gyro(uint8_t i) const { return _gyro[i]; } const Vector3f &get_gyro(void) const { return get_gyro(_primary_gyro); } // set gyro offsets in radians/sec const Vector3f &get_gyro_offsets(uint8_t i) const { return _gyro_offset[i]; } const Vector3f &get_gyro_offsets(void) const { return get_gyro_offsets(_primary_gyro); } //get delta angle if available bool get_delta_angle(uint8_t i, Vector3f &delta_angle, float &delta_angle_dt) const; bool get_delta_angle(Vector3f &delta_angle, float &delta_angle_dt) const { return get_delta_angle(_primary_gyro, delta_angle, delta_angle_dt); } //get delta velocity if available bool get_delta_velocity(uint8_t i, Vector3f &delta_velocity, float &delta_velocity_dt) const; bool get_delta_velocity(Vector3f &delta_velocity, float &delta_velocity_dt) const { return get_delta_velocity(_primary_accel, delta_velocity, delta_velocity_dt); } /// Fetch the current accelerometer values /// /// @returns vector of current accelerations in m/s/s /// const Vector3f &get_accel(uint8_t i) const { return _accel[i]; } const Vector3f &get_accel(void) const { return get_accel(_primary_accel); } uint32_t get_gyro_error_count(uint8_t i) const { return _gyro_error_count[i]; } uint32_t get_accel_error_count(uint8_t i) const { return _accel_error_count[i]; } // multi-device interface bool get_gyro_health(uint8_t instance) const { return (instance<_gyro_count) ? _gyro_healthy[instance] : false; } bool get_gyro_health(void) const { return get_gyro_health(_primary_gyro); } bool get_gyro_health_all(void) const; uint8_t get_gyro_count(void) const { return MIN(INS_MAX_INSTANCES, _accel_count); } bool gyro_calibrated_ok(uint8_t instance) const { return _gyro_cal_ok[instance]; } bool gyro_calibrated_ok_all() const; bool use_gyro(uint8_t instance) const; Gyro_Calibration_Timing gyro_calibration_timing(); bool get_accel_health(uint8_t instance) const { return (instance<_accel_count) ? _accel_healthy[instance] : false; } bool get_accel_health(void) const { return get_accel_health(_primary_accel); } bool get_accel_health_all(void) const; uint8_t get_accel_count(void) const { return MIN(INS_MAX_INSTANCES, _accel_count); } bool accel_calibrated_ok_all() const; bool use_accel(uint8_t instance) const; // get observed sensor rates, including any internal sampling multiplier uint16_t get_gyro_rate_hz(uint8_t instance) const { return uint16_t(_gyro_raw_sample_rates[instance] * _gyro_over_sampling[instance]); } uint16_t get_accel_rate_hz(uint8_t instance) const { return uint16_t(_accel_raw_sample_rates[instance] * _accel_over_sampling[instance]); } // FFT support access #if HAL_WITH_DSP const Vector3f &get_raw_gyro(void) const { return _gyro_raw[_primary_gyro]; } FloatBuffer& get_raw_gyro_window(uint8_t instance, uint8_t axis) { return _gyro_window[instance][axis]; } FloatBuffer& get_raw_gyro_window(uint8_t axis) { return get_raw_gyro_window(_primary_gyro, axis); } uint16_t get_raw_gyro_rate_hz() const { return get_raw_gyro_rate_hz(_primary_gyro); } uint16_t get_raw_gyro_rate_hz(uint8_t instance) const { return _gyro_raw_sample_rates[_primary_gyro]; } #endif bool set_gyro_window_size(uint16_t size); // get accel offsets in m/s/s const Vector3f &get_accel_offsets(uint8_t i) const { return _accel_offset[i]; } const Vector3f &get_accel_offsets(void) const { return get_accel_offsets(_primary_accel); } // get accel scale const Vector3f &get_accel_scale(uint8_t i) const { return _accel_scale[i]; } const Vector3f &get_accel_scale(void) const { return get_accel_scale(_primary_accel); } // return a 3D vector defining the position offset of the IMU accelerometer in metres relative to the body frame origin const Vector3f &get_imu_pos_offset(uint8_t instance) const { return _accel_pos[instance]; } const Vector3f &get_imu_pos_offset(void) const { return _accel_pos[_primary_accel]; } // return the temperature if supported. Zero is returned if no // temperature is available float get_temperature(uint8_t instance) const { return _temperature[instance]; } /* get_delta_time returns the time period in seconds * overwhich the sensor data was collected */ float get_delta_time() const { return MIN(_delta_time, _loop_delta_t_max); } // return the maximum gyro drift rate in radians/s/s. This // depends on what gyro chips are being used float get_gyro_drift_rate(void) const { return ToRad(0.5f/60); } // update gyro and accel values from accumulated samples void update(void) __RAMFUNC__; // wait for a sample to be available void wait_for_sample(void) __RAMFUNC__; // class level parameters static const struct AP_Param::GroupInfo var_info[]; // set overall board orientation void set_board_orientation(enum Rotation orientation, Matrix3f* custom_rotation = nullptr) { _board_orientation = orientation; _custom_rotation = custom_rotation; } // return the selected loop rate at which samples are made avilable uint16_t get_loop_rate_hz(void) const { return _loop_rate; } // return the main loop delta_t in seconds float get_loop_delta_t(void) const { return _loop_delta_t; } bool healthy(void) const { return get_gyro_health() && get_accel_health(); } uint8_t get_primary_accel(void) const { return _primary_accel; } uint8_t get_primary_gyro(void) const { return _primary_gyro; } // Update the harmonic notch frequency void update_harmonic_notch_freq_hz(uint8_t idx, float scaled_freq); // Update the harmonic notch frequencies void update_harmonic_notch_frequencies_hz(uint8_t idx, uint8_t num_freqs, const float scaled_freq[]); // get the gyro filter rate in Hz uint16_t get_gyro_filter_hz(void) const { return _gyro_filter_cutoff; } // get the accel filter rate in Hz uint16_t get_accel_filter_hz(void) const { return _accel_filter_cutoff; } // harmonic notch current center frequency float get_gyro_dynamic_notch_center_freq_hz(uint8_t idx) const { return idx temp_filter; // double precision is needed for good results when we // span a wide range of temperatures PolyFit<4, double, Vector3f> pfit; } state[2]; void add_sample(const Vector3f &sample, float temperature, LearnState &state); void finish_calibration(float temperature); bool save_calibration(float temperature); void reset(float temperature); float start_temp; float start_tmax; uint32_t last_save_ms; TCal &tcal; uint8_t instance(void) const { return tcal.instance(); } Vector3f accel_start; }; AP_Enum enable; // get persistent params for this instance void get_persistent_params(ExpandingString &str) const; private: AP_Float temp_max; AP_Float temp_min; AP_Vector3f accel_coeff[3]; AP_Vector3f gyro_coeff[3]; Vector3f accel_tref; Vector3f gyro_tref; Learn *learn; void correct_sensor(float temperature, float cal_temp, const AP_Vector3f coeff[3], Vector3f &v) const; Vector3f polynomial_eval(float temperature, const AP_Vector3f coeff[3]) const; // get instance number uint8_t instance(void) const; }; // instance number for logging uint8_t tcal_instance(const TCal &tc) const { return &tc - &tcal[0]; } private: TCal tcal[INS_MAX_INSTANCES]; enum class TCalOptions : uint8_t { PERSIST_TEMP_CAL = (1U<<0), PERSIST_ACCEL_CAL = (1U<<1), }; // temperature that last calibration was run at AP_Float caltemp_accel[INS_MAX_INSTANCES]; AP_Float caltemp_gyro[INS_MAX_INSTANCES]; AP_Int32 tcal_options; bool tcal_learning; #endif }; namespace AP { AP_InertialSensor &ins(); };