Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
magnetic.h@0:f68c99fc1bc2, 2016-06-18 (annotated)
- Committer:
- Cotzo
- Date:
- Sat Jun 18 21:16:12 2016 +0000
- Revision:
- 0:f68c99fc1bc2
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cotzo | 0:f68c99fc1bc2 | 1 | // Source: eCompass v3 |
Cotzo | 0:f68c99fc1bc2 | 2 | |
Cotzo | 0:f68c99fc1bc2 | 3 | #include "mbed.h" |
Cotzo | 0:f68c99fc1bc2 | 4 | |
Cotzo | 0:f68c99fc1bc2 | 5 | #ifndef _MAGNETIC_H_ |
Cotzo | 0:f68c99fc1bc2 | 6 | #define _MAGNETIC_H_ |
Cotzo | 0:f68c99fc1bc2 | 7 | |
Cotzo | 0:f68c99fc1bc2 | 8 | typedef int16_t int16; |
Cotzo | 0:f68c99fc1bc2 | 9 | typedef uint16_t uint16; |
Cotzo | 0:f68c99fc1bc2 | 10 | typedef int32_t int32; |
Cotzo | 0:f68c99fc1bc2 | 11 | typedef uint32_t uint32; |
Cotzo | 0:f68c99fc1bc2 | 12 | |
Cotzo | 0:f68c99fc1bc2 | 13 | #define MAGBUFFSIZE (216) // magnetic buffer size: 6 implies 6^3 = 216 entries |
Cotzo | 0:f68c99fc1bc2 | 14 | #define FINVMATRIXSCALING (50.0F) // inverse of FMATRIXSCALING |
Cotzo | 0:f68c99fc1bc2 | 15 | #define FUTPERCOUNT (0.1F) // MAG3110 and FXOS8700 provide 0.1uT per count resolution |
Cotzo | 0:f68c99fc1bc2 | 16 | #define FMATRIXSCALING (0.02F) // approx normalizes geomagnetic field 50uT |
Cotzo | 0:f68c99fc1bc2 | 17 | #define MAXMATINV 6 // maximum size supported in matrix inverse function |
Cotzo | 0:f68c99fc1bc2 | 18 | |
Cotzo | 0:f68c99fc1bc2 | 19 | // 3d arrays, dude... |
Cotzo | 0:f68c99fc1bc2 | 20 | |
Cotzo | 0:f68c99fc1bc2 | 21 | struct MagneticBuffer { |
Cotzo | 0:f68c99fc1bc2 | 22 | int16 iBx[MAGBUFFSIZE]/*[MAGBUFFSIZE][MAGBUFFSIZE]*/; // array of x magnetic fields |
Cotzo | 0:f68c99fc1bc2 | 23 | int16 iBy[MAGBUFFSIZE]/*[MAGBUFFSIZE][MAGBUFFSIZE]*/; // array of y magnetic fields |
Cotzo | 0:f68c99fc1bc2 | 24 | int16 iBz[MAGBUFFSIZE]/*[MAGBUFFSIZE][MAGBUFFSIZE]*/; // array of z magnetic fields |
Cotzo | 0:f68c99fc1bc2 | 25 | //int32 index[MAGBUFFSIZE][MAGBUFFSIZE][MAGBUFFSIZE]; // array of time indices |
Cotzo | 0:f68c99fc1bc2 | 26 | //int32 iMagBufferCount; // number of magnetometer readings |
Cotzo | 0:f68c99fc1bc2 | 27 | }; |
Cotzo | 0:f68c99fc1bc2 | 28 | |
Cotzo | 0:f68c99fc1bc2 | 29 | // magnetic calibration structure |
Cotzo | 0:f68c99fc1bc2 | 30 | struct MagCalibration |
Cotzo | 0:f68c99fc1bc2 | 31 | { |
Cotzo | 0:f68c99fc1bc2 | 32 | float fVx; // x component of computed hard iron offset |
Cotzo | 0:f68c99fc1bc2 | 33 | float fVy; // y component of computed hard iron offset |
Cotzo | 0:f68c99fc1bc2 | 34 | float fVz; // z component of computed hard iron offset |
Cotzo | 0:f68c99fc1bc2 | 35 | float fB; // computed geomagnetic field magnitude in uT |
Cotzo | 0:f68c99fc1bc2 | 36 | float fFitErrorpc; // computed fit error % |
Cotzo | 0:f68c99fc1bc2 | 37 | float ftrVx; // trial value of x component of hard iron offset |
Cotzo | 0:f68c99fc1bc2 | 38 | float ftrVy; // trial value of y component of hard iron offset |
Cotzo | 0:f68c99fc1bc2 | 39 | float ftrVz; // trial value of z component of hard iron offset |
Cotzo | 0:f68c99fc1bc2 | 40 | float ftrB; // trial value of geomagnetic field magnitude in uT |
Cotzo | 0:f68c99fc1bc2 | 41 | float ftrFitErrorpc; // trial value of fit error % |
Cotzo | 0:f68c99fc1bc2 | 42 | int32 iValidMagCal; // valid magnetic calibration flag |
Cotzo | 0:f68c99fc1bc2 | 43 | float xfinvW[3][3]; // estimated inverse soft iron matrix size |
Cotzo | 0:f68c99fc1bc2 | 44 | float *finvW[3]; |
Cotzo | 0:f68c99fc1bc2 | 45 | float xfA[3][3]; // estimated ellipsoid matrix A |
Cotzo | 0:f68c99fc1bc2 | 46 | float *fA[3]; |
Cotzo | 0:f68c99fc1bc2 | 47 | float xinvA[3][3]; // inverse of ellipsoid matrix A |
Cotzo | 0:f68c99fc1bc2 | 48 | float *finvA[3]; |
Cotzo | 0:f68c99fc1bc2 | 49 | float xftrinvW[3][3]; // trial computed inverse soft iron matrix size |
Cotzo | 0:f68c99fc1bc2 | 50 | float *ftrinvW[3]; |
Cotzo | 0:f68c99fc1bc2 | 51 | }; |
Cotzo | 0:f68c99fc1bc2 | 52 | |
Cotzo | 0:f68c99fc1bc2 | 53 | struct vmagi16{ |
Cotzo | 0:f68c99fc1bc2 | 54 | int16_t x; |
Cotzo | 0:f68c99fc1bc2 | 55 | int16_t y; |
Cotzo | 0:f68c99fc1bc2 | 56 | int16_t z; |
Cotzo | 0:f68c99fc1bc2 | 57 | }; |
Cotzo | 0:f68c99fc1bc2 | 58 | |
Cotzo | 0:f68c99fc1bc2 | 59 | struct i16MagCalibration{ |
Cotzo | 0:f68c99fc1bc2 | 60 | int16_t itrVx; |
Cotzo | 0:f68c99fc1bc2 | 61 | int16_t itrVy; |
Cotzo | 0:f68c99fc1bc2 | 62 | int16_t itrVz; |
Cotzo | 0:f68c99fc1bc2 | 63 | }; |
Cotzo | 0:f68c99fc1bc2 | 64 | |
Cotzo | 0:f68c99fc1bc2 | 65 | void fUpdateCalibration4INV(struct MagCalibration *pthisMagCal, |
Cotzo | 0:f68c99fc1bc2 | 66 | struct MagneticBuffer *pthisMagneticBuffer, |
Cotzo | 0:f68c99fc1bc2 | 67 | float **ftmpA4x4, float **ftmpB4x4, float **ftmpA4x1, |
Cotzo | 0:f68c99fc1bc2 | 68 | float **ftmpB4x1, int32 **icolind, int32 **irowind, int32 **ipivot); |
Cotzo | 0:f68c99fc1bc2 | 69 | |
Cotzo | 0:f68c99fc1bc2 | 70 | void ResetMagCalibration(struct MagCalibration *pthisMagCal/*, struct MagneticBuffer *pthisMagneticBuffer*/); |
Cotzo | 0:f68c99fc1bc2 | 71 | |
Cotzo | 0:f68c99fc1bc2 | 72 | void magUpdateCalibration(struct MagCalibration *pthisMagCal, |
Cotzo | 0:f68c99fc1bc2 | 73 | struct MagneticBuffer *pthisMagneticBuffer); |
Cotzo | 0:f68c99fc1bc2 | 74 | |
Cotzo | 0:f68c99fc1bc2 | 75 | #endif |