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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
/*--------------------------------------------------------------------------
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>
#include <sensors.h>
#define LOG_TAG "sensor_cal.common"
#include <utils/Log.h>
#include "compass/AKFS_Device.h"
#include "compass/AKFS_Decomp.h"
#include "compass/AKFS_AOC.h"
#include "compass/AKFS_Math.h"
#include "compass/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
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
struct sensor_vec {
union {
struct {
float data[4];
};
struct {
float x;
float y;
float z;
};
};
};
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 float last_pocket = -1.0f;
static float last_light = -1.0f;
static float last_proximity = -1.0f;
static int convert_magnetic(sensors_event_t *raw, sensors_event_t *result,
struct sensor_algo_args *args __attribute__((unused)))
{
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->magnetic.x;
prms->fva_hdata[0].u.y = raw->magnetic.y;
prms->fva_hdata[0].u.z = raw->magnetic.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->magnetic.x = prms->fv_hvec.u.x;
result->magnetic.y = prms->fv_hvec.u.y;
result->magnetic.z = prms->fv_hvec.u.z;
result->magnetic.status = prms->i16_hstatus;
return 0;
}
static int convert_orientation(sensors_event_t *raw, sensors_event_t *result,
struct sensor_algo_args *args __attribute__((unused)))
{
float av;
float pitch, roll, azimuth;
const float rad2deg = 180 / M_PI;
static struct sensor_vec mag, acc;
if (raw->type == SENSOR_TYPE_MAGNETIC_FIELD) {
mag.x = raw->magnetic.x;
mag.y = raw->magnetic.y;
mag.z = raw->magnetic.z;
}
if (raw->type == SENSOR_TYPE_ACCELEROMETER) {
acc.x = raw->acceleration.x;
acc.y = raw->acceleration.y;
acc.z = raw->acceleration.z;
}
av = sqrtf(acc.x*acc.x + acc.y*acc.y + acc.z*acc.z);
if (av >= DBL_EPSILON) {
pitch = asinf(-acc.y / av);
roll = asinf(acc.x / av);
result->orientation.pitch = pitch * rad2deg;
result->orientation.roll = roll * rad2deg;
azimuth = atan2(-(mag.x) * cosf(roll) + mag.z * sinf(roll),
mag.x*sinf(pitch)*sinf(roll) + mag.y*cosf(pitch) + mag.z*sinf(pitch)*cosf(roll));
result->orientation.azimuth = azimuth * rad2deg;
result->orientation.status = 3;
}
if (raw->type != SENSOR_TYPE_MAGNETIC_FIELD)
return -EAGAIN;
return 0;
}
static int convert_rotation_vector(sensors_event_t *raw, sensors_event_t *result,
struct sensor_algo_args *args __attribute__((unused)))
{
float av;
float pitch, roll, azimuth;
int i;
static struct sensor_vec mag, acc;
if (raw->type == SENSOR_TYPE_MAGNETIC_FIELD) {
mag.x = raw->magnetic.x;
mag.y = raw->magnetic.y;
mag.z = raw->magnetic.z;
}
if (raw->type == SENSOR_TYPE_ACCELEROMETER) {
acc.x = raw->acceleration.x;
acc.y = raw->acceleration.y;
acc.z = raw->acceleration.z;
}
av = sqrtf(acc.x*acc.x + acc.y*acc.y + acc.z*acc.z);
pitch = asinf(-acc.y / av);
roll = asinf(acc.x / av);
azimuth = atan2(-(mag.x) * cosf(roll) + mag.z * sinf(roll),
mag.x*sinf(pitch)*sinf(roll) + mag.y*cosf(pitch) + mag.z*sinf(pitch)*cosf(roll));
float halfAzi = azimuth / 2;
float halfPitch = pitch / 2;
float halfRoll = -roll / 2;
float c1 = cosf(halfAzi);
float s1 = sinf(halfAzi);
float c2 = cosf(halfPitch);
float s2 = sinf(halfPitch);
float c3 = cosf(halfRoll);
float s3 = sinf(halfRoll);
result->data[0] = c1*c2*c3 - s1*s2*s3;
result->data[1] = c1*s2*c3 - s1*c2*s3;
result->data[2] = c1*c2*s3 + s1*s2*c3;
result->data[3] = s1*c2*c3 + c1*s2*s3;
if (halfAzi < M_PI / 2) {
result->data[1] = -result->data[1];
result->data[3] = -result->data[3];
} else {
result->data[2] = -result->data[2];
}
if (raw->type != SENSOR_TYPE_MAGNETIC_FIELD)
return -1;
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;
}
/* The magnetic field raw data is supposed to store at the sensors_event_t:data[4~6]*/
static int convert_uncalibrated_magnetic(sensors_event_t *raw, sensors_event_t *result,
struct sensor_algo_args *args __attribute__((unused)))
{
if (raw->type == SENSOR_TYPE_MAGNETIC_FIELD) {
result->uncalibrated_magnetic.x_uncalib = raw->data[4];
result->uncalibrated_magnetic.y_uncalib = raw->data[5];
result->uncalibrated_magnetic.z_uncalib = raw->data[6];
result->uncalibrated_magnetic.x_bias = raw->data[4] - raw->data[0];
result->uncalibrated_magnetic.y_bias = raw->data[5] - raw->data[1];
result->uncalibrated_magnetic.z_bias = raw->data[6] - raw->data[2];
return 0;
}
return -1;
}
static int convert_pocket(sensors_event_t *raw, sensors_event_t *result,
struct sensor_algo_args *args __attribute__((unused)))
{
float inside;
*result = *raw;
if (raw->type == SENSOR_TYPE_PROXIMITY) {
last_proximity = raw->data[0];
} else if (raw->type == SENSOR_TYPE_LIGHT) {
last_light = raw->data[0];
} else {
ALOGE("type error:%d\n", raw->type);
return -1;
}
ALOGD("last_light:%f last_proximity:%f\n", last_light, last_proximity);
if (last_proximity < 0.0f) {
if (last_light < 0.0f) {
ALOGE("sensor data error\n");
return -1;
} else if (last_light > 1000.0f) {
inside = 0;
} else {
inside = 1;
}
} else if (last_proximity < 5.0f) {
inside = 1;
} else {
inside = 0;
}
if (last_pocket != inside) {
last_pocket = inside;
result->data[0] = inside;
return 0;
}
return -1;
}
static int config_pocket(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);
if (param->common.enable) {
last_pocket = -1.0f;
last_light = -1.0f;
last_proximity = -1.0f;
}
break;
}
return 0;
}
static int cal_init(const struct sensor_cal_module_t *module __attribute__((unused)))
{
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 compass_methods = {
.convert = convert_magnetic,
.config = config_magnetic,
};
static const char* compass_match_table[] = {
COMPASS_NAME,
NULL
};
static struct sensor_algo_methods_t orientation_methods = {
.convert = convert_orientation,
.config = NULL,
};
static const char* orientation_match_table[] = {
ORIENTATION_NAME,
NULL
};
static struct sensor_algo_methods_t rotation_vector_methods = {
.convert = convert_rotation_vector,
.config = NULL,
};
static const char* rotation_vector_match_table[] = {
ROTATION_VECTOR_NAME,
NULL
};
static struct sensor_algo_methods_t mag_uncalib_methods = {
.convert = convert_uncalibrated_magnetic,
.config = NULL,
};
static const char* mag_uncalib_match_table[] = {
MAGNETIC_FIELD_UNCALIBRATED_NAME,
NULL
};
static struct sensor_algo_methods_t pocket_methods = {
.convert = convert_pocket,
.config = config_pocket,
};
static const char* pocket_match_table[] = {
"ltr553-pocket",
"ap3426-pocket",
"oem-pocket",
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 = compass_match_table,
.module = &SENSOR_CAL_MODULE_INFO,
.methods = &compass_methods,
},
{
.tag = SENSOR_CAL_ALGO_TAG,
.version = SENSOR_CAL_ALGO_VERSION,
.type = SENSOR_TYPE_ORIENTATION,
.compatible = orientation_match_table,
.module = &SENSOR_CAL_MODULE_INFO,
.methods = &orientation_methods,
},
{
.tag = SENSOR_CAL_ALGO_TAG,
.version = SENSOR_CAL_ALGO_VERSION,
.type = SENSOR_TYPE_ROTATION_VECTOR,
.compatible = rotation_vector_match_table,
.module = &SENSOR_CAL_MODULE_INFO,
.methods = &rotation_vector_methods,
},
{
.tag = SENSOR_CAL_ALGO_TAG,
.version = SENSOR_CAL_ALGO_VERSION,
.type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
.compatible = mag_uncalib_match_table,
.module = &SENSOR_CAL_MODULE_INFO,
.methods = &mag_uncalib_methods,
},
{
.tag = SENSOR_CAL_ALGO_TAG,
.version = SENSOR_CAL_ALGO_VERSION,
.type = SENSOR_TYPE_POCKET,
.compatible = pocket_match_table,
.module = &SENSOR_CAL_MODULE_INFO,
.methods = &pocket_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_common",
.version = SENSOR_CAL_MODULE_VERSION,
.vendor = "common",
.dso = NULL,
.number = ARRAY_SIZE(algo_list),
.methods = &cal_methods,
.reserved = {0},
};
|