2016-12-07 11:12:40 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Emlid Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 Georgii Staroselskii, Sep 2016
|
|
|
|
*/
|
|
|
|
#include "AP_Compass_IST8310.h"
|
|
|
|
|
2023-02-09 21:48:02 -04:00
|
|
|
#if AP_COMPASS_IST8310_ENABLED
|
|
|
|
|
2016-12-07 11:12:40 -04:00
|
|
|
#include <stdio.h>
|
2016-12-08 20:05:57 -04:00
|
|
|
#include <utility>
|
2016-12-07 11:12:40 -04:00
|
|
|
|
2016-12-08 20:05:57 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2016-12-07 11:12:40 -04:00
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2021-05-30 21:56:07 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2016-12-07 11:12:40 -04:00
|
|
|
|
|
|
|
#define WAI_REG 0x0
|
|
|
|
#define DEVICE_ID 0x10
|
|
|
|
|
2017-03-01 05:33:29 -04:00
|
|
|
#define OUTPUT_X_L_REG 0x3
|
|
|
|
#define OUTPUT_X_H_REG 0x4
|
|
|
|
#define OUTPUT_Y_L_REG 0x5
|
|
|
|
#define OUTPUT_Y_H_REG 0x6
|
|
|
|
#define OUTPUT_Z_L_REG 0x7
|
|
|
|
#define OUTPUT_Z_H_REG 0x8
|
|
|
|
|
2016-12-07 11:12:40 -04:00
|
|
|
#define CNTL1_REG 0xA
|
2017-03-03 19:02:22 -04:00
|
|
|
#define CNTL1_VAL_SINGLE_MEASUREMENT_MODE 0x1
|
2016-12-07 11:12:40 -04:00
|
|
|
|
2017-03-01 05:28:39 -04:00
|
|
|
#define CNTL2_REG 0xB
|
2017-03-03 19:02:22 -04:00
|
|
|
#define CNTL2_VAL_SRST 1
|
2016-12-07 11:12:40 -04:00
|
|
|
|
|
|
|
#define AVGCNTL_REG 0x41
|
2017-03-03 19:02:22 -04:00
|
|
|
#define AVGCNTL_VAL_XZ_0 (0)
|
|
|
|
#define AVGCNTL_VAL_XZ_2 (1)
|
|
|
|
#define AVGCNTL_VAL_XZ_4 (2)
|
|
|
|
#define AVGCNTL_VAL_XZ_8 (3)
|
|
|
|
#define AVGCNTL_VAL_XZ_16 (4)
|
|
|
|
#define AVGCNTL_VAL_Y_0 (0 << 3)
|
|
|
|
#define AVGCNTL_VAL_Y_2 (1 << 3)
|
|
|
|
#define AVGCNTL_VAL_Y_4 (2 << 3)
|
|
|
|
#define AVGCNTL_VAL_Y_8 (3 << 3)
|
|
|
|
#define AVGCNTL_VAL_Y_16 (4 << 3)
|
2016-12-07 11:12:40 -04:00
|
|
|
|
|
|
|
#define PDCNTL_REG 0x42
|
2017-03-03 19:02:22 -04:00
|
|
|
#define PDCNTL_VAL_PULSE_DURATION_NORMAL 0xC0
|
2016-12-07 11:12:40 -04:00
|
|
|
|
2017-05-05 07:09:29 -03:00
|
|
|
#define SAMPLING_PERIOD_USEC (10 * AP_USEC_PER_MSEC)
|
2017-02-24 22:16:45 -04:00
|
|
|
|
2017-03-01 05:32:59 -04:00
|
|
|
/*
|
|
|
|
* FSR:
|
|
|
|
* x, y: +- 1600 µT
|
|
|
|
* z: +- 2500 µT
|
|
|
|
*
|
|
|
|
* Resolution according to datasheet is 0.3µT/LSB
|
|
|
|
*/
|
|
|
|
#define IST8310_RESOLUTION 0.3
|
|
|
|
|
|
|
|
static const int16_t IST8310_MAX_VAL_XY = (1600 / IST8310_RESOLUTION) + 1;
|
|
|
|
static const int16_t IST8310_MIN_VAL_XY = -IST8310_MAX_VAL_XY;
|
|
|
|
static const int16_t IST8310_MAX_VAL_Z = (2500 / IST8310_RESOLUTION) + 1;
|
|
|
|
static const int16_t IST8310_MIN_VAL_Z = -IST8310_MAX_VAL_Z;
|
|
|
|
|
|
|
|
|
2016-12-07 11:12:40 -04:00
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_Backend *AP_Compass_IST8310::probe(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
|
2017-09-15 23:03:43 -03:00
|
|
|
bool force_external,
|
2016-12-08 20:05:57 -04:00
|
|
|
enum Rotation rotation)
|
2016-12-07 11:12:40 -04:00
|
|
|
{
|
2016-12-08 20:05:57 -04:00
|
|
|
if (!dev) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-05-26 22:24:10 -03:00
|
|
|
AP_Compass_IST8310 *sensor = NEW_NOTHROW AP_Compass_IST8310(std::move(dev), force_external, rotation);
|
2016-12-07 11:12:40 -04:00
|
|
|
if (!sensor || !sensor->init()) {
|
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_IST8310::AP_Compass_IST8310(AP_HAL::OwnPtr<AP_HAL::Device> dev,
|
2017-09-15 23:03:43 -03:00
|
|
|
bool force_external,
|
2016-12-08 20:05:57 -04:00
|
|
|
enum Rotation rotation)
|
2018-08-06 19:21:27 -03:00
|
|
|
: _dev(std::move(dev))
|
2016-12-08 20:05:57 -04:00
|
|
|
, _rotation(rotation)
|
2017-09-15 23:03:43 -03:00
|
|
|
, _force_external(force_external)
|
2016-12-07 11:12:40 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Compass_IST8310::init()
|
|
|
|
{
|
2017-03-01 05:28:39 -04:00
|
|
|
uint8_t reset_count = 0;
|
|
|
|
|
2020-01-18 17:42:33 -04:00
|
|
|
_dev->get_semaphore()->take_blocking();
|
2016-12-07 11:12:40 -04:00
|
|
|
|
2016-12-08 20:05:57 -04:00
|
|
|
// high retries for init
|
|
|
|
_dev->set_retries(10);
|
|
|
|
|
2024-04-30 04:27:32 -03:00
|
|
|
/*
|
|
|
|
unfortunately the IST8310 employs the novel concept of a
|
|
|
|
writeable WHOAMI register. The register can become corrupt due
|
|
|
|
to bus noise, and what is worse it persists the corruption even
|
|
|
|
across a power cycle. If you power it off for 30s or more then
|
|
|
|
it will reset to the default of 0x10, but for less than that the
|
|
|
|
value of WAI is unreliable.
|
|
|
|
|
|
|
|
To avoid this issue we do a reset of the device before we probe
|
|
|
|
the WAI register. This is nasty as we don't yet know we've found
|
|
|
|
a real IST8310, but it is the best we can do given the bad
|
|
|
|
hardware design of the sensor
|
|
|
|
*/
|
|
|
|
_dev->write_register(CNTL2_REG, CNTL2_VAL_SRST);
|
|
|
|
hal.scheduler->delay(10);
|
|
|
|
|
2016-12-08 20:05:57 -04:00
|
|
|
uint8_t whoami;
|
|
|
|
if (!_dev->read_registers(WAI_REG, &whoami, 1) ||
|
|
|
|
whoami != DEVICE_ID) {
|
|
|
|
// not an IST8310
|
2016-12-07 11:12:40 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-03-01 05:28:39 -04:00
|
|
|
for (; reset_count < 5; reset_count++) {
|
2017-03-03 19:02:22 -04:00
|
|
|
if (!_dev->write_register(CNTL2_REG, CNTL2_VAL_SRST)) {
|
2017-03-01 05:28:39 -04:00
|
|
|
hal.scheduler->delay(10);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hal.scheduler->delay(10);
|
|
|
|
|
|
|
|
uint8_t cntl2 = 0xFF;
|
|
|
|
if (_dev->read_registers(CNTL2_REG, &cntl2, 1) &&
|
|
|
|
(cntl2 & 0x01) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reset_count == 5) {
|
2018-01-12 04:27:06 -04:00
|
|
|
printf("IST8310: failed to reset device\n");
|
2017-03-01 05:28:39 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:02:22 -04:00
|
|
|
if (!_dev->write_register(AVGCNTL_REG, AVGCNTL_VAL_Y_16 | AVGCNTL_VAL_XZ_16) ||
|
|
|
|
!_dev->write_register(PDCNTL_REG, PDCNTL_VAL_PULSE_DURATION_NORMAL)) {
|
2018-01-12 04:27:06 -04:00
|
|
|
printf("IST8310: found device but could not set it up\n");
|
2016-12-07 11:12:40 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-12-08 20:05:57 -04:00
|
|
|
// lower retries for run
|
|
|
|
_dev->set_retries(3);
|
|
|
|
|
|
|
|
// start state machine: request a sample
|
2016-12-07 11:12:40 -04:00
|
|
|
start_conversion();
|
|
|
|
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
|
2019-11-20 03:18:10 -04:00
|
|
|
// register compass instance
|
|
|
|
_dev->set_device_type(DEVTYPE_IST8310);
|
|
|
|
if (!register_compass(_dev->get_bus_id(), _instance)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
set_dev_id(_instance, _dev->get_bus_id());
|
2016-12-07 11:12:40 -04:00
|
|
|
|
2016-12-08 20:05:57 -04:00
|
|
|
printf("%s found on bus %u id %u address 0x%02x\n", name,
|
2024-07-05 19:58:15 -03:00
|
|
|
_dev->bus_num(), unsigned(_dev->get_bus_id()), _dev->get_bus_address());
|
2016-12-07 11:12:40 -04:00
|
|
|
|
2016-12-08 20:05:57 -04:00
|
|
|
set_rotation(_instance, _rotation);
|
2016-12-07 11:12:40 -04:00
|
|
|
|
2017-09-15 23:03:43 -03:00
|
|
|
if (_force_external) {
|
|
|
|
set_external(_instance, true);
|
|
|
|
}
|
|
|
|
|
2017-03-01 05:33:29 -04:00
|
|
|
_periodic_handle = _dev->register_periodic_callback(SAMPLING_PERIOD_USEC,
|
|
|
|
FUNCTOR_BIND_MEMBER(&AP_Compass_IST8310::timer, void));
|
2016-12-07 11:12:40 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_IST8310::start_conversion()
|
|
|
|
{
|
2017-03-03 19:02:22 -04:00
|
|
|
if (!_dev->write_register(CNTL1_REG, CNTL1_VAL_SINGLE_MEASUREMENT_MODE)) {
|
2017-03-01 05:33:29 -04:00
|
|
|
_ignore_next_sample = true;
|
2017-02-24 22:16:45 -04:00
|
|
|
}
|
2016-12-07 11:12:40 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_Compass_IST8310::timer()
|
2016-12-07 11:12:40 -04:00
|
|
|
{
|
2017-03-01 05:33:29 -04:00
|
|
|
if (_ignore_next_sample) {
|
|
|
|
_ignore_next_sample = false;
|
2017-02-24 22:16:45 -04:00
|
|
|
start_conversion();
|
|
|
|
return;
|
|
|
|
}
|
2016-12-07 11:12:40 -04:00
|
|
|
|
|
|
|
struct PACKED {
|
|
|
|
le16_t rx;
|
|
|
|
le16_t ry;
|
|
|
|
le16_t rz;
|
|
|
|
} buffer;
|
|
|
|
|
2017-03-01 05:33:29 -04:00
|
|
|
bool ret = _dev->read_registers(OUTPUT_X_L_REG, (uint8_t *) &buffer, sizeof(buffer));
|
2016-12-07 11:12:40 -04:00
|
|
|
if (!ret) {
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-12-07 11:12:40 -04:00
|
|
|
}
|
|
|
|
|
2017-03-01 05:33:29 -04:00
|
|
|
start_conversion();
|
2017-02-24 22:16:45 -04:00
|
|
|
|
2017-03-01 05:33:29 -04:00
|
|
|
/* same period, but start counting from now */
|
|
|
|
_dev->adjust_periodic_callback(_periodic_handle, SAMPLING_PERIOD_USEC);
|
2016-12-07 11:12:40 -04:00
|
|
|
|
|
|
|
auto x = static_cast<int16_t>(le16toh(buffer.rx));
|
|
|
|
auto y = static_cast<int16_t>(le16toh(buffer.ry));
|
|
|
|
auto z = static_cast<int16_t>(le16toh(buffer.rz));
|
|
|
|
|
2017-03-01 05:32:59 -04:00
|
|
|
/*
|
|
|
|
* Check if value makes sense according to the FSR and Resolution of
|
|
|
|
* this sensor, discarding outliers
|
|
|
|
*/
|
|
|
|
if (x > IST8310_MAX_VAL_XY || x < IST8310_MIN_VAL_XY ||
|
|
|
|
y > IST8310_MAX_VAL_XY || y < IST8310_MIN_VAL_XY ||
|
|
|
|
z > IST8310_MAX_VAL_Z || z < IST8310_MIN_VAL_Z) {
|
2017-03-01 02:00:45 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:11:34 -04:00
|
|
|
// flip Z to conform to right-hand rule convention
|
|
|
|
z = -z;
|
|
|
|
|
2017-03-01 05:33:29 -04:00
|
|
|
/* Resolution: 0.3 µT/LSB - already convert to milligauss */
|
2016-12-07 11:12:40 -04:00
|
|
|
Vector3f field = Vector3f{x * 3.0f, y * 3.0f, z * 3.0f};
|
|
|
|
|
2018-09-28 15:09:44 -03:00
|
|
|
accumulate_sample(field, _instance);
|
2016-12-07 11:12:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_IST8310::read()
|
|
|
|
{
|
2018-09-28 15:09:44 -03:00
|
|
|
drain_accumulated_samples(_instance);
|
2016-12-07 11:12:40 -04:00
|
|
|
}
|
2023-02-09 21:48:02 -04:00
|
|
|
|
|
|
|
#endif // AP_COMPASS_IST8310_ENABLED
|