Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
fxas21002_gyro.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // FXAS21002C GYROSCOPE SENSOR I2C DRIVER CODE
3 //
4 // Code By: Michael Wrona
5 // Created: 25 July 2020
6 // Modified: 26 Aug 2021
7 // ----------------------------------------------------------------------------
8 // This is the sensor library for the FXAS21002C 3-axis gyroscope sensor in
9 // I2C mode.
10 // Datasheet Specs:
11 // 16-bit ADC
12 // +/- 250dps to 2000dps measurement ranges
13 // Integrated LPF
14 // 8-bit temperature sensor
15 // Temperature sensitivity of +/- 0.08 %/degC (max)
16 // Nonlinearity of +/- 0.5 %FSR
17 // 0.025 dps/sqrt(Hz) noise density (FS=200Hz, CTRL_REG0[FS] = 00, CTRL_REG0[BW] = 00)
18 
19 
20 #pragma once
21 
22 #include <Arduino.h>
23 #include <Wire.h>
24 // #include <limits.h>
25 #include "constants.h"
26 #include "debugging.h"
27 #include "conversions.h"
28 #include "hummingbird_config.h"
29 
30 
31 #ifdef DEBUG
32 #define FXAS21002_DEBUG // Toggle FXAS21002 gyro debug messages
33 #endif
34 
35 
36 
37 // ----------------------------------------------------------------------------
38 // Device I2C addresses
39 // ----------------------------------------------------------------------------
40 constexpr uint8_t FXAS21002C_ADDRESS = 0x21; // 7-bit address
41 constexpr uint8_t FXAS21002C_ID = 0xD7; // Device ID
42 
43 
48 constexpr float GYRO_SENS_250 = 0.0078125f; // 250dps sensitivity [dps/LSB]
49 constexpr float GYRO_SENS_500 = 0.015625f; // 500dps sensitivity [dps/LSB]
50 constexpr float GYRO_SENS_1000 = 0.03125f; // 1000dps sensitivity [dps/LSB]
51 constexpr float GYRO_SENS_2000 = 0.0625f; // 2000dps sensitivity [dps/LSB]
52 
53 
57 typedef enum
58 {
66  GYRO_REG_ID = 0x0C,
67  GYRO_REG_TEMP = 0x12, // (p.45)
68  GYRO_REG_CTRL0 = 0x0D, // (p.38)
70  GYRO_REG_CTRL2 = 0x14
72 
73 
77 typedef enum
78 {
79  GYRO_RNG_250DPS = 250, // 250dps range
80  GYRO_RNG_500DPS = 500, // 500dps range
81  GYRO_RNG_1000DPS = 1000, // 1000dps range
82  GYRO_RNG_2000DPS = 2000 // 2000dps range
84 
85 
90 {
91 public:
92  FXAS21002Gyro(TwoWire *wireInput = &SENSOR_I2C);
95  bool ReadSensor();
96  float GetTemperature();
97  float GetGx();
98  float GetGy();
99  float GetGz();
100  uint32_t prevMeasMicros;
101 protected:
102 private:
103  void I2Cwrite8(uint8_t regOfInterest, uint8_t valToWrite);
104  uint8_t I2Cread8(uint8_t regOfInterest);
105  float _gx;
106  float _gy;
107  float _gz;
109  TwoWire *_SensorWire;
110 };
NXP FXAS21002 gyro sensor driver.
Definition: fxas21002_gyro.h:90
uint8_t I2Cread8(uint8_t regOfInterest)
Read FXAS21002 register value over I2C.
Definition: fxas21002_gyro.cpp:252
float GetGy()
Return gyro y-measurement in [deg/s].
Definition: fxas21002_gyro.cpp:215
float GetTemperature()
Read device's 8-bit temperature register and return in degrees C.
Definition: fxas21002_gyro.cpp:190
float GetGx()
Return gyro x-measurement in [deg/s].
Definition: fxas21002_gyro.cpp:206
bool Initialize(GyroRanges_t rng=GYRO_RNG_1000DPS)
Initialize and configure gyroscope sensor.
Definition: fxas21002_gyro.cpp:48
bool ReadSensor()
Read gyroscope data from device registers.
Definition: fxas21002_gyro.cpp:116
GyroRanges_t gyroRange
Selected gyro measurement range.
Definition: fxas21002_gyro.h:108
TwoWire * _SensorWire
I2C bus the sensor is connected to.
Definition: fxas21002_gyro.h:109
~FXAS21002Gyro()
Definition: fxas21002_gyro.h:93
float _gy
Gyro y reading, [deg/s].
Definition: fxas21002_gyro.h:106
float GetGz()
Return gyro z-measurement in [deg/s].
Definition: fxas21002_gyro.cpp:224
uint32_t prevMeasMicros
Previous measurement micros()
Definition: fxas21002_gyro.h:100
float _gx
Gyro x reading, [deg/s].
Definition: fxas21002_gyro.h:105
float _gz
Gyro z reading, [deg/s].
Definition: fxas21002_gyro.h:107
void I2Cwrite8(uint8_t regOfInterest, uint8_t valToWrite)
Write to FXAS21002 device register over I2C.
Definition: fxas21002_gyro.cpp:236
FXAS21002Gyro(TwoWire *wireInput=&SENSOR_I2C)
FXAS21002 Constructor.
Definition: fxas21002_gyro.cpp:29
constexpr float GYRO_SENS_2000
Definition: fxas21002_gyro.h:51
GyroRanges_t
Gyro measurement ranges.
Definition: fxas21002_gyro.h:78
@ GYRO_RNG_1000DPS
Definition: fxas21002_gyro.h:81
@ GYRO_RNG_250DPS
Definition: fxas21002_gyro.h:79
@ GYRO_RNG_2000DPS
Definition: fxas21002_gyro.h:82
@ GYRO_RNG_500DPS
Definition: fxas21002_gyro.h:80
constexpr uint8_t FXAS21002C_ADDRESS
Definition: fxas21002_gyro.h:40
GyroRegisters_t
FXAS21002 device registers.
Definition: fxas21002_gyro.h:58
@ GYRO_REG_STATUS
Definition: fxas21002_gyro.h:59
@ GYRO_REG_XOUT_LSB
Definition: fxas21002_gyro.h:61
@ GYRO_REG_CTRL2
Definition: fxas21002_gyro.h:70
@ GYRO_REG_XOUT_MSB
Definition: fxas21002_gyro.h:60
@ GYRO_REG_CTRL0
Definition: fxas21002_gyro.h:68
@ GYRO_REG_CTRL1
Definition: fxas21002_gyro.h:69
@ GYRO_REG_ZOUT_MSB
Definition: fxas21002_gyro.h:64
@ GYRO_REG_ID
Definition: fxas21002_gyro.h:66
@ GYRO_REG_TEMP
Definition: fxas21002_gyro.h:67
@ GYRO_REG_ZOUT_LSB
Definition: fxas21002_gyro.h:65
@ GYRO_REG_YOUT_LSB
Definition: fxas21002_gyro.h:63
@ GYRO_REG_YOUT_MSB
Definition: fxas21002_gyro.h:62
constexpr float GYRO_SENS_250
Gyro measurement sensitivities.
Definition: fxas21002_gyro.h:48
constexpr uint8_t FXAS21002C_ID
Definition: fxas21002_gyro.h:41
constexpr float GYRO_SENS_500
Definition: fxas21002_gyro.h:49
constexpr float GYRO_SENS_1000
Definition: fxas21002_gyro.h:50
#define SENSOR_I2C
UBER-EXTREME CAUTION SHOULD BE USED CHANGING PARAMETERS IN THIS FILE.
Definition: hummingbird_config.h:26