1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /*! \file precisionAccelerometer.h
10     \brief Implements accelerometer calibration routines
11 */
12 
13 #ifndef PRECISIONACCELEROMETER_H
14 #define PRECISIONACCELEROMETER_H
15 
16 /// calibration constants
17 #define ACCEL_CAL_AVERAGING_SECS	2		///< calibration measurement averaging period (s)
18 #define MAX_ACCEL_CAL_ORIENTATIONS	12		///< number of stored precision accelerometer measurements
19 
20 /// accelerometer measurement buffer
21 typedef struct AccelBuffer
22 {
23 	float fGsStored[MAX_ACCEL_CAL_ORIENTATIONS][3];	///< uncalibrated accelerometer measurements (g)
24 	float fSumGs[3];				///< averaging sum for current storage location
25 	int16_t iStoreCounter;				///< number of remaining iterations at FUSION_HZ to average measurement
26 	int16_t iStoreLocation;				///< -1 for none, 0 to 11 for the 12 storage locations
27 	int16_t iStoreFlags;				///< denotes which measurements are present
28 } AccelBuffer;
29 
30 /// precision accelerometer calibration structure
31 typedef struct AccelCalibration
32 {
33 	// start of elements stored in flash memory
34 	float fV[3];					///< offset vector (g)
35 	float finvW[3][3];				///< inverse gain matrix
36 	float fR0[3][3];				///< forward rotation matrix for measurement 0
37 	// end of elements stored in flash memory
38 	float fmatA[10][10];			        ///< scratch 10x10 matrix used by calibration algorithms
39 	float fmatB[10][10];			        ///< scratch 10x10 matrix used by calibration algorithms
40 	float fvecA[10];				///< scratch 10x1 vector used by calibration algorithms
41 	float fvecB[4];					///< scratch 4x1 vector used by calibration algorithms
42 	float fA[3][3];					///< ellipsoid matrix A
43 	float finvA[3][3];				///< inverse of the ellipsoid matrix A
44 } AccelCalibration;
45 
46 struct AccelSensor;  // actual typedef is located in sensor_fusion_types.h
47 
48 // function prototypes for functions in precisionAcclerometer.c
49 /// Initialize the accelerometer calibration functions
50 void fInitializeAccelCalibration(
51     struct AccelCalibration *pthisAccelCal,             ///< Accelerometer calibration parameter structure
52     struct AccelBuffer *pthisAccelBuffer,               ///< Buffer of measurements used as input to the accel calibration functions
53     volatile int8_t *AccelCalPacketOn                   ///< Used to coordinate calibration sample storage and communications
54 );
55 /// Update the buffer used to store samples used for accelerometer calibration.
56 void fUpdateAccelBuffer(
57     struct AccelCalibration *pthisAccelCal,             ///< Accelerometer calibration parameter structure
58     struct AccelBuffer *pthisAccelBuffer,               ///< Buffer of measurements used as input to the accel calibration functions
59     struct AccelSensor* pthisAccel,                     ///< Pointer to the accelerometer input/state structure
60     volatile int8_t *AccelCalPacketOn                   ///< Used to coordinate calibration sample storage and communications
61 );
62 /// function maps the accelerometer data fGs (g) onto precision calibrated and de-rotated data fGc (g), iGc (counts)
63 void fInvertAccelCal(
64     struct AccelSensor *pthisAccel,                     ///< Pointer to the accelerometer input/state structure
65     struct AccelCalibration *pthisAccelCal              ///< Accelerometer calibration parameter structure
66 );
67 /// function runs the precision accelerometer calibration
68 void fRunAccelCalibration(
69     struct AccelCalibration *pthisAccelCal,             ///< Accelerometer calibration parameter structure
70     struct AccelBuffer *pthisAccelBuffer,               ///< Buffer of measurements used as input to the accel calibration functions
71     struct AccelSensor* pthisAccel                      ///< Pointer to the accelerometer input/state structure
72 );
73 /// calculate the 4 element calibration from the available measurements
74 void fComputeAccelCalibration4(
75     struct AccelBuffer *pthisAccelBuffer,               ///< Buffer of measurements used as input to the accel calibration functions
76     struct AccelCalibration *pthisAccelCal,             ///< Accelerometer calibration parameter structure
77     struct AccelSensor* pthisAccel                      ///< Pointer to the accelerometer input/state structure
78 );
79 /// calculate the 7 element calibration from the available measurements
80 void fComputeAccelCalibration7(
81     struct AccelBuffer *pthisAccelBuffer,               ///< Buffer of measurements used as input to the accel calibration functions
82     struct AccelCalibration *pthisAccelCal,             ///< Accelerometer calibration parameter structure
83     struct AccelSensor* pthisAccel                      ///< Pointer to the accelerometer input/state structure
84 );
85 /// calculate the 10 element calibration from the available measurements
86 void fComputeAccelCalibration10(
87     struct AccelBuffer *pthisAccelBuffer,               ///< Buffer of measurements used as input to the accel calibration functions
88     struct AccelCalibration *pthisAccelCal,             ///< Accelerometer calibration parameter structure
89     struct AccelSensor* pthisAccel                      ///< Pointer to the accelerometer input/state structure
90 );
91 
92 #endif // PRECISIONACCELEROMETER_H
93