ISSDK  1.7
IoT Sensing Software Development Kit
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
sensor_fusion.h File Reference

The sensor_fusion.h file implements the top level programming interface. More...

#include "math.h"
#include "stdbool.h"
#include "stdio.h"
#include "stdint.h"
#include "issdk_hal.h"
#include "build.h"
#include "magnetic.h"
#include "precisionAccelerometer.h"
#include "orientation.h"
#include "register_io_spi.h"
#include "matrix.h"
Include dependency graph for sensor_fusion.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PhysicalSensor
 An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo devices = 1) More...
 
struct  PressureSensor
 The PressureSensor structure stores raw and processed measurements for an altimeter. More...
 
struct  AccelSensor
 The AccelSensor structure stores raw and processed measurements for a 3-axis accelerometer. More...
 
struct  MagSensor
 The MagSensor structure stores raw and processed measurements for a 3-axis magnetic sensor. More...
 
struct  GyroSensor
 The GyroSensor structure stores raw and processed measurements for a 3-axis gyroscope. More...
 
union  FifoSensor
 The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures. More...
 
struct  SV_1DOF_P_BASIC
 The SV_1DOF_P_BASIC structure contains state information for a pressure sensor/altimeter. More...
 
struct  SV_3DOF_G_BASIC
 This is the 3DOF basic accelerometer state vector structure. More...
 
struct  SV_3DOF_B_BASIC
 This is the 3DOF basic magnetometer state vector structure/. More...
 
struct  SV_3DOF_Y_BASIC
 SV_3DOF_Y_BASIC structure is the 3DOF basic gyroscope state vector structure. More...
 
struct  SV_6DOF_GB_BASIC
 SV_6DOF_GB_BASIC is the 6DOF basic accelerometer and magnetometer state vector structure. More...
 
struct  SV_6DOF_GY_KALMAN
 SV_6DOF_GY_KALMAN is the 6DOF Kalman filter accelerometer and gyroscope state vector structure. More...
 
struct  SV_9DOF_GBY_KALMAN
 SV_9DOF_GBY_KALMAN is the 9DOF Kalman filter accelerometer, magnetometer and gyroscope state vector structure. More...
 
struct  SV_COMMON
 
struct  SensorFusionGlobals
 The top level fusion structure. More...
 

Macros

#define true   1
 Boolean TRUE. More...
 
#define false   0
 Boolean FALSE. More...
 
#define SPI_ADDR   0x00
 
Vector Components

Index values for accessing vector terms

#define CHX   0
 Used to access X-channel entries in various data data structures. More...
 
#define CHY   1
 Used to access Y-channel entries in various data data structures. More...
 
#define CHZ   2
 Used to access Z-channel entries in various data data structures. More...
 
Generic bit-field values

Generic bit-field values

#define B0   (1 << 0)
 
#define B1   (1 << 1)
 
#define B2   (1 << 2)
 
#define B3   (1 << 3)
 
Math Constants

useful multiplicative conversion constants

#define PI   3.141592654F
 pi More...
 
#define PIOVER2   1.570796327F
 pi / 2 More...
 
#define FPIOVER180   0.01745329251994F
 degrees to radians conversion = pi / 180 More...
 
#define F180OVERPI   57.2957795130823F
 radians to degrees conversion = 180 / pi More...
 
#define F180OVERPISQ   3282.8063500117F
 square of F180OVERPI More...
 
#define ONETHIRD   0.33333333F
 one third More...
 
#define ONESIXTH   0.166666667F
 one sixth More...
 
#define ONESIXTEENTH   0.0625F
 one sixteenth More...
 
#define ONEOVER12   0.083333333F
 1 / 12 More...
 
#define ONEOVER48   0.02083333333F
 1 / 48 More...
 
#define ONEOVER120   0.0083333333F
 1 / 120 More...
 
#define ONEOVER3840   0.0002604166667F
 1 / 3840 More...
 
#define ONEOVERSQRT2   0.707106781F
 1/sqrt(2) More...
 
#define SQRT15OVER4   0.968245837F
 sqrt(15)/4 More...
 
#define GTOMSEC2   9.80665
 standard gravity in m/s2 More...
 

Typedefs

typedef enum quaternion quaternion_type
 the quaternion type to be transmitted More...
 
typedef int8_t() initializeSensor_t(struct PhysicalSensor *sensor, struct SensorFusionGlobals *sfg)
 
typedef int8_t() readSensor_t(struct PhysicalSensor *sensor, struct SensorFusionGlobals *sfg)
 
typedef int8_t() readSensors_t(struct SensorFusionGlobals *sfg, uint16_t read_loop_counter)
 
typedef int8_t() installSensor_t(struct SensorFusionGlobals *sfg, struct PhysicalSensor *sensor, uint16_t addr, uint16_t schedule, void *bus_driver, registerDeviceInfo_t *busInfo, initializeSensor_t *initialize, readSensor_t *read)
 
typedef void() initializeFusionEngine_t(struct SensorFusionGlobals *sfg)
 
typedef void() runFusion_t(struct SensorFusionGlobals *sfg)
 
typedef void() clearFIFOs_t(struct SensorFusionGlobals *sfg)
 
typedef void() conditionSensorReadings_t(struct SensorFusionGlobals *sfg)
 
typedef void() applyPerturbation_t(struct SensorFusionGlobals *sfg)
 
typedef void() setStatus_t(struct SensorFusionGlobals *sfg, fusion_status_t status)
 
typedef void() updateStatus_t(struct SensorFusionGlobals *sfg)
 
typedef void() ssSetStatus_t(struct StatusSubsystem *pStatus, fusion_status_t status)
 
typedef void() ssUpdateStatus_t(struct StatusSubsystem *pStatus)
 
typedef struct SV_COMMONSV_ptr
 
typedef struct SensorFusionGlobals SensorFusionGlobals
 The top level fusion structure. More...
 
Integer Typedefs

Typedefs to map common integer types to standard form

typedef unsigned char byte
 
typedef int8_t int8
 
typedef int16_t int16
 
typedef int32_t int32
 
typedef uint8_t uint8
 
typedef uint16_t uint16
 
typedef uint32_t uint32
 

Enumerations

enum  quaternion {
  Q3, Q3M, Q3G, Q6MA,
  Q6AG, Q9
}
 the quaternion type to be transmitted More...
 
enum  fusion_status_t {
  OFF, INITIALIZING, LOWPOWER, NORMAL,
  RECEIVING_WIRED, RECEIVING_WIRELESS, HARD_FAULT, SOFT_FAULT
}
 Application-specific serial communications system. More...
 

Functions

void initSensorFusionGlobals (SensorFusionGlobals *sfg, struct StatusSubsystem *pStatusSubsystem, struct ControlSubsystem *pControlSubsystem)
 utility function to insert default values in the top level structure More...
 
void conditionSensorReadings (SensorFusionGlobals *sfg)
 
void clearFIFOs (SensorFusionGlobals *sfg)
 Function to clear FIFO at the end of each fusion computation. More...
 
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-bit variable (-32768). More...
 
void addToFifo (union FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
 addToFifo is called from within sensor driver read functions More...
 
void ApplyAccelHAL (struct AccelSensor *Accel)
 Apply the accelerometer Hardware Abstraction Layer. More...
 
void ApplyMagHAL (struct MagSensor *Mag)
 Apply the magnetometer Hardware Abstraction Layer. More...
 
void ApplyGyroHAL (struct GyroSensor *Gyro)
 Apply the gyroscope Hardware Abstraction Layer. More...
 

Variables

installSensor_t installSensor
 
initializeFusionEngine_t initializeFusionEngine
 
runFusion_t runFusion
 
readSensors_t readSensors
 
applyPerturbation_t ApplyPerturbation
 ApplyPerturbation is a reverse unit-step test function. More...
 

Detailed Description

The sensor_fusion.h file implements the top level programming interface.

Definition in file sensor_fusion.h.

Macro Definition Documentation

◆ B0

#define B0   (1 << 0)

Definition at line 98 of file sensor_fusion.h.

◆ B1

#define B1   (1 << 1)

Definition at line 99 of file sensor_fusion.h.

◆ B2

#define B2   (1 << 2)

Definition at line 100 of file sensor_fusion.h.

◆ B3

#define B3   (1 << 3)

Definition at line 101 of file sensor_fusion.h.

◆ CHX

#define CHX   0

◆ CHY

#define CHY   1

◆ CHZ

#define CHZ   2

◆ F180OVERPI

#define F180OVERPI   57.2957795130823F

radians to degrees conversion = 180 / pi

Definition at line 110 of file sensor_fusion.h.

Referenced by ApplyPerturbation(), and fRotationVectorDegFromQuaternion().

◆ F180OVERPISQ

#define F180OVERPISQ   3282.8063500117F

square of F180OVERPI

Definition at line 111 of file sensor_fusion.h.

◆ false

#define false   0

Boolean FALSE.

Definition at line 93 of file sensor_fusion.h.

◆ FPIOVER180

#define FPIOVER180   0.01745329251994F

degrees to radians conversion = pi / 180

Definition at line 109 of file sensor_fusion.h.

Referenced by fInit_6DOF_GY_KALMAN(), fInit_9DOF_GBY_KALMAN(), fQuaternionFromRotationVectorDeg(), and fRotationVectorDegFromQuaternion().

◆ GTOMSEC2

#define GTOMSEC2   9.80665

standard gravity in m/s2

Definition at line 121 of file sensor_fusion.h.

Referenced by fInit_9DOF_GBY_KALMAN().

◆ ONEOVER12

#define ONEOVER12   0.083333333F

1 / 12

Definition at line 115 of file sensor_fusion.h.

Referenced by fRun_6DOF_GY_KALMAN().

◆ ONEOVER120

#define ONEOVER120   0.0083333333F

1 / 120

Definition at line 117 of file sensor_fusion.h.

◆ ONEOVER3840

#define ONEOVER3840   0.0002604166667F

1 / 3840

Definition at line 118 of file sensor_fusion.h.

Referenced by fQuaternionFromRotationVectorDeg().

◆ ONEOVER48

#define ONEOVER48   0.02083333333F

1 / 48

Definition at line 116 of file sensor_fusion.h.

Referenced by fQuaternionFromRotationVectorDeg().

◆ ONEOVERSQRT2

#define ONEOVERSQRT2   0.707106781F

1/sqrt(2)

Definition at line 119 of file sensor_fusion.h.

Referenced by ApplyPerturbation(), and fveqconjgquq().

◆ ONESIXTEENTH

#define ONESIXTEENTH   0.0625F

one sixteenth

Definition at line 114 of file sensor_fusion.h.

◆ ONESIXTH

#define ONESIXTH   0.166666667F

one sixth

Definition at line 113 of file sensor_fusion.h.

Referenced by fComputeMagCalibration10(), and fComputeMagCalibration7().

◆ ONETHIRD

#define ONETHIRD   0.33333333F

◆ PI

#define PI   3.141592654F

pi

Definition at line 107 of file sensor_fusion.h.

Referenced by fInitializeMagCalibration().

◆ PIOVER2

#define PIOVER2   1.570796327F

pi / 2

Definition at line 108 of file sensor_fusion.h.

◆ SPI_ADDR

#define SPI_ADDR   0x00

Definition at line 164 of file sensor_fusion.h.

Referenced by FXLS8471Q_Idle(), FXLS8471Q_Init(), and FXLS8471Q_Read().

◆ SQRT15OVER4

#define SQRT15OVER4   0.968245837F

sqrt(15)/4

Definition at line 120 of file sensor_fusion.h.

◆ true

#define true   1

Boolean TRUE.

Definition at line 92 of file sensor_fusion.h.

Typedef Documentation

◆ applyPerturbation_t

typedef void() applyPerturbation_t(struct SensorFusionGlobals *sfg)

Definition at line 171 of file sensor_fusion.h.

◆ byte

typedef unsigned char byte

Definition at line 64 of file sensor_fusion.h.

◆ clearFIFOs_t

typedef void() clearFIFOs_t(struct SensorFusionGlobals *sfg)

Definition at line 169 of file sensor_fusion.h.

◆ conditionSensorReadings_t

typedef void() conditionSensorReadings_t(struct SensorFusionGlobals *sfg)

Definition at line 170 of file sensor_fusion.h.

◆ initializeFusionEngine_t

typedef void() initializeFusionEngine_t(struct SensorFusionGlobals *sfg)

Definition at line 167 of file sensor_fusion.h.

◆ initializeSensor_t

typedef int8_t() initializeSensor_t(struct PhysicalSensor *sensor, struct SensorFusionGlobals *sfg)

Definition at line 142 of file sensor_fusion.h.

◆ installSensor_t

typedef int8_t() installSensor_t(struct SensorFusionGlobals *sfg, struct PhysicalSensor *sensor, uint16_t addr, uint16_t schedule, void *bus_driver, registerDeviceInfo_t *busInfo, initializeSensor_t *initialize, readSensor_t *read)

Definition at line 154 of file sensor_fusion.h.

◆ int16

typedef int16_t int16

Definition at line 66 of file sensor_fusion.h.

◆ int32

typedef int32_t int32

Definition at line 67 of file sensor_fusion.h.

◆ int8

typedef int8_t int8

Definition at line 65 of file sensor_fusion.h.

◆ quaternion_type

the quaternion type to be transmitted

◆ readSensor_t

typedef int8_t() readSensor_t(struct PhysicalSensor *sensor, struct SensorFusionGlobals *sfg)

Definition at line 146 of file sensor_fusion.h.

◆ readSensors_t

typedef int8_t() readSensors_t(struct SensorFusionGlobals *sfg, uint16_t read_loop_counter)

Definition at line 150 of file sensor_fusion.h.

◆ runFusion_t

typedef void() runFusion_t(struct SensorFusionGlobals *sfg)

Definition at line 168 of file sensor_fusion.h.

◆ SensorFusionGlobals

The top level fusion structure.

The top level fusion structure grows/shrinks based upon flag definitions contained in build.h. These same flags will populate the .iFlags field for run-time access.

◆ setStatus_t

typedef void() setStatus_t(struct SensorFusionGlobals *sfg, fusion_status_t status)

Definition at line 172 of file sensor_fusion.h.

◆ ssSetStatus_t

typedef void() ssSetStatus_t(struct StatusSubsystem *pStatus, fusion_status_t status)

Definition at line 174 of file sensor_fusion.h.

◆ ssUpdateStatus_t

typedef void() ssUpdateStatus_t(struct StatusSubsystem *pStatus)

Definition at line 175 of file sensor_fusion.h.

◆ SV_ptr

typedef struct SV_COMMON* SV_ptr

Definition at line 473 of file sensor_fusion.h.

◆ uint16

typedef uint16_t uint16

Definition at line 69 of file sensor_fusion.h.

◆ uint32

typedef uint32_t uint32

Definition at line 70 of file sensor_fusion.h.

◆ uint8

typedef uint8_t uint8

Definition at line 68 of file sensor_fusion.h.

◆ updateStatus_t

typedef void() updateStatus_t(struct SensorFusionGlobals *sfg)

Definition at line 173 of file sensor_fusion.h.

Enumeration Type Documentation

◆ fusion_status_t

Application-specific serial communications system.

Enumerator
OFF 

These are the state definitions for the status subsystem.

Application hasn't started

INITIALIZING 

Initializing sensors and algorithms.

LOWPOWER 

Running in reduced power mode.

NORMAL 

Operation is Nominal.

RECEIVING_WIRED 

Receiving commands over wired interface (momentary)

RECEIVING_WIRELESS 

Receiving commands over wireless interface (momentary)

HARD_FAULT 

Non-recoverable FAULT = something went very wrong.

SOFT_FAULT 

Recoverable FAULT = something went wrong, but we can keep going.

Definition at line 130 of file sensor_fusion.h.

◆ quaternion

enum quaternion

the quaternion type to be transmitted

Enumerator
Q3 

Quaternion derived from 3-axis accel (tilt)

Q3M 

Quaternion derived from 3-axis mag only (auto compass algorithm)

Q3G 

Quaternion derived from 3-axis gyro only (rotation)

Q6MA 

Quaternion derived from 3-axis accel + 3 axis mag (eCompass)

Q6AG 

Quaternion derived from 3-axis accel + 3-axis gyro (gaming)

Q9 

Quaternion derived from full 9-axis sensor fusion.

Definition at line 74 of file sensor_fusion.h.

Function Documentation

◆ addToFifo()

void addToFifo ( union FifoSensor sensor,
uint16_t  maxFifoSize,
int16_t  sample[3] 
)

addToFifo is called from within sensor driver read functions

addToFifo is called from within sensor driver read functions to transfer new readings into the sensor structure corresponding to accel, gyro or mag. This function ensures that the software FIFOs are not overrun.

example usage: if (status==SENSOR_ERROR_NONE) addToFifo((FifoSensor*) &(sfg->Mag), MAG_FIFO_SIZE, sample);

Parameters
sensorpointer to structure of type AccelSensor, MagSensor or GyroSensor
maxFifoSizethe size of the software (not hardware) FIFO
samplethe sample to add

Definition at line 548 of file sensor_fusion.c.

References FifoSensor::Accel, CHX, CHY, CHZ, AccelSensor::iFIFOCount, AccelSensor::iFIFOExceeded, and AccelSensor::iGsFIFO.

Referenced by FXLS8471Q_Read(), FXOS8700_Init(), FXOS8700_ReadMagData(), and MAG3110_Read().

Here is the caller graph for this function:

◆ ApplyAccelHAL()

void ApplyAccelHAL ( struct AccelSensor Accel)

Apply the accelerometer Hardware Abstraction Layer.

Parameters
Accelpointer to accelerometer logical sensor

Definition at line 48 of file hal_frdm_fxs_mult2_b.c.

References CHX, CHY, CHZ, AccelSensor::iFIFOCount, and AccelSensor::iGsFIFO.

Referenced by initializeSensors().

Here is the caller graph for this function:

◆ ApplyGyroHAL()

void ApplyGyroHAL ( struct GyroSensor Gyro)

Apply the gyroscope Hardware Abstraction Layer.

Parameters
Gyropointer to gyroscope logical sensor

Definition at line 103 of file hal_frdm_fxs_mult2_b.c.

References CHX, CHY, CHZ, GyroSensor::iFIFOCount, and GyroSensor::iYsFIFO.

Referenced by processMagData().

Here is the caller graph for this function:

◆ ApplyMagHAL()

void ApplyMagHAL ( struct MagSensor Mag)

Apply the magnetometer Hardware Abstraction Layer.

Parameters
Magpointer to magnetometer logical sensor

Definition at line 75 of file hal_frdm_fxs_mult2_b.c.

References CHX, CHY, CHZ, MagSensor::iBsFIFO, and MagSensor::iFIFOCount.

Referenced by processMagData().

Here is the caller graph for this function:

◆ clearFIFOs()

void clearFIFOs ( SensorFusionGlobals sfg)

Function to clear FIFO at the end of each fusion computation.

Parameters
sfgGlobal data structure pointer

Definition at line 380 of file sensor_fusion.c.

References MagSensor::iFIFOCount, MagSensor::iFIFOExceeded, and SensorFusionGlobals::Mag.

Referenced by initializeFusionEngine(), initSensorFusionGlobals(), and runFusion().

Here is the caller graph for this function:

◆ conditionSample()

void conditionSample ( int16_t  sample[3])

conditionSample ensures that we never encounter the maximum negative two's complement value for a 16-bit variable (-32768).

conditionSample ensures that we never encounter the maximum negative two's complement value for a 16-bit variable (-32768). This value cannot be negated, because the maximum positive value is +32767. We need the ability to negate to gaurantee that subsequent HAL operations can be run successfully.

Parameters
sample16-bit register value from triaxial sensor read

Definition at line 536 of file sensor_fusion.c.

References CHX, CHY, and CHZ.

Referenced by FXLS8471Q_Read(), FXOS8700_Init(), FXOS8700_ReadMagData(), and MAG3110_Read().

Here is the caller graph for this function:

◆ conditionSensorReadings()

void conditionSensorReadings ( SensorFusionGlobals sfg)

conditionSensorReadings() transforms raw software FIFO readings into forms that can be consumed by the sensor fusion engine. This include sample averaging and (in the case of the gyro) integrations, applying hardware abstraction layers, and calibration functions. This function is normally involved via the "sfg." global pointer.

Parameters
sfgGlobal data structure pointer

Definition at line 322 of file sensor_fusion.c.

References MagSensor::isEnabled, SensorFusionGlobals::Mag, and processMagData().

Referenced by initSensorFusionGlobals().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initSensorFusionGlobals()

void initSensorFusionGlobals ( SensorFusionGlobals sfg,
struct StatusSubsystem pStatusSubsystem,
struct ControlSubsystem pControlSubsystem 
)

◆ zeroArray()

void zeroArray ( struct StatusSubsystem pStatus,
void *  data,
uint16_t  size,
uint16_t  numElements,
uint8_t  check 
)
Parameters
pStatusStatus subsystem pointer
datapointer to array to be zeroed
sizedata type size = 8, 16 or 32
numElementsnumber of elements to zero out
checktrue if you would like to verify writes, false otherwise

Definition at line 337 of file sensor_fusion.c.

References data, HARD_FAULT, and StatusSubsystem::set.

Variable Documentation

◆ ApplyPerturbation

applyPerturbation_t ApplyPerturbation

ApplyPerturbation is a reverse unit-step test function.

The ApplyPerturbation function applies a user-specified step function to prior fusion results which is then "released" in the next fusion cycle. When used in conjuction with the NXP Sensor Fusion Toolbox, this provides a visual indication of the dynamic behavior of the library. ApplyPerturbation() is defined in debug.c.

Definition at line 639 of file sensor_fusion.h.

Referenced by initSensorFusionGlobals().

◆ initializeFusionEngine

initializeFusionEngine_t initializeFusionEngine

Definition at line 571 of file sensor_fusion.h.

◆ installSensor

installSensor_t installSensor

Definition at line 570 of file sensor_fusion.h.

◆ readSensors

readSensors_t readSensors

Definition at line 584 of file sensor_fusion.h.

◆ runFusion

runFusion_t runFusion

Definition at line 583 of file sensor_fusion.h.