2016-12-19 02:53:48 -04:00
|
|
|
/*
|
|
|
|
* This file 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 file 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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
Driver by Andrew Tridgell, Nov 2016
|
|
|
|
*/
|
|
|
|
#include "AP_Compass_MMC3416.h"
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <utility>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <stdio.h>
|
2019-01-18 00:23:42 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2016-12-19 02:53:48 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
|
|
|
#define REG_PRODUCT_ID 0x20
|
|
|
|
#define REG_XOUT_L 0x00
|
|
|
|
#define REG_STATUS 0x06
|
|
|
|
#define REG_CONTROL0 0x07
|
|
|
|
#define REG_CONTROL1 0x08
|
|
|
|
|
2016-12-19 06:11:00 -04:00
|
|
|
// bits in REG_CONTROL0
|
|
|
|
#define REG_CONTROL0_REFILL 0x80
|
|
|
|
#define REG_CONTROL0_RESET 0x40
|
|
|
|
#define REG_CONTROL0_SET 0x20
|
2016-12-19 18:12:37 -04:00
|
|
|
#define REG_CONTROL0_NB 0x10
|
2016-12-19 06:11:00 -04:00
|
|
|
#define REG_CONTROL0_TM 0x01
|
|
|
|
|
2016-12-19 18:12:37 -04:00
|
|
|
// datasheet says 50ms min for refill
|
|
|
|
#define MIN_DELAY_SET_RESET 50
|
|
|
|
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_Backend *AP_Compass_MMC3416::probe(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
|
2016-12-19 02:53:48 -04:00
|
|
|
bool force_external,
|
|
|
|
enum Rotation rotation)
|
|
|
|
{
|
|
|
|
if (!dev) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_MMC3416 *sensor = new AP_Compass_MMC3416(std::move(dev), force_external, rotation);
|
2016-12-19 02:53:48 -04:00
|
|
|
if (!sensor || !sensor->init()) {
|
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_MMC3416::AP_Compass_MMC3416(AP_HAL::OwnPtr<AP_HAL::Device> _dev,
|
2016-12-19 02:53:48 -04:00
|
|
|
bool _force_external,
|
|
|
|
enum Rotation _rotation)
|
2018-08-06 19:21:27 -03:00
|
|
|
: dev(std::move(_dev))
|
2016-12-19 02:53:48 -04:00
|
|
|
, force_external(_force_external)
|
|
|
|
, rotation(_rotation)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Compass_MMC3416::init()
|
|
|
|
{
|
2020-05-11 06:38:28 -03:00
|
|
|
dev->get_semaphore()->take_blocking();
|
2016-12-19 02:53:48 -04:00
|
|
|
|
|
|
|
dev->set_retries(10);
|
|
|
|
|
|
|
|
uint8_t whoami;
|
|
|
|
if (!dev->read_registers(REG_PRODUCT_ID, &whoami, 1) ||
|
|
|
|
whoami != 0x06) {
|
|
|
|
// not a MMC3416
|
2016-12-19 18:12:37 -04:00
|
|
|
dev->get_semaphore()->give();
|
|
|
|
return false;
|
2016-12-19 02:53:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset sensor
|
|
|
|
dev->write_register(REG_CONTROL1, 0x80);
|
|
|
|
hal.scheduler->delay(10);
|
|
|
|
|
2016-12-19 03:55:29 -04:00
|
|
|
dev->write_register(REG_CONTROL0, 0x00); // single shot
|
|
|
|
dev->write_register(REG_CONTROL1, 0x00); // 16 bit, 7.92ms
|
2016-12-19 02:53:48 -04:00
|
|
|
|
|
|
|
dev->get_semaphore()->give();
|
|
|
|
|
|
|
|
/* register the compass instance in the frontend */
|
2020-05-11 06:38:28 -03:00
|
|
|
dev->set_device_type(DEVTYPE_MMC3416);
|
|
|
|
if (!register_compass(dev->get_bus_id(), compass_instance)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_dev_id(compass_instance, dev->get_bus_id());
|
2016-12-19 02:53:48 -04:00
|
|
|
|
|
|
|
printf("Found a MMC3416 on 0x%x as compass %u\n", dev->get_bus_id(), compass_instance);
|
|
|
|
|
|
|
|
set_rotation(compass_instance, rotation);
|
|
|
|
|
|
|
|
if (force_external) {
|
|
|
|
set_external(compass_instance, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->set_retries(1);
|
|
|
|
|
2016-12-19 03:55:29 -04:00
|
|
|
// call timer() at 100Hz
|
|
|
|
dev->register_periodic_callback(10000,
|
2017-06-08 02:12:15 -03:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_Compass_MMC3416::timer, void));
|
2016-12-19 02:53:48 -04:00
|
|
|
|
2016-12-19 06:11:00 -04:00
|
|
|
// wait 250ms for the compass to make it's initial readings
|
|
|
|
hal.scheduler->delay(250);
|
|
|
|
|
2016-12-19 02:53:48 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-08 02:12:15 -03:00
|
|
|
void AP_Compass_MMC3416::timer()
|
2016-12-19 02:53:48 -04:00
|
|
|
{
|
2016-12-19 18:12:37 -04:00
|
|
|
const uint16_t measure_count_limit = 50;
|
2016-12-19 06:11:00 -04:00
|
|
|
const uint16_t zero_offset = 32768; // 16 bit mode
|
|
|
|
const uint16_t sensitivity = 2048; // counts per Gauss, 16 bit mode
|
|
|
|
const float counts_to_milliGauss = 1.0e3f / sensitivity;
|
2016-12-19 18:12:37 -04:00
|
|
|
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - last_sample_ms > 500) {
|
|
|
|
// seems to be stuck or on first sample, reset state machine
|
|
|
|
state = STATE_REFILL1;
|
|
|
|
last_sample_ms = now;
|
|
|
|
}
|
2016-12-19 03:55:29 -04:00
|
|
|
|
2016-12-19 06:11:00 -04:00
|
|
|
/*
|
|
|
|
we use the SET/RESET method to remove bridge offset every
|
|
|
|
measure_count_limit measurements. This involves a fairly complex
|
|
|
|
state machine, but means we are much less sensitive to
|
|
|
|
temperature changes
|
|
|
|
*/
|
2016-12-19 03:55:29 -04:00
|
|
|
switch (state) {
|
|
|
|
case STATE_REFILL1:
|
2016-12-19 06:11:00 -04:00
|
|
|
if (dev->write_register(REG_CONTROL0, REG_CONTROL0_REFILL)) {
|
2016-12-19 03:55:29 -04:00
|
|
|
state = STATE_REFILL1_WAIT;
|
2016-12-19 18:12:37 -04:00
|
|
|
refill_start_ms = AP_HAL::millis();
|
2016-12-19 03:55:29 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-12-19 18:12:37 -04:00
|
|
|
case STATE_REFILL1_WAIT: {
|
|
|
|
uint8_t status;
|
|
|
|
if (AP_HAL::millis() - refill_start_ms > MIN_DELAY_SET_RESET &&
|
|
|
|
dev->read_registers(REG_STATUS, &status, 1) &&
|
|
|
|
(status & 0x02) == 0) {
|
2016-12-19 06:11:00 -04:00
|
|
|
if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_SET) ||
|
|
|
|
!dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) { // Take Measurement
|
2016-12-19 03:55:29 -04:00
|
|
|
state = STATE_REFILL1;
|
|
|
|
} else {
|
|
|
|
state = STATE_MEASURE_WAIT1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-12-19 18:12:37 -04:00
|
|
|
}
|
2016-12-19 03:55:29 -04:00
|
|
|
|
|
|
|
case STATE_MEASURE_WAIT1: {
|
|
|
|
uint8_t status;
|
|
|
|
if (dev->read_registers(REG_STATUS, &status, 1) && (status & 1)) {
|
|
|
|
if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data0[0], 6)) {
|
|
|
|
state = STATE_REFILL1;
|
|
|
|
break;
|
|
|
|
}
|
2016-12-19 06:11:00 -04:00
|
|
|
if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_REFILL)) {
|
2016-12-19 03:55:29 -04:00
|
|
|
state = STATE_REFILL1;
|
|
|
|
} else {
|
|
|
|
state = STATE_REFILL2_WAIT;
|
2016-12-19 18:12:37 -04:00
|
|
|
refill_start_ms = AP_HAL::millis();
|
2016-12-19 03:55:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-12-19 02:53:48 -04:00
|
|
|
}
|
|
|
|
|
2016-12-19 18:12:37 -04:00
|
|
|
case STATE_REFILL2_WAIT: {
|
|
|
|
uint8_t status;
|
|
|
|
if (AP_HAL::millis() - refill_start_ms > MIN_DELAY_SET_RESET &&
|
|
|
|
dev->read_registers(REG_STATUS, &status, 1) &&
|
|
|
|
(status & 0x02) == 0) {
|
2016-12-19 06:11:00 -04:00
|
|
|
if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_RESET) ||
|
|
|
|
!dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) { // Take Measurement
|
2016-12-19 03:55:29 -04:00
|
|
|
state = STATE_REFILL1;
|
|
|
|
} else {
|
|
|
|
state = STATE_MEASURE_WAIT2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-12-19 18:12:37 -04:00
|
|
|
}
|
2016-12-19 03:55:29 -04:00
|
|
|
|
|
|
|
case STATE_MEASURE_WAIT2: {
|
|
|
|
uint8_t status;
|
|
|
|
if (!dev->read_registers(REG_STATUS, &status, 1) || !(status & 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint16_t data1[3];
|
|
|
|
if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data1[0], 6)) {
|
|
|
|
state = STATE_REFILL1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Vector3f field;
|
2016-12-19 06:11:00 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
calculate field and offset
|
|
|
|
*/
|
2016-12-19 03:55:29 -04:00
|
|
|
Vector3f f1(float(data0[0]) - zero_offset,
|
|
|
|
float(data0[1]) - zero_offset,
|
|
|
|
float(data0[2]) - zero_offset);
|
|
|
|
Vector3f f2(float(data1[0]) - zero_offset,
|
|
|
|
float(data1[1]) - zero_offset,
|
|
|
|
float(data1[2]) - zero_offset);
|
2016-12-19 06:11:00 -04:00
|
|
|
field = (f1 - f2) * (counts_to_milliGauss / 2);
|
|
|
|
Vector3f new_offset = (f1 + f2) * (counts_to_milliGauss / 2);
|
|
|
|
if (!have_initial_offset) {
|
|
|
|
offset = new_offset;
|
2016-12-19 18:12:37 -04:00
|
|
|
have_initial_offset = true;
|
2016-12-19 06:11:00 -04:00
|
|
|
} else {
|
|
|
|
// low pass changes to the offset
|
2019-04-03 21:36:58 -03:00
|
|
|
offset = offset * 0.95f + new_offset * 0.05f;
|
2016-12-19 06:11:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2020-05-11 06:38:28 -03:00
|
|
|
// @LoggerMessage: MMO
|
|
|
|
// @Description: MMC3416 compass data
|
|
|
|
// @Field: TimeUS: Time since system startup
|
|
|
|
// @Field: Nx: new measurement X axis
|
|
|
|
// @Field: Ny: new measurement Y axis
|
|
|
|
// @Field: Nz: new measurement Z axis
|
|
|
|
// @Field: Ox: new offset X axis
|
|
|
|
// @Field: Oy: new offset Y axis
|
|
|
|
// @Field: Oz: new offset Z axis
|
2019-01-18 00:24:08 -04:00
|
|
|
AP::logger().Write("MMO", "TimeUS,Nx,Ny,Nz,Ox,Oy,Oz", "Qffffff",
|
2016-12-19 18:12:37 -04:00
|
|
|
AP_HAL::micros64(),
|
|
|
|
(double)new_offset.x,
|
|
|
|
(double)new_offset.y,
|
|
|
|
(double)new_offset.z,
|
|
|
|
(double)offset.x,
|
|
|
|
(double)offset.y,
|
|
|
|
(double)offset.z);
|
2016-12-19 06:11:00 -04:00
|
|
|
printf("F(%.1f %.1f %.1f) O(%.1f %.1f %.1f)\n",
|
|
|
|
field.x, field.y, field.z,
|
|
|
|
offset.x, offset.y, offset.z);
|
|
|
|
#endif
|
|
|
|
|
2018-09-28 15:11:42 -03:00
|
|
|
last_sample_ms = AP_HAL::millis();
|
|
|
|
accumulate_sample(field, compass_instance);
|
2016-12-19 06:11:00 -04:00
|
|
|
|
2016-12-19 18:12:37 -04:00
|
|
|
if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) {
|
2016-12-19 06:11:00 -04:00
|
|
|
state = STATE_REFILL1;
|
|
|
|
} else {
|
|
|
|
state = STATE_MEASURE_WAIT3;
|
2016-12-19 03:55:29 -04:00
|
|
|
}
|
|
|
|
break;
|
2016-12-19 02:53:48 -04:00
|
|
|
}
|
2016-12-19 06:11:00 -04:00
|
|
|
|
|
|
|
case STATE_MEASURE_WAIT3: {
|
|
|
|
uint8_t status;
|
|
|
|
if (!dev->read_registers(REG_STATUS, &status, 1) || !(status & 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint16_t data1[3];
|
|
|
|
if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data1[0], 6)) {
|
|
|
|
state = STATE_REFILL1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Vector3f field(float(data1[0]) - zero_offset,
|
|
|
|
float(data1[1]) - zero_offset,
|
|
|
|
float(data1[2]) - zero_offset);
|
|
|
|
field *= -counts_to_milliGauss;
|
|
|
|
field += offset;
|
|
|
|
|
2018-09-28 15:11:42 -03:00
|
|
|
last_sample_ms = AP_HAL::millis();
|
|
|
|
accumulate_sample(field, compass_instance);
|
2016-12-19 06:11:00 -04:00
|
|
|
|
|
|
|
// we stay in STATE_MEASURE_WAIT3 for measure_count_limit cycles
|
|
|
|
if (measure_count++ >= measure_count_limit) {
|
|
|
|
measure_count = 0;
|
|
|
|
state = STATE_REFILL1;
|
|
|
|
} else {
|
|
|
|
if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) { // Take Measurement
|
|
|
|
state = STATE_REFILL1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-12-19 03:55:29 -04:00
|
|
|
}
|
2016-12-19 06:11:00 -04:00
|
|
|
}
|
2016-12-19 02:53:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_MMC3416::read()
|
|
|
|
{
|
2018-09-28 15:11:42 -03:00
|
|
|
drain_accumulated_samples(compass_instance);
|
2016-12-19 02:53:48 -04:00
|
|
|
}
|