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-01-20 06:13:52 -04:00
|
|
|
uint16_t AP_InertialSensor_PX4::_init_sensor( Sample_rate sample_rate )
|
|
|
|
{
|
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:
|
|
|
|
default:
|
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-08-06 03:31:18 -03:00
|
|
|
_delta_time = _sample_time_usec * 1.0e-6f;
|
|
|
|
|
2013-01-03 23:25:57 -04:00
|
|
|
// init accelerometers
|
2013-01-20 06:13:52 -04:00
|
|
|
_accel_fd = open(ACCEL_DEVICE_PATH, O_RDONLY);
|
|
|
|
if (_accel_fd < 0) {
|
2013-01-03 23:25:57 -04:00
|
|
|
hal.scheduler->panic("Unable to open accel device " ACCEL_DEVICE_PATH);
|
|
|
|
}
|
|
|
|
|
2013-01-20 06:13:52 -04:00
|
|
|
_gyro_fd = open(GYRO_DEVICE_PATH, O_RDONLY);
|
|
|
|
if (_gyro_fd < 0) {
|
2013-01-03 23:25:57 -04:00
|
|
|
hal.scheduler->panic("Unable to open gyro device " GYRO_DEVICE_PATH);
|
|
|
|
}
|
|
|
|
|
2013-07-31 09:22:36 -03:00
|
|
|
#ifdef CONFIG_ARCH_BOARD_PX4FMU_V1
|
2013-08-06 02:36:44 -03:00
|
|
|
uint32_t driver_rate = 1000;
|
2013-07-31 09:22:36 -03:00
|
|
|
#else
|
2013-08-06 02:36:44 -03:00
|
|
|
uint32_t driver_rate = 800;
|
|
|
|
#endif
|
|
|
|
|
2013-07-31 09:22:36 -03:00
|
|
|
/*
|
2013-08-06 02:36:44 -03:00
|
|
|
* set the accel and gyro sampling rate.
|
2013-07-31 09:22:36 -03:00
|
|
|
*/
|
2013-08-06 02:36:44 -03:00
|
|
|
ioctl(_accel_fd, ACCELIOCSSAMPLERATE, driver_rate);
|
|
|
|
ioctl(_accel_fd, SENSORIOCSPOLLRATE, driver_rate);
|
|
|
|
ioctl(_gyro_fd, GYROIOCSSAMPLERATE, driver_rate);
|
|
|
|
ioctl(_gyro_fd, SENSORIOCSPOLLRATE, driver_rate);
|
2013-07-31 09:22:36 -03: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;
|
|
|
|
}
|
|
|
|
ioctl(_gyro_fd, GYROIOCSLOWPASS, filter_hz);
|
|
|
|
ioctl(_accel_fd, ACCELIOCSLOWPASS, filter_hz);
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:25:57 -04:00
|
|
|
/*================ AP_INERTIALSENSOR PUBLIC INTERFACE ==================== */
|
|
|
|
|
|
|
|
bool AP_InertialSensor_PX4::update(void)
|
|
|
|
{
|
2013-01-21 04:44:01 -04:00
|
|
|
Vector3f accel_scale = _accel_scale.get();
|
2013-01-20 06:13:52 -04:00
|
|
|
|
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-08-06 02:36:44 -03:00
|
|
|
_accel = _accel_in;
|
2013-08-06 03:31:18 -03:00
|
|
|
_gyro = _gyro_in;
|
2013-01-21 04:44:01 -04:00
|
|
|
|
|
|
|
// add offsets and rotation
|
2013-01-13 01:03:13 -04:00
|
|
|
_accel.rotate(_board_orientation);
|
|
|
|
_accel.x *= accel_scale.x;
|
|
|
|
_accel.y *= accel_scale.y;
|
|
|
|
_accel.z *= accel_scale.z;
|
|
|
|
_accel -= _accel_offset;
|
|
|
|
|
|
|
|
_gyro.rotate(_board_orientation);
|
2013-01-21 04:44:01 -04:00
|
|
|
_gyro -= _gyro_offset;
|
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-08-06 03:31:18 -03:00
|
|
|
_num_samples_available = 0;
|
|
|
|
|
2013-01-03 23:25:57 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-06 03:31:18 -03:00
|
|
|
float AP_InertialSensor_PX4::get_delta_time(void)
|
2013-01-03 23:25:57 -04:00
|
|
|
{
|
2013-01-20 06:13:52 -04:00
|
|
|
return _delta_time;
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float AP_InertialSensor_PX4::get_gyro_drift_rate(void)
|
|
|
|
{
|
|
|
|
// 0.5 degrees/second/minute
|
|
|
|
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-01-20 06:13:52 -04:00
|
|
|
struct accel_report accel_report;
|
|
|
|
struct gyro_report gyro_report;
|
2013-01-21 04:44:01 -04:00
|
|
|
|
2013-07-31 09:22:36 -03:00
|
|
|
while (::read(_accel_fd, &accel_report, sizeof(accel_report)) == sizeof(accel_report) &&
|
2013-01-21 06:51:32 -04:00
|
|
|
accel_report.timestamp != _last_accel_timestamp) {
|
2013-08-06 02:36:44 -03:00
|
|
|
_accel_in = Vector3f(accel_report.x, accel_report.y, accel_report.z);
|
2013-01-21 06:51:32 -04:00
|
|
|
_last_accel_timestamp = accel_report.timestamp;
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 09:22:36 -03:00
|
|
|
while (::read(_gyro_fd, &gyro_report, sizeof(gyro_report)) == sizeof(gyro_report) &&
|
2013-01-21 06:51:32 -04:00
|
|
|
gyro_report.timestamp != _last_gyro_timestamp) {
|
2013-08-06 02:36:44 -03:00
|
|
|
_gyro_in = Vector3f(gyro_report.x, gyro_report.y, gyro_report.z);
|
2013-01-21 06:51:32 -04:00
|
|
|
_last_gyro_timestamp = gyro_report.timestamp;
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
2013-01-21 04:44:01 -04:00
|
|
|
}
|
2013-01-03 23:25:57 -04:00
|
|
|
|
2013-01-21 04:44:01 -04:00
|
|
|
uint16_t AP_InertialSensor_PX4::num_samples_available(void)
|
|
|
|
{
|
2013-08-06 03:31:18 -03:00
|
|
|
uint64_t tnow = hrt_absolute_time();
|
|
|
|
if (tnow - _last_sample_timestamp > _sample_time_usec) {
|
|
|
|
_num_samples_available++;
|
|
|
|
_last_sample_timestamp = tnow;
|
|
|
|
}
|
|
|
|
return _num_samples_available;
|
2013-01-03 23:25:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD
|
|
|
|
|