Path: blob/master/libraries/AP_Compass/AP_Compass_QMC5883P.cpp
9441 views
/*1* This file is free software: you can redistribute it and/or modify it2* under the terms of the GNU General Public License as published by the3* Free Software Foundation, either version 3 of the License, or4* (at your option) any later version.5*6* This file is distributed in the hope that it will be useful, but7* WITHOUT ANY WARRANTY; without even the implied warranty of8* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.9* See the GNU General Public License for more details.10*11* You should have received a copy of the GNU General Public License along12* with this program. If not, see <http://www.gnu.org/licenses/>.13*14* Driver by Lokesh Ramina, Jan 202215*/16#include "AP_Compass_QMC5883P.h"1718#include <stdio.h>1920#include <AP_HAL/AP_HAL.h>21#include <AP_Math/AP_Math.h>2223#if AP_COMPASS_QMC5883P_ENABLED2425//Register Address26#define QMC5883P_REG_ID 0x0027#define QMC5883P_REG_DATA_OUTPUT_X 0x0128#define QMC5883P_REG_DATA_OUTPUT_Z_MSB 0x0629#define QMC5883P_REG_STATUS 0x0930#define QMC5883P_REG_CONF1 0x0A31#define QMC5883P_REG_CONF2 0x0B3233#define QMC5883P_ID_VAL 0x803435//Register values36// Sensor operation modes37#define QMC5883P_MODE_SUSPEND 0x0038#define QMC5883P_MODE_NORMAL 0x0139#define QMC5883P_MODE_SINGLE 0x0240#define QMC5883P_MODE_CONTINUOUS 0x034142// ODR data output rates for 5883L43#define QMC5883P_ODR_10HZ (0x00 << 2)44#define QMC5883P_ODR_50HZ (0x01 << 2)45#define QMC5883P_ODR_100HZ (0x02 << 2)46#define QMC5883P_ODR_200HZ (0x03 << 2)4748// Over sampling Ratio OSR149#define QMC5883P_OSR1_8 (0x00 << 4)50#define QMC5883P_OSR1_4 (0x01 << 4)51#define QMC5883P_OSR1_2 (0x02 << 4)52#define QMC5883P_OSR1_1 (0x03 << 4)5354// Down sampling Rate OSR255#define QMC5883P_OSR2_8 0x085657//RNG58#define QMC5883P_RNG_30G (0x00 << 2)59#define QMC5883P_RNG_12G (0x01 << 2)60#define QMC5883P_RNG_8G (0x10 << 2)61#define QMC5883P_RNG_2G (0x11 << 2)6263#define QMC5883P_SET_XYZ_SIGN 0x296465//Reset66#define QMC5883P_RST 0x806768//Status Val69#define QMC5883P_STATUS_DATA_READY 0x017071#ifndef DEBUG72#define DEBUG 073#endif7475AP_Compass_Backend *AP_Compass_QMC5883P::probe(AP_HAL::OwnPtr<AP_HAL::Device> dev,76bool force_external,77enum Rotation rotation)78{79if (!dev) {80return nullptr;81}82AP_Compass_QMC5883P *sensor = NEW_NOTHROW AP_Compass_QMC5883P(std::move(dev),force_external,rotation);83if (!sensor || !sensor->init()) {84delete sensor;85return nullptr;86}87return sensor;88}8990AP_Compass_QMC5883P::AP_Compass_QMC5883P(AP_HAL::OwnPtr<AP_HAL::Device> dev,91bool force_external,92enum Rotation rotation)93: _dev(std::move(dev))94, _rotation(rotation)95, _force_external(force_external)96{97}9899bool AP_Compass_QMC5883P::init()100{101_dev->get_semaphore()->take_blocking();102103_dev->set_retries(10);104#if DEBUG105_dump_registers();106#endif107if (!_check_whoami()) {108goto fail;109}110//As mentioned in the Datasheet 7.2 to do continues mode 0x29 will set sign for X,Y,Z111if (!_dev->write_register(QMC5883P_REG_DATA_OUTPUT_Z_MSB, QMC5883P_SET_XYZ_SIGN)||112!_dev->write_register(QMC5883P_REG_CONF1,113QMC5883P_MODE_CONTINUOUS|114QMC5883P_ODR_100HZ|115QMC5883P_OSR1_8|116QMC5883P_OSR2_8)||117!_dev->write_register(QMC5883P_REG_CONF2,QMC5883P_OSR2_8)) {118goto fail;119}120121// lower retries for run122_dev->set_retries(3);123124_dev->get_semaphore()->give();125126//register compass instance127_dev->set_device_type(DEVTYPE_QMC5883P);128if (!register_compass(_dev->get_bus_id())) {129return false;130}131132printf("%s found on bus %u id %u address 0x%02x\n", name,133_dev->bus_num(), unsigned(_dev->get_bus_id()), _dev->get_bus_address());134135set_rotation(_rotation);136137if (_force_external) {138set_external(true);139}140141//Enable 100HZ142_dev->register_periodic_callback(10000,143FUNCTOR_BIND_MEMBER(&AP_Compass_QMC5883P::timer, void));144145return true;146147fail:148_dev->get_semaphore()->give();149return false;150}151bool AP_Compass_QMC5883P::_check_whoami()152{153uint8_t whoami;154if (!_dev->read_registers(QMC5883P_REG_ID, &whoami,1)||155whoami != QMC5883P_ID_VAL) {156return false;157}158return true;159}160161void AP_Compass_QMC5883P::timer()162{163struct PACKED {164int16_t rx;165int16_t ry;166int16_t rz;167} buffer;168169const float range_scale = 1000.0f / 3000.0f;170171uint8_t status;172if (!_dev->read_registers(QMC5883P_REG_STATUS,&status,1)) {173return;174}175//new data is ready176if (!(status & QMC5883P_STATUS_DATA_READY)) {177return;178}179180if (!_dev->read_registers(QMC5883P_REG_DATA_OUTPUT_X, (uint8_t *) &buffer, sizeof(buffer))) {181return ;182}183184auto x = buffer.rx;185auto y = buffer.ry;186auto z = buffer.rz;187188#if DEBUG189printf("mag.x:%d\n",x);190printf("mag.y:%d\n",y);191printf("mag.z:%d\n",z);192#endif193194Vector3f field = Vector3f{x * range_scale, y * range_scale, z * range_scale };195196accumulate_sample(field, 20);197}198199void AP_Compass_QMC5883P::read()200{201drain_accumulated_samples();202}203204void AP_Compass_QMC5883P::_dump_registers()205{206printf("QMC5883P registers dump\n");207for (uint8_t reg = QMC5883P_REG_DATA_OUTPUT_X; reg <= 0x30; reg++) {208uint8_t v;209_dev->read_registers(reg,&v,1);210printf("%02x:%02x ", (unsigned)reg, (unsigned)v);211if ((reg - ( QMC5883P_REG_DATA_OUTPUT_X-1)) % 16 == 0) {212printf("\n");213}214}215}216217#endif //AP_COMPASS_QMC5883P_ENABLED218219220