2015-08-17 21:09:52 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2016-03-21 12:01:53 -03:00
|
|
|
#include <utility>
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2016-03-21 10:13:42 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
#include "AP_Compass_LSM303D.h"
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
|
|
|
#include <AP_HAL_Linux/GPIO.h>
|
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
|
|
|
|
#define LSM303D_DRDY_M_PIN RPI_GPIO_27
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LSM303D_DRDY_M_PIN
|
|
|
|
#define LSM303D_DRDY_M_PIN -1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SPI protocol address bits */
|
|
|
|
#define DIR_READ (1<<7)
|
|
|
|
#define DIR_WRITE (0<<7)
|
|
|
|
#define ADDR_INCREMENT (1<<6)
|
|
|
|
|
|
|
|
/* register addresses: A: accel, M: mag, T: temp */
|
|
|
|
#define ADDR_WHO_AM_I 0x0F
|
|
|
|
#define WHO_I_AM 0x49
|
|
|
|
|
|
|
|
#define ADDR_OUT_TEMP_L 0x05
|
|
|
|
#define ADDR_OUT_TEMP_H 0x06
|
|
|
|
#define ADDR_STATUS_M 0x07
|
|
|
|
#define ADDR_OUT_X_L_M 0x08
|
|
|
|
#define ADDR_OUT_X_H_M 0x09
|
|
|
|
#define ADDR_OUT_Y_L_M 0x0A
|
|
|
|
#define ADDR_OUT_Y_H_M 0x0B
|
|
|
|
#define ADDR_OUT_Z_L_M 0x0C
|
|
|
|
#define ADDR_OUT_Z_H_M 0x0D
|
|
|
|
|
|
|
|
#define ADDR_INT_CTRL_M 0x12
|
|
|
|
#define ADDR_INT_SRC_M 0x13
|
|
|
|
#define ADDR_REFERENCE_X 0x1c
|
|
|
|
#define ADDR_REFERENCE_Y 0x1d
|
|
|
|
#define ADDR_REFERENCE_Z 0x1e
|
|
|
|
|
|
|
|
#define ADDR_STATUS_A 0x27
|
|
|
|
#define ADDR_OUT_X_L_A 0x28
|
|
|
|
#define ADDR_OUT_X_H_A 0x29
|
|
|
|
#define ADDR_OUT_Y_L_A 0x2A
|
|
|
|
#define ADDR_OUT_Y_H_A 0x2B
|
|
|
|
#define ADDR_OUT_Z_L_A 0x2C
|
|
|
|
#define ADDR_OUT_Z_H_A 0x2D
|
|
|
|
|
|
|
|
#define ADDR_CTRL_REG0 0x1F
|
|
|
|
#define ADDR_CTRL_REG1 0x20
|
|
|
|
#define ADDR_CTRL_REG2 0x21
|
|
|
|
#define ADDR_CTRL_REG3 0x22
|
|
|
|
#define ADDR_CTRL_REG4 0x23
|
|
|
|
#define ADDR_CTRL_REG5 0x24
|
|
|
|
#define ADDR_CTRL_REG6 0x25
|
|
|
|
#define ADDR_CTRL_REG7 0x26
|
|
|
|
|
|
|
|
#define ADDR_FIFO_CTRL 0x2e
|
|
|
|
#define ADDR_FIFO_SRC 0x2f
|
|
|
|
|
|
|
|
#define ADDR_IG_CFG1 0x30
|
|
|
|
#define ADDR_IG_SRC1 0x31
|
|
|
|
#define ADDR_IG_THS1 0x32
|
|
|
|
#define ADDR_IG_DUR1 0x33
|
|
|
|
#define ADDR_IG_CFG2 0x34
|
|
|
|
#define ADDR_IG_SRC2 0x35
|
|
|
|
#define ADDR_IG_THS2 0x36
|
|
|
|
#define ADDR_IG_DUR2 0x37
|
|
|
|
#define ADDR_CLICK_CFG 0x38
|
|
|
|
#define ADDR_CLICK_SRC 0x39
|
|
|
|
#define ADDR_CLICK_THS 0x3a
|
|
|
|
#define ADDR_TIME_LIMIT 0x3b
|
|
|
|
#define ADDR_TIME_LATENCY 0x3c
|
|
|
|
#define ADDR_TIME_WINDOW 0x3d
|
|
|
|
#define ADDR_ACT_THS 0x3e
|
|
|
|
#define ADDR_ACT_DUR 0x3f
|
|
|
|
|
|
|
|
#define REG1_RATE_BITS_A ((1<<7) | (1<<6) | (1<<5) | (1<<4))
|
|
|
|
#define REG1_POWERDOWN_A ((0<<7) | (0<<6) | (0<<5) | (0<<4))
|
|
|
|
#define REG1_RATE_3_125HZ_A ((0<<7) | (0<<6) | (0<<5) | (1<<4))
|
|
|
|
#define REG1_RATE_6_25HZ_A ((0<<7) | (0<<6) | (1<<5) | (0<<4))
|
|
|
|
#define REG1_RATE_12_5HZ_A ((0<<7) | (0<<6) | (1<<5) | (1<<4))
|
|
|
|
#define REG1_RATE_25HZ_A ((0<<7) | (1<<6) | (0<<5) | (0<<4))
|
|
|
|
#define REG1_RATE_50HZ_A ((0<<7) | (1<<6) | (0<<5) | (1<<4))
|
|
|
|
#define REG1_RATE_100HZ_A ((0<<7) | (1<<6) | (1<<5) | (0<<4))
|
|
|
|
#define REG1_RATE_200HZ_A ((0<<7) | (1<<6) | (1<<5) | (1<<4))
|
|
|
|
#define REG1_RATE_400HZ_A ((1<<7) | (0<<6) | (0<<5) | (0<<4))
|
|
|
|
#define REG1_RATE_800HZ_A ((1<<7) | (0<<6) | (0<<5) | (1<<4))
|
|
|
|
#define REG1_RATE_1600HZ_A ((1<<7) | (0<<6) | (1<<5) | (0<<4))
|
|
|
|
|
|
|
|
#define REG1_BDU_UPDATE (1<<3)
|
|
|
|
#define REG1_Z_ENABLE_A (1<<2)
|
|
|
|
#define REG1_Y_ENABLE_A (1<<1)
|
|
|
|
#define REG1_X_ENABLE_A (1<<0)
|
|
|
|
|
|
|
|
#define REG2_ANTIALIAS_FILTER_BW_BITS_A ((1<<7) | (1<<6))
|
|
|
|
#define REG2_AA_FILTER_BW_773HZ_A ((0<<7) | (0<<6))
|
|
|
|
#define REG2_AA_FILTER_BW_194HZ_A ((0<<7) | (1<<6))
|
|
|
|
#define REG2_AA_FILTER_BW_362HZ_A ((1<<7) | (0<<6))
|
|
|
|
#define REG2_AA_FILTER_BW_50HZ_A ((1<<7) | (1<<6))
|
|
|
|
|
|
|
|
#define REG2_FULL_SCALE_BITS_A ((1<<5) | (1<<4) | (1<<3))
|
|
|
|
#define REG2_FULL_SCALE_2G_A ((0<<5) | (0<<4) | (0<<3))
|
|
|
|
#define REG2_FULL_SCALE_4G_A ((0<<5) | (0<<4) | (1<<3))
|
|
|
|
#define REG2_FULL_SCALE_6G_A ((0<<5) | (1<<4) | (0<<3))
|
|
|
|
#define REG2_FULL_SCALE_8G_A ((0<<5) | (1<<4) | (1<<3))
|
|
|
|
#define REG2_FULL_SCALE_16G_A ((1<<5) | (0<<4) | (0<<3))
|
|
|
|
|
|
|
|
#define REG5_ENABLE_T (1<<7)
|
|
|
|
|
|
|
|
#define REG5_RES_HIGH_M ((1<<6) | (1<<5))
|
|
|
|
#define REG5_RES_LOW_M ((0<<6) | (0<<5))
|
|
|
|
|
|
|
|
#define REG5_RATE_BITS_M ((1<<4) | (1<<3) | (1<<2))
|
|
|
|
#define REG5_RATE_3_125HZ_M ((0<<4) | (0<<3) | (0<<2))
|
|
|
|
#define REG5_RATE_6_25HZ_M ((0<<4) | (0<<3) | (1<<2))
|
|
|
|
#define REG5_RATE_12_5HZ_M ((0<<4) | (1<<3) | (0<<2))
|
|
|
|
#define REG5_RATE_25HZ_M ((0<<4) | (1<<3) | (1<<2))
|
|
|
|
#define REG5_RATE_50HZ_M ((1<<4) | (0<<3) | (0<<2))
|
|
|
|
#define REG5_RATE_100HZ_M ((1<<4) | (0<<3) | (1<<2))
|
|
|
|
#define REG5_RATE_DO_NOT_USE_M ((1<<4) | (1<<3) | (0<<2))
|
|
|
|
|
|
|
|
#define REG6_FULL_SCALE_BITS_M ((1<<6) | (1<<5))
|
|
|
|
#define REG6_FULL_SCALE_2GA_M ((0<<6) | (0<<5))
|
|
|
|
#define REG6_FULL_SCALE_4GA_M ((0<<6) | (1<<5))
|
|
|
|
#define REG6_FULL_SCALE_8GA_M ((1<<6) | (0<<5))
|
|
|
|
#define REG6_FULL_SCALE_12GA_M ((1<<6) | (1<<5))
|
|
|
|
|
|
|
|
#define REG7_CONT_MODE_M ((0<<1) | (0<<0))
|
|
|
|
|
|
|
|
#define INT_CTRL_M 0x12
|
|
|
|
#define INT_SRC_M 0x13
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
#define LSM303D_MAG_DEFAULT_RANGE_GA 2
|
2015-08-17 21:09:52 -03:00
|
|
|
#define LSM303D_MAG_DEFAULT_RATE 100
|
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
AP_Compass_LSM303D::AP_Compass_LSM303D(Compass &compass, AP_HAL::OwnPtr<AP_HAL::Device> dev)
|
2016-03-21 10:13:42 -03:00
|
|
|
: AP_Compass_Backend(compass)
|
2016-03-21 12:01:53 -03:00
|
|
|
, _dev(std::move(dev))
|
2016-03-21 10:13:42 -03:00
|
|
|
{
|
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
AP_Compass_Backend *AP_Compass_LSM303D::probe(Compass &compass,
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> dev)
|
2015-08-17 21:09:52 -03:00
|
|
|
{
|
2016-03-21 12:01:53 -03:00
|
|
|
AP_Compass_LSM303D *sensor = new AP_Compass_LSM303D(compass, std::move(dev));
|
2016-03-21 10:13:42 -03:00
|
|
|
if (!sensor || !sensor->init()) {
|
2015-08-17 21:09:52 -03:00
|
|
|
delete sensor;
|
2016-03-21 10:13:42 -03:00
|
|
|
return nullptr;
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
2016-03-21 10:13:42 -03:00
|
|
|
|
2015-08-17 21:09:52 -03:00
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AP_Compass_LSM303D::_register_read(uint8_t reg)
|
|
|
|
{
|
2016-03-21 12:01:53 -03:00
|
|
|
uint8_t val = 0;
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
reg |= DIR_READ;
|
|
|
|
_dev->read_registers(reg, &val, 1);
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
return val;
|
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
bool AP_Compass_LSM303D::_block_read(uint8_t reg, uint8_t *buf, uint32_t size)
|
|
|
|
{
|
|
|
|
reg |= DIR_READ | ADDR_INCREMENT;
|
|
|
|
return _dev->read_registers(reg, buf, size);
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_LSM303D::_register_write(uint8_t reg, uint8_t val)
|
|
|
|
{
|
2016-03-21 12:01:53 -03:00
|
|
|
_dev->write_register(reg, val);
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_LSM303D::_register_modify(uint8_t reg, uint8_t clearbits, uint8_t setbits)
|
|
|
|
{
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
val = _register_read(reg);
|
|
|
|
val &= ~clearbits;
|
|
|
|
val |= setbits;
|
|
|
|
_register_write(reg, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the LSM303D has new data available for both the mag and
|
|
|
|
* the accels.
|
|
|
|
*/
|
|
|
|
bool AP_Compass_LSM303D::_data_ready()
|
|
|
|
{
|
|
|
|
return (_drdy_pin_m->read()) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read Sensor data
|
2016-03-21 12:01:53 -03:00
|
|
|
bool AP_Compass_LSM303D::_read_sample()
|
2015-08-17 21:09:52 -03:00
|
|
|
{
|
2016-03-21 12:01:53 -03:00
|
|
|
struct PACKED {
|
|
|
|
uint8_t status;
|
|
|
|
int16_t x;
|
|
|
|
int16_t y;
|
|
|
|
int16_t z;
|
|
|
|
} rx;
|
|
|
|
|
2015-08-17 21:09:52 -03:00
|
|
|
if (_register_read(ADDR_CTRL_REG7) != _reg7_expected) {
|
2016-03-21 10:13:42 -03:00
|
|
|
hal.console->println("LSM303D _read_data_transaction_accel: _reg7_expected unexpected");
|
2015-08-17 21:09:52 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_data_ready()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
if (!_block_read(ADDR_STATUS_M, (uint8_t *) &rx, sizeof(rx))) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:07:52 -03:00
|
|
|
/* check for overrun */
|
|
|
|
if ((rx.status & 0x70) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:14:55 -03:00
|
|
|
if (rx.x == 0 && rx.y == 0 && rx.z == 0) {
|
2015-08-17 21:09:52 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
_mag_x = rx.x;
|
|
|
|
_mag_y = rx.y;
|
|
|
|
_mag_z = rx.z;
|
|
|
|
|
2015-08-17 21:09:52 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
bool AP_Compass_LSM303D::init()
|
2015-08-17 21:09:52 -03:00
|
|
|
{
|
|
|
|
// TODO: support users without data ready pin
|
2016-03-21 10:13:42 -03:00
|
|
|
if (LSM303D_DRDY_M_PIN < 0) {
|
2015-08-17 21:09:52 -03:00
|
|
|
return false;
|
2016-03-21 10:13:42 -03:00
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
_drdy_pin_m = hal.gpio->channel(LSM303D_DRDY_M_PIN);
|
|
|
|
_drdy_pin_m->mode(HAL_GPIO_INPUT);
|
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
hal.scheduler->suspend_timer_procs();
|
|
|
|
bool success = _hardware_init();
|
|
|
|
hal.scheduler->resume_timer_procs();
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
_initialised = true;
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2015-09-14 10:22:23 -03:00
|
|
|
/* register the compass instance in the frontend */
|
2015-08-17 21:09:52 -03:00
|
|
|
_compass_instance = register_compass();
|
2016-03-21 10:13:42 -03:00
|
|
|
set_dev_id(_compass_instance, AP_COMPASS_TYPE_LSM303D);
|
|
|
|
|
2015-08-17 21:09:52 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX && CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
|
2016-03-21 12:01:53 -03:00
|
|
|
// FIXME: wrong way to force internal compass
|
2015-08-17 21:09:52 -03:00
|
|
|
set_external(_compass_instance, false);
|
|
|
|
#endif
|
|
|
|
|
2015-09-14 10:22:23 -03:00
|
|
|
hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&AP_Compass_LSM303D::_update, void));
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
return true;
|
2015-09-14 10:22:23 -03:00
|
|
|
}
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
bool AP_Compass_LSM303D::_hardware_init()
|
2015-08-17 21:09:52 -03:00
|
|
|
{
|
2016-03-21 12:01:53 -03:00
|
|
|
if (!_dev->get_semaphore()->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2015-11-19 23:09:17 -04:00
|
|
|
AP_HAL::panic("LSM303D: Unable to get semaphore");
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// initially run the bus at low speed
|
2016-03-21 12:01:53 -03:00
|
|
|
_dev->set_speed(AP_HAL::Device::SPEED_LOW);
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
// Test WHOAMI
|
|
|
|
uint8_t whoami = _register_read(ADDR_WHO_AM_I);
|
|
|
|
if (whoami != WHO_I_AM) {
|
|
|
|
hal.console->printf("LSM303D: unexpected WHOAMI 0x%x\n", (unsigned)whoami);
|
|
|
|
goto fail_whoami;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t tries;
|
|
|
|
for (tries = 0; tries < 5; tries++) {
|
|
|
|
// ensure the chip doesn't interpret any other bus traffic as I2C
|
|
|
|
_disable_i2c();
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
/* enable mag */
|
|
|
|
_reg7_expected = REG7_CONT_MODE_M;
|
|
|
|
_register_write(ADDR_CTRL_REG7, _reg7_expected);
|
|
|
|
_register_write(ADDR_CTRL_REG5, REG5_RES_HIGH_M);
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
// DRDY on MAG on INT2
|
|
|
|
_register_write(ADDR_CTRL_REG4, 0x04);
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
_mag_set_range(LSM303D_MAG_DEFAULT_RANGE_GA);
|
|
|
|
_mag_set_samplerate(LSM303D_MAG_DEFAULT_RATE);
|
|
|
|
|
|
|
|
hal.scheduler->delay(10);
|
|
|
|
if (_data_ready()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tries == 5) {
|
|
|
|
hal.console->println("Failed to boot LSM303D 5 times");
|
|
|
|
goto fail_tries;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dev->set_speed(AP_HAL::Device::SPEED_HIGH);
|
|
|
|
_dev->get_semaphore()->give();
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
return true;
|
2016-03-21 12:01:53 -03:00
|
|
|
|
|
|
|
fail_tries:
|
|
|
|
fail_whoami:
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
_dev->set_speed(AP_HAL::Device::SPEED_HIGH);
|
|
|
|
return false;
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_LSM303D::_update()
|
|
|
|
{
|
2016-03-21 10:13:42 -03:00
|
|
|
uint32_t time_us = AP_HAL::micros();
|
|
|
|
Vector3f raw_field;
|
|
|
|
|
2015-11-19 23:09:17 -04:00
|
|
|
if (AP_HAL::micros() - _last_update_timestamp < 10000) {
|
2015-08-17 21:09:52 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
if (!_dev->get_semaphore()->take_nonblocking()) {
|
2015-08-17 21:09:52 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:01:53 -03:00
|
|
|
if (!_read_sample()) {
|
2016-03-21 10:13:42 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
raw_field = Vector3f(_mag_x, _mag_y, _mag_z) * _mag_range_scale;
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
// rotate raw_field from sensor frame to body frame
|
|
|
|
rotate_field(raw_field, _compass_instance);
|
2015-08-17 21:09:52 -03:00
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
// publish raw_field (uncorrected point sample) for calibration use
|
|
|
|
publish_raw_field(raw_field, time_us, _compass_instance);
|
|
|
|
|
|
|
|
// correct raw_field for known errors
|
|
|
|
correct_field(raw_field, _compass_instance);
|
|
|
|
|
|
|
|
_mag_x_accum += raw_field.x;
|
|
|
|
_mag_y_accum += raw_field.y;
|
|
|
|
_mag_z_accum += raw_field.z;
|
|
|
|
_accum_count++;
|
|
|
|
if (_accum_count == 10) {
|
|
|
|
_mag_x_accum /= 2;
|
|
|
|
_mag_y_accum /= 2;
|
|
|
|
_mag_z_accum /= 2;
|
|
|
|
_accum_count = 5;
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
2016-03-21 10:13:42 -03:00
|
|
|
|
|
|
|
_last_update_timestamp = AP_HAL::micros();
|
|
|
|
|
|
|
|
fail:
|
2016-03-21 12:01:53 -03:00
|
|
|
_dev->get_semaphore()->give();
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read Sensor data
|
|
|
|
void AP_Compass_LSM303D::read()
|
|
|
|
{
|
|
|
|
if (!_initialised) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_accum_count == 0) {
|
|
|
|
/* We're not ready to publish*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-14 10:22:23 -03:00
|
|
|
hal.scheduler->suspend_timer_procs();
|
2016-03-21 10:13:42 -03:00
|
|
|
Vector3f field(_mag_x_accum, _mag_y_accum, _mag_z_accum);
|
2015-08-17 21:09:52 -03:00
|
|
|
field /= _accum_count;
|
|
|
|
|
|
|
|
_accum_count = 0;
|
|
|
|
_mag_x_accum = _mag_y_accum = _mag_z_accum = 0;
|
2015-09-14 10:22:23 -03:00
|
|
|
hal.scheduler->resume_timer_procs();
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
publish_filtered_field(field, _compass_instance);
|
|
|
|
}
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
void AP_Compass_LSM303D::_disable_i2c()
|
2015-08-17 21:09:52 -03:00
|
|
|
{
|
|
|
|
// TODO: use the register names
|
|
|
|
uint8_t a = _register_read(0x02);
|
|
|
|
_register_write(0x02, (0x10 | a));
|
|
|
|
a = _register_read(0x02);
|
|
|
|
_register_write(0x02, (0xF7 & a));
|
|
|
|
a = _register_read(0x15);
|
|
|
|
_register_write(0x15, (0x80 | a));
|
|
|
|
a = _register_read(0x02);
|
|
|
|
_register_write(0x02, (0xE7 & a));
|
|
|
|
}
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
bool AP_Compass_LSM303D::_mag_set_range(uint8_t max_ga)
|
2015-08-17 21:09:52 -03:00
|
|
|
{
|
|
|
|
uint8_t setbits = 0;
|
|
|
|
uint8_t clearbits = REG6_FULL_SCALE_BITS_M;
|
|
|
|
float new_scale_ga_digit = 0.0f;
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
if (max_ga == 0) {
|
2015-08-17 21:09:52 -03:00
|
|
|
max_ga = 12;
|
2016-03-21 10:13:42 -03:00
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
if (max_ga <= 2) {
|
|
|
|
_mag_range_ga = 2;
|
|
|
|
setbits |= REG6_FULL_SCALE_2GA_M;
|
2015-09-14 10:22:23 -03:00
|
|
|
new_scale_ga_digit = 0.080f;
|
2015-08-17 21:09:52 -03:00
|
|
|
} else if (max_ga <= 4) {
|
|
|
|
_mag_range_ga = 4;
|
|
|
|
setbits |= REG6_FULL_SCALE_4GA_M;
|
2015-09-14 10:22:23 -03:00
|
|
|
new_scale_ga_digit = 0.160f;
|
2015-08-17 21:09:52 -03:00
|
|
|
} else if (max_ga <= 8) {
|
|
|
|
_mag_range_ga = 8;
|
|
|
|
setbits |= REG6_FULL_SCALE_8GA_M;
|
2015-09-14 10:22:23 -03:00
|
|
|
new_scale_ga_digit = 0.320f;
|
2015-08-17 21:09:52 -03:00
|
|
|
} else if (max_ga <= 12) {
|
|
|
|
_mag_range_ga = 12;
|
|
|
|
setbits |= REG6_FULL_SCALE_12GA_M;
|
2015-09-14 10:22:23 -03:00
|
|
|
new_scale_ga_digit = 0.479f;
|
2015-08-17 21:09:52 -03:00
|
|
|
} else {
|
2016-03-21 10:13:42 -03:00
|
|
|
return false;
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
_mag_range_scale = new_scale_ga_digit;
|
|
|
|
_register_modify(ADDR_CTRL_REG6, clearbits, setbits);
|
2016-03-21 10:13:42 -03:00
|
|
|
|
|
|
|
return true;
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
bool AP_Compass_LSM303D::_mag_set_samplerate(uint16_t frequency)
|
2015-08-17 21:09:52 -03:00
|
|
|
{
|
|
|
|
uint8_t setbits = 0;
|
|
|
|
uint8_t clearbits = REG5_RATE_BITS_M;
|
|
|
|
|
2016-03-21 10:13:42 -03:00
|
|
|
if (frequency == 0) {
|
2015-08-17 21:09:52 -03:00
|
|
|
frequency = 100;
|
2016-03-21 10:13:42 -03:00
|
|
|
}
|
2015-08-17 21:09:52 -03:00
|
|
|
|
|
|
|
if (frequency <= 25) {
|
|
|
|
setbits |= REG5_RATE_25HZ_M;
|
|
|
|
_mag_samplerate = 25;
|
|
|
|
} else if (frequency <= 50) {
|
|
|
|
setbits |= REG5_RATE_50HZ_M;
|
|
|
|
_mag_samplerate = 50;
|
|
|
|
} else if (frequency <= 100) {
|
|
|
|
setbits |= REG5_RATE_100HZ_M;
|
|
|
|
_mag_samplerate = 100;
|
|
|
|
} else {
|
2016-03-21 10:13:42 -03:00
|
|
|
return false;
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
_register_modify(ADDR_CTRL_REG5, clearbits, setbits);
|
2016-03-21 10:13:42 -03:00
|
|
|
|
|
|
|
return true;
|
2015-08-17 21:09:52 -03:00
|
|
|
}
|