18#ifndef SENSOR_FUSION_H
19#define SENSOR_FUSION_H
80 #define PI 3.141592654F
82#define PIOVER2 1.570796327F
83#define FPIOVER180 0.01745329251994F
84#define F180OVERPI 57.2957795130823F
85#define F180OVERPISQ 3282.8063500117F
86#define ONETHIRD 0.33333333F
87#define ONESIXTH 0.166666667F
88#define ONESIXTEENTH 0.0625F
89#define ONEOVER12 0.083333333F
90#define ONEOVER48 0.02083333333F
91#define ONEOVER120 0.0083333333F
92#define ONEOVER3840 0.0002604166667F
93#define ONEOVERSQRT2 0.707106781F
94#define SQRT15OVER4 0.968245837F
95#define GTOMSEC2 9.80665
116typedef int8_t (initializeSensor_t) (
120typedef int8_t (readSensor_t) (
124typedef int8_t (readSensors_t) (
126 uint8_t read_loop_counter
138typedef void (initializeFusionEngine_t) (
struct SensorFusionGlobals *sfg,
int pin_i2c_sda1,
int pin_i2c_scl);
555initializeFusionEngine_t initializeFusionEngine ;
567runFusion_t runFusion;
568readSensors_t readSensors;
573 uint16_t numElements,
596 uint16_t maxFifoSize,
Board configuration file.
#define GYRO_FIFO_SIZE
FXAX21000, FXAS21002 have 32 element FIFO.
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
#define MAG_FIFO_SIZE
FXOS8700 (mag) and MAG3110 have no FIFO so equivalent to 1 element FIFO. For.
Build configuration file.
The driver_sensors_types.h file contains sensor state structs and error definitions.
Lower level magnetic calibration interface.
Matrix manipulation functions.
Functions to convert between various orientation representations.
Implements accelerometer calibration routines.
void zeroArray(struct StatusSubsystem *pStatus, void *data, uint16_t size, uint16_t numElements, uint8_t check)
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two's complement value for a 16-...
enum quaternion quaternion_type
the quaternion type to be transmitted
void initSensorFusionGlobals(SensorFusionGlobals *sfg, struct StatusSubsystem *pStatusSubsystem, struct ControlSubsystem *pControlSubsystem)
utility function to insert default values in the top level structure
quaternion
the quaternion type to be transmitted
@ Q6MA
Quaternion derived from 3-axis accel + 3 axis mag (eCompass)
@ Q3
Quaternion derived from 3-axis accel (tilt)
@ Q6AG
Quaternion derived from 3-axis accel + 3-axis gyro (gaming)
@ Q9
Quaternion derived from full 9-axis sensor fusion.
@ Q3M
Quaternion derived from 3-axis mag only (auto compass algorithm)
@ Q3G
Quaternion derived from 3-axis gyro only (rotation)
fusion_status_t
Application-specific serial communications system.
@ RECEIVING_WIRED
Receiving commands over wired interface (momentary)
@ HARD_FAULT
Non-recoverable FAULT = something went very wrong.
@ LOWPOWER
Running in reduced power mode.
@ NORMAL
Operation is Nominal.
@ RECEIVING_WIRELESS
Receiving commands over wireless interface (momentary)
@ INITIALIZING
Initializing sensors and algorithms.
@ OFF
These are the state definitions for the status subsystem.
@ SOFT_FAULT
Recoverable FAULT = something went wrong, but we can keep going.
void ApplyMagHAL(struct MagSensor *Mag)
Apply the magnetometer Hardware Abstraction Layer.
void clearFIFOs(SensorFusionGlobals *sfg)
Function to clear FIFO at the end of each fusion computation.
void ApplyGyroHAL(struct GyroSensor *Gyro)
Apply the gyroscope Hardware Abstraction Layer.
applyPerturbation_t ApplyPerturbation
ApplyPerturbation is a reverse unit-step test function.
int8_t() installSensor_t(struct SensorFusionGlobals *sfg, struct PhysicalSensor *sensor, uint16_t addr, uint16_t schedule, registerDeviceInfo_t *busInfo, initializeSensor_t *initialize, readSensor_t *read)
void addToFifo(union FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
void conditionSensorReadings(SensorFusionGlobals *sfg)
void ApplyAccelHAL(struct AccelSensor *Accel)
Apply the accelerometer Hardware Abstraction Layer.
accelerometer measurement buffer
precision accelerometer calibration structure
The AccelSensor structure stores raw and processed measurements for a 3-axis accelerometer.
float fgPerCount
g per count
float fGc[3]
averaged precision calibrated measurement (g)
int16_t iCountsPerg
counts per g
bool isEnabled
true if the device is sampling
uint8_t iFIFOCount
number of measurements read from FIFO
float fCountsPerg
counts per g
uint16_t iFIFOExceeded
Number of samples received in excess of software FIFO size.
int16_t iGc[3]
averaged precision calibrated measurement (counts)
int16_t iGsFIFO[ACCEL_FIFO_SIZE][3]
FIFO measurements (counts)
int16_t iGs[3]
averaged measurement (counts)
float fGs[3]
averaged measurement (g)
uint8_t iWhoAmI
sensor whoami
The ControlSubsystem encapsulates command and data streaming functions.
The GyroSensor structure stores raw and processed measurements for a 3-axis gyroscope.
uint8_t iWhoAmI
sensor whoami
int16_t iYs[3]
average measurement (counts)
int16_t iCountsPerDegPerSec
counts per deg/s
int16_t iYsFIFO[GYRO_FIFO_SIZE][3]
FIFO measurements (counts)
uint16_t iFIFOExceeded
Number of samples received in excess of software FIFO size.
uint8_t iFIFOCount
number of measurements read from FIFO
float fDegPerSecPerCount
deg/s per count
float fYs[3]
averaged measurement (deg/s)
bool isEnabled
true if the device is sampling
Magnetic Calibration Structure.
The MagSensor structure stores raw and processed measurements for a 3-axis magnetic sensor.
int16_t iBc[3]
averaged calibrated measurement (counts)
uint8_t iFIFOCount
number of measurements read from FIFO
float fCountsPeruT
counts per uT
float fuTPerCount
uT per count
float fBs[3]
averaged un-calibrated measurement (uT)
uint8_t iWhoAmI
sensor whoami
int16_t iBsFIFO[MAG_FIFO_SIZE][3]
FIFO measurements (counts)
uint16_t iFIFOExceeded
Number of samples received in excess of software FIFO size.
float fBc[3]
averaged calibrated measurement (uT)
bool isEnabled
true if the device is sampling
int16_t iBs[3]
averaged uncalibrated measurement (counts)
int16_t iCountsPeruT
counts per uT
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
registerDeviceInfo_t deviceInfo
I2C device context.
readSensor_t * read
pointer to function to read sensor using the supplied drivers
uint16_t addr
I2C address if applicable.
initializeSensor_t * initialize
pointer to function to initialize sensor using the supplied drivers
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
registerDeviceInfo_t * busInfo
information required for bus power management
struct PhysicalSensor * next
pointer to next sensor in this linked list
uint8_t schedule
Parameter to control sensor sampling rate.
The PressureSensor structure stores raw and processed measurements for an altimeter.
bool isEnabled
true if the device is sampling
int32_t iP
most recent unaveraged pressure (counts)
float fT
most recent unaveraged temperature (C)
uint8_t iWhoAmI
sensor whoami
float fmPerCount
meters per count
int32_t iH
most recent unaveraged height (counts)
float fCPerCount
degrees Celsius per count
int16_t iT
most recent unaveraged temperature (counts)
float fH
most recent unaveraged height (m)
quaternion structure definition
The SV_1DOF_P_BASIC structure contains state information for a pressure sensor/altimeter.
float fdeltat
fusion time interval (s)
float flpf
low pass filter coefficient
int8_t resetflag
flag to request re-initialization on next pass
int32_t systick
systick timer
float fLPH
low pass filtered height (m)
float fLPT
low pass filtered temperature (C)
This is the 3DOF basic magnetometer state vector structure/.
float fLPChi
low pass tilt from vertical (deg)
int32_t systick
systick timer
Quaternion fq
unfiltered orientation quaternion
int8_t resetflag
flag to request re-initialization on next pass
float fLPPsi
low pass yaw (deg)
float fOmega[3]
angular velocity (deg/s)
float fR[3][3]
unfiltered orientation matrix
float flpf
low pass filter coefficient
Quaternion fLPq
low pass filtered orientation quaternion
float fdeltat
fusion time interval (s)
float fLPRVec[3]
rotation vector
float fLPThe
low pass pitch (deg)
float fLPRho
low pass compass (deg)
float fLPPhi
low pass roll (deg)
float fLPR[3][3]
low pass filtered orientation matrix
This is the 3DOF basic accelerometer state vector structure.
float fR[3][3]
unfiltered orientation matrix
Quaternion fq
unfiltered orientation quaternion
float fdeltat
fusion time interval (s)
float fLPThe
low pass pitch (deg)
float fLPPsi
low pass yaw (deg)
float fLPR[3][3]
low pass filtered orientation matrix
Quaternion fLPq
low pass filtered orientation quaternion
float fLPRho
low pass compass (deg)
int32_t systick
systick timer
float fLPRVec[3]
rotation vector
float flpf
low pass filter coefficient
float fLPChi
low pass tilt from vertical (deg)
int8_t resetflag
flag to request re-initialization on next pass
float fOmega[3]
angular velocity (deg/s)
float fLPPhi
low pass roll (deg)
SV_3DOF_Y_BASIC structure is the 3DOF basic gyroscope state vector structure.
float fRVec[3]
rotation vector
int32_t systick
systick timer
Quaternion fq
unfiltered orientation quaternion
float fOmega[3]
angular velocity (deg/s)
float fR[3][3]
unfiltered orientation matrix
int8_t resetflag
flag to request re-initialization on next pass
float fChi
tilt from vertical (deg)
float fdeltat
fusion filter sampling interval (s)
SV_6DOF_GB_BASIC is the 6DOF basic accelerometer and magnetometer state vector structure.
Quaternion fq
unfiltered orientation quaternion
float fLPPhi
low pass roll (deg)
Quaternion fLPq
low pass filtered orientation quaternion
float fLPR[3][3]
low pass filtered orientation matrix
int8_t resetflag
flag to request re-initialization on next pass
float fLPDelta
low pass filtered inclination angle (deg)
float fdeltat
fusion time interval (s)
float fR[3][3]
unfiltered orientation matrix
float fLPChi
low pass tilt from vertical (deg)
float flpf
low pass filter coefficient
float fDelta
unfiltered inclination angle (deg)
float fOmega[3]
virtual gyro angular velocity (deg/s)
float fLPPsi
low pass yaw (deg)
float fLPThe
low pass pitch (deg)
float fLPRho
low pass compass (deg)
float fLPRVec[3]
rotation vector
int32_t systick
systick timer
SV_6DOF_GY_KALMAN is the 6DOF Kalman filter accelerometer and gyroscope state vector structure.
float fQw6x6[6][6]
covariance matrix Qw
float fOmega[3]
average angular velocity (deg/s)
float fAccGl[3]
linear acceleration (g) in global frame
float fAlphaOver2
PI / 180 * fdeltat / 2.
float fqgErrPl[3]
gravity vector tilt orientation quaternion error (dimensionless)
float fbPl[3]
gyro offset (deg/s)
float fAlphaSqOver4
(PI / 180 * fdeltat)^2 / 4
float fQwCT6x3[6][3]
Qw.C^T matrix.
float fRPl[3][3]
a posteriori orientation matrix
float fK6x3[6][3]
kalman filter gain matrix K
float fAlphaQwbOver6
(PI / 180 * fdeltat) * Qwb / 6
float fMaxGyroOffsetChange
maximum permissible gyro offset change per iteration (deg/s)
float fRhoPl
compass (deg)
Quaternion fqPl
a posteriori orientation quaternion
float fdeltat
sensor fusion interval (s)
float fChiPl
tilt from vertical (deg)
float fZErr[3]
measurement error vector
int32_t systick
systick timer;
float fbErrPl[3]
gyro offset error (deg/s)
int8_t resetflag
flag to request re-initialization on next pass
float fAlphaSqQvYQwbOver12
(PI / 180 * fdeltat)^2 * (QvY + Qwb) / 12
float fQv
measurement noise covariance matrix leading diagonal
float fRVecPl[3]
rotation vector
SV_9DOF_GBY_KALMAN is the 9DOF Kalman filter accelerometer, magnetometer and gyroscope state vector s...
int32_t systick
systick timer;
float fAlphaOver2
PI / 180 * fdeltat / 2.
float fChiPl
tilt from vertical (deg)
float fDeltaPl
a posteriori inclination angle from Kalman filter (deg)
float fqmErrPl[3]
geomagnetic vector tilt orientation quaternion error (dimensionless)
float fqgErrPl[3]
gravity vector tilt orientation quaternion error (dimensionless)
float fbErrPl[3]
gyro offset error (deg/s)
float fcosDeltaPl
cos(fDeltaPl)
float fsinDeltaPl
sin(fDeltaPl)
float fRhoPl
compass (deg)
float fK9x6[9][6]
kalman filter gain matrix K
float fAlphaSqOver4
(PI / 180 * fdeltat)^2 / 4
float fQw9x9[9][9]
covariance matrix Qw
Quaternion fqPl
a posteriori orientation quaternion
float fAlphaSqQvYQwbOver12
(PI / 180 * fdeltat)^2 * (QvY + Qwb) / 12
float fRVecPl[3]
rotation vector
float fgdeltat
g (m/s2) * fdeltat
float fVelGl[3]
velocity (m/s) in global frame
float fAlphaQwbOver6
(PI / 180 * fdeltat) * Qwb / 6
float fDisGl[3]
displacement (m) in global frame
float fMaxGyroOffsetChange
maximum permissible gyro offset change per iteration (deg/s)
float fOmega[3]
average angular velocity (deg/s)
int8_t resetflag
flag to request re-initialization on next pass
float fAccGl[3]
linear acceleration (g) in global frame
float fbPl[3]
gyro offset (deg/s)
float fZErr[6]
measurement error vector
float fRPl[3][3]
a posteriori orientation matrix
float fdeltat
sensor fusion interval (s)
float fQwCT9x6[9][6]
Qw.C^T matrix.
float fQv6x1[6]
measurement noise covariance matrix leading diagonal
int8_t iFirstAccelMagLock
denotes that 9DOF orientation has locked to 6DOF eCompass
int32_t systick
systick timer;
float fChi
tilt from vertical (deg)
Quaternion fq
orientation quaternion
float fOmega[3]
average angular velocity (deg/s)
float fRVec[3]
rotation vector
float fRM[3][3]
orientation matrix
The top level fusion structure.
struct MagCalibration MagCal
mag cal storage
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
clearFIFOs_t * clearFIFOs
clear sensor FIFOs
installSensor_t * installSensor
function for installing a new sensor
struct PhysicalSensor * pSensors
a linked list of physical sensors
int32_t systick_I2C
systick counter to benchmark I2C reads
updateStatus_t * testStatus
increment to next enumerated status value (test only)
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
setStatus_t * queueStatus
queue status change for next regular interval
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
getStatus_t * getStatus
fetch the current status from the Status Subsystem
struct MagSensor Mag
magnetometer storage
int32_t systick_Spare
systick counter for counts spare waiting for timing interrupt
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
runFusion_t * runFusion
run the fusion routines
volatile uint8_t iPerturbation
test perturbation to be applied
readSensors_t * readSensors
read all physical sensors
setStatus_t * setStatus
change status indicator immediately
updateStatus_t * updateStatus
status=next status
uint32_t iFlags
a bit-field of sensors and algorithms used
StatusSubsystem() provides an object-like interface for communicating status to the user.
fusion_status_t status
Current status.
The TempSensor structure stores raw temperature readings.
This structure defines the device specific info required by register I/O.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...