1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
/*--------------------------------------------------------------------------
Copyright (c) 2014, The Linux Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of The Linux Foundation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------*/
#include <stdio.h>
#include <CalibrationModule.h>
#define LOG_TAG "sensor_cal.akm"
#include <utils/Log.h>
#include "AKFS_Device.h"
#include "AKFS_Decomp.h"
#include "AKFS_AOC.h"
#include "AKFS_Math.h"
#include "AKFS_VNorm.h"
#define SENSOR_CAL_ALGO_VERSION 1
#define AKM_MAG_SENSE (1.0)
#define CSPEC_HNAVE_V 8
#define AKFS_GEOMAG_MAX 70
struct sensor_cal_module_t SENSOR_CAL_MODULE_INFO;
static struct sensor_cal_algo_t algo_list[];
/*! A parameter structure. */
/* ix*_ : x-bit integer */
/* f**_ : floating value */
/* p**_ : pointer */
/* e**_ : enum */
/* *s*_ : struct */
/* *v*_ : vector (special type of struct) */
/* **a_ : array */
typedef struct _AKMPRMS{
/* Variables for Decomp. */
AKFVEC fva_hdata[AKFS_HDATA_SIZE];
uint8vec i8v_asa;
/* Variables forAOC. */
AKFS_AOC_VAR s_aocv;
/* Variables for Magnetometer buffer. */
AKFVEC fva_hvbuf[AKFS_HDATA_SIZE];
AKFVEC fv_ho;
AKFVEC fv_hs;
AKFS_PATNO e_hpat;
/* Variables for Accelerometer buffer. */
AKFVEC fva_avbuf[AKFS_ADATA_SIZE];
AKFVEC fv_ao;
AKFVEC fv_as;
/* Variables for Direction. */
AKFLOAT f_azimuth;
AKFLOAT f_pitch;
AKFLOAT f_roll;
/* Variables for vector output */
AKFVEC fv_hvec;
AKFVEC fv_avec;
int16 i16_hstatus;
} AKMPRMS;
static AKMPRMS g_prms;
static int convert_magnetic(sensors_vec_t *raw, sensors_vec_t *result,
struct sensor_algo_args *args)
{
int16 akret;
int16 aocret;
AKFLOAT radius;
AKMPRMS *prms = &g_prms;
int i;
/* Shift out old data from the buffer for better calibration */
for (i = AKFS_HDATA_SIZE - 1; i >= 1; i--) {
prms->fva_hdata[i] = prms->fva_hdata[i - 1];
}
prms->fva_hdata[0].u.x = raw->x;
prms->fva_hdata[0].u.y = raw->y;
prms->fva_hdata[0].u.z = raw->z;
/* Offset calculation is done in this function */
/* hdata[in] : Android coordinate, sensitivity adjusted. */
/* ho [out]: Android coordinate, sensitivity adjusted. */
aocret = AKFS_AOC(
&prms->s_aocv,
prms->fva_hdata,
&prms->fv_ho
);
/* Subtract offset */
/* hdata[in] : Android coordinate, sensitivity adjusted. */
/* ho [in] : Android coordinate, sensitivity adjusted. */
/* hvbuf[out]: Android coordinate, sensitivity adjusted, */
/* offset subtracted. */
akret = AKFS_VbNorm(
AKFS_HDATA_SIZE,
prms->fva_hdata,
1,
&prms->fv_ho,
&prms->fv_hs,
AKM_MAG_SENSE,
AKFS_HDATA_SIZE,
prms->fva_hvbuf
);
if (akret == AKFS_ERROR) {
ALOGE("error here!");
return -1;
}
/* Averaging */
/* hvbuf[in] : Android coordinate, sensitivity adjusted, */
/* offset subtracted. */
/* hvec [out]: Android coordinate, sensitivity adjusted, */
/* offset subtracted, averaged. */
akret = AKFS_VbAve(
AKFS_HDATA_SIZE,
prms->fva_hvbuf,
CSPEC_HNAVE_V,
&prms->fv_hvec
);
if (akret == AKFS_ERROR) {
ALOGE("error here!");
return -1;
}
/* Check the size of magnetic vector */
radius = AKFS_SQRT(
(prms->fv_hvec.u.x * prms->fv_hvec.u.x) +
(prms->fv_hvec.u.y * prms->fv_hvec.u.y) +
(prms->fv_hvec.u.z * prms->fv_hvec.u.z));
if (radius > AKFS_GEOMAG_MAX) {
prms->i16_hstatus = 0;
} else {
if (aocret == AKFS_SUCCESS) {
prms->i16_hstatus = 3;
}
}
result->x = prms->fv_hvec.u.x;
result->y = prms->fv_hvec.u.y;
result->z = prms->fv_hvec.u.z;
result->status = prms->i16_hstatus;
return 0;
}
static int config_magnetic(int cmd, struct sensor_algo_args *args)
{
struct compass_algo_args *param = (struct compass_algo_args*)args;
switch (cmd) {
case CMD_ENABLE:
ALOGD("Enable status changed to %d\n", param->common.enable);
break;
case CMD_DELAY:
ALOGD("Polling rate changed to %d\n", param->common.delay_ms);
break;
case CMD_BATCH:
break;
}
return 0;
}
static int cal_init(const struct sensor_cal_module_t *module)
{
AKMPRMS *prms = &g_prms;
/* Clear all data. */
memset(prms, 0, sizeof(AKMPRMS));
/* Sensitivity */
prms->fv_hs.u.x = AKM_MAG_SENSE;
prms->fv_hs.u.y = AKM_MAG_SENSE;
prms->fv_hs.u.z = AKM_MAG_SENSE;
/* Initialize buffer */
AKFS_InitBuffer(AKFS_HDATA_SIZE, prms->fva_hdata);
AKFS_InitBuffer(AKFS_HDATA_SIZE, prms->fva_hvbuf);
AKFS_InitBuffer(AKFS_ADATA_SIZE, prms->fva_avbuf);
/* Initialize for AOC */
AKFS_InitAOC(&prms->s_aocv);
/* Initialize magnetic status */
prms->i16_hstatus = 0;
return 0;
}
static void cal_deinit()
{
ALOGI("%s called\n", __func__);
}
static int cal_get_algo_list(const struct sensor_cal_algo_t **algo)
{
*algo = algo_list;
return 0;
}
static struct sensor_algo_methods_t algo_methods = {
.convert = convert_magnetic,
.config = config_magnetic,
};
static const char* sensor_match_table[] = {
"akm09911-mag",
"akm8963-mag",
"compass",
NULL
};
static struct sensor_cal_algo_t algo_list[] = {
{
.tag = SENSOR_CAL_ALGO_TAG,
.version = SENSOR_CAL_ALGO_VERSION,
.type = SENSOR_TYPE_MAGNETIC_FIELD,
.compatible = sensor_match_table,
.module = &SENSOR_CAL_MODULE_INFO,
.methods = &algo_methods,
},
};
static struct sensor_cal_methods_t cal_methods = {
.init = cal_init,
.deinit = cal_deinit,
.get_algo_list = cal_get_algo_list,
};
struct sensor_cal_module_t SENSOR_CAL_MODULE_INFO = {
.tag = SENSOR_CAL_MODULE_TAG,
.id = "cal_module_akm",
.version = SENSOR_CAL_MODULE_VERSION,
.vendor = "akm",
.dso = NULL,
.number = 1,
.methods = &cal_methods,
.reserved = {0},
};
|