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
|
|
|
|
2013-01-10 06:01:55 -04:00
|
|
|
#include <AP_HAL.h>
|
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"
|
|
|
|
|
2013-10-08 03:28:39 -03:00
|
|
|
const extern AP_HAL::HAL& hal;
|
|
|
|
|
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
|
|
|
|
|
|
|
// ADC channel reading the gyro temperature
|
|
|
|
const uint8_t AP_InertialSensor_Oilpan::_gyro_temp_ch = 3;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2012-12-19 02:02:13 -04:00
|
|
|
#define ToRad(x) radians(x) // *pi/180
|
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
|
2013-01-10 14:42:24 -04:00
|
|
|
const float AP_InertialSensor_Oilpan::_gyro_gain_x = ToRad(0.4f);
|
|
|
|
const float AP_InertialSensor_Oilpan::_gyro_gain_y = ToRad(0.41f);
|
|
|
|
const float AP_InertialSensor_Oilpan::_gyro_gain_z = ToRad(0.41f);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
|
|
|
/* ------ Public functions -------------------------------------------*/
|
|
|
|
|
2013-04-12 01:30:35 -03:00
|
|
|
AP_InertialSensor_Oilpan::AP_InertialSensor_Oilpan( AP_ADC * adc ) :
|
|
|
|
AP_InertialSensor(),
|
2012-08-17 03:19:56 -03:00
|
|
|
_adc(adc)
|
2011-11-12 23:20:25 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-11 21:27:19 -03:00
|
|
|
uint16_t AP_InertialSensor_Oilpan::_init_sensor( Sample_rate sample_rate)
|
2011-11-12 23:20:25 -04:00
|
|
|
{
|
2012-10-11 21:27:19 -03:00
|
|
|
_adc->Init();
|
2012-05-08 23:26:35 -03:00
|
|
|
|
2012-11-29 07:56:13 -04:00
|
|
|
switch (sample_rate) {
|
|
|
|
case RATE_50HZ:
|
|
|
|
_sample_threshold = 20;
|
|
|
|
break;
|
|
|
|
case RATE_100HZ:
|
|
|
|
_sample_threshold = 10;
|
|
|
|
break;
|
|
|
|
case RATE_200HZ:
|
|
|
|
_sample_threshold = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-30 19:01:38 -03:00
|
|
|
#if defined(__AVR_ATmega1280__)
|
2012-08-17 03:19:56 -03:00
|
|
|
return AP_PRODUCT_ID_APM1_1280;
|
2012-05-08 23:26:35 -03:00
|
|
|
#else
|
2012-08-17 03:19:56 -03:00
|
|
|
return AP_PRODUCT_ID_APM1_2560;
|
2012-05-08 23:26:35 -03:00
|
|
|
#endif
|
2011-11-12 23:20:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_Oilpan::update()
|
|
|
|
{
|
2013-12-10 22:50:50 -04:00
|
|
|
if (!wait_for_sample(100)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-17 03:19:56 -03:00
|
|
|
float adc_values[6];
|
2013-12-08 18:50:12 -04:00
|
|
|
Vector3f gyro_offset = _gyro_offset[0].get();
|
|
|
|
Vector3f accel_scale = _accel_scale[0].get();
|
|
|
|
Vector3f accel_offset = _accel_offset[0].get();
|
2012-11-20 03:25:36 -04:00
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
_delta_time_micros = _adc->Ch6(_sensors, adc_values);
|
2012-08-17 03:19:56 -03:00
|
|
|
_temp = _adc->Ch(_gyro_temp_ch);
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2013-12-08 18:50:12 -04:00
|
|
|
_gyro[0] = Vector3f(_sensor_signs[0] * ( adc_values[0] - OILPAN_RAW_GYRO_OFFSET ),
|
|
|
|
_sensor_signs[1] * ( adc_values[1] - OILPAN_RAW_GYRO_OFFSET ),
|
|
|
|
_sensor_signs[2] * ( adc_values[2] - OILPAN_RAW_GYRO_OFFSET ));
|
|
|
|
_gyro[0].rotate(_board_orientation);
|
|
|
|
_gyro[0].x *= _gyro_gain_x;
|
|
|
|
_gyro[0].y *= _gyro_gain_y;
|
|
|
|
_gyro[0].z *= _gyro_gain_z;
|
|
|
|
_gyro[0] -= gyro_offset;
|
|
|
|
|
|
|
|
_previous_accel[0] = _accel[0];
|
|
|
|
|
|
|
|
_accel[0] = Vector3f(_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));
|
|
|
|
_accel[0].rotate(_board_orientation);
|
|
|
|
_accel[0].x *= accel_scale.x;
|
|
|
|
_accel[0].y *= accel_scale.y;
|
|
|
|
_accel[0].z *= accel_scale.z;
|
|
|
|
_accel[0] *= OILPAN_ACCEL_SCALE_1G;
|
|
|
|
_accel[0] -= accel_offset;
|
2012-07-19 02:56:13 -03:00
|
|
|
|
|
|
|
/*
|
2012-08-17 03:19:56 -03:00
|
|
|
* X = 1619.30 to 2445.69
|
|
|
|
* Y = 1609.45 to 2435.42
|
|
|
|
* Z = 1627.44 to 2434.82
|
|
|
|
*/
|
2011-11-12 23:20:25 -04:00
|
|
|
|
2012-08-17 03:19:56 -03:00
|
|
|
return true;
|
2011-11-12 23:20:25 -04:00
|
|
|
}
|
|
|
|
|
2014-01-02 20:46:24 -04:00
|
|
|
float AP_InertialSensor_Oilpan::get_delta_time() const {
|
2013-01-11 06:17:21 -04:00
|
|
|
return _delta_time_micros * 1.0e-6;
|
2012-08-17 03:19:56 -03:00
|
|
|
}
|
2011-11-12 23:20:25 -04:00
|
|
|
|
|
|
|
/* ------ Private functions -------------------------------------------*/
|
|
|
|
|
2012-03-08 03:10:27 -04:00
|
|
|
// return the oilpan gyro drift rate in radian/s/s
|
|
|
|
float AP_InertialSensor_Oilpan::get_gyro_drift_rate(void)
|
|
|
|
{
|
|
|
|
// 3.0 degrees/second/minute
|
|
|
|
return ToRad(3.0/60);
|
|
|
|
}
|
2012-08-30 04:48:36 -03:00
|
|
|
|
2013-09-26 21:33:08 -03:00
|
|
|
// return true if a new sample is available
|
2013-12-08 05:43:53 -04:00
|
|
|
bool AP_InertialSensor_Oilpan::_sample_available()
|
2012-08-30 04:48:36 -03:00
|
|
|
{
|
2013-09-26 21:33:08 -03:00
|
|
|
return (_adc->num_samples_available(_sensors) / _sample_threshold) > 0;
|
2012-08-30 04:48:36 -03:00
|
|
|
}
|
2013-10-08 03:28:39 -03:00
|
|
|
|
|
|
|
bool AP_InertialSensor_Oilpan::wait_for_sample(uint16_t timeout_ms)
|
|
|
|
{
|
2013-12-08 05:43:53 -04:00
|
|
|
if (_sample_available()) {
|
2013-10-08 03:28:39 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
uint32_t start = hal.scheduler->millis();
|
|
|
|
while ((hal.scheduler->millis() - start) < timeout_ms) {
|
|
|
|
hal.scheduler->delay_microseconds(100);
|
2013-12-08 05:43:53 -04:00
|
|
|
if (_sample_available()) {
|
2013-10-08 03:28:39 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-10 06:01:55 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|
|
|
|
|