Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
fxos8700_accelmag.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // FXOS8700 ACCELEROMETER & MAGNETOMETER SENSOR LIBRARY
3 //
4 // Code By: Michael Wrona
5 // Created: 26 Aug 2021
6 // ----------------------------------------------------------------------------
7 // Source code for the FXOS8700 accelerometer and magnetometer sensor library.
8 // IMPORTANT NOTES:
9 // - Hybrid mode is needed because the magnetometer and accelerometer USE THE
10 // SAME ADC! See p.22 for more.
11 // - Because we are using the LIS3MDL compass in the GPS, I chose to disable
12 // hybrid mode and only use the FXOS8700's accelerometer (8/26/2021).
13 
14 
15 
16 
17 #pragma once
18 
19 #include <Wire.h>
20 #include <Arduino.h>
21 #include "hummingbird_config.h"
22 #include "debugging.h"
23 
24 #ifdef DEBUG
25 #define FXOS8700_DEBUG // Toggle printing FXOS8700 debug messages to the debug port
26 #endif
27 
28 
29 constexpr uint8_t FXOS8700_ID = 0xC7; // Unique ID
30 constexpr uint8_t FXOS8700_ADDRESS = 0x1F; // I2C address
31 
32 // ----------------------------------------------------------------------------
33 // Define conversion factors to convert accel. and mag. data to floats
34 // ----------------------------------------------------------------------------
35 constexpr float ACCELMAG_CVT_GS_2G = 0.000244140625f; // Convert int16 to g's for +/- 2G range
36 constexpr float ACCELMAG_CVT_GS_4G = 0.00048828125f; // Convert int16 to g's for +/- 4G range
37 constexpr float ACCELMAG_CVT_GS_8G = 0.0009765625f; // Convert int16 to g's for +/- 8G range
38 constexpr float ACCELMAG_CVT_UT = 0.1f; // Convert int16 to uT
39 
43 typedef enum
44 {
45  ACCEL_RNG_2G = 0x00,
46  ACCEL_RNG_4G = 0x01,
47  ACCEL_RNG_8G = 0x02
49 
53 typedef enum
54 {
56  ACCELMAG_REG_ID = 0x0D,
78  ACCELMAG_REG_MCTRL3 = 0x5D
80 
81 
86 {
87 public:
88  FXOS8700AccelMag(TwoWire *wireInput = &SENSOR_I2C);
90  bool Initialize(AccelRanges_t accRange = ACCEL_RNG_4G);
91  bool ReadSensor();
92  float GetAx();
93  float GetAy();
94  float GetAz();
95  uint32_t prevMeasMicros;
97 protected:
98 private:
99  float _ax;
100  float _ay;
101  float _az;
102  TwoWire *_SensorWire;
103  uint8_t I2Cread8(uint8_t regOfInterest);
104  void I2Cwrite8(uint8_t regOfInterest, uint8_t valToWrite);
105 };
NXP Semiconductor FXOS8700 accelerometer/magnetometer sensor class.
Definition: fxos8700_accelmag.h:86
float _az
Z-acceleration [G's].
Definition: fxos8700_accelmag.h:101
uint8_t I2Cread8(uint8_t regOfInterest)
Read FXOS8700 register value over I2C.
Definition: fxos8700_accelmag.cpp:236
AccelRanges_t accelRange
Measurement range.
Definition: fxos8700_accelmag.h:96
float _ax
X-acceleration [G's].
Definition: fxos8700_accelmag.h:99
float GetAx()
Return x-acceleromter measurement in [G's].
Definition: fxos8700_accelmag.cpp:191
~FXOS8700AccelMag()
Definition: fxos8700_accelmag.h:89
float GetAz()
Return z-acceleromter measurement in [G's].
Definition: fxos8700_accelmag.cpp:208
FXOS8700AccelMag(TwoWire *wireInput=&SENSOR_I2C)
Constructor for the FXOS8700 Accelerometer/Magnetometer class.
Definition: fxos8700_accelmag.cpp:27
uint32_t prevMeasMicros
Previous measurement micros()
Definition: fxos8700_accelmag.h:95
float GetAy()
Return y-acceleromter measurement in [G's].
Definition: fxos8700_accelmag.cpp:200
bool Initialize(AccelRanges_t accRange=ACCEL_RNG_4G)
Initialize accelerometer, set accel.
Definition: fxos8700_accelmag.cpp:43
TwoWire * _SensorWire
I2C bus that the sensor is on.
Definition: fxos8700_accelmag.h:102
bool ReadSensor()
Read acceleration data from the FXOS8700 sensor.
Definition: fxos8700_accelmag.cpp:114
void I2Cwrite8(uint8_t regOfInterest, uint8_t valToWrite)
Write to FXOS8700 register over I2C.
Definition: fxos8700_accelmag.cpp:220
float _ay
Y-acceleration [G's].
Definition: fxos8700_accelmag.h:100
constexpr uint8_t FXOS8700_ID
Definition: fxos8700_accelmag.h:29
MagAccelRegisters_t
Accelerometer & magnetometer registers.
Definition: fxos8700_accelmag.h:54
@ ACCELMAG_REG_MSTATUS
Definition: fxos8700_accelmag.h:69
@ ACCELMAG_REG_CTRL1
Definition: fxos8700_accelmag.h:64
@ ACCELMAG_REG_AOUT_Z_LSB
Definition: fxos8700_accelmag.h:63
@ ACCELMAG_REG_AOUT_Y_MSB
Definition: fxos8700_accelmag.h:60
@ ACCELMAG_REG_CTRL2
Definition: fxos8700_accelmag.h:65
@ ACCELMAG_REG_AOUT_X_LSB
Definition: fxos8700_accelmag.h:59
@ ACCELMAG_REG_AOUT_X_MSB
Definition: fxos8700_accelmag.h:58
@ ACCELMAG_REG_MCTRL1
Definition: fxos8700_accelmag.h:76
@ ACCELMAG_REG_STATUS
Definition: fxos8700_accelmag.h:55
@ ACCELMAG_REG_XYZ_CFG
Definition: fxos8700_accelmag.h:57
@ ACCELMAG_REG_MCTRL3
Definition: fxos8700_accelmag.h:78
@ ACCELMAG_REG_ID
Definition: fxos8700_accelmag.h:56
@ ACCELMAG_REG_MOUT_Y_MSB
Definition: fxos8700_accelmag.h:72
@ ACCELMAG_REG_MOUT_X_MSB
Definition: fxos8700_accelmag.h:70
@ ACCELMAG_REG_MOUT_Z_LSB
Definition: fxos8700_accelmag.h:75
@ ACCELMAG_REG_CTRL5
Definition: fxos8700_accelmag.h:68
@ ACCELMAG_REG_MOUT_X_LSB
Definition: fxos8700_accelmag.h:71
@ ACCELMAG_REG_MCTRL2
Definition: fxos8700_accelmag.h:77
@ ACCELMAG_REG_AOUT_Y_LSB
Definition: fxos8700_accelmag.h:61
@ ACCELMAG_REG_MOUT_Z_MSB
Definition: fxos8700_accelmag.h:74
@ ACCELMAG_REG_CTRL3
Definition: fxos8700_accelmag.h:66
@ ACCELMAG_REG_AOUT_Z_MSB
Definition: fxos8700_accelmag.h:62
@ ACCELMAG_REG_MOUT_Y_LSB
Definition: fxos8700_accelmag.h:73
@ ACCELMAG_REG_CTRL4
Definition: fxos8700_accelmag.h:67
constexpr float ACCELMAG_CVT_UT
Definition: fxos8700_accelmag.h:38
AccelRanges_t
Accelerometer measurement ranges.
Definition: fxos8700_accelmag.h:44
@ ACCEL_RNG_4G
Definition: fxos8700_accelmag.h:46
@ ACCEL_RNG_2G
Definition: fxos8700_accelmag.h:45
@ ACCEL_RNG_8G
Definition: fxos8700_accelmag.h:47
constexpr uint8_t FXOS8700_ADDRESS
Definition: fxos8700_accelmag.h:30
constexpr float ACCELMAG_CVT_GS_8G
Definition: fxos8700_accelmag.h:37
constexpr float ACCELMAG_CVT_GS_4G
Definition: fxos8700_accelmag.h:36
constexpr float ACCELMAG_CVT_GS_2G
Definition: fxos8700_accelmag.h:35
#define SENSOR_I2C
UBER-EXTREME CAUTION SHOULD BE USED CHANGING PARAMETERS IN THIS FILE.
Definition: hummingbird_config.h:26