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_AK8963.h
Views: 1798
#pragma once12#include "AP_Compass_config.h"34#if AP_COMPASS_AK8963_ENABLED56#include <AP_Common/AP_Common.h>7#include <AP_HAL/AP_HAL.h>8#include <AP_HAL/I2CDevice.h>9#include <AP_HAL/SPIDevice.h>10#include <AP_Math/AP_Math.h>1112#include "AP_Compass.h"13#include "AP_Compass_Backend.h"1415class AuxiliaryBus;16class AuxiliaryBusSlave;17class AP_InertialSensor;18class AP_AK8963_BusDriver;1920class AP_Compass_AK8963 : public AP_Compass_Backend21{22public:23/* Probe for AK8963 standalone on I2C bus */24static AP_Compass_Backend *probe(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,25enum Rotation rotation);2627/* Probe for AK8963 on auxiliary bus of MPU9250, connected through I2C */28static AP_Compass_Backend *probe_mpu9250(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,29enum Rotation rotation);3031/* Probe for AK8963 on auxiliary bus of MPU9250, connected through SPI */32static AP_Compass_Backend *probe_mpu9250(uint8_t mpu9250_instance,33enum Rotation rotation);3435static constexpr const char *name = "AK8963";3637virtual ~AP_Compass_AK8963();3839void read() override;4041/* Must be public so the BusDriver can access its definition */42struct PACKED sample_regs {43int16_t val[3];44uint8_t st2;45};4647private:48AP_Compass_AK8963(AP_AK8963_BusDriver *bus,49enum Rotation rotation);5051bool init();52void _make_factory_sensitivity_adjustment(Vector3f &field) const;53void _make_adc_sensitivity_adjustment(Vector3f &field) const;5455bool _reset();56bool _setup_mode();57bool _check_id();58bool _calibrate();5960void _update();6162AP_AK8963_BusDriver *_bus;6364float _magnetometer_ASA[3] {0, 0, 0};6566uint8_t _compass_instance;67bool _initialized;68enum Rotation _rotation;69};7071class AP_AK8963_BusDriver72{73public:74virtual ~AP_AK8963_BusDriver() { }7576virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) = 0;77virtual bool register_read(uint8_t reg, uint8_t *val) = 0;78virtual bool register_write(uint8_t reg, uint8_t val) = 0;7980virtual AP_HAL::Semaphore *get_semaphore() = 0;8182virtual bool configure() { return true; }83virtual bool start_measurements() { return true; }84virtual AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t, AP_HAL::Device::PeriodicCb) = 0;8586// set device type within a device class87virtual void set_device_type(uint8_t devtype) = 0;8889// return 24 bit bus identifier90virtual uint32_t get_bus_id(void) const = 0;91};9293class AP_AK8963_BusDriver_HALDevice: public AP_AK8963_BusDriver94{95public:96AP_AK8963_BusDriver_HALDevice(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev);9798virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;99virtual bool register_read(uint8_t reg, uint8_t *val) override;100virtual bool register_write(uint8_t reg, uint8_t val) override;101102virtual AP_HAL::Semaphore *get_semaphore() override;103AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;104105// set device type within a device class106void set_device_type(uint8_t devtype) override {107_dev->set_device_type(devtype);108}109110// return 24 bit bus identifier111uint32_t get_bus_id(void) const override {112return _dev->get_bus_id();113}114115private:116AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;117};118119class AP_AK8963_BusDriver_Auxiliary : public AP_AK8963_BusDriver120{121public:122AP_AK8963_BusDriver_Auxiliary(AP_InertialSensor &ins, uint8_t backend_id,123uint8_t backend_instance, uint8_t addr);124~AP_AK8963_BusDriver_Auxiliary();125126bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;127bool register_read(uint8_t reg, uint8_t *val) override;128bool register_write(uint8_t reg, uint8_t val) override;129130AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;131132AP_HAL::Semaphore *get_semaphore() override;133134bool configure() override;135bool start_measurements() override;136137// set device type within a device class138void set_device_type(uint8_t devtype) override;139140// return 24 bit bus identifier141uint32_t get_bus_id(void) const override;142143private:144AuxiliaryBus *_bus;145AuxiliaryBusSlave *_slave;146bool _started;147};148149#endif // AP_COMPASS_AK8963_ENABLED150151152