2014-10-14 01:48:33 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-10-15 23:31:21 -03:00
|
|
|
IMU driver backend class. Each supported gyro/accel sensor type
|
|
|
|
needs to have an object derived from this class.
|
|
|
|
|
|
|
|
Note that drivers can implement just gyros or just accels, and can
|
|
|
|
also provide multiple gyro/accel instances.
|
2014-10-14 01:48:33 -03:00
|
|
|
*/
|
2016-01-19 21:26:31 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
#include "AP_InertialSensor.h"
|
2014-10-14 01:48:33 -03:00
|
|
|
|
2015-08-05 13:36:14 -03:00
|
|
|
class AuxiliaryBus;
|
2019-01-18 00:23:42 -04:00
|
|
|
class AP_Logger;
|
2015-08-05 13:36:14 -03:00
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
class AP_InertialSensor_Backend
|
|
|
|
{
|
|
|
|
public:
|
2014-10-15 05:54:30 -03:00
|
|
|
AP_InertialSensor_Backend(AP_InertialSensor &imu);
|
2015-10-01 20:04:04 -03:00
|
|
|
AP_InertialSensor_Backend(const AP_InertialSensor_Backend &that) = delete;
|
2014-10-14 01:48:33 -03:00
|
|
|
|
|
|
|
// we declare a virtual destructor so that drivers can
|
|
|
|
// override with a custom destructor if need be.
|
|
|
|
virtual ~AP_InertialSensor_Backend(void) {}
|
|
|
|
|
2016-01-19 21:26:31 -04:00
|
|
|
/*
|
2014-10-14 01:48:33 -03:00
|
|
|
* Update the sensor data. Called by the frontend to transfer
|
2014-10-15 20:32:40 -03:00
|
|
|
* accumulated sensor readings to the frontend structure via the
|
2015-02-17 02:50:59 -04:00
|
|
|
* _publish_gyro() and _publish_accel() functions
|
2014-10-14 01:48:33 -03:00
|
|
|
*/
|
|
|
|
virtual bool update() = 0;
|
|
|
|
|
2015-11-15 20:38:43 -04:00
|
|
|
/*
|
|
|
|
* optional function to accumulate more samples. This is needed for drivers that don't use a timer to gather samples
|
2014-10-14 01:48:33 -03:00
|
|
|
*/
|
2015-11-15 20:38:43 -04:00
|
|
|
virtual void accumulate() {}
|
2014-10-14 01:48:33 -03:00
|
|
|
|
2015-08-05 10:01:52 -03:00
|
|
|
/*
|
|
|
|
* Configure and start all sensors. The empty implementation allows
|
|
|
|
* subclasses to already start the sensors when it's detected
|
|
|
|
*/
|
|
|
|
virtual void start() { }
|
|
|
|
|
2015-08-05 13:36:14 -03:00
|
|
|
/*
|
|
|
|
* Return an AuxiliaryBus if backend has another bus it is able to export
|
|
|
|
*/
|
2015-10-02 15:02:16 -03:00
|
|
|
virtual AuxiliaryBus *get_auxiliary_bus() { return nullptr; }
|
2015-08-05 13:36:14 -03:00
|
|
|
|
2016-09-03 17:20:19 -03:00
|
|
|
/*
|
|
|
|
* Return the unique identifier for this backend: it's the same for
|
|
|
|
* several sensors if the backend registers more gyros/accels
|
|
|
|
*/
|
2015-08-16 16:16:11 -03:00
|
|
|
int16_t get_id() const { return _id; }
|
|
|
|
|
2019-03-11 09:56:40 -03:00
|
|
|
//Returns the Clip Limit
|
|
|
|
float get_clip_limit() const { return _clip_limit; }
|
|
|
|
|
2017-04-30 21:53:41 -03:00
|
|
|
// notify of a fifo reset
|
|
|
|
void notify_fifo_reset(void);
|
|
|
|
|
2016-11-04 21:11:00 -03:00
|
|
|
/*
|
|
|
|
device driver IDs. These are used to fill in the devtype field
|
|
|
|
of the device ID, which shows up as INS*ID* parameters to
|
|
|
|
users. The values are chosen for compatibility with existing PX4
|
|
|
|
drivers.
|
|
|
|
If a change is made to a driver that would make existing
|
|
|
|
calibration values invalid then this number must be changed.
|
|
|
|
*/
|
|
|
|
enum DevTypes {
|
|
|
|
DEVTYPE_BMI160 = 0x09,
|
|
|
|
DEVTYPE_L3G4200D = 0x10,
|
|
|
|
DEVTYPE_ACC_LSM303D = 0x11,
|
|
|
|
DEVTYPE_ACC_BMA180 = 0x12,
|
|
|
|
DEVTYPE_ACC_MPU6000 = 0x13,
|
|
|
|
DEVTYPE_ACC_MPU9250 = 0x16,
|
2017-10-30 06:03:04 -03:00
|
|
|
DEVTYPE_ACC_IIS328DQ = 0x17,
|
2017-10-26 11:31:07 -03:00
|
|
|
DEVTYPE_ACC_LSM9DS1 = 0x18,
|
2016-11-04 21:11:00 -03:00
|
|
|
DEVTYPE_GYR_MPU6000 = 0x21,
|
|
|
|
DEVTYPE_GYR_L3GD20 = 0x22,
|
2017-10-30 06:03:04 -03:00
|
|
|
DEVTYPE_GYR_MPU9250 = 0x24,
|
|
|
|
DEVTYPE_GYR_I3G4250D = 0x25,
|
2017-10-26 11:31:07 -03:00
|
|
|
DEVTYPE_GYR_LSM9DS1 = 0x26,
|
2018-04-10 08:08:52 -03:00
|
|
|
DEVTYPE_INS_ICM20789 = 0x27,
|
|
|
|
DEVTYPE_INS_ICM20689 = 0x28,
|
2018-06-07 02:59:18 -03:00
|
|
|
DEVTYPE_INS_BMI055 = 0x29,
|
2018-06-27 00:31:23 -03:00
|
|
|
DEVTYPE_SITL = 0x2A,
|
2019-01-31 03:00:34 -04:00
|
|
|
DEVTYPE_INS_BMI088 = 0x2B,
|
2019-02-15 08:12:45 -04:00
|
|
|
DEVTYPE_INS_ICM20948 = 0x2C,
|
|
|
|
DEVTYPE_INS_ICM20648 = 0x2D,
|
2019-03-10 21:15:37 -03:00
|
|
|
DEVTYPE_INS_ICM20649 = 0x2E,
|
|
|
|
DEVTYPE_INS_ICM20602 = 0x2F,
|
2019-03-25 15:25:33 -03:00
|
|
|
DEVTYPE_INS_ICM20601 = 0x30,
|
2016-11-04 21:11:00 -03:00
|
|
|
};
|
2018-03-18 20:28:33 -03:00
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
protected:
|
2014-10-15 20:32:40 -03:00
|
|
|
// access to frontend
|
|
|
|
AP_InertialSensor &_imu;
|
2014-10-14 01:48:33 -03:00
|
|
|
|
2016-11-03 21:06:19 -03:00
|
|
|
// semaphore for access to shared frontend data
|
2018-10-11 20:35:03 -03:00
|
|
|
HAL_Semaphore_Recursive _sem;
|
2016-11-03 21:06:19 -03:00
|
|
|
|
2019-03-11 09:56:40 -03:00
|
|
|
//Default Clip Limit
|
|
|
|
float _clip_limit = 15.5f * GRAVITY_MSS;
|
|
|
|
|
2015-02-17 02:54:17 -04:00
|
|
|
void _rotate_and_correct_accel(uint8_t instance, Vector3f &accel);
|
|
|
|
void _rotate_and_correct_gyro(uint8_t instance, Vector3f &gyro);
|
|
|
|
|
2015-02-17 02:50:59 -04:00
|
|
|
// rotate gyro vector, offset and publish
|
2015-08-28 12:18:09 -03:00
|
|
|
void _publish_gyro(uint8_t instance, const Vector3f &gyro);
|
2015-02-17 02:50:59 -04:00
|
|
|
|
2017-04-30 21:53:41 -03:00
|
|
|
// this should be called every time a new gyro raw sample is
|
|
|
|
// available - be it published or not the sample is raw in the
|
|
|
|
// sense that it's not filtered yet, but it must be rotated and
|
|
|
|
// corrected (_rotate_and_correct_gyro)
|
|
|
|
// The sample_us value must be provided for non-FIFO based
|
|
|
|
// sensors, and should be set to zero for FIFO based sensors
|
2015-11-15 20:58:08 -04:00
|
|
|
void _notify_new_gyro_raw_sample(uint8_t instance, const Vector3f &accel, uint64_t sample_us=0);
|
2015-09-08 14:05:37 -03:00
|
|
|
|
2015-02-17 02:50:59 -04:00
|
|
|
// rotate accel vector, scale, offset and publish
|
2015-08-28 12:18:09 -03:00
|
|
|
void _publish_accel(uint8_t instance, const Vector3f &accel);
|
2014-10-14 01:48:33 -03:00
|
|
|
|
2015-08-27 16:05:13 -03:00
|
|
|
// this should be called every time a new accel raw sample is available -
|
|
|
|
// be it published or not
|
|
|
|
// the sample is raw in the sense that it's not filtered yet, but it must
|
|
|
|
// be rotated and corrected (_rotate_and_correct_accel)
|
2017-04-30 21:53:41 -03:00
|
|
|
// The sample_us value must be provided for non-FIFO based
|
|
|
|
// sensors, and should be set to zero for FIFO based sensors
|
2016-08-31 01:56:27 -03:00
|
|
|
void _notify_new_accel_raw_sample(uint8_t instance, const Vector3f &accel, uint64_t sample_us=0, bool fsync_set=false);
|
2015-08-27 16:05:13 -03:00
|
|
|
|
2017-05-01 00:01:43 -03:00
|
|
|
// set the amount of oversamping a accel is doing
|
|
|
|
void _set_accel_oversampling(uint8_t instance, uint8_t n);
|
|
|
|
|
|
|
|
// set the amount of oversamping a gyro is doing
|
|
|
|
void _set_gyro_oversampling(uint8_t instance, uint8_t n);
|
2018-03-18 20:28:33 -03:00
|
|
|
|
|
|
|
// indicate the backend is doing sensor-rate sampling for this accel
|
|
|
|
void _set_accel_sensor_rate_sampling_enabled(uint8_t instance, bool value) {
|
|
|
|
const uint8_t bit = (1<<instance);
|
|
|
|
if (value) {
|
|
|
|
_imu._accel_sensor_rate_sampling_enabled |= bit;
|
|
|
|
} else {
|
|
|
|
_imu._accel_sensor_rate_sampling_enabled &= ~bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _set_gyro_sensor_rate_sampling_enabled(uint8_t instance, bool value) {
|
|
|
|
const uint8_t bit = (1<<instance);
|
|
|
|
if (value) {
|
|
|
|
_imu._gyro_sensor_rate_sampling_enabled |= bit;
|
|
|
|
} else {
|
|
|
|
_imu._gyro_sensor_rate_sampling_enabled &= ~bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _set_raw_sample_accel_multiplier(uint8_t instance, uint16_t mul) {
|
|
|
|
_imu._accel_raw_sampling_multiplier[instance] = mul;
|
|
|
|
}
|
|
|
|
void _set_raw_sampl_gyro_multiplier(uint8_t instance, uint16_t mul) {
|
|
|
|
_imu._gyro_raw_sampling_multiplier[instance] = mul;
|
|
|
|
}
|
|
|
|
|
2017-04-30 21:53:41 -03:00
|
|
|
// update the sensor rate for FIFO sensors
|
2018-03-18 20:28:33 -03:00
|
|
|
void _update_sensor_rate(uint16_t &count, uint32_t &start_us, float &rate_hz) const;
|
2017-04-30 21:53:41 -03:00
|
|
|
|
2015-06-09 17:47:16 -03:00
|
|
|
// set accelerometer max absolute offset for calibration
|
2015-06-29 21:51:43 -03:00
|
|
|
void _set_accel_max_abs_offset(uint8_t instance, float offset);
|
2015-06-09 17:47:16 -03:00
|
|
|
|
2019-04-04 02:00:04 -03:00
|
|
|
// get accelerometer raw sample rate.
|
|
|
|
float _accel_raw_sample_rate(uint8_t instance) const {
|
AP_InertialSensor: use raw sample rate terminology
In order to avoid confusion between sample rate from sensor and sample rate
from the frontend class (AP_InertialSensor), use "raw sample rate" to refer to
the former.
The changes in the code were basically done with the following commands:
git grep -wl _accel_sample_rates | xargs sed -i "s,\<_accel_sample_rates\>,_accel_raw_sample_rates,g"
git grep -wl _set_accel_sample_rate | xargs sed -i "s,\<_set_accel_sample_rate\>,_set_accel_raw_sample_rate,g"
git grep -wl _accel_sample_rate | xargs sed -i "s,\<_accel_sample_rate\>,_accel_raw_sample_rate,g"
git grep -wl _gyro_sample_rates | xargs sed -i "s,\<_gyro_sample_rates\>,_gyro_raw_sample_rates,g"
git grep -wl _set_gyro_sample_rate | xargs sed -i "s,\<_set_gyro_sample_rate\>,_set_gyro_raw_sample_rate,g"
git grep -wl _gyro_sample_rate | xargs sed -i "s,\<_gyro_sample_rate\>,_gyro_raw_sample_rate,g"
And also with minor changes on indentation and comments.
2015-09-10 09:56:28 -03:00
|
|
|
return _imu._accel_raw_sample_rates[instance];
|
2015-08-03 16:22:41 -03:00
|
|
|
}
|
|
|
|
|
2019-04-04 02:00:04 -03:00
|
|
|
// set accelerometer raw sample rate; note that the storage type
|
|
|
|
// is actually float!
|
2018-01-17 03:17:33 -04:00
|
|
|
void _set_accel_raw_sample_rate(uint8_t instance, uint16_t rate_hz) {
|
|
|
|
_imu._accel_raw_sample_rates[instance] = rate_hz;
|
|
|
|
}
|
|
|
|
|
2015-11-15 20:05:20 -04:00
|
|
|
// get gyroscope raw sample rate
|
2019-04-04 02:00:04 -03:00
|
|
|
float _gyro_raw_sample_rate(uint8_t instance) const {
|
AP_InertialSensor: use raw sample rate terminology
In order to avoid confusion between sample rate from sensor and sample rate
from the frontend class (AP_InertialSensor), use "raw sample rate" to refer to
the former.
The changes in the code were basically done with the following commands:
git grep -wl _accel_sample_rates | xargs sed -i "s,\<_accel_sample_rates\>,_accel_raw_sample_rates,g"
git grep -wl _set_accel_sample_rate | xargs sed -i "s,\<_set_accel_sample_rate\>,_set_accel_raw_sample_rate,g"
git grep -wl _accel_sample_rate | xargs sed -i "s,\<_accel_sample_rate\>,_accel_raw_sample_rate,g"
git grep -wl _gyro_sample_rates | xargs sed -i "s,\<_gyro_sample_rates\>,_gyro_raw_sample_rates,g"
git grep -wl _set_gyro_sample_rate | xargs sed -i "s,\<_set_gyro_sample_rate\>,_set_gyro_raw_sample_rate,g"
git grep -wl _gyro_sample_rate | xargs sed -i "s,\<_gyro_sample_rate\>,_gyro_raw_sample_rate,g"
And also with minor changes on indentation and comments.
2015-09-10 09:56:28 -03:00
|
|
|
return _imu._gyro_raw_sample_rates[instance];
|
2015-09-10 08:56:34 -03:00
|
|
|
}
|
|
|
|
|
2019-04-04 02:00:04 -03:00
|
|
|
// set gyro raw sample rate; note that the storage type is
|
|
|
|
// actually float!
|
2018-01-17 03:17:33 -04:00
|
|
|
void _set_gyro_raw_sample_rate(uint8_t instance, uint16_t rate_hz) {
|
|
|
|
_imu._gyro_raw_sample_rates[instance] = rate_hz;
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:32:54 -03:00
|
|
|
// publish a temperature value
|
|
|
|
void _publish_temperature(uint8_t instance, float temperature);
|
|
|
|
|
2014-12-29 06:19:35 -04:00
|
|
|
// set accelerometer error_count
|
|
|
|
void _set_accel_error_count(uint8_t instance, uint32_t error_count);
|
|
|
|
|
|
|
|
// set gyro error_count
|
|
|
|
void _set_gyro_error_count(uint8_t instance, uint32_t error_count);
|
|
|
|
|
2016-11-10 02:14:38 -04:00
|
|
|
// increment accelerometer error_count
|
|
|
|
void _inc_accel_error_count(uint8_t instance);
|
|
|
|
|
|
|
|
// increment gyro error_count
|
|
|
|
void _inc_gyro_error_count(uint8_t instance);
|
|
|
|
|
2015-08-16 16:16:11 -03:00
|
|
|
// backend unique identifier or -1 if backend doesn't identify itself
|
|
|
|
int16_t _id = -1;
|
|
|
|
|
2014-10-16 17:52:21 -03:00
|
|
|
// return the default filter frequency in Hz for the sample rate
|
2019-05-25 07:59:57 -03:00
|
|
|
uint16_t _accel_filter_cutoff(void) const { return _imu._accel_filter_cutoff; }
|
2015-03-11 21:58:36 -03:00
|
|
|
|
|
|
|
// return the default filter frequency in Hz for the sample rate
|
2019-05-25 07:59:57 -03:00
|
|
|
uint16_t _gyro_filter_cutoff(void) const { return _imu._gyro_filter_cutoff; }
|
2014-10-16 17:52:21 -03:00
|
|
|
|
2015-03-11 20:02:36 -03:00
|
|
|
// return the requested sample rate in Hz
|
|
|
|
uint16_t get_sample_rate_hz(void) const;
|
|
|
|
|
2019-05-17 12:57:43 -03:00
|
|
|
// return the notch filter center in Hz for the sample rate
|
|
|
|
uint16_t _gyro_notch_center_freq_hz(void) const { return _imu._notch_filter.center_freq_hz(); }
|
|
|
|
|
|
|
|
// return the notch filter bandwidth in Hz for the sample rate
|
2019-05-25 07:59:57 -03:00
|
|
|
uint16_t _gyro_notch_bandwidth_hz(void) const { return _imu._notch_filter.bandwidth_hz(); }
|
2019-05-17 12:57:43 -03:00
|
|
|
|
|
|
|
// return the notch filter attenuation in dB for the sample rate
|
|
|
|
float _gyro_notch_attenuation_dB(void) const { return _imu._notch_filter.attenuation_dB(); }
|
|
|
|
|
|
|
|
uint8_t _gyro_notch_enabled(void) const { return _imu._notch_filter.enabled(); }
|
|
|
|
|
2015-11-15 20:05:20 -04:00
|
|
|
// common gyro update function for all backends
|
|
|
|
void update_gyro(uint8_t instance);
|
|
|
|
|
|
|
|
// common accel update function for all backends
|
|
|
|
void update_accel(uint8_t instance);
|
2016-01-19 21:26:31 -04:00
|
|
|
|
2015-11-15 20:05:20 -04:00
|
|
|
// support for updating filter at runtime
|
2019-05-25 07:59:57 -03:00
|
|
|
uint16_t _last_accel_filter_hz[INS_MAX_INSTANCES];
|
|
|
|
uint16_t _last_gyro_filter_hz[INS_MAX_INSTANCES];
|
2019-05-17 12:57:43 -03:00
|
|
|
uint16_t _last_notch_center_freq_hz[INS_MAX_INSTANCES];
|
2019-05-25 07:59:57 -03:00
|
|
|
uint16_t _last_notch_bandwidth_hz[INS_MAX_INSTANCES];
|
2019-05-17 12:57:43 -03:00
|
|
|
float _last_notch_attenuation_dB[INS_MAX_INSTANCES];
|
2016-01-19 21:26:31 -04:00
|
|
|
|
2016-11-03 06:19:04 -03:00
|
|
|
void set_gyro_orientation(uint8_t instance, enum Rotation rotation) {
|
|
|
|
_imu._gyro_orientation[instance] = rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_accel_orientation(uint8_t instance, enum Rotation rotation) {
|
|
|
|
_imu._accel_orientation[instance] = rotation;
|
|
|
|
}
|
2016-11-09 22:39:17 -04:00
|
|
|
|
|
|
|
// increment clipping counted. Used by drivers that do decimation before supplying
|
|
|
|
// samples to the frontend
|
|
|
|
void increment_clip_count(uint8_t instance) {
|
|
|
|
_imu._accel_clip_count[instance]++;
|
|
|
|
}
|
2016-11-15 01:51:18 -04:00
|
|
|
|
|
|
|
// should fast sampling be enabled on this IMU?
|
|
|
|
bool enable_fast_sampling(uint8_t instance) {
|
|
|
|
return (_imu._fast_sampling_mask & (1U<<instance)) != 0;
|
|
|
|
}
|
2017-04-30 21:53:41 -03:00
|
|
|
|
2018-03-18 20:28:33 -03:00
|
|
|
// called by subclass when data is received from the sensor, thus
|
|
|
|
// at the 'sensor rate'
|
|
|
|
void _notify_new_accel_sensor_rate_sample(uint8_t instance, const Vector3f &accel);
|
|
|
|
void _notify_new_gyro_sensor_rate_sample(uint8_t instance, const Vector3f &gyro);
|
2018-03-07 04:09:15 -04:00
|
|
|
|
2017-04-30 21:53:41 -03:00
|
|
|
/*
|
|
|
|
notify of a FIFO reset so we don't use bad data to update observed sensor rate
|
|
|
|
*/
|
|
|
|
void notify_accel_fifo_reset(uint8_t instance);
|
|
|
|
void notify_gyro_fifo_reset(uint8_t instance);
|
2016-11-03 06:19:04 -03:00
|
|
|
|
2014-10-15 20:32:40 -03:00
|
|
|
// note that each backend is also expected to have a static detect()
|
2014-10-14 01:48:33 -03:00
|
|
|
// function which instantiates an instance of the backend sensor
|
2014-10-15 20:32:40 -03:00
|
|
|
// driver if the sensor is available
|
2017-09-08 11:42:57 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool should_log_imu_raw() const;
|
|
|
|
void log_accel_raw(uint8_t instance, const uint64_t sample_us, const Vector3f &accel);
|
|
|
|
void log_gyro_raw(uint8_t instance, const uint64_t sample_us, const Vector3f &gryo);
|
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
};
|