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 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_MAGNETOMETER_MAG_CAL_H_
17 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_MAGNETOMETER_MAG_CAL_H_
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <sys/types.h>
22 #ifdef DIVERSITY_CHECK_ENABLED
23 #include "calibration/common/diversity_checker.h"
24 #endif
25 #include "common/math/mat.h"
26 #include "common/math/vec.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct KasaFit {
33   float acc_x, acc_y, acc_z, acc_w;
34   float acc_xx, acc_xy, acc_xz, acc_xw;
35   float acc_yy, acc_yz, acc_yw, acc_zz, acc_zw;
36   size_t nsamples;
37 };
38 
39 struct MagCal {
40 #ifdef DIVERSITY_CHECK_ENABLED
41   struct DiversityChecker diversity_checker;
42 #endif
43   struct KasaFit kasa;
44 
45   uint64_t start_time;
46   uint64_t update_time;
47 
48   float x_bias, y_bias, z_bias;
49   float radius;
50 
51   float c00, c01, c02, c10, c11, c12, c20, c21, c22;
52 };
53 
54 void initKasa(struct KasaFit *kasa);
55 
56 #ifdef DIVERSITY_CHECK_ENABLED
57 void initMagCal(struct MagCal *moc, float x_bias, float y_bias, float z_bias,
58                 float c00, float c01, float c02, float c10, float c11,
59                 float c12, float c20, float c21, float c22,
60                 size_t min_num_diverse_vectors, size_t max_num_max_distance,
61                 float var_threshold, float max_min_threshold, float local_field,
62                 float threshold_tuning_param, float max_distance_tuning_param);
63 #else
64 void initMagCal(struct MagCal *moc, float x_bias, float y_bias, float z_bias,
65                 float c00, float c01, float c02, float c10, float c11,
66                 float c12, float c20, float c21, float c22);
67 #endif
68 
69 void magCalDestroy(struct MagCal *moc);
70 
71 bool magCalUpdate(struct MagCal *moc, uint64_t sample_time_us, float x, float y,
72                   float z);
73 
74 void magCalGetBias(struct MagCal *moc, float *x, float *y, float *z);
75 
76 void magCalAddBias(struct MagCal *moc, float x, float y, float z);
77 
78 void magCalRemoveBias(struct MagCal *moc, float xi, float yi, float zi,
79                       float *xo, float *yo, float *zo);
80 
81 void magCalSetSoftiron(struct MagCal *moc, float c00, float c01, float c02,
82                        float c10, float c11, float c12, float c20, float c21,
83                        float c22);
84 
85 void magCalRemoveSoftiron(struct MagCal *moc, float xi, float yi, float zi,
86                           float *xo, float *yo, float *zo);
87 
88 void magKasaReset(struct KasaFit *kasa);
89 
90 void magCalReset(struct MagCal *moc);
91 
92 int magKasaFit(struct KasaFit *kasa, struct Vec3 *bias, float *radius);
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_MAGNETOMETER_MAG_CAL_H_
99