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
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-10-15 20:31:21 -03:00
|
|
|
|
2014-07-06 23:03:57 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1
|
2011-11-12 23:20:25 -04:00
|
|
|
#include "AP_InertialSensor_Oilpan.h"
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_ADC/AP_ADC.h>
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2013-10-08 03:28:39 -03:00
|
|
|
const extern AP_HAL::HAL& hal;
|
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
// this driver assumes an AP_ADC object has been declared globally
|
|
|
|
extern AP_ADC_ADS7844 apm1_adc;
|
|
|
|
|
2011-11-12 23:20:25 -04:00
|
|
|
// ADC channel mappings on for the APM Oilpan
|
|
|
|
// Sensors: GYROX, GYROY, GYROZ, ACCELX, ACCELY, ACCELZ
|
|
|
|
const uint8_t AP_InertialSensor_Oilpan::_sensors[6] = { 1, 2, 0, 4, 5, 6 };
|
|
|
|
|
|
|
|
// ADC result sign adjustment for sensors.
|
2012-08-17 03:19:56 -03:00
|
|
|
const int8_t AP_InertialSensor_Oilpan::_sensor_signs[6] =
|
|
|
|
{ 1, -1, -1, 1, -1, -1 };
|
2011-11-12 23:20:25 -04:00
|
|
|
|
|
|
|
// Maximum possible value returned by an offset-corrected sensor channel
|
|
|
|
const float AP_InertialSensor_Oilpan::_adc_constraint = 900;
|
|
|
|
|
|
|
|
// ADC : Voltage reference 3.3v / 12bits(4096 steps) => 0.8mV/ADC step
|
|
|
|
// ADXL335 Sensitivity(from datasheet) => 330mV/g,
|
|
|
|
// 0.8mV/ADC step => 330/0.8 = 412
|
|
|
|
// Tested value : 418
|
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
// Oilpan accelerometer scaling & offsets
|
2013-04-05 10:57:46 -03:00
|
|
|
#define OILPAN_ACCEL_SCALE_1G (GRAVITY_MSS * 2.0f / (2465.0f - 1617.0f))
|
2013-01-10 14:42:24 -04:00
|
|
|
#define OILPAN_RAW_ACCEL_OFFSET ((2465.0f + 1617.0f) * 0.5f)
|
|
|
|
#define OILPAN_RAW_GYRO_OFFSET 1658.0f
|
2011-11-12 23:20:25 -04:00
|
|
|
|
|
|
|
// IDG500 Sensitivity (from datasheet) => 2.0mV/degree/s,
|
|
|
|
// 0.8mV/ADC step => 0.8/3.33 = 0.4
|
|
|
|
// Tested values : 0.4026, ?, 0.4192
|
2014-10-15 20:31:21 -03:00
|
|
|
const float AP_InertialSensor_Oilpan::_gyro_gain_x = radians(0.4f);
|
|
|
|
const float AP_InertialSensor_Oilpan::_gyro_gain_y = radians(0.41f);
|
|
|
|
const float AP_InertialSensor_Oilpan::_gyro_gain_z = radians(0.41f);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
|
|
|
/* ------ Public functions -------------------------------------------*/
|
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
AP_InertialSensor_Oilpan::AP_InertialSensor_Oilpan(AP_InertialSensor &imu) :
|
|
|
|
AP_InertialSensor_Backend(imu)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
detect the sensor
|
|
|
|
*/
|
2014-10-16 17:52:21 -03:00
|
|
|
AP_InertialSensor_Backend *AP_InertialSensor_Oilpan::detect(AP_InertialSensor &_imu)
|
2011-11-12 23:20:25 -04:00
|
|
|
{
|
2014-10-15 20:31:21 -03:00
|
|
|
AP_InertialSensor_Oilpan *sensor = new AP_InertialSensor_Oilpan(_imu);
|
|
|
|
if (sensor == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-16 17:52:21 -03:00
|
|
|
if (!sensor->_init_sensor()) {
|
2014-10-15 20:31:21 -03:00
|
|
|
delete sensor;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensor;
|
2011-11-12 23:20:25 -04:00
|
|
|
}
|
|
|
|
|
2014-10-16 17:52:21 -03:00
|
|
|
bool AP_InertialSensor_Oilpan::_init_sensor(void)
|
2011-11-12 23:20:25 -04:00
|
|
|
{
|
2014-10-15 20:31:21 -03:00
|
|
|
apm1_adc.Init();
|
2012-05-08 23:26:35 -03:00
|
|
|
|
2014-10-16 17:52:21 -03:00
|
|
|
switch (_imu.get_sample_rate()) {
|
2014-10-15 20:31:21 -03:00
|
|
|
case AP_InertialSensor::RATE_50HZ:
|
2012-11-29 07:56:13 -04:00
|
|
|
_sample_threshold = 20;
|
|
|
|
break;
|
2014-10-15 20:31:21 -03:00
|
|
|
case AP_InertialSensor::RATE_100HZ:
|
2012-11-29 07:56:13 -04:00
|
|
|
_sample_threshold = 10;
|
|
|
|
break;
|
2014-10-15 20:31:21 -03:00
|
|
|
case AP_InertialSensor::RATE_200HZ:
|
2012-11-29 07:56:13 -04:00
|
|
|
_sample_threshold = 5;
|
|
|
|
break;
|
2014-10-15 20:31:21 -03:00
|
|
|
default:
|
|
|
|
// can't do this speed
|
|
|
|
return false;
|
2012-11-29 07:56:13 -04:00
|
|
|
}
|
|
|
|
|
2014-10-15 20:31:21 -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_APM1_2560;
|
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
return true;
|
2011-11-12 23:20:25 -04:00
|
|
|
}
|
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
/*
|
|
|
|
copy data from ADC to frontend
|
|
|
|
*/
|
2011-11-12 23:20:25 -04:00
|
|
|
bool AP_InertialSensor_Oilpan::update()
|
|
|
|
{
|
2012-08-17 03:19:56 -03:00
|
|
|
float adc_values[6];
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
apm1_adc.Ch6(_sensors, adc_values);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
// copy gyros to frontend
|
|
|
|
Vector3f v;
|
|
|
|
v(_sensor_signs[0] * ( adc_values[0] - OILPAN_RAW_GYRO_OFFSET ) * _gyro_gain_x,
|
|
|
|
_sensor_signs[1] * ( adc_values[1] - OILPAN_RAW_GYRO_OFFSET ) * _gyro_gain_y,
|
|
|
|
_sensor_signs[2] * ( adc_values[2] - OILPAN_RAW_GYRO_OFFSET ) * _gyro_gain_z);
|
2015-08-28 10:36:28 -03:00
|
|
|
_rotate_and_correct_gyro(_gyro_instance, v);
|
2015-08-28 12:18:09 -03:00
|
|
|
_publish_gyro(_gyro_instance, v);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
// copy accels to frontend
|
|
|
|
v(_sensor_signs[3] * (adc_values[3] - OILPAN_RAW_ACCEL_OFFSET),
|
|
|
|
_sensor_signs[4] * (adc_values[4] - OILPAN_RAW_ACCEL_OFFSET),
|
|
|
|
_sensor_signs[5] * (adc_values[5] - OILPAN_RAW_ACCEL_OFFSET));
|
|
|
|
v *= OILPAN_ACCEL_SCALE_1G;
|
2015-08-28 10:36:28 -03:00
|
|
|
_rotate_and_correct_accel(_accel_instance, v);
|
2015-08-28 12:18:09 -03:00
|
|
|
_publish_accel(_accel_instance, v);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2014-10-15 20:31:21 -03:00
|
|
|
return true;
|
2012-03-08 03:10:27 -04:00
|
|
|
}
|
2012-08-30 04:48:36 -03:00
|
|
|
|
2013-09-26 21:33:08 -03:00
|
|
|
// return true if a new sample is available
|
2014-10-15 20:31:21 -03:00
|
|
|
bool AP_InertialSensor_Oilpan::_sample_available() const
|
2012-08-30 04:48:36 -03:00
|
|
|
{
|
2014-10-15 20:31:21 -03:00
|
|
|
return apm1_adc.num_samples_available(_sensors) >= _sample_threshold;
|
2013-10-08 03:28:39 -03:00
|
|
|
}
|
|
|
|
|
2013-01-10 06:01:55 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|
|
|
|
|