2017-06-16 02:52:02 -03: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 RadioLink LjWang, Jun 2017
|
2017-06-22 03:37:00 -03:00
|
|
|
* GPS compass module See<http://www.radiolink.com>
|
2017-06-16 02:52:02 -03:00
|
|
|
*/
|
|
|
|
#include "AP_Compass_QMC5883L.h"
|
|
|
|
|
2023-02-19 21:05:13 -04:00
|
|
|
#if AP_COMPASS_QMC5883L_ENABLED
|
|
|
|
|
2017-06-16 02:52:02 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
#define QMC5883L_REG_CONF1 0x09
|
|
|
|
#define QMC5883L_REG_CONF2 0x0A
|
|
|
|
|
|
|
|
// data output rates for 5883L
|
|
|
|
#define QMC5883L_ODR_10HZ (0x00 << 2)
|
|
|
|
#define QMC5883L_ODR_50HZ (0x01 << 2)
|
|
|
|
#define QMC5883L_ODR_100HZ (0x02 << 2)
|
|
|
|
#define QMC5883L_ODR_200HZ (0x03 << 2)
|
|
|
|
|
|
|
|
// Sensor operation modes
|
|
|
|
#define QMC5883L_MODE_STANDBY 0x00
|
|
|
|
#define QMC5883L_MODE_CONTINUOUS 0x01
|
|
|
|
|
|
|
|
#define QMC5883L_RNG_2G (0x00 << 4)
|
|
|
|
#define QMC5883L_RNG_8G (0x01 << 4)
|
|
|
|
|
|
|
|
#define QMC5883L_OSR_512 (0x00 << 6)
|
|
|
|
#define QMC5883L_OSR_256 (0x01 << 6)
|
|
|
|
#define QMC5883L_OSR_128 (0x10 << 6)
|
|
|
|
#define QMC5883L_OSR_64 (0x11 << 6)
|
|
|
|
|
|
|
|
#define QMC5883L_RST 0x80
|
|
|
|
|
|
|
|
#define QMC5883L_REG_DATA_OUTPUT_X 0x00
|
|
|
|
#define QMC5883L_REG_STATUS 0x06
|
|
|
|
|
|
|
|
#define QMC5883L_REG_ID 0x0D
|
|
|
|
#define QMC5883_ID_VAL 0xFF
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_Backend *AP_Compass_QMC5883L::probe(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
|
|
|
|
bool force_external,
|
|
|
|
enum Rotation rotation)
|
2017-06-16 02:52:02 -03:00
|
|
|
{
|
|
|
|
if (!dev) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-05-26 22:24:10 -03:00
|
|
|
AP_Compass_QMC5883L *sensor = NEW_NOTHROW AP_Compass_QMC5883L(std::move(dev),force_external,rotation);
|
2017-06-16 02:52:02 -03:00
|
|
|
if (!sensor || !sensor->init()) {
|
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_QMC5883L::AP_Compass_QMC5883L(AP_HAL::OwnPtr<AP_HAL::Device> dev,
|
|
|
|
bool force_external,
|
|
|
|
enum Rotation rotation)
|
|
|
|
: _dev(std::move(dev))
|
2017-06-16 02:52:02 -03:00
|
|
|
, _rotation(rotation)
|
2017-06-22 03:37:00 -03:00
|
|
|
, _force_external(force_external)
|
2017-06-16 02:52:02 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Compass_QMC5883L::init()
|
|
|
|
{
|
2020-01-18 17:42:33 -04:00
|
|
|
_dev->get_semaphore()->take_blocking();
|
2017-06-16 02:52:02 -03:00
|
|
|
|
|
|
|
_dev->set_retries(10);
|
|
|
|
|
2017-07-28 03:34:23 -03:00
|
|
|
#if 0
|
|
|
|
_dump_registers();
|
|
|
|
#endif
|
|
|
|
|
2017-07-25 07:33:26 -03:00
|
|
|
if(!_check_whoami()){
|
|
|
|
goto fail;
|
2017-06-16 02:52:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_dev->write_register(0x0B, 0x01)||
|
|
|
|
!_dev->write_register(0x20, 0x40)||
|
|
|
|
!_dev->write_register(0x21, 0x01)||
|
|
|
|
!_dev->write_register(QMC5883L_REG_CONF1,
|
|
|
|
QMC5883L_MODE_CONTINUOUS|
|
|
|
|
QMC5883L_ODR_100HZ|
|
|
|
|
QMC5883L_OSR_512|
|
|
|
|
QMC5883L_RNG_8G)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lower retries for run
|
|
|
|
_dev->set_retries(3);
|
|
|
|
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
|
2019-11-20 03:18:10 -04:00
|
|
|
//register compass instance
|
|
|
|
_dev->set_device_type(DEVTYPE_QMC5883L);
|
|
|
|
if (!register_compass(_dev->get_bus_id(), _instance)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
set_dev_id(_instance, _dev->get_bus_id());
|
2017-06-16 02:52:02 -03:00
|
|
|
|
|
|
|
printf("%s found on bus %u id %u address 0x%02x\n", name,
|
|
|
|
_dev->bus_num(), _dev->get_bus_id(), _dev->get_bus_address());
|
|
|
|
|
|
|
|
set_rotation(_instance, _rotation);
|
|
|
|
|
2017-06-22 03:37:00 -03:00
|
|
|
if (_force_external) {
|
|
|
|
set_external(_instance, true);
|
|
|
|
}
|
|
|
|
|
2017-06-16 02:52:02 -03:00
|
|
|
//Enable 100HZ
|
2017-06-17 04:20:54 -03:00
|
|
|
_dev->register_periodic_callback(10000,
|
2017-06-16 02:52:02 -03:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_Compass_QMC5883L::timer, void));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2017-06-17 04:20:54 -03:00
|
|
|
fail:
|
2017-06-16 02:52:02 -03:00
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 07:33:26 -03:00
|
|
|
bool AP_Compass_QMC5883L::_check_whoami()
|
|
|
|
{
|
|
|
|
uint8_t whoami;
|
2017-07-28 03:34:23 -03:00
|
|
|
//Affected by other devices,must read registers 0x00 once or reset,after can read the ID registers reliably
|
|
|
|
_dev->read_registers(0x00,&whoami,1);
|
|
|
|
if (!_dev->read_registers(0x0C, &whoami,1)||
|
|
|
|
whoami != 0x01){
|
2017-07-25 07:33:26 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!_dev->read_registers(QMC5883L_REG_ID, &whoami,1)||
|
|
|
|
whoami != QMC5883_ID_VAL){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-16 02:52:02 -03:00
|
|
|
|
|
|
|
void AP_Compass_QMC5883L::timer()
|
|
|
|
{
|
|
|
|
struct PACKED {
|
|
|
|
le16_t rx;
|
|
|
|
le16_t ry;
|
|
|
|
le16_t rz;
|
|
|
|
} buffer;
|
|
|
|
|
|
|
|
const float range_scale = 1000.0f / 3000.0f;
|
|
|
|
|
|
|
|
uint8_t status;
|
|
|
|
if(!_dev->read_registers(QMC5883L_REG_STATUS,&status,1)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//new data is ready
|
2017-06-18 05:15:18 -03:00
|
|
|
if (!(status & 0x04)) {
|
2017-06-16 02:52:02 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_dev->read_registers(QMC5883L_REG_DATA_OUTPUT_X, (uint8_t *) &buffer, sizeof(buffer))){
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("mag.x:%d\n",x);
|
|
|
|
printf("mag.y:%d\n",y);
|
|
|
|
printf("mag.z:%d\n",z);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Vector3f field = Vector3f{x * range_scale , y * range_scale, z * range_scale };
|
|
|
|
|
2017-06-22 03:37:00 -03:00
|
|
|
// rotate to the desired orientation
|
|
|
|
if (is_external(_instance)) {
|
|
|
|
field.rotate(ROTATION_YAW_90);
|
|
|
|
}
|
|
|
|
|
2018-09-28 14:47:10 -03:00
|
|
|
accumulate_sample(field, _instance, 20);
|
2017-06-16 02:52:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_QMC5883L::read()
|
|
|
|
{
|
2018-09-28 14:47:10 -03:00
|
|
|
drain_accumulated_samples(_instance);
|
2017-06-16 02:52:02 -03:00
|
|
|
}
|
2017-07-28 03:34:23 -03:00
|
|
|
|
|
|
|
void AP_Compass_QMC5883L::_dump_registers()
|
|
|
|
{
|
|
|
|
printf("QMC5883L registers dump\n");
|
|
|
|
for (uint8_t reg = QMC5883L_REG_DATA_OUTPUT_X; reg <= 0x30; reg++) {
|
|
|
|
uint8_t v;
|
|
|
|
_dev->read_registers(reg,&v,1);
|
|
|
|
printf("%02x:%02x ", (unsigned)reg, (unsigned)v);
|
|
|
|
if ((reg - ( QMC5883L_REG_DATA_OUTPUT_X-1)) % 16 == 0) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 21:05:13 -04:00
|
|
|
#endif // AP_COMPASS_QMC5883L_ENABLED
|