2013-01-03 23:25:57 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
#include "AP_InertialSensor_PX4.h"
|
|
|
|
|
|
|
|
const extern AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <drivers/drv_accel.h>
|
|
|
|
#include <drivers/drv_gyro.h>
|
2013-08-06 03:31:18 -03:00
|
|
|
#include <drivers/drv_hrt.h>
|
2013-01-21 04:44:01 -04:00
|
|
|
|
2013-12-13 05:47:24 -04:00
|
|
|
#include <AP_Notify.h>
|
|
|
|
|
2013-01-20 06:13:52 -04:00
|
|
|
uint16_t AP_InertialSensor_PX4::_init_sensor( Sample_rate sample_rate )
|
|
|
|
{
|
2013-12-08 05:44:31 -04:00
|
|
|
// assumes max 2 instances
|
|
|
|
_accel_fd[0] = open(ACCEL_DEVICE_PATH, O_RDONLY);
|
|
|
|
_accel_fd[1] = open(ACCEL_DEVICE_PATH "1", O_RDONLY);
|
|
|
|
_gyro_fd[0] = open(GYRO_DEVICE_PATH, O_RDONLY);
|
|
|
|
_gyro_fd[1] = open(GYRO_DEVICE_PATH "1", O_RDONLY);
|
|
|
|
|
|
|
|
if (_accel_fd[0] < 0) {
|
|
|
|
hal.scheduler->panic("Unable to open accel device " ACCEL_DEVICE_PATH);
|
|
|
|
}
|
|
|
|
if (_gyro_fd[0] < 0) {
|
|
|
|
hal.scheduler->panic("Unable to open gyro device " GYRO_DEVICE_PATH);
|
|
|
|
}
|
2013-12-08 18:50:12 -04:00
|
|
|
_num_accel_instances = _accel_fd[1] >= 0?2:1;
|
|
|
|
_num_gyro_instances = _gyro_fd[1] >= 0?2:1;
|
2013-12-08 05:44:31 -04:00
|
|
|
|
2013-01-03 23:25:57 -04:00
|
|
|
switch (sample_rate) {
|
|
|
|
case RATE_50HZ:
|
2013-08-06 02:36:44 -03:00
|
|
|
_default_filter_hz = 15;
|
2013-08-06 03:31:18 -03:00
|
|
|
_sample_time_usec = 20000;
|
2013-01-03 23:25:57 -04:00
|
|
|
break;
|
|
|
|
case RATE_100HZ:
|
2013-08-06 02:36:44 -03:00
|
|
|
_default_filter_hz = 30;
|
2013-08-06 03:31:18 -03:00
|
|
|
_sample_time_usec = 10000;
|
2013-01-03 23:25:57 -04:00
|
|
|
break;
|
|
|
|
case RATE_200HZ:
|
2013-08-06 02:36:44 -03:00
|
|
|
_default_filter_hz = 30;
|
2013-08-06 03:31:18 -03:00
|
|
|
_sample_time_usec = 5000;
|
2013-01-03 23:25:57 -04:00
|
|
|
break;
|
2013-12-16 23:24:44 -04:00
|
|
|
case RATE_400HZ:
|
|
|
|
default:
|
|
|
|
_default_filter_hz = 30;
|
|
|
|
_sample_time_usec = 2500;
|
|
|
|
break;
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
|
|
|
|
2013-02-06 20:20:45 -04:00
|
|
|
_set_filter_frequency(_mpu6000_filter);
|
|
|
|
|
2013-09-08 01:30:20 -03:00
|
|
|
#if defined(CONFIG_ARCH_BOARD_PX4FMU_V2)
|
|
|
|
return AP_PRODUCT_ID_PX4_V2;
|
|
|
|
#else
|
2013-01-03 23:25:57 -04:00
|
|
|
return AP_PRODUCT_ID_PX4;
|
2013-09-08 01:30:20 -03:00
|
|
|
#endif
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
|
|
|
|
2013-02-06 20:20:45 -04:00
|
|
|
/*
|
|
|
|
set the filter frequency
|
|
|
|
*/
|
|
|
|
void AP_InertialSensor_PX4::_set_filter_frequency(uint8_t filter_hz)
|
|
|
|
{
|
|
|
|
if (filter_hz == 0) {
|
|
|
|
filter_hz = _default_filter_hz;
|
|
|
|
}
|
2013-12-10 23:57:13 -04:00
|
|
|
for (uint8_t i=0; i<_num_gyro_instances; i++) {
|
2013-12-08 05:44:31 -04:00
|
|
|
ioctl(_gyro_fd[i], GYROIOCSLOWPASS, filter_hz);
|
2013-12-10 23:57:13 -04:00
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<_num_accel_instances; i++) {
|
2013-12-08 05:44:31 -04:00
|
|
|
ioctl(_accel_fd[i], ACCELIOCSLOWPASS, filter_hz);
|
|
|
|
}
|
2013-02-06 20:20:45 -04:00
|
|
|
}
|
|
|
|
|
2013-01-03 23:25:57 -04:00
|
|
|
/*================ AP_INERTIALSENSOR PUBLIC INTERFACE ==================== */
|
|
|
|
|
2013-12-08 05:44:31 -04:00
|
|
|
// multi-device interface
|
2013-12-08 18:50:12 -04:00
|
|
|
bool AP_InertialSensor_PX4::get_gyro_health(uint8_t instance) const
|
2013-12-08 05:44:31 -04:00
|
|
|
{
|
2013-12-14 00:33:46 -04:00
|
|
|
if (_sample_time_usec == 0 || _last_get_sample_timestamp == 0) {
|
2013-12-08 05:44:31 -04:00
|
|
|
// not initialised yet, show as healthy to prevent scary GCS
|
|
|
|
// warnings
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-13 05:47:24 -04:00
|
|
|
if (instance >= _num_gyro_instances) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-08 05:44:31 -04:00
|
|
|
|
2013-12-14 00:33:46 -04:00
|
|
|
if ((_last_get_sample_timestamp - _last_gyro_timestamp[instance]) > 2*_sample_time_usec) {
|
2013-12-08 05:44:31 -04:00
|
|
|
// gyros have not updated
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AP_InertialSensor_PX4::get_gyro_count(void) const
|
|
|
|
{
|
|
|
|
return _num_gyro_instances;
|
|
|
|
}
|
|
|
|
|
2013-12-08 18:50:12 -04:00
|
|
|
bool AP_InertialSensor_PX4::get_accel_health(uint8_t k) const
|
2013-12-08 05:44:31 -04:00
|
|
|
{
|
2013-12-14 00:33:46 -04:00
|
|
|
if (_sample_time_usec == 0 || _last_get_sample_timestamp == 0) {
|
2013-12-08 05:44:31 -04:00
|
|
|
// not initialised yet, show as healthy to prevent scary GCS
|
|
|
|
// warnings
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-13 05:47:24 -04:00
|
|
|
if (k >= _num_accel_instances) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-08 05:44:31 -04:00
|
|
|
|
2013-12-14 00:33:46 -04:00
|
|
|
if ((_last_get_sample_timestamp - _last_accel_timestamp[k]) > 2*_sample_time_usec) {
|
2013-12-08 05:44:31 -04:00
|
|
|
// accels have not updated
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-08 18:50:12 -04:00
|
|
|
if (fabsf(_accel[k].x) > 30 && fabsf(_accel[k].y) > 30 && fabsf(_accel[k].z) > 30 &&
|
|
|
|
(_previous_accel[k] - _accel[k]).length() < 0.01f) {
|
2013-12-08 05:44:31 -04:00
|
|
|
// unchanging accel, large in all 3 axes. This is a likely
|
|
|
|
// accelerometer failure of the LSM303d
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
2013-12-09 05:02:04 -04:00
|
|
|
|
2013-12-08 05:44:31 -04:00
|
|
|
uint8_t AP_InertialSensor_PX4::get_accel_count(void) const
|
|
|
|
{
|
|
|
|
return _num_accel_instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_PX4::update(void)
|
|
|
|
{
|
2013-12-10 22:50:50 -04:00
|
|
|
if (!wait_for_sample(100)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-06 03:31:18 -03:00
|
|
|
// get the latest sample from the sensor drivers
|
|
|
|
_get_sample();
|
2013-01-21 04:44:01 -04:00
|
|
|
|
2013-11-06 22:53:59 -04:00
|
|
|
|
2013-12-08 18:50:12 -04:00
|
|
|
for (uint8_t k=0; k<_num_accel_instances; k++) {
|
|
|
|
_previous_accel[k] = _accel[k];
|
|
|
|
_accel[k] = _accel_in[k];
|
|
|
|
_accel[k].rotate(_board_orientation);
|
|
|
|
_accel[k].x *= _accel_scale[k].get().x;
|
|
|
|
_accel[k].y *= _accel_scale[k].get().y;
|
|
|
|
_accel[k].z *= _accel_scale[k].get().z;
|
|
|
|
_accel[k] -= _accel_offset[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t k=0; k<_num_gyro_instances; k++) {
|
|
|
|
_gyro[k] = _gyro_in[k];
|
|
|
|
_gyro[k].rotate(_board_orientation);
|
|
|
|
_gyro[k] -= _gyro_offset[k];
|
|
|
|
}
|
2013-01-03 23:25:57 -04:00
|
|
|
|
2013-02-06 20:20:45 -04:00
|
|
|
if (_last_filter_hz != _mpu6000_filter) {
|
|
|
|
_set_filter_frequency(_mpu6000_filter);
|
|
|
|
_last_filter_hz = _mpu6000_filter;
|
|
|
|
}
|
|
|
|
|
2013-10-13 08:15:50 -03:00
|
|
|
_have_sample_available = false;
|
2013-08-06 03:31:18 -03:00
|
|
|
|
2013-01-03 23:25:57 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-02 20:46:24 -04:00
|
|
|
float AP_InertialSensor_PX4::get_delta_time(void) const
|
2013-01-03 23:25:57 -04:00
|
|
|
{
|
2013-12-08 05:44:31 -04:00
|
|
|
return _sample_time_usec * 1.0e-6f;
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float AP_InertialSensor_PX4::get_gyro_drift_rate(void)
|
|
|
|
{
|
2013-12-08 05:44:31 -04:00
|
|
|
// assume 0.5 degrees/second/minute
|
2013-01-03 23:25:57 -04:00
|
|
|
return ToRad(0.5/60);
|
|
|
|
}
|
|
|
|
|
2013-08-06 03:31:18 -03:00
|
|
|
void AP_InertialSensor_PX4::_get_sample(void)
|
2013-01-03 23:25:57 -04:00
|
|
|
{
|
2013-12-10 23:57:13 -04:00
|
|
|
for (uint8_t i=0; i<_num_accel_instances; i++) {
|
2013-12-08 05:44:31 -04:00
|
|
|
struct accel_report accel_report;
|
|
|
|
while (_accel_fd[i] != -1 &&
|
|
|
|
::read(_accel_fd[i], &accel_report, sizeof(accel_report)) == sizeof(accel_report) &&
|
|
|
|
accel_report.timestamp != _last_accel_timestamp[i]) {
|
|
|
|
_accel_in[i] = Vector3f(accel_report.x, accel_report.y, accel_report.z);
|
|
|
|
_last_accel_timestamp[i] = accel_report.timestamp;
|
|
|
|
}
|
|
|
|
}
|
2013-12-10 23:57:13 -04:00
|
|
|
for (uint8_t i=0; i<_num_gyro_instances; i++) {
|
2013-12-08 05:44:31 -04:00
|
|
|
struct gyro_report gyro_report;
|
|
|
|
while (_gyro_fd[i] != -1 &&
|
|
|
|
::read(_gyro_fd[i], &gyro_report, sizeof(gyro_report)) == sizeof(gyro_report) &&
|
|
|
|
gyro_report.timestamp != _last_gyro_timestamp[i]) {
|
|
|
|
_gyro_in[i] = Vector3f(gyro_report.x, gyro_report.y, gyro_report.z);
|
|
|
|
_last_gyro_timestamp[i] = gyro_report.timestamp;
|
|
|
|
}
|
2013-11-10 00:35:28 -04:00
|
|
|
}
|
2013-12-14 00:33:46 -04:00
|
|
|
_last_get_sample_timestamp = hrt_absolute_time();
|
2013-01-21 04:44:01 -04:00
|
|
|
}
|
2013-01-03 23:25:57 -04:00
|
|
|
|
2013-12-08 05:44:31 -04:00
|
|
|
bool AP_InertialSensor_PX4::_sample_available(void)
|
2013-01-21 04:44:01 -04:00
|
|
|
{
|
2013-08-06 03:31:18 -03:00
|
|
|
uint64_t tnow = hrt_absolute_time();
|
2013-10-13 08:15:50 -03:00
|
|
|
while (tnow - _last_sample_timestamp > _sample_time_usec) {
|
|
|
|
_have_sample_available = true;
|
|
|
|
_last_sample_timestamp += _sample_time_usec;
|
2013-08-06 03:31:18 -03:00
|
|
|
}
|
2013-10-13 08:15:50 -03:00
|
|
|
return _have_sample_available;
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
|
|
|
|
2013-10-08 03:28:39 -03:00
|
|
|
bool AP_InertialSensor_PX4::wait_for_sample(uint16_t timeout_ms)
|
|
|
|
{
|
2013-12-08 05:44:31 -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) {
|
2013-10-13 18:45:58 -03:00
|
|
|
uint64_t tnow = hrt_absolute_time();
|
|
|
|
// we spin for the last timing_lag microseconds. Before that
|
|
|
|
// we yield the CPU to allow IO to happen
|
|
|
|
const uint16_t timing_lag = 400;
|
|
|
|
if (_last_sample_timestamp + _sample_time_usec > tnow+timing_lag) {
|
|
|
|
hal.scheduler->delay_microseconds(_last_sample_timestamp + _sample_time_usec - (tnow+timing_lag));
|
|
|
|
}
|
2013-12-08 05:44:31 -04:00
|
|
|
if (_sample_available()) {
|
2013-10-08 03:28:39 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:53:59 -04:00
|
|
|
/**
|
|
|
|
try to detect bad accel/gyro sensors
|
|
|
|
*/
|
2013-12-08 05:44:31 -04:00
|
|
|
bool AP_InertialSensor_PX4::healthy(void) const
|
2013-11-06 22:53:59 -04:00
|
|
|
{
|
2013-12-08 18:50:12 -04:00
|
|
|
return get_gyro_health(0) && get_accel_health(0);
|
2013-11-06 22:53:59 -04:00
|
|
|
}
|
|
|
|
|
2013-12-09 05:02:04 -04:00
|
|
|
uint8_t AP_InertialSensor_PX4::_get_primary_gyro(void) const
|
|
|
|
{
|
2013-12-10 22:50:50 -04:00
|
|
|
for (uint8_t i=0; i<_num_gyro_instances; i++) {
|
2013-12-09 05:02:04 -04:00
|
|
|
if (get_gyro_health(i)) return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-27 01:27:46 -04:00
|
|
|
uint8_t AP_InertialSensor_PX4::get_primary_accel(void) const
|
2013-12-09 05:02:04 -04:00
|
|
|
{
|
2013-12-10 22:50:50 -04:00
|
|
|
for (uint8_t i=0; i<_num_accel_instances; i++) {
|
2013-12-09 05:02:04 -04:00
|
|
|
if (get_accel_health(i)) return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:25:57 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|
|
|
|
|