2012-11-11 20:26:20 -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/>.
|
|
|
|
*/
|
|
|
|
|
2011-02-14 00:27:07 -04:00
|
|
|
/*
|
2012-08-17 03:19:22 -03:00
|
|
|
* AP_Compass_HMC5843.cpp - Arduino Library for HMC5843 I2C magnetometer
|
|
|
|
* Code by Jordi Muñoz and Jose Julio. DIYDrones.com
|
|
|
|
*
|
|
|
|
* Sensor is conected to I2C port
|
|
|
|
* Sensor is initialized in Continuos mode (10Hz)
|
|
|
|
*
|
|
|
|
*/
|
2011-02-14 00:27:07 -04:00
|
|
|
|
|
|
|
// AVR LibC Includes
|
2013-01-10 15:22:37 -04:00
|
|
|
#include <AP_Math.h>
|
2012-10-11 17:48:39 -03:00
|
|
|
#include <AP_HAL.h>
|
2011-02-14 00:27:07 -04:00
|
|
|
|
|
|
|
#include "AP_Compass_HMC5843.h"
|
|
|
|
|
2012-10-11 17:48:39 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-02-28 06:41:20 -04:00
|
|
|
#define COMPASS_ADDRESS 0x1E
|
2011-02-14 00:27:07 -04:00
|
|
|
#define ConfigRegA 0x00
|
|
|
|
#define ConfigRegB 0x01
|
|
|
|
#define magGain 0x20
|
|
|
|
#define PositiveBiasConfig 0x11
|
|
|
|
#define NegativeBiasConfig 0x12
|
|
|
|
#define NormalOperation 0x10
|
|
|
|
#define ModeRegister 0x02
|
|
|
|
#define ContinuousConversion 0x00
|
|
|
|
#define SingleConversion 0x01
|
|
|
|
|
2011-07-03 05:32:58 -03:00
|
|
|
// ConfigRegA valid sample averaging for 5883L
|
|
|
|
#define SampleAveraging_1 0x00
|
|
|
|
#define SampleAveraging_2 0x01
|
|
|
|
#define SampleAveraging_4 0x02
|
|
|
|
#define SampleAveraging_8 0x03
|
|
|
|
|
|
|
|
// ConfigRegA valid data output rates for 5883L
|
|
|
|
#define DataOutputRate_0_75HZ 0x00
|
|
|
|
#define DataOutputRate_1_5HZ 0x01
|
|
|
|
#define DataOutputRate_3HZ 0x02
|
|
|
|
#define DataOutputRate_7_5HZ 0x03
|
|
|
|
#define DataOutputRate_15HZ 0x04
|
|
|
|
#define DataOutputRate_30HZ 0x05
|
|
|
|
#define DataOutputRate_75HZ 0x06
|
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
// constructor
|
|
|
|
AP_Compass_HMC5843::AP_Compass_HMC5843(Compass &compass):
|
|
|
|
AP_Compass_Backend(compass),
|
2015-04-24 02:12:41 -03:00
|
|
|
_retry_time(0),
|
2015-03-13 05:16:50 -03:00
|
|
|
_i2c_sem(NULL),
|
|
|
|
_mag_x(0),
|
|
|
|
_mag_y(0),
|
|
|
|
_mag_z(0),
|
|
|
|
_mag_x_accum(0),
|
|
|
|
_mag_y_accum(0),
|
|
|
|
_mag_z_accum(0),
|
2015-02-23 19:17:44 -04:00
|
|
|
_accum_count(0),
|
2015-03-13 05:16:50 -03:00
|
|
|
_last_accum_time(0),
|
|
|
|
_compass_instance(0),
|
2015-02-23 19:17:44 -04:00
|
|
|
_product_id(0)
|
2014-11-15 21:58:23 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
// detect the sensor
|
|
|
|
AP_Compass_Backend *AP_Compass_HMC5843::detect(Compass &compass)
|
|
|
|
{
|
|
|
|
AP_Compass_HMC5843 *sensor = new AP_Compass_HMC5843(compass);
|
|
|
|
if (sensor == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!sensor->init()) {
|
|
|
|
delete sensor;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
2011-07-09 09:10:00 -03:00
|
|
|
// read_register - read a register value
|
2011-12-28 05:31:36 -04:00
|
|
|
bool AP_Compass_HMC5843::read_register(uint8_t address, uint8_t *value)
|
2011-07-09 09:10:00 -03:00
|
|
|
{
|
2012-10-11 17:48:39 -03:00
|
|
|
if (hal.i2c->readRegister((uint8_t)COMPASS_ADDRESS, address, value) != 0) {
|
2015-02-23 19:17:44 -04:00
|
|
|
_retry_time = hal.scheduler->millis() + 1000;
|
2012-08-17 03:19:22 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-07-09 09:10:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write_register - update a register value
|
2012-10-11 17:48:39 -03:00
|
|
|
bool AP_Compass_HMC5843::write_register(uint8_t address, uint8_t value)
|
2011-07-09 09:10:00 -03:00
|
|
|
{
|
2012-10-11 17:48:39 -03:00
|
|
|
if (hal.i2c->writeRegister((uint8_t)COMPASS_ADDRESS, address, value) != 0) {
|
2015-02-23 19:17:44 -04:00
|
|
|
_retry_time = hal.scheduler->millis() + 1000;
|
2012-08-17 03:19:22 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-07-09 09:10:00 -03:00
|
|
|
}
|
|
|
|
|
2011-08-13 05:17:25 -03:00
|
|
|
// Read Sensor data
|
|
|
|
bool AP_Compass_HMC5843::read_raw()
|
|
|
|
{
|
2012-08-17 03:19:22 -03:00
|
|
|
uint8_t buff[6];
|
|
|
|
|
2012-10-11 17:48:39 -03:00
|
|
|
if (hal.i2c->readRegisters(COMPASS_ADDRESS, 0x03, 6, buff) != 0) {
|
2015-02-23 19:17:44 -04:00
|
|
|
hal.i2c->setHighSpeed(false);
|
|
|
|
_retry_time = hal.scheduler->millis() + 1000;
|
2013-01-04 18:26:26 -04:00
|
|
|
_i2c_sem->give();
|
2012-08-17 03:19:22 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t rx, ry, rz;
|
2013-09-23 00:20:15 -03:00
|
|
|
rx = (((int16_t)buff[0]) << 8) | buff[1];
|
2015-02-23 19:17:44 -04:00
|
|
|
if (_product_id == AP_COMPASS_TYPE_HMC5883L) {
|
2013-09-23 00:20:15 -03:00
|
|
|
rz = (((int16_t)buff[2]) << 8) | buff[3];
|
|
|
|
ry = (((int16_t)buff[4]) << 8) | buff[5];
|
2012-08-17 03:19:22 -03:00
|
|
|
} else {
|
2013-09-23 00:20:15 -03:00
|
|
|
ry = (((int16_t)buff[2]) << 8) | buff[3];
|
|
|
|
rz = (((int16_t)buff[4]) << 8) | buff[5];
|
2012-08-17 03:19:22 -03:00
|
|
|
}
|
|
|
|
if (rx == -4096 || ry == -4096 || rz == -4096) {
|
|
|
|
// no valid data available
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-26 00:42:00 -03:00
|
|
|
_mag_x = -rx;
|
|
|
|
_mag_y = ry;
|
|
|
|
_mag_z = -rz;
|
2012-08-17 03:19:22 -03:00
|
|
|
|
|
|
|
return true;
|
2011-08-13 05:17:25 -03:00
|
|
|
}
|
|
|
|
|
2011-07-03 05:32:58 -03:00
|
|
|
|
2012-08-26 00:42:00 -03:00
|
|
|
// accumulate a reading from the magnetometer
|
|
|
|
void AP_Compass_HMC5843::accumulate(void)
|
|
|
|
{
|
2013-10-07 21:10:53 -03:00
|
|
|
if (!_initialised) {
|
|
|
|
// someone has tried to enable a compass for the first time
|
|
|
|
// mid-flight .... we can't do that yet (especially as we won't
|
|
|
|
// have the right orientation!)
|
|
|
|
return;
|
|
|
|
}
|
2012-10-11 17:48:39 -03:00
|
|
|
uint32_t tnow = hal.scheduler->micros();
|
2015-02-23 19:17:44 -04:00
|
|
|
if (_accum_count != 0 && (tnow - _last_accum_time) < 13333) {
|
2012-08-26 00:42:00 -03:00
|
|
|
// the compass gets new data at 75Hz
|
|
|
|
return;
|
|
|
|
}
|
2013-01-04 18:26:26 -04:00
|
|
|
|
2013-10-07 21:28:05 -03:00
|
|
|
if (!_i2c_sem->take(1)) {
|
2013-01-09 05:42:20 -04:00
|
|
|
// the bus is busy - try again later
|
|
|
|
return;
|
|
|
|
}
|
2013-01-04 18:26:26 -04:00
|
|
|
bool result = read_raw();
|
|
|
|
_i2c_sem->give();
|
|
|
|
|
|
|
|
if (result) {
|
2012-08-26 00:42:00 -03:00
|
|
|
// the _mag_N values are in the range -2048 to 2047, so we can
|
|
|
|
// accumulate up to 15 of them in an int16_t. Let's make it 14
|
|
|
|
// for ease of calculation. We expect to do reads at 10Hz, and
|
|
|
|
// we get new data at most 75Hz, so we don't expect to
|
|
|
|
// accumulate more than 8 before a read
|
|
|
|
_mag_x_accum += _mag_x;
|
|
|
|
_mag_y_accum += _mag_y;
|
|
|
|
_mag_z_accum += _mag_z;
|
|
|
|
_accum_count++;
|
|
|
|
if (_accum_count == 14) {
|
|
|
|
_mag_x_accum /= 2;
|
|
|
|
_mag_y_accum /= 2;
|
|
|
|
_mag_z_accum /= 2;
|
|
|
|
_accum_count = 7;
|
|
|
|
}
|
|
|
|
_last_accum_time = tnow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-28 05:31:36 -04:00
|
|
|
|
|
|
|
/*
|
2012-08-17 03:19:22 -03:00
|
|
|
* re-initialise after a IO error
|
2011-12-28 05:31:36 -04:00
|
|
|
*/
|
|
|
|
bool AP_Compass_HMC5843::re_initialise()
|
|
|
|
{
|
2012-08-17 03:19:22 -03:00
|
|
|
if (!write_register(ConfigRegA, _base_config) ||
|
|
|
|
!write_register(ConfigRegB, magGain) ||
|
|
|
|
!write_register(ModeRegister, ContinuousConversion))
|
|
|
|
return false;
|
|
|
|
return true;
|
2011-12-28 05:31:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-14 00:27:07 -04:00
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2012-08-17 03:19:22 -03:00
|
|
|
bool
|
2011-06-28 13:30:42 -03:00
|
|
|
AP_Compass_HMC5843::init()
|
2011-02-14 00:27:07 -04:00
|
|
|
{
|
2012-08-17 03:19:22 -03:00
|
|
|
int numAttempts = 0, good_count = 0;
|
|
|
|
bool success = false;
|
2012-10-11 17:48:39 -03:00
|
|
|
uint8_t calibration_gain = 0x20;
|
2012-08-17 03:19:22 -03:00
|
|
|
uint16_t expected_x = 715;
|
|
|
|
uint16_t expected_yz = 715;
|
|
|
|
float gain_multiple = 1.0;
|
|
|
|
|
2014-11-13 07:30:20 -04:00
|
|
|
hal.scheduler->suspend_timer_procs();
|
2012-10-11 17:48:39 -03:00
|
|
|
hal.scheduler->delay(10);
|
2012-08-17 03:19:22 -03:00
|
|
|
|
2013-01-04 18:26:26 -04:00
|
|
|
_i2c_sem = hal.i2c->get_semaphore();
|
2013-01-09 05:42:20 -04:00
|
|
|
if (!_i2c_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
|
|
|
hal.scheduler->panic(PSTR("Failed to get HMC5843 semaphore"));
|
|
|
|
}
|
2013-01-04 18:26:26 -04:00
|
|
|
|
2012-08-17 03:19:22 -03:00
|
|
|
// determine if we are using 5843 or 5883L
|
2013-10-07 21:10:53 -03:00
|
|
|
_base_config = 0;
|
2012-08-17 03:19:22 -03:00
|
|
|
if (!write_register(ConfigRegA, SampleAveraging_8<<5 | DataOutputRate_75HZ<<2 | NormalOperation) ||
|
|
|
|
!read_register(ConfigRegA, &_base_config)) {
|
2013-01-04 18:26:26 -04:00
|
|
|
_i2c_sem->give();
|
2014-11-13 07:30:20 -04:00
|
|
|
hal.scheduler->resume_timer_procs();
|
2012-08-17 03:19:22 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( _base_config == (SampleAveraging_8<<5 | DataOutputRate_75HZ<<2 | NormalOperation)) {
|
|
|
|
// a 5883L supports the sample averaging config
|
2015-02-23 19:17:44 -04:00
|
|
|
_product_id = AP_COMPASS_TYPE_HMC5883L;
|
2012-08-17 03:19:22 -03:00
|
|
|
calibration_gain = 0x60;
|
2014-01-22 01:07:50 -04:00
|
|
|
/*
|
|
|
|
note that the HMC5883 datasheet gives the x and y expected
|
|
|
|
values as 766 and the z as 713. Experiments have shown the x
|
|
|
|
axis is around 766, and the y and z closer to 713.
|
|
|
|
*/
|
2012-08-17 03:19:22 -03:00
|
|
|
expected_x = 766;
|
|
|
|
expected_yz = 713;
|
2015-04-24 00:28:15 -03:00
|
|
|
gain_multiple = 660.0f / 1090; // adjustment for runtime vs calibration gain
|
2012-08-17 03:19:22 -03:00
|
|
|
} else if (_base_config == (NormalOperation | DataOutputRate_75HZ<<2)) {
|
2015-02-23 19:17:44 -04:00
|
|
|
_product_id = AP_COMPASS_TYPE_HMC5843;
|
2012-08-17 03:19:22 -03:00
|
|
|
} else {
|
|
|
|
// not behaving like either supported compass type
|
2013-01-04 18:26:26 -04:00
|
|
|
_i2c_sem->give();
|
2014-11-13 07:30:20 -04:00
|
|
|
hal.scheduler->resume_timer_procs();
|
2012-08-17 03:19:22 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
calibration[0] = 0;
|
|
|
|
calibration[1] = 0;
|
|
|
|
calibration[2] = 0;
|
|
|
|
|
2014-01-22 01:07:50 -04:00
|
|
|
while ( success == 0 && numAttempts < 25 && good_count < 5)
|
2012-08-17 03:19:22 -03:00
|
|
|
{
|
|
|
|
// record number of attempts at initialisation
|
|
|
|
numAttempts++;
|
|
|
|
|
|
|
|
// force positiveBias (compass should return 715 for all channels)
|
|
|
|
if (!write_register(ConfigRegA, PositiveBiasConfig))
|
|
|
|
continue; // compass not responding on the bus
|
2012-10-11 17:48:39 -03:00
|
|
|
hal.scheduler->delay(50);
|
2012-08-17 03:19:22 -03:00
|
|
|
|
|
|
|
// set gains
|
|
|
|
if (!write_register(ConfigRegB, calibration_gain) ||
|
|
|
|
!write_register(ModeRegister, SingleConversion))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// read values from the compass
|
2012-10-11 17:48:39 -03:00
|
|
|
hal.scheduler->delay(50);
|
2012-08-17 03:19:22 -03:00
|
|
|
if (!read_raw())
|
|
|
|
continue; // we didn't read valid values
|
|
|
|
|
2012-10-11 17:48:39 -03:00
|
|
|
hal.scheduler->delay(10);
|
2012-08-17 03:19:22 -03:00
|
|
|
|
|
|
|
float cal[3];
|
|
|
|
|
2014-01-22 01:07:50 -04:00
|
|
|
// hal.console->printf_P(PSTR("mag %d %d %d\n"), _mag_x, _mag_y, _mag_z);
|
2013-01-10 14:42:24 -04:00
|
|
|
cal[0] = fabsf(expected_x / (float)_mag_x);
|
|
|
|
cal[1] = fabsf(expected_yz / (float)_mag_y);
|
|
|
|
cal[2] = fabsf(expected_yz / (float)_mag_z);
|
2012-08-17 03:19:22 -03:00
|
|
|
|
2014-01-22 01:07:50 -04:00
|
|
|
// hal.console->printf_P(PSTR("cal=%.2f %.2f %.2f\n"), cal[0], cal[1], cal[2]);
|
|
|
|
|
|
|
|
// we throw away the first two samples as the compass may
|
|
|
|
// still be changing its state from the application of the
|
|
|
|
// strap excitation. After that we accept values in a
|
|
|
|
// reasonable range
|
|
|
|
if (numAttempts > 2 &&
|
|
|
|
cal[0] > 0.7f && cal[0] < 1.35f &&
|
2013-11-27 02:36:34 -04:00
|
|
|
cal[1] > 0.7f && cal[1] < 1.35f &&
|
|
|
|
cal[2] > 0.7f && cal[2] < 1.35f) {
|
2014-01-22 01:07:50 -04:00
|
|
|
// hal.console->printf_P(PSTR("cal=%.2f %.2f %.2f good\n"), cal[0], cal[1], cal[2]);
|
2012-08-17 03:19:22 -03:00
|
|
|
good_count++;
|
|
|
|
calibration[0] += cal[0];
|
|
|
|
calibration[1] += cal[1];
|
|
|
|
calibration[2] += cal[2];
|
|
|
|
}
|
2011-08-13 05:17:25 -03:00
|
|
|
|
|
|
|
#if 0
|
2012-08-17 03:19:22 -03:00
|
|
|
/* useful for debugging */
|
2013-09-27 21:44:41 -03:00
|
|
|
hal.console->printf_P(PSTR("MagX: %d MagY: %d MagZ: %d\n"), (int)_mag_x, (int)_mag_y, (int)_mag_z);
|
|
|
|
hal.console->printf_P(PSTR("CalX: %.2f CalY: %.2f CalZ: %.2f\n"), cal[0], cal[1], cal[2]);
|
2011-08-13 05:17:25 -03:00
|
|
|
#endif
|
2012-08-17 03:19:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (good_count >= 5) {
|
2014-01-22 01:07:50 -04:00
|
|
|
/*
|
|
|
|
The use of gain_multiple below is incorrect, as the gain
|
|
|
|
difference between 2.5Ga mode and 1Ga mode is already taken
|
|
|
|
into account by the expected_x and expected_yz values. We
|
|
|
|
are not going to fix it however as it would mean all
|
|
|
|
APM1/APM2 users redoing their compass calibration. The
|
|
|
|
impact is that the values we report on APM1/APM2 are lower
|
|
|
|
than they should be (by a multiple of about 0.6). This
|
|
|
|
doesn't have any impact other than the learned compass
|
|
|
|
offsets
|
|
|
|
*/
|
2012-08-17 03:19:22 -03:00
|
|
|
calibration[0] = calibration[0] * gain_multiple / good_count;
|
|
|
|
calibration[1] = calibration[1] * gain_multiple / good_count;
|
|
|
|
calibration[2] = calibration[2] * gain_multiple / good_count;
|
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
/* best guess */
|
|
|
|
calibration[0] = 1.0;
|
|
|
|
calibration[1] = 1.0;
|
|
|
|
calibration[2] = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// leave test mode
|
|
|
|
if (!re_initialise()) {
|
2013-01-04 18:26:26 -04:00
|
|
|
_i2c_sem->give();
|
2014-11-13 07:30:20 -04:00
|
|
|
hal.scheduler->resume_timer_procs();
|
2012-08-17 03:19:22 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-04 18:26:26 -04:00
|
|
|
_i2c_sem->give();
|
2014-11-13 07:30:20 -04:00
|
|
|
hal.scheduler->resume_timer_procs();
|
2012-08-17 03:19:22 -03:00
|
|
|
_initialised = true;
|
|
|
|
|
2012-08-26 00:42:00 -03:00
|
|
|
// perform an initial read
|
|
|
|
read();
|
|
|
|
|
2014-01-22 01:07:50 -04:00
|
|
|
#if 0
|
|
|
|
hal.console->printf_P(PSTR("CalX: %.2f CalY: %.2f CalZ: %.2f\n"),
|
|
|
|
calibration[0], calibration[1], calibration[2]);
|
|
|
|
#endif
|
|
|
|
|
2015-03-13 02:43:34 -03:00
|
|
|
if (success) {
|
|
|
|
// register the compass instance in the frontend
|
|
|
|
_compass_instance = register_compass();
|
2015-07-06 09:03:30 -03:00
|
|
|
set_dev_id(_compass_instance, _product_id);
|
2015-03-13 02:43:34 -03:00
|
|
|
}
|
2014-11-15 21:58:23 -04:00
|
|
|
|
2012-08-17 03:19:22 -03:00
|
|
|
return success;
|
2011-02-14 00:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read Sensor data
|
2015-02-23 19:17:44 -04:00
|
|
|
void AP_Compass_HMC5843::read()
|
2011-02-14 00:27:07 -04:00
|
|
|
{
|
2012-08-17 03:19:22 -03:00
|
|
|
if (!_initialised) {
|
|
|
|
// someone has tried to enable a compass for the first time
|
|
|
|
// mid-flight .... we can't do that yet (especially as we won't
|
|
|
|
// have the right orientation!)
|
2015-02-23 19:17:44 -04:00
|
|
|
return;
|
2012-08-17 03:19:22 -03:00
|
|
|
}
|
2015-02-23 19:17:44 -04:00
|
|
|
if (_retry_time != 0) {
|
2012-10-11 17:48:39 -03:00
|
|
|
if (hal.scheduler->millis() < _retry_time) {
|
2015-02-23 19:17:44 -04:00
|
|
|
return;
|
2012-08-17 03:19:22 -03:00
|
|
|
}
|
|
|
|
if (!re_initialise()) {
|
2012-10-11 17:48:39 -03:00
|
|
|
_retry_time = hal.scheduler->millis() + 1000;
|
|
|
|
hal.i2c->setHighSpeed(false);
|
2015-02-23 19:17:44 -04:00
|
|
|
return;
|
2012-08-17 03:19:22 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 00:42:00 -03:00
|
|
|
if (_accum_count == 0) {
|
|
|
|
accumulate();
|
2015-02-23 19:17:44 -04:00
|
|
|
if (_retry_time != 0) {
|
2012-10-11 17:48:39 -03:00
|
|
|
hal.i2c->setHighSpeed(false);
|
2015-02-23 19:17:44 -04:00
|
|
|
return;
|
2012-08-26 00:42:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
Vector3f field(_mag_x_accum * calibration[0],
|
|
|
|
_mag_y_accum * calibration[1],
|
|
|
|
_mag_z_accum * calibration[2]);
|
|
|
|
field /= _accum_count;
|
|
|
|
|
2012-08-26 00:42:00 -03:00
|
|
|
_accum_count = 0;
|
|
|
|
_mag_x_accum = _mag_y_accum = _mag_z_accum = 0;
|
2012-08-17 03:19:22 -03:00
|
|
|
|
|
|
|
// rotate to the desired orientation
|
2015-02-23 19:17:44 -04:00
|
|
|
if (_product_id == AP_COMPASS_TYPE_HMC5883L) {
|
|
|
|
field.rotate(ROTATION_YAW_90);
|
2012-08-17 03:19:22 -03:00
|
|
|
}
|
2013-01-13 01:02:49 -04:00
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
publish_field(field, _compass_instance);
|
|
|
|
_retry_time = 0;
|
2011-02-14 00:27:07 -04:00
|
|
|
}
|