|
Sensor Fusion Library 0.6.1
Orientation sensing for Espressif (ESP32, ESP8266) processors
|
#include <sensor_fusion_class.h>

Public Member Functions | |
| SensorFusion () | |
| bool | InstallSensor (uint8_t sensor_i2c_addr, SensorType sensor_type) |
| Install Sensor in linked list The max length of the list is checked, and if there is room, the given sensor is inserted at the head of the list. An accelerometer and magnetometer may be combined in one IC - if that is the case then only one call is required to install, provided the associated *_Init() and *_Read() function reads both the accel & magnetometer data. The Init() and Read() functions of each sensor are defined in driver_*.* files. | |
| bool | InitializeInputOutputSubsystem (const Stream *serial_port=NULL, const void *tcp_client=NULL) |
| void | Begin (int pin_i2c_sda=-1, int pin_i2c_scl=-1) |
| void | UpdateWiFiStream (void *tcp_client) |
| Update the TCP client pointer. Call when a new TCP connection is made, as reported by WiFiServer::available() When tcp_client is NULL, output via WiFi is not attempted. | |
| void | ReadSensors (void) |
| Reads all sensors. Applies HAL remapping, removes invalid values, and stores data for later processing. Depending on whether a sensor has a built-in FIFO buffer, that sensor may not be read each time this method is called. See kLoopsPerMagRead, etc., in sensor_fusion_class.h. | |
| void | RunFusion (void) |
| Apply fusion algorithm to sensor raw data. Sensor readings contained in global struct are calibrated and processed. Status is updated and displayed. Loop counter used for coordinating sensor reads is reset. | |
| void | ProduceToolboxOutput (void) |
| Generate and send out data, formatted for NXP Orientation Sensor Toolbox. It is not mandatory to call this routine, if Toolbox output is not needed. | |
| bool | SendArbitraryData (const char *buffer, uint16_t data_length) |
| void | ProcessCommands (void) |
| Process any incoming commands. Commands may arrive by serial or WiFi connection, depending on which of these is enabled (if any). It is not mandatory to call this routine, if command responses are not needed. | |
| void | InjectCommand (const char *command) |
| Inject and process a single command in the control subsystem. | |
| void | SaveMagneticCalibration (void) |
| Save current magnetic calibration to non-volatile memory. | |
| bool | IsDataValid (void) |
| int | GetSystemStatus (void) |
| float | GetHeadingDegrees (void) |
| float | GetPitchDegrees (void) |
| float | GetRollDegrees (void) |
| float | GetHeadingRadians (void) |
| float | GetPitchRadians (void) |
| float | GetRollRadians (void) |
| float | GetAccelXGees (void) |
| float | GetAccelYGees (void) |
| float | GetAccelZGees (void) |
| float | GetAccelXMPerSS (void) |
| float | GetAccelYMPerSS (void) |
| float | GetAccelZMPerSS (void) |
| float | GetTurnRateDegPerS (void) |
| float | GetPitchRateDegPerS (void) |
| float | GetRollRateDegPerS (void) |
| float | GetTurnRateRadPerS (void) |
| float | GetPitchRateRadPerS (void) |
| float | GetRollRateRadPerS (void) |
| float | GetTemperatureC (void) |
| float | GetTemperatureK (void) |
| void | GetOrientationQuaternion (Quaternion *quat) |
| Return the orientation as a quaternion. | |
| float | GetMagneticFitError (void) |
| float | GetMagneticFitErrorTrial (void) |
| float | GetMagneticBMag (void) |
| float | GetMagneticBMagTrial (void) |
| float | GetMagneticInclinationDeg (void) |
| float | GetMagneticInclinationRad (void) |
| float | GetMagneticNoiseCovariance (void) |
| float | GetMagneticCalSolver (void) |
Private Member Functions | |
| void | InitializeStatusSubsystem (void) |
| Initialize the Status reporting system. Status is indicated by LEDs, but other means can be added. | |
| void | InitializeSensorFusionGlobals (void) |
| Set the starting values of variables contained in sfg_. | |
Private Attributes | |
| SensorFusionGlobals * | sfg_ |
| Primary sensor fusion data structure. | |
| ControlSubsystem * | control_subsystem_ |
| command and data streaming structure | |
| StatusSubsystem * | status_subsystem_ |
| visual status indicator structure | |
| PhysicalSensor * | sensors_ |
| linked list of up to 4 sensors | |
| uint8_t | num_sensors_installed_ |
| tracks how many sensors have been added to list | |
| const uint8_t | kLoopsPerMagRead |
| how often a magnetometer read is performed | |
| const uint8_t | kLoopsPerThermRead |
| how often a thermometer read is performed | |
| const uint8_t | kLoopsPerAccelRead |
| how often an accelerometer read is performed | |
| const uint8_t | kLoopsPerGyroRead |
| how often a gyroscope read is performed | |
| const uint8_t | kLoopsPerFusionCalc |
| how often to fuse. Usually the max of previous 3 constants. | |
| uint8_t | loops_per_fuse_counter_ |
| counts how many times through loop have been done | |
Class that wraps the various mostly-C-style functions of the sensor fusion code into easier to use methods. Not all the lower-level functions are exposed however; for more advanced use it will be necessary to call them directly.
Definition at line 46 of file sensor_fusion_class.h.
| SensorFusion::SensorFusion | ( | ) |
Constructor creates and initializes a structure of variables used throughout the functions. It initializes the control and status subsystems as well as several subordinate data structures.
Definition at line 45 of file sensor_fusion_class.cc.
| void SensorFusion::Begin | ( | int | pin_i2c_sda = -1, |
| int | pin_i2c_scl = -1 |
||
| ) |
Initialize the Sensors. Read calibrations. Set status to Normal.
Definition at line 143 of file sensor_fusion_class.cc.
| float SensorFusion::GetAccelXGees | ( | void | ) |
Definition at line 454 of file sensor_fusion_class.cc.
| float SensorFusion::GetAccelXMPerSS | ( | void | ) |
Definition at line 461 of file sensor_fusion_class.cc.
| float SensorFusion::GetAccelYGees | ( | void | ) |
Definition at line 468 of file sensor_fusion_class.cc.
| float SensorFusion::GetAccelYMPerSS | ( | void | ) |
Definition at line 475 of file sensor_fusion_class.cc.
| float SensorFusion::GetAccelZGees | ( | void | ) |
Definition at line 482 of file sensor_fusion_class.cc.
| float SensorFusion::GetAccelZMPerSS | ( | void | ) |
Definition at line 489 of file sensor_fusion_class.cc.
| float SensorFusion::GetHeadingDegrees | ( | void | ) |
Definition at line 345 of file sensor_fusion_class.cc.
| float SensorFusion::GetHeadingRadians | ( | void | ) |
Definition at line 355 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticBMag | ( | void | ) |
This value does not change with each sensor reading - it only updates if a new calibration parameter set is calculated and becomes the currently-used calibration.
Definition at line 559 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticBMagTrial | ( | void | ) |
This value is the current best-estimate of the B field using recent magnetic sensor readings. It updates with each new sensor reading added to the buffer. If the new trial calibration parameters are accepted and replace the current calibration, then this B field becomes associated with that set.
This parameter provides an indication of magnetic interference; if it differs widely from the B value of the current calibration then one can conclude that the current calibration is not valid for the present magnetic environment.
Definition at line 547 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticCalSolver | ( | void | ) |
The solver is the number of elements (complexity) of the calibration algorithm. 0 means no calibration; 10 is the most complex.
Definition at line 617 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticFitError | ( | void | ) |
This function returns the measure of the goodness of fit of the currently-used calibration parameter set. Units are in % and a value less than 3.5% is considered good. If no calibration is available on startup (i.e. none saved in EEPROM) then a value of 0 is returned briefly during program startup, indicating that insufficient data are available to calculate a fit.
Definition at line 529 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticFitErrorTrial | ( | void | ) |
The background calibration task uses recent magnetic readings to periodically calculate a new set of calibration parameters. This function returns the measure of goodness of fit of the new calibration parameter set. Units are in % and a value less than 3.5% is considered good. A value of 0 is returned briefly during program startup, indicating that insufficient data are available to calculate a fit.
Definition at line 515 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticInclinationDeg | ( | void | ) |
This is the a posteriori value from Kalman filter output and updates with each sensor reading. It can be used as an indication of magnetic interference; if its value changes by more than, say, 10 degrees from recent previous values then it indicates a disturbance in the magnetic environment.
Magnetic inclination values vary over the earth's surface and are available from sources such as NOAA.
Definition at line 576 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticInclinationRad | ( | void | ) |
This is the a posteriori value from Kalman filter output and updates with each sensor reading. It can be used as an indication of magnetic interference; if its value changes by more than, say, 10 degrees from recent previous values then it indicates a disturbance in the magnetic environment.
Magnetic inclination values vary over the earth's surface and are available from sources such as NOAA.
Definition at line 593 of file sensor_fusion_class.cc.
| float SensorFusion::GetMagneticNoiseCovariance | ( | void | ) |
This is proportional to how heavily the magnetic readings are weighted in calculating the a posteriori orientation. This is a good measure of magnetic interference; when the noise covariance value rises above about 0.00056 it indicates the current magnetic reading does not lie close to the surface of the calibrated geomagnetic sphere.
Definition at line 607 of file sensor_fusion_class.cc.
| void SensorFusion::GetOrientationQuaternion | ( | Quaternion * | quat | ) |
Return the orientation as a quaternion.
| quat | pointer to quaternion structure, to be filled by this method |
Definition at line 497 of file sensor_fusion_class.cc.
| float SensorFusion::GetPitchDegrees | ( | void | ) |
Definition at line 362 of file sensor_fusion_class.cc.
| float SensorFusion::GetPitchRadians | ( | void | ) |
Definition at line 369 of file sensor_fusion_class.cc.
| float SensorFusion::GetPitchRateDegPerS | ( | void | ) |
Definition at line 426 of file sensor_fusion_class.cc.
| float SensorFusion::GetPitchRateRadPerS | ( | void | ) |
Definition at line 433 of file sensor_fusion_class.cc.
| float SensorFusion::GetRollDegrees | ( | void | ) |
Definition at line 376 of file sensor_fusion_class.cc.
| float SensorFusion::GetRollRadians | ( | void | ) |
Definition at line 383 of file sensor_fusion_class.cc.
| float SensorFusion::GetRollRateDegPerS | ( | void | ) |
Definition at line 440 of file sensor_fusion_class.cc.
| float SensorFusion::GetRollRateRadPerS | ( | void | ) |
Definition at line 447 of file sensor_fusion_class.cc.
| int SensorFusion::GetSystemStatus | ( | void | ) |
System status is indicated by an integer. See sensor_fusion.h for details. Common values are: 1 (Initializing); 3 (Normal); and 7 (Soft Fault - usually caused by communications fault with sensor IC)
Definition at line 317 of file sensor_fusion_class.cc.
| float SensorFusion::GetTemperatureC | ( | void | ) |
This is the temperature of the sensor IC die. It is not factory-trimmed, so it very likely has an offset that should be calibrated out for best accuracy.
Definition at line 394 of file sensor_fusion_class.cc.
| float SensorFusion::GetTemperatureK | ( | void | ) |
This is the temperature of the sensor IC die. It is not factory-trimmed, so it very likely has an offset that should be calibrated out for best accuracy.
Definition at line 405 of file sensor_fusion_class.cc.
| float SensorFusion::GetTurnRateDegPerS | ( | void | ) |
Definition at line 412 of file sensor_fusion_class.cc.
| float SensorFusion::GetTurnRateRadPerS | ( | void | ) |
Definition at line 419 of file sensor_fusion_class.cc.
| bool SensorFusion::InitializeInputOutputSubsystem | ( | const Stream * | serial_port = NULL, |
| const void * | tcp_client = NULL |
||
| ) |
Initialize the Control subsystem, which receives external commands and sends data packets.
| serial_port | A Stream pointer to which to send output and receive commands. If NULL, then output is not attempted. |
| tcp_client | A WiFiClient pointer to which to send output and receive commands. If NULL, then output is not attempted. |
Definition at line 135 of file sensor_fusion_class.cc.
|
private |
Set the starting values of variables contained in sfg_.
Definition at line 637 of file sensor_fusion_class.cc.
|
private |
Initialize the Status reporting system. Status is indicated by LEDs, but other means can be added.
Definition at line 628 of file sensor_fusion_class.cc.
| void SensorFusion::InjectCommand | ( | const char * | command | ) |
Inject and process a single command in the control subsystem.
Command provided by this call bypasses the usual input route (serial UART or WiFi) and is instead provided directly in this function call. This provides an alterative way of manipulating the fusion algortithm without needing to use the entire control or input subsystem, For a list of valid commands, see control_input.c in the Orientation library.
| command | is a four-character sequence selected from the list of valid commands. Illegal commands are ignored without any action taken. |
Definition at line 273 of file sensor_fusion_class.cc.
| bool SensorFusion::InstallSensor | ( | uint8_t | sensor_i2c_addr, |
| SensorType | sensor_type | ||
| ) |
Install Sensor in linked list The max length of the list is checked, and if there is room, the given sensor is inserted at the head of the list. An accelerometer and magnetometer may be combined in one IC - if that is the case then only one call is required to install, provided the associated *_Init() and *_Read() function reads both the accel & magnetometer data. The Init() and Read() functions of each sensor are defined in driver_*.* files.
| sensor_i2c_addr | is the I2C bus address of the sensor IC |
| sensor_type | indicates the type of sensor (e.g. magnetometer) |
Definition at line 75 of file sensor_fusion_class.cc.
| bool SensorFusion::IsDataValid | ( | void | ) |
Definition at line 301 of file sensor_fusion_class.cc.
| void SensorFusion::ProcessCommands | ( | void | ) |
Process any incoming commands. Commands may arrive by serial or WiFi connection, depending on which of these is enabled (if any). It is not mandatory to call this routine, if command responses are not needed.
Definition at line 255 of file sensor_fusion_class.cc.
| void SensorFusion::ProduceToolboxOutput | ( | void | ) |
Generate and send out data, formatted for NXP Orientation Sensor Toolbox. It is not mandatory to call this routine, if Toolbox output is not needed.
Definition at line 219 of file sensor_fusion_class.cc.
| void SensorFusion::ReadSensors | ( | void | ) |
Reads all sensors. Applies HAL remapping, removes invalid values, and stores data for later processing. Depending on whether a sensor has a built-in FIFO buffer, that sensor may not be read each time this method is called. See kLoopsPerMagRead, etc., in sensor_fusion_class.h.
Definition at line 171 of file sensor_fusion_class.cc.
| void SensorFusion::RunFusion | ( | void | ) |
Apply fusion algorithm to sensor raw data. Sensor readings contained in global struct are calibrated and processed. Status is updated and displayed. Loop counter used for coordinating sensor reads is reset.
Definition at line 185 of file sensor_fusion_class.cc.
| void SensorFusion::SaveMagneticCalibration | ( | void | ) |
Save current magnetic calibration to non-volatile memory.
Passes to the fusion control subsystem the command to save the current magnetic calibration parameters to EEPROM. This calibration will then be loaded each time the system restarts.
One can pass this command directly using InjectCommand(), but SaveMagneticCalibration() is provided as a convenience.
The fusion system continuously runs a time-sliced calibration routine in the background, and if a superior set of calibration parameters are calculated based on recent magnetic measurements, then these new parameters replace the current ones in RAM. However, it is only via this command that the parameters can be caused to persist beyond the next system reset.
Definition at line 294 of file sensor_fusion_class.cc.
| bool SensorFusion::SendArbitraryData | ( | const char * | buffer, |
| uint16_t | data_length | ||
| ) |
places data from buffer into Control subsystem's output buffer, and sends it out via serial and/or wifi. Any existing data in the output buffer that hasn't already been sent will be overwritten. Returns true on success, false on problem such as data_length too long for the transmit buffer.
Definition at line 236 of file sensor_fusion_class.cc.
| void SensorFusion::UpdateWiFiStream | ( | void * | tcp_client | ) |
Update the TCP client pointer. Call when a new TCP connection is made, as reported by WiFiServer::available() When tcp_client is NULL, output via WiFi is not attempted.
| tcp_client | A WiFiClient pointer used for input/output |
Definition at line 159 of file sensor_fusion_class.cc.
|
private |
command and data streaming structure
Definition at line 99 of file sensor_fusion_class.h.
|
private |
how often an accelerometer read is performed
Definition at line 117 of file sensor_fusion_class.h.
|
private |
how often to fuse. Usually the max of previous 3 constants.
Definition at line 121 of file sensor_fusion_class.h.
|
private |
how often a gyroscope read is performed
Definition at line 119 of file sensor_fusion_class.h.
|
private |
how often a magnetometer read is performed
Constants of the form kLoopsPer_____ set the relationship between number of sensor reads and each execution of the fusion algorithm. Normally there is a 1:1 relationship (i.e. read, fuse, read, fuse,...) but other arrangements are possible (e.g. read, read, fuse, read, read,...) The rate at which main loop() executes is set by LOOP_RATE_HZ in build.h
Definition at line 113 of file sensor_fusion_class.h.
|
private |
how often a thermometer read is performed
Definition at line 115 of file sensor_fusion_class.h.
|
private |
counts how many times through loop have been done
Definition at line 123 of file sensor_fusion_class.h.
|
private |
tracks how many sensors have been added to list
Definition at line 102 of file sensor_fusion_class.h.
|
private |
linked list of up to 4 sensors
Definition at line 101 of file sensor_fusion_class.h.
|
private |
Primary sensor fusion data structure.
Definition at line 97 of file sensor_fusion_class.h.
|
private |
visual status indicator structure
Definition at line 100 of file sensor_fusion_class.h.