ISSDK  1.7
IoT Sensing Software Development Kit
magnetic.h
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file magnetic.h
36  \brief Lower level magnetic calibration interface
37 
38  Many developers can utilize the NXP Sensor Fusion Library without ever
39  making any adjustment to the lower level magnetic calibration functions
40  defined in this file.
41 */
42 
43 #ifndef MAGNETIC_H
44 #define MAGNETIC_H
45 
46 #ifndef F_USING_MAG
47 #define F_USING_MAG 0x0002 // normally should be defined in build.h
48 #endif
49 
50 #if F_USING_MAG
51 /// @name Magnetic Calibration Constants
52 ///@{
53 #define MAGBUFFSIZEX 14 ///< x dimension in magnetometer buffer (12x24 equals 288 elements)
54 #define MAGBUFFSIZEY (2 * MAGBUFFSIZEX) ///< y dimension in magnetometer buffer (12x24 equals 288 elements)
55 #define MINMEASUREMENTS4CAL 110 ///< minimum number of measurements for 4 element calibration
56 #define MINMEASUREMENTS7CAL 220 ///< minimum number of measurements for 7 element calibration
57 #define MINMEASUREMENTS10CAL 330 ///< minimum number of measurements for 10 element calibration
58 #define MAXMEASUREMENTS 360 ///< maximum number of measurements used for calibration
59 #define CAL_INTERVAL_SECS 300 ///< 300s or 5min interval for regular calibration checks
60 #define MINBFITUT 10.0F ///< minimum acceptable geomagnetic field B (uT) for valid calibration
61 #define MAXBFITUT 90.0F ///< maximum acceptable geomagnetic field B (uT) for valid calibration
62 #define FITERRORAGINGSECS 86400.0F ///< 24 hours: time (s) for fit error to increase (age) by e=2.718
63 #define MESHDELTACOUNTS 50 ///< magnetic buffer mesh spacing in counts (here 5uT)
64 #define DEFAULTB 50.0F ///< default geomagnetic field (uT)
65 ///@}
66 
67 /// The Magnetometer Measurement Buffer holds a 3-dimensional "constellation"
68 /// of data points.
69 ///
70 /// The constellation of points are used to compute magnetic hard/soft iron compensation terms.
71 /// The contents of this buffer are updated on a continuing basis.
72 struct MagBuffer
73 {
74  int16_t iBs[3][MAGBUFFSIZEX][MAGBUFFSIZEY]; ///< uncalibrated magnetometer readings
75  int32_t index[MAGBUFFSIZEX][MAGBUFFSIZEY]; ///< array of time indices
76  int16_t tanarray[MAGBUFFSIZEX - 1]; ///< array of tangents of (100 * angle)
77  int16_t iMagBufferCount; ///< number of magnetometer readings
78 };
79 
80 /// Magnetic Calibration Structure
82 {
83  // start of elements stored to flash memory on Save (16 * 4 = 64 bytes)
84  float fV[3]; ///< current hard iron offset x, y, z, (uT)
85  float finvW[3][3]; ///< current inverse soft iron matrix
86  float fB; ///< current geomagnetic field magnitude (uT)
87  float fBSq; ///< square of fB (uT^2)
88  float fFitErrorpc; ///< current fit error %
89  int32_t iValidMagCal; ///< solver used: 0 (no calibration) or 4, 7, 10 element
90  // end of elements stored to flash memory
91  // start of working elements not stored to flash memory
92  float ftrV[3]; ///< trial value of hard iron offset z, y, z (uT)
93  float ftrinvW[3][3]; ///< trial inverse soft iron matrix size
94  float ftrB; ///< trial value of geomagnetic field magnitude in uT
95  float ftrFitErrorpc; ///< trial value of fit error %
96  float fA[3][3]; ///< ellipsoid matrix A
97  float finvA[3][3]; ///< inverse of ellipsoid matrix A
98  float fmatA[10][10]; ///< scratch 10x10 float matrix used by calibration algorithms
99  float fmatB[10][10]; ///< scratch 10x10 float matrix used by calibration algorithms
100  float fvecA[10]; ///< scratch 10x1 vector used by calibration algorithms
101  float fvecB[4]; ///< scratch 4x1 vector used by calibration algorithms
102  float fYTY; ///< Y^T.Y for 4 element calibration = (iB^2)^2
103  int32_t iSumBs[3]; ///< sum of measurements in buffer (counts)
104  int32_t iMeanBs[3]; ///< average magnetic measurement (counts)
105  int32_t itimeslice; ///< counter for tine slicing magnetic calibration calculations
106  int8_t iCalInProgress; ///< flag denoting that a calibration is in progress
107  int8_t iNewCalibrationAvailable; ///< flag denoting that a new calibration has been computed
108  int8_t iInitiateMagCal; ///< flag to start a new magnetic calibration
109  int8_t iMagBufferReadOnly; ///< flag to denote that the magnetic measurement buffer is temporarily read only
110  int8_t i4ElementSolverTried; ///< flag to denote at least one attempt made with 4 element calibration
111  int8_t i7ElementSolverTried; ///< flag to denote at least one attempt made with 4 element calibration
112  int8_t i10ElementSolverTried; ///< flag to denote at least one attempt made with 4 element calibration
113 };
114 
115 
116 struct MagSensor; // actual typedef is located in sensor_fusion_types.h
117 
118 /// @name Function prototypes for functions in magnetic.c
119 /// These functions comprise the core of the magnetic calibration features of
120 /// the library. Parameter descriptions are not included here,
121 /// as details are provided in sensor_fusion.h.
122 ///@{
123 void fInitializeMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer);
124 void iUpdateMagBuffer(struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32_t loopcounter);
125 void fInvertMagCal(struct MagSensor *pthisMag, struct MagCalibration *pthisMagCal);
126 void fRunMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor* pthisMag, int32_t loopcounter);
127 void fUpdateMagCalibration4(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
128 void fUpdateMagCalibration7(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
129 void fUpdateMagCalibration10(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
130 void fUpdateMagCalibration4Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
131 void fUpdateMagCalibration7Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
132 void fUpdateMagCalibration10Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
133 ///@}
134 #else // if F_USING_MAG
135 struct MagBuffer
136 {
137  void *placeholder;
138 };
139 
140 /// Magnetic Calibration Structure
141 struct MagCalibration
142 {
143  void *placeholder;
144 };
145 #endif // if F_USING_MAG
146 #endif // #ifndef MAGNETIC_H
int8_t iMagBufferReadOnly
flag to denote that the magnetic measurement buffer is temporarily read only
Definition: magnetic.h:109
int32_t iValidMagCal
solver used: 0 (no calibration) or 4, 7, 10 element
Definition: magnetic.h:89
int8_t iNewCalibrationAvailable
flag denoting that a new calibration has been computed
Definition: magnetic.h:107
float fYTY
Y^T.Y for 4 element calibration = (iB^2)^2.
Definition: magnetic.h:102
#define MAGBUFFSIZEX
x dimension in magnetometer buffer (12x24 equals 288 elements)
Definition: magnetic.h:53
void fUpdateMagCalibration7Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:700
void fUpdateMagCalibration7(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
int8_t iCalInProgress
flag denoting that a calibration is in progress
Definition: magnetic.h:106
int8_t i10ElementSolverTried
flag to denote at least one attempt made with 4 element calibration
Definition: magnetic.h:112
void fUpdateMagCalibration4Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:469
void fInitializeMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer)
Definition: magnetic.c:50
void fUpdateMagCalibration4(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
The MagSensor structure stores raw and processed measurements for a 3-axis magnetic sensor...
void fUpdateMagCalibration10Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:987
float ftrFitErrorpc
trial value of fit error %
Definition: magnetic.h:95
int8_t i7ElementSolverTried
flag to denote at least one attempt made with 4 element calibration
Definition: magnetic.h:111
float fFitErrorpc
current fit error %
Definition: magnetic.h:88
int16_t iBs[3][MAGBUFFSIZEX][MAGBUFFSIZEY]
uncalibrated magnetometer readings
Definition: magnetic.h:74
int16_t tanarray[MAGBUFFSIZEX - 1]
array of tangents of (100 * angle)
Definition: magnetic.h:76
float fB
current geomagnetic field magnitude (uT)
Definition: magnetic.h:86
int16_t iMagBufferCount
number of magnetometer readings
Definition: magnetic.h:77
Magnetic Calibration Structure.
Definition: magnetic.h:81
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
int32_t itimeslice
counter for tine slicing magnetic calibration calculations
Definition: magnetic.h:105
int8_t i4ElementSolverTried
flag to denote at least one attempt made with 4 element calibration
Definition: magnetic.h:110
void fInvertMagCal(struct MagSensor *pthisMag, struct MagCalibration *pthisMagCal)
Definition: magnetic.c:311
void fRunMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32_t loopcounter)
Definition: magnetic.c:339
float fBSq
square of fB (uT^2)
Definition: magnetic.h:87
float ftrB
trial value of geomagnetic field magnitude in uT
Definition: magnetic.h:94
#define MAGBUFFSIZEY
y dimension in magnetometer buffer (12x24 equals 288 elements)
Definition: magnetic.h:54
int8_t iInitiateMagCal
flag to start a new magnetic calibration
Definition: magnetic.h:108
int32_t index[MAGBUFFSIZEX][MAGBUFFSIZEY]
array of time indices
Definition: magnetic.h:75
void fUpdateMagCalibration10(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
void iUpdateMagBuffer(struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32_t loopcounter)
Definition: magnetic.c:117