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>
|
2012-10-11 21:27:19 -03:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
#include <AP_Math.h>
|
2012-12-12 17:57:34 -04:00
|
|
|
#include <AP_Progmem.h>
|
2011-11-12 23:20:25 -04:00
|
|
|
#include "AP_InertialSensor.h"
|
|
|
|
|
2012-12-27 06:28:41 -04:00
|
|
|
// enable debug to see a register dump on startup
|
|
|
|
#define MPU6000_DEBUG 0
|
|
|
|
|
2014-10-16 19:24:08 -03:00
|
|
|
// on fast CPUs we sample at 1kHz and use a software filter
|
|
|
|
#if HAL_CPU_CLASS >= HAL_CPU_CLASS_75
|
|
|
|
#define MPU6000_FAST_SAMPLING 1
|
|
|
|
#else
|
|
|
|
#define MPU6000_FAST_SAMPLING 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MPU6000_FAST_SAMPLING
|
|
|
|
#include <Filter.h>
|
|
|
|
#include <LowPassFilter2p.h>
|
|
|
|
#endif
|
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
class AP_InertialSensor_MPU6000 : public AP_InertialSensor_Backend
|
2011-11-12 23:20:25 -04:00
|
|
|
{
|
2012-08-17 03:19:57 -03:00
|
|
|
public:
|
2014-10-15 05:54:30 -03:00
|
|
|
AP_InertialSensor_MPU6000(AP_InertialSensor &imu);
|
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
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
// detect the sensor
|
2014-10-16 17:52:21 -03:00
|
|
|
static AP_InertialSensor_Backend *detect(AP_InertialSensor &imu);
|
2014-10-15 05:54:30 -03:00
|
|
|
|
2014-10-14 01:48:33 -03:00
|
|
|
private:
|
|
|
|
#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;
|
2011-12-26 16:34:18 -04:00
|
|
|
|
2014-10-16 17:52:21 -03:00
|
|
|
bool _init_sensor(void);
|
2013-12-08 05:43:53 -04:00
|
|
|
bool _sample_available();
|
2013-09-28 03:29:24 -03:00
|
|
|
void _read_data_transaction();
|
|
|
|
bool _data_ready();
|
|
|
|
void _poll_data(void);
|
|
|
|
uint8_t _register_read( uint8_t reg );
|
2013-10-29 03:42:35 -03:00
|
|
|
void _register_write( uint8_t reg, uint8_t val );
|
2014-07-03 07:09:34 -03:00
|
|
|
void _register_write_check(uint8_t reg, uint8_t val);
|
2014-10-16 17:52:21 -03:00
|
|
|
bool _hardware_init(void);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2013-09-28 03:29:24 -03:00
|
|
|
AP_HAL::SPIDeviceDriver *_spi;
|
|
|
|
AP_HAL::Semaphore *_spi_sem;
|
2012-10-11 21:27:19 -03:00
|
|
|
|
2012-08-17 03:19:57 -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-14 01:48:33 -03:00
|
|
|
// count of bus errors
|
2013-10-28 04:20:47 -03:00
|
|
|
uint16_t _error_count;
|
2013-10-29 03:42:35 -03: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;
|
|
|
|
|
|
|
|
#if MPU6000_FAST_SAMPLING
|
|
|
|
Vector3f _accel_filtered;
|
|
|
|
Vector3f _gyro_filtered;
|
|
|
|
|
|
|
|
// Low Pass filters for gyro and accel
|
2015-02-17 18:48:26 -04:00
|
|
|
LowPassFilter2pVector3f _accel_filter;
|
|
|
|
LowPassFilter2pVector3f _gyro_filter;
|
2014-10-16 19:24:08 -03:00
|
|
|
#else
|
2013-10-29 03:42:35 -03:00
|
|
|
// accumulation in timer - must be read with timer disabled
|
|
|
|
// the sum of the values since last read
|
|
|
|
Vector3l _accel_sum;
|
|
|
|
Vector3l _gyro_sum;
|
2014-10-16 19:24:08 -03:00
|
|
|
#endif
|
2014-10-14 01:48:33 -03:00
|
|
|
volatile uint16_t _sum_count;
|
2011-11-12 23:20:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // __AP_INERTIAL_SENSOR_MPU6000_H__
|