OSDN Git Service

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