1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /////////////////////////////////////////////////////////////////////////////
17 /*
18  * This module contains a data structure and corresponding helper functions for
19  * a three-axis sensor calibration.  The calibration consists of a bias vector,
20  * bias, and a lower-diagonal scaling and skew matrix, scale_skew_mat.
21  *
22  * The calibration is applied to impaired sensor data as follows:
23  *
24  * corrected_data = scale_skew_mat * (impaired_data - bias).
25  */
26 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_COMMON_CALIBRATION_DATA_H_
27 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_COMMON_CALIBRATION_DATA_H_
28 
29 #include <stdint.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #define THREE_AXIS_DIM (3)
36 
37 // Calibration data structure.
38 struct ThreeAxisCalData {
39   // Scale factor and skew terms.  Used to construct the following lower
40   // diagonal scale_skew_mat:
41   // scale_skew_mat = [scale_factor_x    0         0
42   //                   skew_yx    scale_factor_y   0
43   //                   skew_zx       skew_zy   scale_factor_z].
44   float scale_factor_x;
45   float scale_factor_y;
46   float scale_factor_z;
47   float skew_yx;
48   float skew_zx;
49   float skew_zy;
50 
51   // Sensor bias offset.
52   float bias[THREE_AXIS_DIM];
53 
54   // Calibration time.
55   uint64_t calibration_time_nanos;
56 };
57 
58 // Set calibration data to identity scale factors, zero skew and
59 // zero bias.
60 void calDataReset(struct ThreeAxisCalData *calstruct);
61 
62 // Apply a stored calibration to correct a single sample of impaired sensor
63 // data.
64 void calDataCorrectData(const struct ThreeAxisCalData* calstruct,
65                         const float x_impaired[THREE_AXIS_DIM],
66                         float* x_corrected);
67 
68 #ifdef __cplusplus
69 }
70 #endif
71 
72 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_COMMON_CALIBRATION_DATA_H_
73