OSDN Git Service

GMINL-3234: Tweak magneto filter window size
[android-x86/hardware-intel-libsensors.git] / transform.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <math.h>
7 #include <utils/Log.h>
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
10 #include "calibration.h"
11 #include "common.h"
12 #include "description.h"
13 #include "transform.h"
14 #include "utils.h"
15 #include "filtering.h"
16
17 /*----------------------------------------------------------------------------*/
18
19 /* Macros related to Intel Sensor Hub */
20
21 #define GRAVITY 9.80665f
22
23 /* 720 LSG = 1G */
24 #define LSG                         (1024.0f)
25 #define NUMOFACCDATA                (8.0f)
26
27 /* conversion of acceleration data to SI units (m/s^2) */
28 #define CONVERT_A                   (GRAVITY_EARTH / LSG / NUMOFACCDATA)
29 #define CONVERT_A_X(x)              ((float(x)/1000) * (GRAVITY * -1.0))
30 #define CONVERT_A_Y(x)              ((float(x)/1000) * (GRAVITY * 1.0))
31 #define CONVERT_A_Z(x)              ((float(x)/1000) * (GRAVITY * 1.0))
32
33 /* conversion of magnetic data to uT units */
34 #define CONVERT_M                   (1.0f/6.6f)
35 #define CONVERT_M_X                 (-CONVERT_M)
36 #define CONVERT_M_Y                 (-CONVERT_M)
37 #define CONVERT_M_Z                 (CONVERT_M)
38
39 #define CONVERT_GAUSS_TO_MICROTESLA(x)        ( (x) * 100 )
40
41 /* conversion of orientation data to degree units */
42 #define CONVERT_O                   (1.0f/64.0f)
43 #define CONVERT_O_A                 (CONVERT_O)
44 #define CONVERT_O_P                 (CONVERT_O)
45 #define CONVERT_O_R                 (-CONVERT_O)
46
47 /*conversion of gyro data to SI units (radian/sec) */
48 #define CONVERT_GYRO                ((2000.0f/32767.0f)*((float)M_PI / 180.0f))
49 #define CONVERT_GYRO_X              (-CONVERT_GYRO)
50 #define CONVERT_GYRO_Y              (-CONVERT_GYRO)
51 #define CONVERT_GYRO_Z              (CONVERT_GYRO)
52
53 #define BIT(x) (1 << (x))
54
55 inline unsigned int set_bit_range(int start, int end)
56 {
57     int i;
58     unsigned int value = 0;
59
60     for (i = start; i < end; ++i)
61         value |= BIT(i);
62     return value;
63 }
64
65 inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
66 {
67     int divider=1;
68     int i;
69     float sample;
70     int mul = 1.0;
71
72     value = value & set_bit_range(0, size*8);
73     if (value & BIT(size*8-1)) {
74         value =  ((1LL << (size*8)) - value);
75         mul = -1.0;
76     }
77     sample = value * 1.0;
78     if (exponent < 0) {
79         exponent = abs(exponent);
80         for (i = 0; i < exponent; ++i) {
81             divider = divider*10;
82         }
83         return mul * sample/divider;
84     } else {
85         return mul * sample * pow(10.0, exponent);
86     }
87 }
88
89 // Platform sensor orientation
90 #define DEF_ORIENT_ACCEL_X                   -1
91 #define DEF_ORIENT_ACCEL_Y                   -1
92 #define DEF_ORIENT_ACCEL_Z                   -1
93
94 #define DEF_ORIENT_GYRO_X                   1
95 #define DEF_ORIENT_GYRO_Y                   1
96 #define DEF_ORIENT_GYRO_Z                   1
97
98 // G to m/s2
99 #define CONVERT_FROM_VTF16(s,d,x)      (convert_from_vtf_format(s,d,x))
100 #define CONVERT_A_G_VTF16E14_X(s,d,x)  (DEF_ORIENT_ACCEL_X *\
101                                         convert_from_vtf_format(s,d,x)*GRAVITY)
102 #define CONVERT_A_G_VTF16E14_Y(s,d,x)  (DEF_ORIENT_ACCEL_Y *\
103                                         convert_from_vtf_format(s,d,x)*GRAVITY)
104 #define CONVERT_A_G_VTF16E14_Z(s,d,x)  (DEF_ORIENT_ACCEL_Z *\
105                                         convert_from_vtf_format(s,d,x)*GRAVITY)
106
107 // Degree/sec to radian/sec
108 #define CONVERT_G_D_VTF16E14_X(s,d,x)  (DEF_ORIENT_GYRO_X *\
109                                         convert_from_vtf_format(s,d,x) * \
110                                         ((float)M_PI/180.0f))
111 #define CONVERT_G_D_VTF16E14_Y(s,d,x)  (DEF_ORIENT_GYRO_Y *\
112                                         convert_from_vtf_format(s,d,x) * \
113                                         ((float)M_PI/180.0f))
114 #define CONVERT_G_D_VTF16E14_Z(s,d,x)  (DEF_ORIENT_GYRO_Z *\
115                                         convert_from_vtf_format(s,d,x) * \
116                                         ((float)M_PI/180.0f))
117
118 // Milli gauss to micro tesla
119 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x)/10)
120 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x)/10)
121 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x)/10)
122
123
124 /*----------------------------------------------------------------------------*/
125
126 static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
127 {
128         uint64_t u64;
129         int i;
130         int zeroed_bits = type->storagebits - type->realbits;
131         uint64_t sign_mask;
132         uint64_t value_mask;
133
134         u64 = 0;
135
136         if (type->endianness == 'b')
137                 for (i=0; i<type->storagebits/8; i++)
138                         u64 = (u64 << 8) | sample[i];
139         else
140                 for (i=type->storagebits/8 - 1; i>=0; i--)
141                         u64 = (u64 << 8) | sample[i];
142
143         u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
144
145         if (type->sign == 'u')
146                 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
147
148         /* Signed integer */
149
150         switch (type->realbits) {
151                 case 0 ... 1:
152                         return 0;
153
154                 case 8:
155                         return (int64_t) (int8_t) u64;
156
157                 case 16:
158                         return (int64_t) (int16_t) u64;
159
160                 case 32:
161                         return (int64_t) (int32_t) u64;
162
163                 case 64:
164                         return (int64_t) u64;
165
166                 default:
167                         sign_mask = 1 << (type->realbits-1);
168                         value_mask = sign_mask - 1;
169
170                         if (u64 & sign_mask)
171                                 /* Negative value: return 2-complement */
172                                 return - ((~u64 & value_mask) + 1);
173                         else
174                                 return (int64_t) u64; /* Positive value */
175         }
176 }
177
178
179 static void reorder_fields(float* data, unsigned char map[MAX_CHANNELS])
180 {
181         int i;
182         float temp[MAX_CHANNELS];
183
184         for (i=0; i<MAX_CHANNELS; i++)
185                 temp[i] = data[map[i]];
186
187         for (i=0; i<MAX_CHANNELS; i++)
188                 data[i] = temp[i];
189 }
190
191
192 static void denoise (struct sensor_info_t* si, struct sensors_event_t* data,
193                      int num_fields, int max_samples)
194 {
195         /*
196          * Smooth out incoming data using a moving average over a number of
197          * samples. We accumulate one second worth of samples, or max_samples,
198          * depending on which is lower.
199          */
200
201         int i;
202         int f;
203         int sampling_rate = (int) si->sampling_rate;
204         int history_size;
205         int history_full = 0;
206
207         /* Don't denoise anything if we have less than two samples per second */
208         if (sampling_rate < 2)
209                 return;
210
211         /* Restrict window size to the min of sampling_rate and max_samples */
212         if (sampling_rate > max_samples)
213                 history_size = max_samples;
214         else
215                 history_size = sampling_rate;
216
217         /* Reset history if we're operating on an incorrect window size */
218         if (si->history_size != history_size) {
219                 si->history_size = history_size;
220                 si->history_entries = 0;
221                 si->history_index = 0;
222                 si->history = (float*) realloc(si->history,
223                                 si->history_size * num_fields * sizeof(float));
224                 if (si->history) {
225                         si->history_sum = (float*) realloc(si->history_sum,
226                                 num_fields * sizeof(float));
227                         if (si->history_sum)
228                                 memset(si->history_sum, 0, num_fields * sizeof(float));
229                 }
230         }
231
232         if (!si->history || !si->history_sum)
233                 return; /* Unlikely, but still... */
234
235         /* Update initialized samples count */
236         if (si->history_entries < si->history_size)
237                 si->history_entries++;
238         else
239                 history_full = 1;
240
241         /* Record new sample and calculate the moving sum */
242         for (f=0; f < num_fields; f++) {
243                 /**
244                  * A field is going to be overwritten if
245                  * history is full, so decrease the history sum
246                  */
247                 if (history_full)
248                         si->history_sum[f] -=
249                                 si->history[si->history_index * num_fields + f];
250
251                 si->history[si->history_index * num_fields + f] = data->data[f];
252                 si->history_sum[f] += data->data[f];
253
254                 /* For now simply compute a mobile mean for each field */
255                 /* and output filtered data */
256                 data->data[f] = si->history_sum[f] / si->history_entries;
257         }
258
259         /* Update our rolling index (next evicted cell) */
260         si->history_index = (si->history_index + 1) % si->history_size;
261 }
262
263
264 static void clamp_gyro_readings_to_zero (int s, struct sensors_event_t* data)
265 {
266         float x, y, z;
267         float near_zero;
268
269         switch (sensor_info[s].type) {
270                 case SENSOR_TYPE_GYROSCOPE:
271                         x = data->data[0];
272                         y = data->data[1];
273                         z = data->data[2];
274                         break;
275
276                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
277                         x = data->data[0] - data->uncalibrated_gyro.bias[0];
278                         y = data->data[1] - data->uncalibrated_gyro.bias[1];
279                         z = data->data[2] - data->uncalibrated_gyro.bias[2];
280                         break;
281
282                 default:
283                         return;
284         }
285
286         /* If we're calibrated, don't filter out as much */
287         if (sensor_info[s].cal_level > 0)
288                 near_zero = 0.02; /* rad/s */
289         else
290                 near_zero = 0.1;
291
292         /* If motion on all axes is small enough */
293         if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
294
295                 /*
296                  * Report that we're not moving at all... but not exactly zero
297                  * as composite sensors (orientation, rotation vector) don't
298                  * seem to react very well to it.
299                  */
300                 switch (sensor_info[s].type) {
301                         case SENSOR_TYPE_GYROSCOPE:
302                                 data->data[0] *= 0.000001;
303                                 data->data[1] *= 0.000001;
304                                 data->data[2] *= 0.000001;
305                                 break;
306
307                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
308                                 data->data[0]= data->uncalibrated_gyro.bias[0]
309                                                 + 0.000001 * x;
310                                 data->data[1]= data->uncalibrated_gyro.bias[1]
311                                                 + 0.000001 * y;
312                                 data->data[2]= data->uncalibrated_gyro.bias[2]
313                                                 + 0.000001 * z;
314                                 break;
315                 }
316         }
317 }
318
319
320 static int finalize_sample_default (int s, struct sensors_event_t* data)
321 {
322         /* Swap fields if we have a custom channel ordering on this sensor */
323         if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
324                 reorder_fields(data->data, sensor_info[s].order);
325
326         sensor_info[s].event_count++;
327         switch (sensor_info[s].type) {
328                 case SENSOR_TYPE_ACCELEROMETER:
329                         /* Always consider the accelerometer accurate */
330                         data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
331                         if (sensor_info[s].quirks & QUIRK_NOISY)
332                                 denoise(&sensor_info[s], data, 3, 20);
333                         break;
334
335                 case SENSOR_TYPE_MAGNETIC_FIELD:
336                         calibrate_compass (data, &sensor_info[s], get_timestamp());
337                         if (sensor_info[s].quirks & QUIRK_NOISY)
338                                 denoise(&sensor_info[s], data, 3, 30);
339                         break;
340
341                 case SENSOR_TYPE_GYROSCOPE:
342
343                         /*
344                          * Report medium accuracy by default ; higher accuracy
345                          * levels will be reported once, and if, we achieve
346                          * calibration.
347                          */
348                         data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
349
350                         /* ... fall through */
351
352                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
353
354                         /*
355                          * We're only trying to calibrate data from continuously
356                          * firing gyroscope drivers, as motion based ones use
357                          * movement thresholds that may lead us to incorrectly
358                          * estimate bias.
359                          */
360                         if (sensor_info[s].selected_trigger !=
361                                 sensor_info[s].motion_trigger_name)
362                                         calibrate_gyro(data, &sensor_info[s]);
363
364                         /* For noisy sensors we'll drop a very few number
365                          * of samples to make sure we have at least MIN_SAMPLES events
366                          * in the filtering queue. This is to make sure we are not sending
367                          * events that can disturb our mean or stddev.
368                          */
369                         if (sensor_info[s].quirks & QUIRK_NOISY) {
370                                 denoise_median(&sensor_info[s], data, 3);
371                                 if((sensor_info[s].selected_trigger !=
372                                         sensor_info[s].motion_trigger_name) &&
373                                         sensor_info[s].event_count < MIN_SAMPLES)
374                                                 return 0;
375                         }
376
377                         /* Clamp near zero moves to (0,0,0) if appropriate */
378                         clamp_gyro_readings_to_zero(s, data);
379                         break;
380
381                 case SENSOR_TYPE_LIGHT:
382                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
383                 case SENSOR_TYPE_TEMPERATURE:
384                         /* Only keep two decimals for these readings */
385                         data->data[0] = 0.01 * ((int) (data->data[0] * 100));
386
387                         /* ... fall through ... */
388
389                 case SENSOR_TYPE_PROXIMITY:
390                         /*
391                          * These are on change sensors ; drop the sample if it
392                          * has the same value as the previously reported one.
393                          */
394                         if (data->data[0] == sensor_info[s].prev_val)
395                                 return 0;
396
397                         sensor_info[s].prev_val = data->data[0];
398                         break;
399         }
400
401         /* Add this event to our global records, for filtering purposes */
402         record_sample(s, data);
403
404         return 1; /* Return sample to Android */
405 }
406
407
408 static float transform_sample_default(int s, int c, unsigned char* sample_data)
409 {
410         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
411         int64_t              s64 = sample_as_int64(sample_data, sample_type);
412         float scale = sensor_info[s].scale ?
413                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
414
415         /* In case correction has been requested using properties, apply it */
416         scale *= sensor_info[s].channel[c].opt_scale;
417
418         /* Apply default scaling rules */
419         return (sensor_info[s].offset + s64) * scale;
420 }
421
422
423 static int finalize_sample_ISH (int s, struct sensors_event_t* data)
424 {
425         float pitch, roll, yaw;
426
427         /* Swap fields if we have a custom channel ordering on this sensor */
428         if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
429                 reorder_fields(data->data, sensor_info[s].order);
430
431         if (sensor_info[s].type == SENSOR_TYPE_ORIENTATION) {
432
433                 pitch = data->data[0];
434                 roll = data->data[1];
435                 yaw = data->data[2];
436
437                 data->data[0] = 360.0 - yaw;
438                 data->data[1] = -pitch;
439                 data->data[2] = -roll;
440         }
441
442         /* Add this event to our global records, for filtering purposes */
443         record_sample(s, data);
444
445         return 1; /* Return sample to Android */
446 }
447
448
449 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
450 {
451         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
452         int val         = (int) sample_as_int64(sample_data, sample_type);
453         float correction;
454         int data_bytes  = (sample_type->realbits)/8;
455         int exponent    = sensor_info[s].offset;
456
457         /* In case correction has been requested using properties, apply it */
458         correction = sensor_info[s].channel[c].opt_scale;
459
460         switch (sensor_info[s].type) {
461                 case SENSOR_TYPE_ACCELEROMETER:
462                         switch (c) {
463                                 case 0:
464                                         return  correction *
465                                                 CONVERT_A_G_VTF16E14_X(
466                                                 data_bytes, exponent, val);
467
468                                 case 1:
469                                         return  correction *
470                                                 CONVERT_A_G_VTF16E14_Y(
471                                                 data_bytes, exponent, val);
472
473                                 case 2:
474                                         return  correction *
475                                                 CONVERT_A_G_VTF16E14_Z(
476                                                 data_bytes, exponent, val);
477                         }
478                         break;
479
480
481                 case SENSOR_TYPE_GYROSCOPE:
482                         switch (c) {
483                                 case 0:
484                                         return  correction *
485                                                 CONVERT_G_D_VTF16E14_X(
486                                                 data_bytes, exponent, val);
487
488                                 case 1:
489                                         return  correction *
490                                                 CONVERT_G_D_VTF16E14_Y(
491                                                 data_bytes, exponent, val);
492
493                                 case 2:
494                                         return  correction *
495                                                 CONVERT_G_D_VTF16E14_Z(
496                                                 data_bytes, exponent, val);
497                         }
498                         break;
499
500                 case SENSOR_TYPE_MAGNETIC_FIELD:
501                         switch (c) {
502                                 case 0:
503                                         return  correction *
504                                                 CONVERT_M_MG_VTF16E14_X(
505                                                 data_bytes, exponent, val);
506
507                                 case 1:
508                                         return  correction *
509                                                 CONVERT_M_MG_VTF16E14_Y(
510                                                 data_bytes, exponent, val);
511
512                                 case 2:
513                                         return  correction *
514                                                 CONVERT_M_MG_VTF16E14_Z(
515                                                 data_bytes, exponent, val);
516                         }
517                         break;
518
519                 case SENSOR_TYPE_LIGHT:
520                                 return (float) val;
521
522                 case SENSOR_TYPE_ORIENTATION:
523                         return  correction * convert_from_vtf_format(
524                                                 data_bytes, exponent, val);
525
526                 case SENSOR_TYPE_ROTATION_VECTOR:
527                         return  correction * convert_from_vtf_format(
528                                                 data_bytes, exponent, val);
529         }
530
531         return 0;
532 }
533
534
535 void select_transform (int s)
536 {
537         char prop_name[PROP_NAME_MAX];
538         char prop_val[PROP_VALUE_MAX];
539         int i                   = sensor_info[s].catalog_index;
540         const char *prefix      = sensor_catalog[i].tag;
541
542         sprintf(prop_name, PROP_BASE, prefix, "transform");
543
544         if (property_get(prop_name, prop_val, "")) {
545                 if (!strcmp(prop_val, "ISH")) {
546                         ALOGI(  "Using Intel Sensor Hub semantics on %s\n",
547                                 sensor_info[s].friendly_name);
548
549                         sensor_info[s].ops.transform = transform_sample_ISH;
550                         sensor_info[s].ops.finalize = finalize_sample_ISH;
551                         return;
552                 }
553         }
554
555         sensor_info[s].ops.transform = transform_sample_default;
556         sensor_info[s].ops.finalize = finalize_sample_default;
557 }
558
559
560 float acquire_immediate_value(int s, int c)
561 {
562         char sysfs_path[PATH_MAX];
563         float val;
564         int ret;
565         int dev_num = sensor_info[s].dev_num;
566         int i = sensor_info[s].catalog_index;
567         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
568         const char* input_path = sensor_catalog[i].channel[c].input_path;
569         float scale = sensor_info[s].scale ?
570                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
571         float offset = sensor_info[s].offset;
572         int sensor_type = sensor_catalog[i].type;
573         float correction;
574
575         /* In case correction has been requested using properties, apply it */
576         correction = sensor_info[s].channel[c].opt_scale;
577
578         /* Acquire a sample value for sensor s / channel c through sysfs */
579
580         if (input_path[0]) {
581                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
582                 ret = sysfs_read_float(sysfs_path, &val);
583
584                 if (!ret) {
585                         return val * correction;
586                 }
587         };
588
589         if (!raw_path[0])
590                 return 0;
591
592         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
593         ret = sysfs_read_float(sysfs_path, &val);
594
595         if (ret == -1)
596                 return 0;
597
598         /*
599         There is no transform ops defined yet for Raw sysfs values
600         Use this function to perform transformation as well.
601         */
602         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
603                 return  CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
604                         correction;
605
606         return (val + offset) * scale * correction;
607 }