2013-01-04 01:10:51 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
/*
|
|
|
|
* AP_Compass_PX4.cpp - Arduino Library for PX4 magnetometer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2014-07-07 09:30:23 -03:00
|
|
|
#include <drivers/drv_device.h>
|
2013-01-04 01:10:51 -04:00
|
|
|
#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
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool AP_Compass_PX4::init(void)
|
|
|
|
{
|
2013-12-09 01:05:57 -04:00
|
|
|
_mag_fd[0] = open(MAG_DEVICE_PATH, O_RDONLY);
|
2014-07-03 23:07:47 -03:00
|
|
|
_mag_fd[1] = open(MAG_DEVICE_PATH "1", O_RDONLY);
|
|
|
|
_mag_fd[2] = open(MAG_DEVICE_PATH "2", O_RDONLY);
|
|
|
|
|
|
|
|
_num_instances = 0;
|
|
|
|
for (uint8_t i=0; i<COMPASS_MAX_INSTANCES; i++) {
|
|
|
|
if (_mag_fd[i] >= 0) {
|
|
|
|
_num_instances = i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_num_instances == 0) {
|
2013-09-08 19:25:35 -03:00
|
|
|
hal.console->printf("Unable to open " MAG_DEVICE_PATH "\n");
|
2013-01-04 01:10:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
2014-07-07 09:30:23 -03:00
|
|
|
// get device id
|
|
|
|
_dev_id[i] = ioctl(_mag_fd[i], DEVIOCGDEVICEID, 0);
|
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
// average over up to 20 samples
|
|
|
|
if (ioctl(_mag_fd[i], SENSORIOCSQUEUEDEPTH, 20) != 0) {
|
|
|
|
hal.console->printf("Failed to setup compass queue\n");
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-04 05:12:03 -04:00
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
// remember if the compass is external
|
|
|
|
_is_external[i] = (ioctl(_mag_fd[i], MAGIOCGEXTERNAL, 0) > 0);
|
2014-03-13 02:26:43 -03:00
|
|
|
if (_is_external[i]) {
|
2013-12-09 01:05:57 -04:00
|
|
|
hal.console->printf("Using external compass[%u]\n", (unsigned)i);
|
|
|
|
}
|
|
|
|
_count[0] = 0;
|
|
|
|
_sum[i].zero();
|
2013-12-09 02:54:54 -04:00
|
|
|
_healthy[i] = false;
|
2013-09-08 19:25:35 -03:00
|
|
|
}
|
2013-08-30 01:02:09 -03:00
|
|
|
|
2013-08-28 06:18:05 -03:00
|
|
|
// give the driver a chance to run, and gather one sample
|
2013-04-30 19:54:36 -03:00
|
|
|
hal.scheduler->delay(40);
|
2013-08-28 06:18:05 -03:00
|
|
|
accumulate();
|
2013-12-09 01:05:57 -04:00
|
|
|
if (_count[0] == 0) {
|
2013-09-08 19:25:35 -03:00
|
|
|
hal.console->printf("Failed initial compass accumulate\n");
|
|
|
|
}
|
2013-01-04 01:10:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Compass_PX4::read(void)
|
|
|
|
{
|
2013-04-30 19:54:36 -03:00
|
|
|
// try to accumulate one more sample, so we have the latest data
|
2013-08-28 06:18:05 -03:00
|
|
|
accumulate();
|
2013-04-30 19:54:36 -03:00
|
|
|
|
|
|
|
// consider the compass healthy if we got a reading in the last 0.2s
|
2013-12-09 02:46:41 -04:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
|
|
|
_healthy[i] = (hrt_absolute_time() - _last_timestamp[i] < 200000);
|
2013-01-04 01:10:51 -04:00
|
|
|
}
|
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
2014-03-20 02:27:32 -03:00
|
|
|
// avoid division by zero if we haven't received any mag reports
|
|
|
|
if (_count[i] == 0) continue;
|
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
_sum[i] /= _count[i];
|
|
|
|
_sum[i] *= 1000;
|
2013-05-01 23:27:35 -03:00
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
// apply default board orientation for this compass type. This is
|
|
|
|
// a noop on most boards
|
|
|
|
_sum[i].rotate(MAG_BOARD_ORIENTATION);
|
2013-05-01 23:27:35 -03:00
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
// override any user setting of COMPASS_EXTERNAL
|
|
|
|
_external.set(_is_external[0]);
|
2013-08-30 01:02:09 -03:00
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
if (_is_external[i]) {
|
|
|
|
// add user selectable orientation
|
|
|
|
_sum[i].rotate((enum Rotation)_orientation.get());
|
|
|
|
} else {
|
|
|
|
// add in board orientation from AHRS
|
|
|
|
_sum[i].rotate(_board_orientation);
|
|
|
|
}
|
2013-08-30 01:02:09 -03:00
|
|
|
|
2013-12-09 02:33:54 -04:00
|
|
|
_sum[i] += _offset[i].get();
|
2013-01-04 01:10:51 -04:00
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
// apply motor compensation
|
|
|
|
if (_motor_comp_type != AP_COMPASS_MOT_COMP_DISABLED && _thr_or_curr != 0.0f) {
|
2013-12-09 04:45:31 -04:00
|
|
|
_motor_offset[i] = _motor_compensation[i].get() * _thr_or_curr;
|
|
|
|
_sum[i] += _motor_offset[i];
|
2013-12-09 01:05:57 -04:00
|
|
|
} else {
|
2013-12-09 04:45:31 -04:00
|
|
|
_motor_offset[i].zero();
|
2013-12-09 01:05:57 -04:00
|
|
|
}
|
2013-04-30 19:54:36 -03:00
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
_field[i] = _sum[i];
|
2013-04-30 19:54:36 -03:00
|
|
|
|
2013-12-09 01:05:57 -04:00
|
|
|
_sum[i].zero();
|
|
|
|
_count[i] = 0;
|
|
|
|
}
|
2013-01-04 01:10:51 -04:00
|
|
|
|
2014-07-22 09:57:22 -03:00
|
|
|
last_update = _last_timestamp[get_primary()];
|
2013-04-30 19:54:36 -03:00
|
|
|
|
2014-07-22 09:57:22 -03:00
|
|
|
return _healthy[get_primary()];
|
2013-01-04 01:10:51 -04:00
|
|
|
}
|
|
|
|
|
2013-08-28 06:18:05 -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-12-09 01:05:57 -04:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
|
|
|
while (::read(_mag_fd[i], &mag_report, sizeof(mag_report)) == sizeof(mag_report) &&
|
|
|
|
mag_report.timestamp != _last_timestamp[i]) {
|
|
|
|
_sum[i] += Vector3f(mag_report.x, mag_report.y, mag_report.z);
|
|
|
|
_count[i]++;
|
|
|
|
_last_timestamp[i] = mag_report.timestamp;
|
|
|
|
}
|
2013-01-04 05:12:03 -04:00
|
|
|
}
|
2013-01-04 01:10:51 -04:00
|
|
|
}
|
|
|
|
|
2014-07-22 09:57:22 -03:00
|
|
|
uint8_t AP_Compass_PX4::get_primary(void) const
|
2013-12-09 05:01:42 -04:00
|
|
|
{
|
2014-05-21 23:52:25 -03:00
|
|
|
if (_primary < _num_instances && _healthy[_primary]) {
|
|
|
|
return _primary;
|
|
|
|
}
|
2013-12-09 05:01:42 -04:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
|
|
|
if (_healthy[i]) return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-04 01:10:51 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|