Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
inertial_nav_system.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // ACCELEROMETER, GYRO, AND MAGNETOMETER INERTIAL SENSOR SYSTEM
3 //
4 // Code By: Michael Wrona
5 // Created: 27 Feb 2021
6 // ----------------------------------------------------------------------------
14 #pragma once
15 
16 
17 #include <Wire.h>
18 #include <Arduino.h>
19 #include <math.h>
20 #include "debugging.h"
21 #include "maths/vectors.h"
22 #include "gravity_computer.h"
23 #include "hummingbird_config.h"
28 
29 
30 #ifdef DEBUG
31  #define INS_DEBUG // Print INS debug messages to the debug port.
32 #endif
33 
34 /* Filters */
35 constexpr float INS_ACCEL_LPF_SF = 0.95f; // Smoothing factor (alpha) of accelerometer low-pass filter, [0, 1]
36 // constexpr float INS_GYRO_LPF_SF = 0.98f; // Gyro low pass filter smoothing factor [0, 1]
37 
38 /* Turn-on biases */
39 constexpr uint32_t INS_BIAS_INIT_TIME = 1000; // [millisec] Amount of time taken to determine accel. and gyro turn-on bias
40 
41 /* Measurement ranges */
42 constexpr GyroRanges_t INS_GYRO_RANGE = GYRO_RNG_1000DPS; // Gyro measurement range
43 constexpr AccelRanges_t INS_ACCEL_RANGE = ACCEL_RNG_4G; // Accelerometer measurement range
44 
45 
46 // ----------------------------------------------------------------------------
47 // InertialNavSystem()
48 // ----------------------------------------------------------------------------
55 {
56 public:
59 
60  // Do not allow copies (singleton)
61  static InertialNavSystem &GetInstance(); // Accessor
64 
65  void SetSampleRate(float fs_hz);
66  bool Initialize();
67  bool Update();
68  float GetAccelPitch();
69  float GetAccelRoll();
70 
71  Vectorf Gyro; // [rad/s], [gx, gy, gz] Gyro measurements (filtered)
72  Vectorf GyroRaw; // [deg/s], [gx, gy, gz] Raw gyro measurements
73  Vectorf GyroTOBias; // [rad/s], [bgx, bgy, bgz] Measured gyro turn-on biases
74  Vectorf Accel; // [m/s/s], [ax, ay, az] Accelerometer measurements (filtered)
75  Vectorf AccelRaw; // [g's], [ax, ay, az] Raw accelerometer measurements
76  Vectorf AccelTOBias; // [m/s/s], [bax, bay, baz] Measured accelerometer turn-on biases
77  uint32_t prevUpdateMicros; // [us] Previous INS update micros()
78 protected:
79 private:
80  void UpdateAccelAngles();
81  bool MeasureInitGyroBiases(uint32_t samplePeriod);
82  bool MeasureInitAccelBiases(uint32_t samplePeriod);
83 
84  float roll; // [rad] Accelerometer roll angle (NED)
85  float pitch; // [rad] Accelerometer pitch angle (NED)
86 
87  FXOS8700AccelMag AccelMagSensor; // Accelerometer/magnetometer sensor class
88  FXAS21002Gyro GyroSensor; // Gyroscope sensor class
89  LowPassFilter AxLPF; // Ax data filter
90  LowPassFilter AyLPF; // Ay data filter
91  LowPassFilter AzLPF; // Az data filter
92 };
93 
94 
95 
96 // Only one instance of InertialNavSystem
97 extern InertialNavSystem &INS;
NXP FXAS21002 gyro sensor driver.
Definition: fxas21002_gyro.h:90
NXP Semiconductor FXOS8700 accelerometer/magnetometer sensor class.
Definition: fxos8700_accelmag.h:86
Constructor for the InertialNavSystem class.
Definition: inertial_nav_system.h:55
bool Initialize()
Initialize the INS.
Definition: inertial_nav_system.cpp:46
bool MeasureInitGyroBiases(uint32_t samplePeriod)
Measure initial gyro turn-on biases.
Definition: inertial_nav_system.cpp:258
~InertialNavSystem()
Definition: inertial_nav_system.cpp:379
float GetAccelPitch()
Definition: inertial_nav_system.cpp:212
LowPassFilter AzLPF
Definition: inertial_nav_system.h:91
bool MeasureInitAccelBiases(uint32_t samplePeriod)
Measure initial accelerometer turn-on bias.
Definition: inertial_nav_system.cpp:310
bool Update()
Record accelerometer and gyro measurements, apply noise filters, and update accel.
Definition: inertial_nav_system.cpp:132
Vectorf AccelTOBias
Definition: inertial_nav_system.h:76
float GetAccelRoll()
Definition: inertial_nav_system.cpp:218
static InertialNavSystem & GetInstance()
Definition: inertial_nav_system.cpp:382
InertialNavSystem(const InertialNavSystem &)=delete
float roll
Definition: inertial_nav_system.h:84
Vectorf AccelRaw
Definition: inertial_nav_system.h:75
LowPassFilter AxLPF
Definition: inertial_nav_system.h:89
void UpdateAccelAngles()
Compute accelerometer roll and pitch angles.
Definition: inertial_nav_system.cpp:235
uint32_t prevUpdateMicros
Definition: inertial_nav_system.h:77
FXOS8700AccelMag AccelMagSensor
Definition: inertial_nav_system.h:87
LowPassFilter AyLPF
Definition: inertial_nav_system.h:90
FXAS21002Gyro GyroSensor
Definition: inertial_nav_system.h:88
InertialNavSystem & operator=(const InertialNavSystem &)=delete
void SetSampleRate(float fs_hz)
InertialNavSystem()
The inertial sensor software system brings together the accelerometer and gyro, sensors to form the...
Definition: inertial_nav_system.cpp:27
float pitch
Definition: inertial_nav_system.h:85
Vectorf Gyro
Definition: inertial_nav_system.h:71
Vectorf GyroRaw
Definition: inertial_nav_system.h:72
Vectorf GyroTOBias
Definition: inertial_nav_system.h:73
Vectorf Accel
Definition: inertial_nav_system.h:74
This is a simple implementation of a discrete low pass filter to filter noisy signals.
Definition: low_pass_filter.h:25
A vector object is definied by it's rows/length.
Definition: vectors.h:46
GyroRanges_t
Gyro measurement ranges.
Definition: fxas21002_gyro.h:78
@ GYRO_RNG_1000DPS
Definition: fxas21002_gyro.h:81
AccelRanges_t
Accelerometer measurement ranges.
Definition: fxos8700_accelmag.h:44
@ ACCEL_RNG_4G
Definition: fxos8700_accelmag.h:46
constexpr uint32_t INS_BIAS_INIT_TIME
Definition: inertial_nav_system.h:39
constexpr GyroRanges_t INS_GYRO_RANGE
Definition: inertial_nav_system.h:42
constexpr AccelRanges_t INS_ACCEL_RANGE
Definition: inertial_nav_system.h:43
InertialNavSystem & INS
Definition: inertial_nav_system.cpp:388
constexpr float INS_ACCEL_LPF_SF
The inertial sensor software system brings together the accelerometer and gyro, sensors to form the...
Definition: inertial_nav_system.h:35