2011-12-21 00:30:22 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-11-12 23:20:25 -04:00
|
|
|
|
|
|
|
#ifndef __AP_INERTIAL_SENSOR_MPU6000_H__
|
|
|
|
#define __AP_INERTIAL_SENSOR_MPU6000_H__
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2015-10-14 10:54:59 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <AP_Progmem/AP_Progmem.h>
|
2015-10-14 10:54:59 -03:00
|
|
|
#include <Filter/Filter.h>
|
|
|
|
#include <Filter/LowPassFilter2p.h>
|
|
|
|
#include <Filter/LowPassFilter.h>
|
2015-08-16 16:23:24 -03:00
|
|
|
|
2011-11-12 23:20:25 -04:00
|
|
|
#include "AP_InertialSensor.h"
|
2015-08-16 16:23:24 -03:00
|
|
|
#include "AuxiliaryBus.h"
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2012-12-27 06:28:41 -04:00
|
|
|
// enable debug to see a register dump on startup
|
|
|
|
#define MPU6000_DEBUG 0
|
|
|
|
|
2015-09-25 11:04:49 -03:00
|
|
|
#define MPU6000_SAMPLE_SIZE 14
|
2015-07-02 14:22:36 -03:00
|
|
|
#define MPU6000_MAX_FIFO_SAMPLES 3
|
|
|
|
#define MAX_DATA_READ (MPU6000_MAX_FIFO_SAMPLES * MPU6000_SAMPLE_SIZE)
|
|
|
|
|
2015-08-16 16:23:24 -03:00
|
|
|
class AP_MPU6000_AuxiliaryBus;
|
|
|
|
class AP_MPU6000_AuxiliaryBusSlave;
|
|
|
|
|
2015-06-05 04:47:31 -03:00
|
|
|
class AP_MPU6000_BusDriver
|
|
|
|
{
|
2015-07-25 14:29:13 -03:00
|
|
|
public:
|
2015-07-28 13:43:27 -03:00
|
|
|
virtual ~AP_MPU6000_BusDriver() { };
|
2015-08-05 13:29:35 -03:00
|
|
|
virtual void init() = 0;
|
|
|
|
virtual void start(bool &fifo_mode, uint8_t &max_samples) = 0;
|
2015-07-25 14:29:13 -03:00
|
|
|
virtual void read8(uint8_t reg, uint8_t *val) = 0;
|
2015-08-16 16:06:41 -03:00
|
|
|
|
|
|
|
/// Copy data from the device to @p buf starting at @p reg with @size
|
|
|
|
/// length.
|
|
|
|
virtual void read_block(uint8_t reg, uint8_t *buf, uint32_t size) = 0;
|
2015-07-25 14:29:13 -03:00
|
|
|
virtual void write8(uint8_t reg, uint8_t val) = 0;
|
|
|
|
virtual void set_bus_speed(AP_HAL::SPIDeviceDriver::bus_speed speed) = 0;
|
2015-08-04 14:09:07 -03:00
|
|
|
virtual void read_data_transaction(uint8_t* samples,
|
2015-07-25 14:29:13 -03:00
|
|
|
AP_HAL::DigitalSource *_drdy_pin,
|
|
|
|
uint8_t &n_samples) = 0;
|
|
|
|
virtual AP_HAL::Semaphore* get_semaphore() = 0;
|
2015-10-02 15:02:16 -03:00
|
|
|
virtual bool has_auxiliary_bus() = 0;
|
2015-06-05 04:47:31 -03:00
|
|
|
};
|
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
class AP_InertialSensor_MPU6000 : public AP_InertialSensor_Backend
|
2011-11-12 23:20:25 -04:00
|
|
|
{
|
2015-08-16 16:23:24 -03:00
|
|
|
friend AP_MPU6000_AuxiliaryBus;
|
|
|
|
friend AP_MPU6000_AuxiliaryBusSlave;
|
|
|
|
|
2012-08-17 03:19:57 -03:00
|
|
|
public:
|
2015-06-15 09:58:30 -03:00
|
|
|
AP_InertialSensor_MPU6000(AP_InertialSensor &imu, AP_MPU6000_BusDriver *bus);
|
2015-07-28 13:43:27 -03:00
|
|
|
~AP_InertialSensor_MPU6000();
|
2015-07-28 11:56:34 -03:00
|
|
|
static AP_InertialSensor_Backend *detect_i2c(AP_InertialSensor &_imu,
|
|
|
|
AP_HAL::I2CDriver *i2c,
|
|
|
|
uint8_t addr);
|
2015-07-02 14:22:36 -03:00
|
|
|
static AP_InertialSensor_Backend *detect_spi(AP_InertialSensor &_imu);
|
2015-09-13 15:28:02 -03:00
|
|
|
static AP_InertialSensor_MPU6000 &from(AP_InertialSensor_Backend &backend) {
|
|
|
|
return static_cast<AP_InertialSensor_MPU6000&>(backend);
|
|
|
|
}
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
/* update accel and gyro state */
|
|
|
|
bool update();
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
bool gyro_sample_available(void) { return _sum_count >= _sample_count; }
|
|
|
|
bool accel_sample_available(void) { return _sum_count >= _sample_count; }
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2015-08-16 16:23:24 -03:00
|
|
|
/*
|
|
|
|
* Return an AuxiliaryBus if the bus driver allows it
|
|
|
|
*/
|
2015-10-02 15:02:16 -03:00
|
|
|
AuxiliaryBus *get_auxiliary_bus() override;
|
2015-08-16 16:23:24 -03:00
|
|
|
|
2015-08-05 13:29:35 -03:00
|
|
|
void start() override;
|
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
private:
|
2015-07-28 13:43:27 -03:00
|
|
|
static AP_InertialSensor_Backend *_detect(AP_InertialSensor &_imu,
|
2015-08-05 13:29:35 -03:00
|
|
|
AP_MPU6000_BusDriver *bus,
|
|
|
|
int16_t id = -1);
|
2015-07-28 13:43:27 -03:00
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
#if MPU6000_DEBUG
|
|
|
|
void _dump_registers(void);
|
|
|
|
#endif
|
2013-10-29 18:54:03 -03:00
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
// instance numbers of accel and gyro data
|
|
|
|
uint8_t _gyro_instance;
|
|
|
|
uint8_t _accel_instance;
|
2012-11-07 02:20:22 -04:00
|
|
|
|
2013-10-29 03:42:35 -03:00
|
|
|
AP_HAL::DigitalSource *_drdy_pin;
|
2015-07-25 14:29:13 -03:00
|
|
|
bool _init_sensor(void);
|
|
|
|
bool _sample_available();
|
|
|
|
void _read_data_transaction();
|
|
|
|
bool _data_ready();
|
|
|
|
void _poll_data(void);
|
2015-08-16 16:06:41 -03:00
|
|
|
void _read_block(uint8_t reg, uint8_t *buf, uint32_t size);
|
2015-07-25 14:29:13 -03:00
|
|
|
uint8_t _register_read( uint8_t reg);
|
|
|
|
void _register_write( uint8_t reg, uint8_t val );
|
|
|
|
void _register_write_check(uint8_t reg, uint8_t val);
|
|
|
|
bool _hardware_init(void);
|
|
|
|
void _accumulate(uint8_t *samples, uint8_t n_samples);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2015-06-05 04:47:31 -03:00
|
|
|
AP_MPU6000_BusDriver *_bus;
|
2015-07-02 14:22:36 -03:00
|
|
|
AP_HAL::Semaphore *_bus_sem;
|
2012-10-11 21:27:19 -03:00
|
|
|
|
2015-10-02 15:02:16 -03:00
|
|
|
AP_MPU6000_AuxiliaryBus *_auxiliary_bus = nullptr;
|
2015-08-16 16:23:24 -03:00
|
|
|
|
2015-07-25 14:29:13 -03:00
|
|
|
static const float _gyro_scale;
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2013-02-06 19:23:08 -04:00
|
|
|
// support for updating filter at runtime
|
2015-03-11 21:58:36 -03:00
|
|
|
int8_t _last_accel_filter_hz;
|
|
|
|
int8_t _last_gyro_filter_hz;
|
2013-02-06 19:23:08 -04:00
|
|
|
|
2015-03-11 21:58:36 -03:00
|
|
|
void _set_filter_register(uint16_t filter_hz);
|
2013-02-06 19:23:08 -04:00
|
|
|
|
2014-10-16 19:24:08 -03:00
|
|
|
// how many hardware samples before we report a sample to the caller
|
|
|
|
uint8_t _sample_count;
|
|
|
|
|
|
|
|
Vector3f _accel_filtered;
|
|
|
|
Vector3f _gyro_filtered;
|
2015-09-25 11:04:49 -03:00
|
|
|
float _temp_filtered;
|
2014-10-16 19:24:08 -03:00
|
|
|
|
2015-07-25 14:29:13 -03:00
|
|
|
// Low Pass filters for gyro and accel
|
2015-02-17 18:48:26 -04:00
|
|
|
LowPassFilter2pVector3f _accel_filter;
|
|
|
|
LowPassFilter2pVector3f _gyro_filter;
|
2015-09-25 11:04:49 -03:00
|
|
|
LowPassFilter2pFloat _temp_filter;
|
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
volatile uint16_t _sum_count;
|
2015-07-02 14:22:36 -03:00
|
|
|
bool _fifo_mode;
|
2015-08-04 01:24:27 -03:00
|
|
|
uint8_t *_samples = nullptr;
|
2011-11-12 23:20:25 -04:00
|
|
|
};
|
|
|
|
|
2015-06-05 04:47:31 -03:00
|
|
|
class AP_MPU6000_BusDriver_SPI : public AP_MPU6000_BusDriver
|
|
|
|
{
|
2015-07-25 14:29:13 -03:00
|
|
|
public:
|
|
|
|
AP_MPU6000_BusDriver_SPI(void);
|
2015-08-05 13:29:35 -03:00
|
|
|
void init();
|
|
|
|
void start(bool &fifo_mode, uint8_t &max_samples);
|
2015-07-25 14:29:13 -03:00
|
|
|
void read8(uint8_t reg, uint8_t *val);
|
2015-08-16 16:06:41 -03:00
|
|
|
void read_block(uint8_t reg, uint8_t *buf, uint32_t size) override;
|
2015-07-25 14:29:13 -03:00
|
|
|
void write8(uint8_t reg, uint8_t val);
|
|
|
|
void set_bus_speed(AP_HAL::SPIDeviceDriver::bus_speed speed);
|
2015-08-04 14:09:07 -03:00
|
|
|
void read_data_transaction(uint8_t* samples,
|
|
|
|
AP_HAL::DigitalSource *_drdy_pin,
|
|
|
|
uint8_t &n_samples);
|
2015-07-25 14:29:13 -03:00
|
|
|
AP_HAL::Semaphore* get_semaphore();
|
2015-10-02 15:02:16 -03:00
|
|
|
bool has_auxiliary_bus() override;
|
2015-07-25 14:29:13 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
AP_HAL::SPIDeviceDriver *_spi;
|
|
|
|
AP_HAL::Semaphore *_spi_sem;
|
|
|
|
// count of bus errors
|
|
|
|
uint16_t _error_count;
|
2015-06-05 04:47:31 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
class AP_MPU6000_BusDriver_I2C : public AP_MPU6000_BusDriver
|
|
|
|
{
|
2015-07-25 14:29:13 -03:00
|
|
|
public:
|
|
|
|
AP_MPU6000_BusDriver_I2C(AP_HAL::I2CDriver *i2c, uint8_t addr);
|
2015-08-05 13:29:35 -03:00
|
|
|
void init();
|
|
|
|
void start(bool &fifo_mode, uint8_t &max_samples);
|
2015-07-25 14:29:13 -03:00
|
|
|
void read8(uint8_t reg, uint8_t *val);
|
2015-08-16 16:06:41 -03:00
|
|
|
void read_block(uint8_t reg, uint8_t *buf, uint32_t size) override;
|
2015-07-25 14:29:13 -03:00
|
|
|
void write8(uint8_t reg, uint8_t val);
|
|
|
|
void set_bus_speed(AP_HAL::SPIDeviceDriver::bus_speed speed);
|
2015-08-04 14:09:07 -03:00
|
|
|
void read_data_transaction(uint8_t* samples,
|
|
|
|
AP_HAL::DigitalSource *_drdy_pin,
|
|
|
|
uint8_t &n_samples);
|
2015-07-25 14:29:13 -03:00
|
|
|
AP_HAL::Semaphore* get_semaphore();
|
2015-10-02 15:02:16 -03:00
|
|
|
bool has_auxiliary_bus() override;
|
2015-07-25 14:29:13 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t _addr;
|
|
|
|
AP_HAL::I2CDriver *_i2c;
|
|
|
|
AP_HAL::Semaphore *_i2c_sem;
|
|
|
|
uint8_t _rx[MAX_DATA_READ];
|
2015-06-05 04:47:31 -03:00
|
|
|
};
|
|
|
|
|
2015-08-16 16:23:24 -03:00
|
|
|
|
|
|
|
class AP_MPU6000_AuxiliaryBusSlave : public AuxiliaryBusSlave
|
|
|
|
{
|
|
|
|
friend class AP_MPU6000_AuxiliaryBus;
|
|
|
|
|
|
|
|
public:
|
|
|
|
int passthrough_read(uint8_t reg, uint8_t *buf, uint8_t size) override;
|
|
|
|
int passthrough_write(uint8_t reg, uint8_t val) override;
|
|
|
|
|
|
|
|
int read(uint8_t *buf) override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AP_MPU6000_AuxiliaryBusSlave(AuxiliaryBus &bus, uint8_t addr, uint8_t instance);
|
|
|
|
int _set_passthrough(uint8_t reg, uint8_t size, uint8_t *out = nullptr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
const uint8_t _mpu6000_addr;
|
|
|
|
const uint8_t _mpu6000_reg;
|
|
|
|
const uint8_t _mpu6000_ctrl;
|
|
|
|
const uint8_t _mpu6000_do;
|
|
|
|
|
|
|
|
uint8_t _ext_sens_data = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AP_MPU6000_AuxiliaryBus : public AuxiliaryBus
|
|
|
|
{
|
|
|
|
friend class AP_InertialSensor_MPU6000;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AP_HAL::Semaphore *get_semaphore() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AP_MPU6000_AuxiliaryBus(AP_InertialSensor_MPU6000 &backend);
|
|
|
|
|
|
|
|
AuxiliaryBusSlave *_instantiate_slave(uint8_t addr, uint8_t instance) override;
|
|
|
|
int _configure_periodic_read(AuxiliaryBusSlave *slave, uint8_t reg,
|
|
|
|
uint8_t size) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _configure_slaves();
|
|
|
|
|
|
|
|
static const uint8_t MAX_EXT_SENS_DATA = 24;
|
|
|
|
uint8_t _ext_sens_data = 0;
|
|
|
|
};
|
|
|
|
|
2011-11-12 23:20:25 -04:00
|
|
|
#endif // __AP_INERTIAL_SENSOR_MPU6000_H__
|