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 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_MAGNETOMETER_MAG_CAL_MAG_CAL_H_
18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_MAGNETOMETER_MAG_CAL_MAG_CAL_H_
19 
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include "calibration/diversity_checker/diversity_checker.h"
25 #include "common/math/kasa.h"
26 #include "common/math/mat.h"
27 #include "common/math/vec.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 enum MagUpdate {
34   NO_UPDATE = 0x00,
35   UPDATE_BIAS = 0x01,
36   UPDATE_SPHERE_FIT = 0x02,
37   UPDATE_BIAS_MAGGYRO_MEDIUM = 0x04,
38   UPDATE_BIAS_MAGGYRO_HIGH = 0x08,
39   MAGGYRO_TIMEOUT = 0x10,
40 };
41 
42 #ifdef MAG_CAL_DEBUG_ENABLE
43 struct MagDbg {
44   uint32_t mag_trigger_count;
45   uint32_t kasa_count;
46 };
47 #endif
48 
49 // MagCal algorithm parameters (see MagCal for details).
50 struct MagCalParameters {
51   uint32_t min_batch_window_in_micros;
52   float x_bias;  // [micro-Tesla]
53   float y_bias;  // [micro-Tesla]
54   float z_bias;  // [micro-Tesla]
55   float c00;
56   float c01;
57   float c02;
58   float c10;
59   float c11;
60   float c12;
61   float c20;
62   float c21;
63   float c22;
64 };
65 
66 struct MagCal {
67   struct DiversityChecker diversity_checker;
68   struct KasaFit kasa;
69 
70   uint64_t start_time;   // [micro-seconds]
71   uint64_t update_time;  // [micro-seconds]
72   uint32_t min_batch_window_in_micros;
73   float x_bias, y_bias, z_bias;
74   float radius;  // [micro-Tesla]
75   bool kasa_batching;
76   float c00, c01, c02, c10, c11, c12, c20, c21, c22;
77 
78 #ifdef MAG_CAL_DEBUG_ENABLE
79   struct MagDbg mag_dbg;
80 #endif
81 };
82 
83 void initMagCal(struct MagCal *moc,
84                 const struct MagCalParameters *mag_cal_parameters,
85                 const struct DiversityCheckerParameters *diverse_parameters);
86 
87 void magCalDestroy(struct MagCal *moc);
88 
89 enum MagUpdate magCalUpdate(struct MagCal *moc, uint64_t sample_time_us,
90                             float x, float y, float z);
91 
92 void magCalGetBias(const struct MagCal *moc, float *x, float *y, float *z);
93 
94 void magCalAddBias(struct MagCal *moc, float x, float y, float z);
95 
96 void magCalRemoveBias(struct MagCal *moc, float xi, float yi, float zi,
97                       float *xo, float *yo, float *zo);
98 
99 void magCalSetSoftiron(struct MagCal *moc, float c00, float c01, float c02,
100                        float c10, float c11, float c12, float c20, float c21,
101                        float c22);
102 
103 void magCalRemoveSoftiron(struct MagCal *moc, float xi, float yi, float zi,
104                           float *xo, float *yo, float *zo);
105 
106 void magCalReset(struct MagCal *moc);
107 
108 #if defined MAG_CAL_DEBUG_ENABLE
109 void magLogPrint(struct DiversityChecker *moc, float temp);
110 #endif
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_MAGNETOMETER_MAG_CAL_MAG_CAL_H_
117