2013-09-23 00:32:19 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
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 21:24:32 -03:00
|
|
|
Flymaple IMU driver by Mike McCauley
|
2013-09-23 00:32:19 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Interface to the Flymaple sensors:
|
2013-09-25 21:15:31 -03:00
|
|
|
// ITG3205 Gyroscope http://www.sparkfun.com/datasheets/Sensors/Gyro/PS-ITG-3200-00-01.4.pdf
|
|
|
|
// ADXL345 Accelerometer http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
|
2013-09-23 00:32:19 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-09-23 00:32:19 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_FLYMAPLE
|
2013-09-27 02:31:28 -03:00
|
|
|
|
2013-09-23 00:32:19 -03:00
|
|
|
#include "AP_InertialSensor_Flymaple.h"
|
|
|
|
|
|
|
|
const extern AP_HAL::HAL& hal;
|
|
|
|
|
2013-09-27 02:31:28 -03:00
|
|
|
// This is how often we wish to make raw samples of the sensors in Hz
|
|
|
|
const uint32_t raw_sample_rate_hz = 800;
|
|
|
|
// And the equivalent time between samples in microseconds
|
|
|
|
const uint32_t raw_sample_interval_us = (1000000 / raw_sample_rate_hz);
|
2013-09-23 00:32:19 -03:00
|
|
|
|
|
|
|
///////
|
2013-09-25 21:15:31 -03:00
|
|
|
/// Accelerometer ADXL345 register definitions
|
2013-09-23 00:32:19 -03:00
|
|
|
#define FLYMAPLE_ACCELEROMETER_ADDRESS 0x53
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_XL345_DEVID 0xe5
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_ADXLREG_BW_RATE 0x2c
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_ADXLREG_POWER_CTL 0x2d
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_ADXLREG_DATA_FORMAT 0x31
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_ADXLREG_DEVID 0x00
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_ADXLREG_DATAX0 0x32
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_GRAVITY 248
|
|
|
|
|
2013-09-25 21:15:31 -03:00
|
|
|
// ADXL345 accelerometer scaling
|
|
|
|
// Result will be scaled to 1m/s/s
|
|
|
|
// ADXL345 in Full resolution mode (any g scaling) is 256 counts/g, so scale by 9.81/256 = 0.038320312
|
|
|
|
#define FLYMAPLE_ACCELEROMETER_SCALE_M_S (GRAVITY_MSS / 256.0f)
|
|
|
|
|
|
|
|
/// Gyro ITG3205 register definitions
|
2013-09-23 00:32:19 -03:00
|
|
|
#define FLYMAPLE_GYRO_ADDRESS 0x68
|
2013-09-25 21:15:31 -03:00
|
|
|
#define FLYMAPLE_GYRO_WHO_AM_I 0x00
|
2013-09-23 00:32:19 -03:00
|
|
|
#define FLYMAPLE_GYRO_PWR_MGM 0x3e
|
|
|
|
#define FLYMAPLE_GYRO_DLPF_FS 0x16
|
|
|
|
#define FLYMAPLE_GYRO_INT_CFG 0x17
|
|
|
|
#define FLYMAPLE_GYRO_SMPLRT_DIV 0x15
|
|
|
|
#define FLYMAPLE_GYRO_GYROX_H 0x1d
|
|
|
|
|
2013-09-25 21:15:31 -03:00
|
|
|
// ITG3200 Gyroscope scaling
|
|
|
|
// ITG3200 is 14.375 LSB degrees/sec with FS_SEL=3
|
|
|
|
// Result wil be radians/sec
|
|
|
|
#define FLYMAPLE_GYRO_SCALE_R_S (1.0f / 14.375f) * (3.1415926f / 180.0f)
|
|
|
|
|
2014-10-15 21:24:32 -03:00
|
|
|
AP_InertialSensor_Flymaple::AP_InertialSensor_Flymaple(AP_InertialSensor &imu) :
|
|
|
|
AP_InertialSensor_Backend(imu),
|
|
|
|
_have_gyro_sample(false),
|
|
|
|
_have_accel_sample(false),
|
2015-02-17 18:48:26 -04:00
|
|
|
_accel_filter(raw_sample_rate_hz, 10),
|
|
|
|
_gyro_filter(raw_sample_rate_hz, 10),
|
2014-10-15 21:24:32 -03:00
|
|
|
_last_gyro_timestamp(0),
|
|
|
|
_last_accel_timestamp(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/*
|
|
|
|
detect the sensor
|
|
|
|
*/
|
2014-10-16 17:52:21 -03:00
|
|
|
AP_InertialSensor_Backend *AP_InertialSensor_Flymaple::detect(AP_InertialSensor &_imu)
|
2014-10-15 21:24:32 -03:00
|
|
|
{
|
|
|
|
AP_InertialSensor_Flymaple *sensor = new AP_InertialSensor_Flymaple(_imu);
|
|
|
|
if (sensor == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-16 17:52:21 -03:00
|
|
|
if (!sensor->_init_sensor()) {
|
2014-10-15 21:24:32 -03:00
|
|
|
delete sensor;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
2014-10-16 17:52:21 -03:00
|
|
|
bool AP_InertialSensor_Flymaple::_init_sensor(void)
|
2013-09-23 00:32:19 -03:00
|
|
|
{
|
2013-10-03 04:40:24 -03:00
|
|
|
// get pointer to i2c bus semaphore
|
|
|
|
AP_HAL::Semaphore* i2c_sem = hal.i2c->get_semaphore();
|
|
|
|
|
|
|
|
// take i2c bus sempahore
|
|
|
|
if (!i2c_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER))
|
|
|
|
return false;
|
|
|
|
|
2013-09-23 00:32:19 -03:00
|
|
|
// Init the accelerometer
|
|
|
|
uint8_t data;
|
|
|
|
hal.i2c->readRegister(FLYMAPLE_ACCELEROMETER_ADDRESS, FLYMAPLE_ACCELEROMETER_ADXLREG_DEVID, &data);
|
|
|
|
if (data != FLYMAPLE_ACCELEROMETER_XL345_DEVID)
|
2015-10-24 18:45:41 -03:00
|
|
|
hal.scheduler->panic("AP_InertialSensor_Flymaple: could not find ADXL345 accelerometer sensor");
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.i2c->writeRegister(FLYMAPLE_ACCELEROMETER_ADDRESS, FLYMAPLE_ACCELEROMETER_ADXLREG_POWER_CTL, 0x00);
|
|
|
|
hal.scheduler->delay(5);
|
|
|
|
hal.i2c->writeRegister(FLYMAPLE_ACCELEROMETER_ADDRESS, FLYMAPLE_ACCELEROMETER_ADXLREG_POWER_CTL, 0xff);
|
|
|
|
hal.scheduler->delay(5);
|
2013-09-25 21:15:31 -03:00
|
|
|
// Measure mode:
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.i2c->writeRegister(FLYMAPLE_ACCELEROMETER_ADDRESS, FLYMAPLE_ACCELEROMETER_ADXLREG_POWER_CTL, 0x08);
|
|
|
|
hal.scheduler->delay(5);
|
2013-09-25 21:15:31 -03:00
|
|
|
// Full resolution, 8g:
|
|
|
|
// Caution, this must agree with FLYMAPLE_ACCELEROMETER_SCALE_1G
|
|
|
|
// In full resoution mode, the scale factor need not change
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.i2c->writeRegister(FLYMAPLE_ACCELEROMETER_ADDRESS, FLYMAPLE_ACCELEROMETER_ADXLREG_DATA_FORMAT, 0x08);
|
|
|
|
hal.scheduler->delay(5);
|
2013-09-27 02:31:28 -03:00
|
|
|
// Normal power, 800Hz Output Data Rate, 400Hz bandwidth:
|
|
|
|
hal.i2c->writeRegister(FLYMAPLE_ACCELEROMETER_ADDRESS, FLYMAPLE_ACCELEROMETER_ADXLREG_BW_RATE, 0x0d);
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.scheduler->delay(5);
|
2013-09-25 21:15:31 -03:00
|
|
|
// Power up default is FIFO bypass mode. FIFO is not used by the chip
|
2013-09-23 00:32:19 -03:00
|
|
|
|
2013-09-25 21:15:31 -03:00
|
|
|
// Init the Gyro
|
|
|
|
// Expect to read the same as the Gyro I2C adress:
|
|
|
|
hal.i2c->readRegister(FLYMAPLE_GYRO_ADDRESS, FLYMAPLE_GYRO_WHO_AM_I, &data);
|
|
|
|
if (data != FLYMAPLE_GYRO_ADDRESS)
|
2015-10-24 18:45:41 -03:00
|
|
|
hal.scheduler->panic("AP_InertialSensor_Flymaple: could not find ITG-3200 accelerometer sensor");
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.i2c->writeRegister(FLYMAPLE_GYRO_ADDRESS, FLYMAPLE_GYRO_PWR_MGM, 0x00);
|
|
|
|
hal.scheduler->delay(1);
|
2013-09-25 21:15:31 -03:00
|
|
|
// Sample rate divider: with 8kHz internal clock (see FLYMAPLE_GYRO_DLPF_FS),
|
|
|
|
// get 500Hz sample rate, 2 samples
|
|
|
|
hal.i2c->writeRegister(FLYMAPLE_GYRO_ADDRESS, FLYMAPLE_GYRO_SMPLRT_DIV, 0x0f);
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.scheduler->delay(1);
|
2013-09-25 21:15:31 -03:00
|
|
|
// 2000 degrees/sec, 256Hz LPF, 8kHz internal sample rate
|
2013-09-27 02:31:28 -03:00
|
|
|
// This is the least amount of filtering we can configure for this device
|
2013-09-25 21:15:31 -03:00
|
|
|
hal.i2c->writeRegister(FLYMAPLE_GYRO_ADDRESS, FLYMAPLE_GYRO_DLPF_FS, 0x18);
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.scheduler->delay(1);
|
2013-09-25 21:15:31 -03:00
|
|
|
// No interrupts
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.i2c->writeRegister(FLYMAPLE_GYRO_ADDRESS, FLYMAPLE_GYRO_INT_CFG, 0x00);
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2013-09-27 02:31:28 -03:00
|
|
|
// Set up the filter desired
|
2015-03-11 21:58:36 -03:00
|
|
|
_set_filter_frequency(_accel_filter_cutoff());
|
2013-09-25 21:15:31 -03:00
|
|
|
|
2014-10-15 21:24:32 -03:00
|
|
|
// give back i2c semaphore
|
2013-10-03 04:40:24 -03:00
|
|
|
i2c_sem->give();
|
|
|
|
|
2014-10-15 21:24:32 -03:00
|
|
|
_gyro_instance = _imu.register_gyro();
|
|
|
|
_accel_instance = _imu.register_accel();
|
|
|
|
|
2014-10-15 23:14:56 -03:00
|
|
|
_product_id = AP_PRODUCT_ID_FLYMAPLE;
|
|
|
|
|
2014-10-15 21:24:32 -03:00
|
|
|
return true;
|
2013-09-23 00:32:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set the filter frequency
|
|
|
|
*/
|
|
|
|
void AP_InertialSensor_Flymaple::_set_filter_frequency(uint8_t filter_hz)
|
|
|
|
{
|
2015-02-17 18:48:26 -04:00
|
|
|
_accel_filter.set_cutoff_frequency(raw_sample_rate_hz, filter_hz);
|
|
|
|
_gyro_filter.set_cutoff_frequency(raw_sample_rate_hz, filter_hz);
|
2013-09-23 00:32:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-27 02:31:28 -03:00
|
|
|
// This takes about 20us to run
|
2013-09-23 00:32:19 -03:00
|
|
|
bool AP_InertialSensor_Flymaple::update(void)
|
|
|
|
{
|
2014-10-15 21:24:32 -03:00
|
|
|
Vector3f accel, gyro;
|
2013-09-23 00:32:19 -03:00
|
|
|
|
|
|
|
hal.scheduler->suspend_timer_procs();
|
2014-10-15 21:24:32 -03:00
|
|
|
accel = _accel_filtered;
|
|
|
|
gyro = _gyro_filtered;
|
|
|
|
_have_gyro_sample = false;
|
|
|
|
_have_accel_sample = false;
|
2013-09-23 00:32:19 -03:00
|
|
|
hal.scheduler->resume_timer_procs();
|
|
|
|
|
2015-08-28 12:18:09 -03:00
|
|
|
_publish_accel(_accel_instance, accel);
|
|
|
|
_publish_gyro(_gyro_instance, gyro);
|
2013-09-23 00:32:19 -03:00
|
|
|
|
2015-03-11 21:58:36 -03:00
|
|
|
if (_last_filter_hz != _accel_filter_cutoff()) {
|
|
|
|
_set_filter_frequency(_accel_filter_cutoff());
|
|
|
|
_last_filter_hz = _accel_filter_cutoff();
|
2013-09-23 00:32:19 -03:00
|
|
|
}
|
2013-09-27 02:31:28 -03:00
|
|
|
|
2013-09-23 00:32:19 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:42:39 -03:00
|
|
|
// This needs to get called as often as possible.
|
|
|
|
// Its job is to accumulate samples as fast as is reasonable for the accel and gyro
|
|
|
|
// sensors.
|
2014-10-15 21:24:32 -03:00
|
|
|
// Note that this is called from gyro_sample_available() and
|
|
|
|
// accel_sample_available(), which is really not good enough for
|
|
|
|
// 800Hz, as it is common for the main loop to take more than 1.5ms
|
|
|
|
// before wait_for_sample() is called again. We can't just call this
|
|
|
|
// from a timer as timers run with interrupts disabled, and the I2C
|
|
|
|
// operations take too long
|
|
|
|
// So we are stuck with a suboptimal solution. The results are not so
|
|
|
|
// good in terms of timing. It may be better with the FIFOs enabled
|
2013-09-23 00:32:19 -03:00
|
|
|
void AP_InertialSensor_Flymaple::_accumulate(void)
|
|
|
|
{
|
2013-10-03 04:40:24 -03:00
|
|
|
// get pointer to i2c bus semaphore
|
|
|
|
AP_HAL::Semaphore* i2c_sem = hal.i2c->get_semaphore();
|
2013-09-23 00:32:19 -03:00
|
|
|
|
2013-10-03 04:40:24 -03:00
|
|
|
// take i2c bus sempahore
|
|
|
|
if (!i2c_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER))
|
|
|
|
return;
|
2013-09-26 22:42:39 -03:00
|
|
|
|
2013-09-23 00:32:19 -03:00
|
|
|
// Read accelerometer
|
2013-09-25 21:15:31 -03:00
|
|
|
// ADXL345 is in the default FIFO bypass mode, so the FIFO is not used
|
|
|
|
uint8_t buffer[6];
|
2014-10-15 21:24:32 -03:00
|
|
|
uint32_t now = hal.scheduler->micros();
|
2013-10-11 20:19:45 -03:00
|
|
|
// This takes about 250us at 400kHz I2C speed
|
2013-10-03 04:40:24 -03:00
|
|
|
if ((now - _last_accel_timestamp) >= raw_sample_interval_us
|
2013-09-26 22:42:39 -03:00
|
|
|
&& hal.i2c->readRegisters(FLYMAPLE_ACCELEROMETER_ADDRESS, FLYMAPLE_ACCELEROMETER_ADXLREG_DATAX0, 6, buffer) == 0)
|
2013-09-23 00:32:19 -03:00
|
|
|
{
|
2013-09-24 08:57:19 -03:00
|
|
|
// The order is a bit wierd here since the standard we have adopted for Flymaple
|
|
|
|
// sensor orientation is different to what the board designers intended
|
|
|
|
// Caution, to support alternative chip orientations on other bords, may
|
|
|
|
// need to add a chip orientation rotate
|
2013-09-24 19:56:01 -03:00
|
|
|
int16_t y = -((((int16_t)buffer[1]) << 8) | buffer[0]); // chip X axis
|
|
|
|
int16_t x = -((((int16_t)buffer[3]) << 8) | buffer[2]); // chip Y axis
|
2013-09-24 08:57:19 -03:00
|
|
|
int16_t z = -((((int16_t)buffer[5]) << 8) | buffer[4]); // chip Z axis
|
2015-08-28 11:05:59 -03:00
|
|
|
Vector3f accel = Vector3f(x,y,z);
|
|
|
|
// Adjust for chip scaling to get m/s/s
|
|
|
|
accel *= FLYMAPLE_ACCELEROMETER_SCALE_M_S;
|
|
|
|
_rotate_and_correct_accel(_accel_instance, accel);
|
2015-08-27 16:05:13 -03:00
|
|
|
_notify_new_accel_raw_sample(_accel_instance, accel);
|
2015-08-28 11:05:59 -03:00
|
|
|
_accel_filtered = _accel_filter.apply(accel);
|
2014-10-15 21:24:32 -03:00
|
|
|
_have_accel_sample = true;
|
2013-09-26 22:42:39 -03:00
|
|
|
_last_accel_timestamp = now;
|
2013-09-23 00:32:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read gyro
|
2013-09-26 22:42:39 -03:00
|
|
|
now = hal.scheduler->micros();
|
2013-10-11 20:19:45 -03:00
|
|
|
// This takes about 250us at 400kHz I2C speed
|
2013-10-03 04:40:24 -03:00
|
|
|
if ((now - _last_gyro_timestamp) >= raw_sample_interval_us
|
2013-09-26 22:42:39 -03:00
|
|
|
&& hal.i2c->readRegisters(FLYMAPLE_GYRO_ADDRESS, FLYMAPLE_GYRO_GYROX_H, 6, buffer) == 0)
|
2013-09-23 00:32:19 -03:00
|
|
|
{
|
2013-09-27 02:31:28 -03:00
|
|
|
// See above re order of samples in buffer
|
2013-09-24 20:21:02 -03:00
|
|
|
int16_t y = -((((int16_t)buffer[0]) << 8) | buffer[1]); // chip X axis
|
|
|
|
int16_t x = -((((int16_t)buffer[2]) << 8) | buffer[3]); // chip Y axis
|
|
|
|
int16_t z = -((((int16_t)buffer[4]) << 8) | buffer[5]); // chip Z axis
|
2015-08-28 11:05:59 -03:00
|
|
|
Vector3f gyro = Vector3f(x,y,z);
|
|
|
|
// Adjust for chip scaling to get radians/sec
|
|
|
|
gyro *= FLYMAPLE_GYRO_SCALE_R_S;
|
|
|
|
_rotate_and_correct_gyro(_gyro_instance, gyro);
|
2015-09-08 14:05:37 -03:00
|
|
|
_notify_new_gyro_raw_sample(_gyro_instance, gyro);
|
2015-08-28 11:05:59 -03:00
|
|
|
_gyro_filtered = _gyro_filter.apply(gyro);
|
2014-10-15 21:24:32 -03:00
|
|
|
_have_gyro_sample = true;
|
2013-09-26 22:42:39 -03:00
|
|
|
_last_gyro_timestamp = now;
|
2013-09-23 00:32:19 -03:00
|
|
|
}
|
|
|
|
|
2013-10-03 04:40:24 -03:00
|
|
|
// give back i2c semaphore
|
|
|
|
i2c_sem->give();
|
2013-09-23 00:32:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD
|