Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Compass/AP_Compass_MMC5xx3.cpp
Views: 1798
/*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*/1415#include "AP_Compass_MMC5xx3.h"1617#if AP_COMPASS_MMC5XX3_ENABLED1819#include <AP_HAL/AP_HAL.h>20#include <stdio.h>2122extern const AP_HAL::HAL &hal;2324#define REG_PRODUCT_ID 0x2F25#define REG_XOUT_L 0x0026#define REG_STATUS 0x0827#define REG_CONTROL0 0x0928#define REG_CONTROL1 0x0A29#define REG_CONTROL2 0x0B3031// bits in REG_CONTROL032#define REG_CONTROL0_RESET 0x10 // Set coil for measuring offset33#define REG_CONTROL0_SET 0x08 // Reset coil for measuring offset34#define REG_CONTROL0_TMM 0x01 // Take Measurement for Magnetic field35#define REG_CONTROL0_TMT 0x02 // Take Measurement for Temperature3637// bits in REG_CONTROL138#define REG_CONTROL1_SW_RST 0x80 // Software reset39#define REG_CONTROL1_BW0 0x0140#define REG_CONTROL1_BW1 0x024142#define MMC5983_ID 0x304344AP_Compass_Backend *AP_Compass_MMC5XX3::probe(AP_HAL::OwnPtr<AP_HAL::Device> dev,45bool force_external,46enum Rotation rotation)47{48if (!dev) {49return nullptr;50}51AP_Compass_MMC5XX3 *sensor = NEW_NOTHROW AP_Compass_MMC5XX3(std::move(dev), force_external, rotation);52if (!sensor || !sensor->init()) {53delete sensor;54return nullptr;55}5657return sensor;58}5960AP_Compass_MMC5XX3::AP_Compass_MMC5XX3(AP_HAL::OwnPtr<AP_HAL::Device> _dev,61bool _force_external,62enum Rotation _rotation)63: dev(std::move(_dev))64, force_external(_force_external)65, have_initial_offset(false)66, rotation(_rotation)67{68}6970bool AP_Compass_MMC5XX3::init()71{72// take i2c bus semaphore73WITH_SEMAPHORE(dev->get_semaphore());7475dev->set_retries(10);7677// setup to allow reads on SPI78if (dev->bus_type() == AP_HAL::Device::BUS_TYPE_SPI) {79dev->set_read_flag(0x80);80}8182// Reading REG_PRODUCT_ID fails sometimes on SPI, so we retry up to 10 times83uint8_t whoami = 0;84uint8_t tries = 10;85while (whoami == 0 && tries > 0) {86tries--;87dev->read_registers(REG_PRODUCT_ID, &whoami, 1);88hal.scheduler->delay(5);89}9091if (whoami != MMC5983_ID) {92printf("MMC5983 got unexpected product id: %d, expected: %d\n", whoami, MMC5983_ID);93// not a MMC598394return false;95}9697// reset sensor98dev->write_register(REG_CONTROL1, REG_CONTROL1_SW_RST);99100// 10ms minimum startup time101hal.scheduler->delay(15);102103// setup for 100Hz output104if (!dev->write_register(REG_CONTROL1, 0)) {105return false;106}107108109/* register the compass instance in the frontend */110dev->set_device_type(DEVTYPE_MMC5983);111if (!register_compass(dev->get_bus_id(), compass_instance)) {112return false;113}114115set_dev_id(compass_instance, dev->get_bus_id());116117printf("Found a MMC5983 on 0x%x as compass %u\n", unsigned(dev->get_bus_id()), compass_instance);118119set_rotation(compass_instance, rotation);120121if (force_external) {122set_external(compass_instance, true);123}124125dev->set_retries(1);126127// call timer() at 100Hz128dev->register_periodic_callback(10000U,129FUNCTOR_BIND_MEMBER(&AP_Compass_MMC5XX3::timer, void));130131return true;132}133134void AP_Compass_MMC5XX3::timer()135{136// recalculate the offset with set/reset operation every measure_count_limit measurements137// sensor is read at about 100Hz, so about every 10 seconds138const uint16_t measure_count_limit = 1000U;139const uint16_t zero_offset = 32768U; // 16 bit mode140const uint16_t sensitivity = 4096U; // counts per Gauss, 16 bit mode141constexpr float counts_to_milliGauss = 1.0e3f / sensitivity;142143/*144we use the SET/RESET method to remove bridge offset every145measure_count_limit measurements. This involves a fairly complex146state machine, but means we are much less sensitive to147temperature changes148*/149switch (state) {150151// perform a set operation152case MMCState::STATE_SET: {153if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_SET)) {154break;155}156// minimum time to wait after set/reset before take measurement request is 1ms157state = MMCState::STATE_SET_MEASURE;158break;159}160161// request a measurement for field and offset calculation after set operation162case MMCState::STATE_SET_MEASURE: {163if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TMM)) {164break;165}166state = MMCState::STATE_SET_WAIT;167break;168}169170// wait for measurement to be ready after set operation, then read the171// measurement data and request a reset operation172case MMCState::STATE_SET_WAIT: {173uint8_t status;174if (!dev->read_registers(REG_STATUS, &status, 1)) {175state = MMCState::STATE_SET;176break;177}178179// check if measurement is ready180if (!(status & 1)) {181break;182}183184// read measurement185if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data0[0], 6)) {186state = MMCState::STATE_SET;187break;188}189190// request set operation191if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_RESET)) {192break;193}194// minimum time to wait after set/reset before take measurement request is 1ms195state = MMCState::STATE_RESET_MEASURE;196break;197}198199// request a measurement for field and offset calculation after reset operation200case MMCState::STATE_RESET_MEASURE: {201// take measurement request202if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TMM)) {203state = MMCState::STATE_SET;204break;205}206207state = MMCState::STATE_RESET_WAIT;208break;209}210211// wait for measurement to be ready after reset operation,212// then read the measurement data, calculate the field and offset,213// and begin requesting field measurements214case MMCState::STATE_RESET_WAIT: {215uint8_t status;216if (!dev->read_registers(REG_STATUS, &status, 1)) {217state = MMCState::STATE_SET;218break;219}220// check if measurement is ready221if (!(status & 1)) {222break;223}224225uint8_t data1[6];226if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data1[0], 6)) {227state = MMCState::STATE_SET;228break;229}230231/*232calculate field and offset233*/234Vector3f f1 {float((data0[0] << 8) + data0[1]) - zero_offset,235float((data0[2] << 8) + data0[3]) - zero_offset,236float((data0[4] << 8) + data0[5]) - zero_offset};237Vector3f f2 {float((data1[0] << 8) + data1[1]) - zero_offset,238float((data1[2] << 8) + data1[3]) - zero_offset,239float((data1[4] << 8) + data1[5]) - zero_offset};240241Vector3f field {(f2 - f1) * counts_to_milliGauss * 0.5f};242Vector3f new_offset {(f1 + f2) * counts_to_milliGauss * 0.5f};243244if (!have_initial_offset) {245offset = new_offset;246have_initial_offset = true;247} else {248// low pass changes to the offset249offset = offset * 0.5f + new_offset * 0.5f;250}251252accumulate_sample(field, compass_instance);253254if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TMM)) {255printf("failed to initiate measurement\n");256state = MMCState::STATE_SET;257} else {258state = MMCState::STATE_MEASURE;259}260261break;262}263264// take repeated field measurements, set/reset is performed again after265// measure_count_limit measurements266case MMCState::STATE_MEASURE: {267uint8_t status;268if (!dev->read_registers(REG_STATUS, &status, 1)) {269state = MMCState::STATE_SET;270break;271}272273// check if measurement is ready274if (!(status & 1)) {275break;276}277278uint8_t data1[6];279if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data1[0], 6)) {280printf("cant read data\n");281state = MMCState::STATE_SET;282break;283}284285Vector3f field {float((data1[0] << 8) + data1[1]) - zero_offset,286float((data1[2] << 8) + data1[3]) - zero_offset,287float((data1[4] << 8) + data1[5]) - zero_offset};288field *= counts_to_milliGauss;289field -= offset;290accumulate_sample(field, compass_instance);291292// we stay in STATE_MEASURE for measure_count_limit cycles293if (measure_count++ >= measure_count_limit) {294measure_count = 0;295state = MMCState::STATE_SET;296} else {297if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TMM)) { // Take Measurement298state = MMCState::STATE_SET;299}300}301break;302}303}304}305306void AP_Compass_MMC5XX3::read()307{308drain_accumulated_samples(compass_instance);309}310311#endif // AP_COMPASS_MMC5XX3_ENABLED312313314315