2016-01-19 21:26:31 -04:00
|
|
|
#pragma once
|
2014-05-01 17:41:22 -03:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2016-01-19 21:26:31 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2016-01-20 18:20:35 -04:00
|
|
|
#include <AP_HAL/I2CDevice.h>
|
|
|
|
#include <AP_HAL/SPIDevice.h>
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <Filter/Filter.h>
|
|
|
|
#include <Filter/LowPassFilter2p.h>
|
2016-01-19 21:26:31 -04:00
|
|
|
|
|
|
|
#include "AP_InertialSensor_Backend.h"
|
2014-05-01 17:41:22 -03:00
|
|
|
#include "AP_InertialSensor.h"
|
2015-09-28 10:50:30 -03:00
|
|
|
#include "AuxiliaryBus.h"
|
|
|
|
|
|
|
|
class AP_MPU9250_AuxiliaryBus;
|
|
|
|
class AP_MPU9250_AuxiliaryBusSlave;
|
2014-05-01 17:41:22 -03:00
|
|
|
|
2014-10-15 20:31:06 -03:00
|
|
|
class AP_InertialSensor_MPU9250 : public AP_InertialSensor_Backend
|
2014-05-01 17:41:22 -03:00
|
|
|
{
|
2015-09-28 10:50:30 -03:00
|
|
|
friend AP_MPU9250_AuxiliaryBus;
|
|
|
|
friend AP_MPU9250_AuxiliaryBusSlave;
|
|
|
|
|
2014-05-01 17:41:22 -03:00
|
|
|
public:
|
2016-01-20 18:20:35 -04:00
|
|
|
virtual ~AP_InertialSensor_MPU9250();
|
|
|
|
|
|
|
|
static AP_InertialSensor_MPU9250 &from(AP_InertialSensor_Backend &backend) {
|
|
|
|
return static_cast<AP_InertialSensor_MPU9250&>(backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
static AP_InertialSensor_Backend *probe(AP_InertialSensor &imu,
|
2016-11-05 02:03:31 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
|
|
|
|
enum Rotation rotation = ROTATION_NONE);
|
2014-05-01 17:41:22 -03:00
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
static AP_InertialSensor_Backend *probe(AP_InertialSensor &imu,
|
2016-11-05 02:03:31 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev,
|
|
|
|
enum Rotation rotation = ROTATION_NONE);
|
2014-05-01 17:41:22 -03:00
|
|
|
|
2014-10-15 20:31:06 -03:00
|
|
|
/* update accel and gyro state */
|
2016-11-21 01:49:16 -04:00
|
|
|
bool update() override;
|
|
|
|
void accumulate() override;
|
2014-05-01 17:41:22 -03:00
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
/*
|
|
|
|
* Return an AuxiliaryBus if the bus driver allows it
|
|
|
|
*/
|
|
|
|
AuxiliaryBus *get_auxiliary_bus() override;
|
2015-09-28 10:50:30 -03:00
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
void start() override;
|
2014-05-01 17:41:22 -03:00
|
|
|
|
|
|
|
private:
|
2016-01-20 18:20:35 -04:00
|
|
|
AP_InertialSensor_MPU9250(AP_InertialSensor &imu,
|
2016-11-05 02:03:31 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> dev,
|
|
|
|
enum Rotation rotation);
|
2015-06-08 17:06:17 -03:00
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
bool _init();
|
|
|
|
bool _hardware_init();
|
2015-10-02 14:55:38 -03:00
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
void _set_filter_register(uint16_t filter_hz);
|
|
|
|
bool _has_auxiliary_bus();
|
2015-10-02 14:55:38 -03:00
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
/* Read a single sample */
|
2016-11-03 21:06:19 -03:00
|
|
|
bool _read_sample();
|
2015-09-28 13:48:18 -03:00
|
|
|
|
2016-11-09 04:39:28 -04:00
|
|
|
void _fifo_reset();
|
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
/* Check if there's data available by reading register */
|
|
|
|
bool _data_ready();
|
|
|
|
bool _data_ready(uint8_t int_status);
|
2015-09-28 13:48:18 -03:00
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
/* Read and write functions taking the differences between buses into
|
|
|
|
* account */
|
|
|
|
bool _block_read(uint8_t reg, uint8_t *buf, uint32_t size);
|
|
|
|
uint8_t _register_read(uint8_t reg);
|
2016-11-10 02:27:06 -04:00
|
|
|
void _register_write(uint8_t reg, uint8_t val, bool checked=false);
|
2015-09-28 10:50:30 -03:00
|
|
|
|
2016-11-26 04:02:22 -04:00
|
|
|
bool _accumulate(uint8_t *samples, uint8_t n_samples);
|
|
|
|
bool _accumulate_fast_sampling(uint8_t *samples, uint8_t n_samples);
|
2015-09-28 10:50:30 -03:00
|
|
|
|
2016-11-26 04:02:22 -04:00
|
|
|
bool _check_raw_temp(int16_t t2);
|
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
// instance numbers of accel and gyro data
|
|
|
|
uint8_t _gyro_instance;
|
|
|
|
uint8_t _accel_instance;
|
2015-09-28 10:50:30 -03:00
|
|
|
|
2016-11-09 04:39:28 -04:00
|
|
|
float _temp_filtered;
|
|
|
|
LowPassFilter2pFloat _temp_filter;
|
|
|
|
|
2016-01-20 18:20:35 -04:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> _dev;
|
|
|
|
AP_MPU9250_AuxiliaryBus *_auxiliary_bus;
|
2016-11-05 02:03:31 -03:00
|
|
|
|
|
|
|
enum Rotation _rotation;
|
2016-11-09 04:39:28 -04:00
|
|
|
|
|
|
|
// are we doing more than 1kHz sampling?
|
|
|
|
bool _fast_sampling;
|
2016-11-09 04:54:53 -04:00
|
|
|
|
2016-11-23 02:02:39 -04:00
|
|
|
// Last status from register user control
|
|
|
|
uint8_t _last_stat_user_ctrl;
|
|
|
|
|
2016-11-09 04:39:28 -04:00
|
|
|
// buffer for fifo read
|
|
|
|
uint8_t *_fifo_buffer;
|
2016-11-10 02:27:06 -04:00
|
|
|
|
2016-11-26 04:02:22 -04:00
|
|
|
int16_t _raw_temp;
|
|
|
|
|
2016-11-21 01:49:16 -04:00
|
|
|
/*
|
|
|
|
accumulators for fast sampling
|
|
|
|
See description in _accumulate_fast_sampling()
|
|
|
|
*/
|
2016-11-15 01:51:18 -04:00
|
|
|
struct {
|
2016-11-21 01:49:16 -04:00
|
|
|
Vector3f accel;
|
|
|
|
Vector3f gyro;
|
2016-11-15 01:51:18 -04:00
|
|
|
uint8_t count;
|
2016-11-21 01:49:16 -04:00
|
|
|
LowPassFilterVector3f accel_filter{8000, 188};
|
|
|
|
LowPassFilterVector3f gyro_filter{8000, 188};
|
2016-11-15 01:51:18 -04:00
|
|
|
} _accum;
|
2015-09-28 10:50:30 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
class AP_MPU9250_AuxiliaryBusSlave : public AuxiliaryBusSlave
|
|
|
|
{
|
|
|
|
friend class AP_MPU9250_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_MPU9250_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 _mpu9250_addr;
|
|
|
|
const uint8_t _mpu9250_reg;
|
|
|
|
const uint8_t _mpu9250_ctrl;
|
|
|
|
const uint8_t _mpu9250_do;
|
|
|
|
|
|
|
|
uint8_t _ext_sens_data = 0;
|
|
|
|
};
|
2016-01-20 18:20:35 -04:00
|
|
|
|
|
|
|
class AP_MPU9250_AuxiliaryBus : public AuxiliaryBus
|
|
|
|
{
|
|
|
|
friend class AP_InertialSensor_MPU9250;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AP_HAL::Semaphore *get_semaphore() override;
|
|
|
|
|
|
|
|
protected:
|
2016-11-04 06:24:24 -03:00
|
|
|
AP_MPU9250_AuxiliaryBus(AP_InertialSensor_MPU9250 &backend, uint32_t devid);
|
2016-01-20 18:20:35 -04:00
|
|
|
|
|
|
|
AuxiliaryBusSlave *_instantiate_slave(uint8_t addr, uint8_t instance);
|
|
|
|
int _configure_periodic_read(AuxiliaryBusSlave *slave, uint8_t reg,
|
|
|
|
uint8_t size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _configure_slaves();
|
|
|
|
|
|
|
|
static const uint8_t MAX_EXT_SENS_DATA = 24;
|
|
|
|
uint8_t _ext_sens_data = 0;
|
|
|
|
};
|