ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
magnetic.h
Go to the documentation of this file.
1 // Copyright (c) 2014, 2015, Freescale Semiconductor, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of Freescale Semiconductor, Inc. nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL FREESCALE SEMICONDUCTOR, INC. BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 #ifndef MAGNETIC_H
27 #define MAGNETIC_H
28 
29 
30 #include "sensor_data_types.h"
31 #include "magnetic_config.h"
32 
33 // magnetic calibration constants
34 //#define MAGBUFFSIZEX 14 // x dimension in magnetometer buffer (12x24 equals 288 elements)
35 //#define MAGBUFFSIZEY (2 * MAGBUFFSIZEX) // y dimension in magnetometer buffer (12x24 equals 288 elements)
36 //#define MINMEASUREMENTS4CAL 100 // minimum number of measurements for 4 element calibration
37 //#define MINMEASUREMENTS7CAL 150 // minimum number of measurements for 7 element calibration
38 //#define MINMEASUREMENTS10CAL 250 // minimum number of measurements for 10 element calibration
39 //#define MAXMEASUREMENTS 340 // maximum number of measurements used for calibration
40 //#define INTERVAL4CAL 75 // 3s at 25Hz: 4 element interval (samples)
41 //#define INTERVAL7CAL 250 // 10s at 25Hz: 7 element interval (samples)
42 //#define INTERVAL10CAL 750 // 30s at 25Hz: 10 element interval (samples)
43 //#define MINBFITUT 10.0F // minimum acceptable geomagnetic field B (uT) for valid calibration
44 //#define MAXBFITUT 90.0F // maximum acceptable geomagnetic field B (uT) for valid calibration
45 //#define FITERRORAGINGSECS 86400.0F // 24 hours: time (s) for fit error to increase (age) by e=2.718
46 //#define MESHDELTACOUNTS 50 // magnetic buffer mesh spacing in counts (here 5uT)
47 #define DEFAULTB 50.0F // default geomagnetic field (uT)
48 
49 // magnetometer measurement buffer
51 {
52  int16 iBs[3][MAGBUFFSIZEX][MAGBUFFSIZEY]; // uncalibrated magnetometer readings
53  int32 index[MAGBUFFSIZEX][MAGBUFFSIZEY]; // array of time indices
54  int16 tanarray[MAGBUFFSIZEX - 1]; // array of tangents of (100 * angle)
55  int16 iMagBufferCount; // number of magnetometer readings
56 };
57 
58 // magnetic calibration structure
60 {
61  float fV[3]; // current hard iron offset x, y, z, (uT)
62  float finvW[3][3]; // current inverse soft iron matrix
63  float fB; // current geomagnetic field magnitude (uT)
64  float fFitErrorpc; // current fit error percentage
65  float ftrV[3]; // trial value of hard iron offset z, y, z (uT)
66  float ftrinvW[3][3]; // trial inverse soft iron matrix size
67  float ftrB; // trial value of geomagnetic field magnitude in uT
68  float ftrFitErrorpc; // trial value of fit error percentage
69  float fA[3][3]; // ellipsoid matrix A
70  float finvA[3][3]; // inverse of ellipsoid matrix A
71  float fmatA[10][10]; // scratch 10x10 matrix used by calibration algorithms
72  float fmatB[10][10]; // scratch 10x10 matrix used by calibration algorithms
73  float fvecA[10]; // scratch 10x1 vector used by calibration algorithms
74  float fvecB[4]; // scratch 4x1 vector used by calibration algorithms
75  int8 iCalInProgress; // flag denoting that a calibration is in progress
76  int8 iMagCalHasRun; // flag denoting that at least one calibration has been launched
77  int8 iValidMagCal; // integer value 0, 4, 7, 10 denoting both valid calibration and solver used
78 };
79 
80 // function prototypes for functions in magnetic.c
81 void fInitMagCalibration(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer);
82 void fRunMagCalibration(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor* pthisMag);
83 void iUpdateMagnetometerBuffer(struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32 loopcounter);
84 void fInvertMagCal(struct MagSensor *pthisMag, struct MagCalibration *pthisMagCal);
85 void fUpdateCalibration4INV(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
86 void fUpdateCalibration7EIG(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
87 void fUpdateCalibration10EIG(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
88 
89 #endif // #ifndef MAGNETIC_H
float fvecA[10]
Definition: magnetic.h:73
float fV[3]
Definition: magnetic.h:61
int16 tanarray[MAGBUFFSIZEX-1]
Definition: magnetic.h:54
float ftrinvW[3][3]
Definition: magnetic.h:66
void fUpdateCalibration7EIG(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:530
float fmatA[10][10]
Definition: magnetic.h:71
void fUpdateCalibration10EIG(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:677
int8 iValidMagCal
Definition: magnetic.h:77
float fA[3][3]
Definition: magnetic.h:69
int32 index[MAGBUFFSIZEX][MAGBUFFSIZEY]
Definition: magnetic.h:53
int8 iCalInProgress
Definition: magnetic.h:75
float ftrV[3]
Definition: magnetic.h:65
float ftrFitErrorpc
Definition: magnetic.h:68
#define MAGBUFFSIZEX
signed short int int16
Definition: isf_types.h:73
#define MAGBUFFSIZEY
float fFitErrorpc
Definition: magnetic.h:64
int8 iMagCalHasRun
Definition: magnetic.h:76
float fvecB[4]
Definition: magnetic.h:74
float finvA[3][3]
Definition: magnetic.h:70
void fUpdateCalibration4INV(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:351
signed long int int32
Definition: isf_types.h:74
void fInvertMagCal(struct MagSensor *pthisMag, struct MagCalibration *pthisMagCal)
Definition: magnetic.c:242
int16 iBs[3][MAGBUFFSIZEX][MAGBUFFSIZEY]
Definition: magnetic.h:52
float fmatB[10][10]
Definition: magnetic.h:72
void fRunMagCalibration(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:278
float ftrB
Definition: magnetic.h:67
void iUpdateMagnetometerBuffer(struct MagneticBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32 loopcounter)
Definition: magnetic.c:75
int16 iMagBufferCount
Definition: magnetic.h:55
void fInitMagCalibration(struct MagCalibration *pthisMagCal, struct MagneticBuffer *pthisMagBuffer)
Definition: magnetic.c:38
float finvW[3][3]
Definition: magnetic.h:62
signed char int8
Definition: isf_types.h:72