2013-01-04 01:10:51 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
* AP_Compass_PX4.cpp - Arduino Library for PX4 magnetometer
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and / or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
#include "AP_Compass_PX4.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <drivers/drv_mag.h>
|
2013-04-30 19:54:36 -03:00
|
|
|
#include <drivers/drv_hrt.h>
|
2013-01-04 01:10:51 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-04-30 19:54:36 -03:00
|
|
|
int AP_Compass_PX4::_mag_fd = -1;
|
|
|
|
Vector3f AP_Compass_PX4::_sum;
|
|
|
|
uint32_t AP_Compass_PX4::_count = 0;
|
|
|
|
uint32_t AP_Compass_PX4::_last_timer = 0;
|
|
|
|
uint64_t AP_Compass_PX4::_last_timestamp = 0;
|
|
|
|
|
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool AP_Compass_PX4::init(void)
|
|
|
|
{
|
|
|
|
_mag_fd = open(MAG_DEVICE_PATH, O_RDONLY);
|
|
|
|
if (_mag_fd < 0) {
|
|
|
|
hal.console->printf("Unable to open " MAG_DEVICE_PATH);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the mag internal poll rate to at least 150Hz */
|
|
|
|
ioctl(_mag_fd, MAGIOCSSAMPLERATE, 150);
|
|
|
|
|
|
|
|
/* set the driver to poll at 150Hz */
|
|
|
|
ioctl(_mag_fd, SENSORIOCSPOLLRATE, 150);
|
|
|
|
|
2013-01-04 05:12:03 -04:00
|
|
|
// average over up to 10 samples
|
|
|
|
ioctl(_mag_fd, SENSORIOCSQUEUEDEPTH, 10);
|
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
healthy = false;
|
|
|
|
_count = 0;
|
|
|
|
_sum.zero();
|
|
|
|
|
2013-04-30 19:54:36 -03:00
|
|
|
hal.scheduler->register_timer_process(_compass_timer);
|
|
|
|
|
|
|
|
// give the timer a chance to run, and gather one sample
|
|
|
|
hal.scheduler->delay(40);
|
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Compass_PX4::read(void)
|
|
|
|
{
|
2013-04-30 19:54:36 -03:00
|
|
|
hal.scheduler->suspend_timer_procs();
|
|
|
|
|
|
|
|
// try to accumulate one more sample, so we have the latest data
|
|
|
|
_accumulate();
|
|
|
|
|
|
|
|
// consider the compass healthy if we got a reading in the last 0.2s
|
|
|
|
healthy = (hrt_absolute_time() - _last_timestamp < 200000);
|
|
|
|
if (!healthy || _count == 0) {
|
|
|
|
hal.scheduler->resume_timer_procs();
|
|
|
|
return healthy;
|
2013-01-04 01:10:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_sum /= _count;
|
|
|
|
_sum *= 1000;
|
2013-05-01 23:27:35 -03:00
|
|
|
|
|
|
|
// apply default board orientation for this compass type. This is
|
|
|
|
// a noop on most boards
|
2013-05-02 00:00:52 -03:00
|
|
|
_sum.rotate(MAG_BOARD_ORIENTATION);
|
2013-05-01 23:27:35 -03:00
|
|
|
|
|
|
|
// add user selectable orientation
|
|
|
|
_sum.rotate((enum Rotation)_orientation.get());
|
|
|
|
|
|
|
|
// and add in AHRS_ORIENTATION setting
|
2013-01-13 01:02:49 -04:00
|
|
|
_sum.rotate(_board_orientation);
|
2013-01-04 01:10:51 -04:00
|
|
|
_sum += _offset.get();
|
|
|
|
|
2013-03-03 10:02:12 -04:00
|
|
|
// apply motor compensation
|
2013-04-30 19:54:36 -03:00
|
|
|
if (_motor_comp_type != AP_COMPASS_MOT_COMP_DISABLED && _thr_or_curr != 0.0f) {
|
2013-03-03 10:02:12 -04:00
|
|
|
_motor_offset = _motor_compensation.get() * _thr_or_curr;
|
2013-02-27 03:57:04 -04:00
|
|
|
_sum += _motor_offset;
|
2013-04-30 19:54:36 -03:00
|
|
|
} else {
|
2013-02-27 03:57:04 -04:00
|
|
|
_motor_offset.x = 0;
|
|
|
|
_motor_offset.y = 0;
|
|
|
|
_motor_offset.z = 0;
|
|
|
|
}
|
2013-04-30 19:54:36 -03:00
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
mag_x = _sum.x;
|
|
|
|
mag_y = _sum.y;
|
|
|
|
mag_z = _sum.z;
|
2013-04-30 19:54:36 -03:00
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
_sum.zero();
|
|
|
|
_count = 0;
|
|
|
|
|
2013-04-30 19:54:36 -03:00
|
|
|
hal.scheduler->resume_timer_procs();
|
|
|
|
|
2013-01-22 05:08:33 -04:00
|
|
|
last_update = _last_timestamp;
|
2013-04-30 19:54:36 -03:00
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-30 19:54:36 -03:00
|
|
|
void AP_Compass_PX4::_accumulate(void)
|
2013-01-04 01:10:51 -04:00
|
|
|
{
|
2013-01-04 05:12:03 -04:00
|
|
|
struct mag_report mag_report;
|
2013-01-22 05:08:33 -04:00
|
|
|
while (::read(_mag_fd, &mag_report, sizeof(mag_report)) == sizeof(mag_report) &&
|
|
|
|
mag_report.timestamp != _last_timestamp) {
|
2013-01-04 05:12:03 -04:00
|
|
|
_sum += Vector3f(mag_report.x, mag_report.y, mag_report.z);
|
|
|
|
_count++;
|
2013-01-22 05:08:33 -04:00
|
|
|
_last_timestamp = mag_report.timestamp;
|
2013-01-04 05:12:03 -04:00
|
|
|
}
|
2013-01-04 01:10:51 -04:00
|
|
|
}
|
|
|
|
|
2013-04-30 19:54:36 -03:00
|
|
|
void AP_Compass_PX4::accumulate(void)
|
|
|
|
{
|
|
|
|
// let the timer do the work
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_PX4::_compass_timer(uint32_t now)
|
|
|
|
{
|
|
|
|
// try to accumulate samples at 100Hz
|
|
|
|
if (now - _last_timer < 10000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_last_timer = hal.scheduler->micros();
|
|
|
|
_accumulate();
|
|
|
|
}
|
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|