OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/mr1/sensors/master' into mr1
[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 #include "enumeration.h"
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 #define PROXIMITY_THRESHOLD 1
61
62 inline unsigned int set_bit_range (int start, int end)
63 {
64         int i;
65         unsigned int value = 0;
66
67         for (i = start; i < end; ++i)
68                 value |= BIT(i);
69
70         return value;
71 }
72
73 inline float convert_from_vtf_format (int size, int exponent, unsigned int value)
74 {
75         int divider = 1;
76         int i;
77         float sample;
78         float mul = 1.0;
79
80         value = value & set_bit_range(0, size * 8);
81
82         if (value & BIT(size*8-1)) {
83                 value =  ((1LL << (size * 8)) - value);
84                 mul = -1.0;
85         }
86
87         sample = value * 1.0;
88
89         if (exponent < 0) {
90                 exponent = abs(exponent);
91                 for (i = 0; i < exponent; ++i)
92                         divider = divider * 10;
93
94                 return mul * sample/divider;
95         }
96
97         return mul * sample * pow(10.0, exponent);
98 }
99
100 /* Platform sensor orientation */
101 #define DEF_ORIENT_ACCEL_X      -1
102 #define DEF_ORIENT_ACCEL_Y      -1
103 #define DEF_ORIENT_ACCEL_Z      -1
104
105 #define DEF_ORIENT_GYRO_X       1
106 #define DEF_ORIENT_GYRO_Y       1
107 #define DEF_ORIENT_GYRO_Z       1
108
109 /* G to m/s^2 */
110 #define CONVERT_FROM_VTF16(s,d,x)      convert_from_vtf_format(s,d,x)
111 #define CONVERT_A_G_VTF16E14_X(s,d,x)  (DEF_ORIENT_ACCEL_X * convert_from_vtf_format(s,d,x) * GRAVITY)
112 #define CONVERT_A_G_VTF16E14_Y(s,d,x)  (DEF_ORIENT_ACCEL_Y * convert_from_vtf_format(s,d,x) * GRAVITY)
113 #define CONVERT_A_G_VTF16E14_Z(s,d,x)  (DEF_ORIENT_ACCEL_Z * convert_from_vtf_format(s,d,x) * GRAVITY)
114
115 /* Degree/sec to radian/sec */
116 #define CONVERT_G_D_VTF16E14_X(s,d,x)  (DEF_ORIENT_GYRO_X * convert_from_vtf_format(s,d,x) * M_PI / 180)
117 #define CONVERT_G_D_VTF16E14_Y(s,d,x)  (DEF_ORIENT_GYRO_Y * convert_from_vtf_format(s,d,x) * M_PI / 180)
118 #define CONVERT_G_D_VTF16E14_Z(s,d,x)  (DEF_ORIENT_GYRO_Z * convert_from_vtf_format(s,d,x) * M_PI / 180)
119
120 /* Milli gauss to micro tesla */
121 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
122 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
123 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
124
125
126 static int64_t sample_as_int64 (unsigned char* sample, 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                                 return - ((~u64 & value_mask) + 1);     /* Negative value: return 2-complement */
172                         else
173                                 return (int64_t) u64;                   /* Positive value */
174         }
175 }
176
177
178 static void reorder_fields (float* data, unsigned char map[MAX_CHANNELS])
179 {
180         int i;
181         float temp[MAX_CHANNELS];
182
183         for (i=0; i<MAX_CHANNELS; i++)
184                 temp[i] = data[map[i]];
185
186         for (i=0; i<MAX_CHANNELS; i++)
187                 data[i] = temp[i];
188 }
189
190
191 static void clamp_gyro_readings_to_zero (int s, sensors_event_t* data)
192 {
193         float x, y, z;
194         float near_zero;
195
196         x = data->data[0];
197         y = data->data[1];
198         z = data->data[2];
199
200         /* If we're calibrated, don't filter out as much */
201         if (sensor[s].cal_level > 0)
202                 near_zero = 0.02; /* rad/s */
203         else
204                 near_zero = 0.1;
205
206         /* If motion on all axes is small enough */
207         if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
208
209                 /*
210                  * Report that we're not moving at all... but not exactly zero as composite sensors (orientation, rotation vector) don't
211                  * seem to react very well to it.
212                  */
213
214                 data->data[0] *= 0.000001;
215                 data->data[1] *= 0.000001;
216                 data->data[2] *= 0.000001;
217         }
218 }
219
220
221 static void process_event_gyro_uncal (int s, int i, sensors_event_t* data)
222 {
223         gyro_cal_t* gyro_data;
224
225         if (sensor[s].type == SENSOR_TYPE_GYROSCOPE) {
226                 gyro_data = (gyro_cal_t*) sensor[s].cal_data;
227
228                 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
229
230                 sensor[i].sample.type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
231                 sensor[i].sample.sensor = s;
232
233                 sensor[i].sample.data[0] = data->data[0] + gyro_data->bias_x;
234                 sensor[i].sample.data[1] = data->data[1] + gyro_data->bias_y;
235                 sensor[i].sample.data[2] = data->data[2] + gyro_data->bias_z;
236
237                 sensor[i].sample.uncalibrated_gyro.bias[0] = gyro_data->bias_x;
238                 sensor[i].sample.uncalibrated_gyro.bias[1] = gyro_data->bias_y;
239                 sensor[i].sample.uncalibrated_gyro.bias[2] = gyro_data->bias_z;
240
241                 sensor[i].report_pending = 1;
242         }
243 }
244
245 static void process_event_magn_uncal (int s, int i, sensors_event_t* data)
246 {
247         compass_cal_t* magn_data;
248
249         if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD) {
250                 magn_data = (compass_cal_t*) sensor[s].cal_data;
251
252                 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
253
254                 sensor[i].sample.type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
255                 sensor[i].sample.sensor = s;
256
257                 sensor[i].sample.data[0] = data->data[0] + magn_data->offset[0][0];
258                 sensor[i].sample.data[1] = data->data[1] + magn_data->offset[1][0];
259                 sensor[i].sample.data[2] = data->data[2] + magn_data->offset[2][0];
260
261                 sensor[i].sample.uncalibrated_magnetic.bias[0] = magn_data->offset[0][0];
262                 sensor[i].sample.uncalibrated_magnetic.bias[1] = magn_data->offset[1][0];
263                 sensor[i].sample.uncalibrated_magnetic.bias[2] = magn_data->offset[2][0];
264
265                 sensor[i].report_pending = 1;
266         }
267 }
268
269 static void process_event (int s, sensors_event_t* data)
270 {
271         /*
272          * This gets the real event (post process - calibration, filtering & co.) and makes it into a virtual one.
273          * The specific processing function for each sensor will populate the necessary fields and set up the report pending flag.
274          */
275
276          int i;
277
278          /* Go through out virtual sensors and check if we can use this event */
279          for (i = 0; i < sensor_count; i++)
280                 switch (sensor[i].type) {
281                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
282                                 process_event_gyro_uncal(s, i, data);
283                                 break;
284                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
285                                 process_event_magn_uncal(s, i, data);
286                                 break;
287                         default:
288                                 break;
289                 }
290 }
291
292
293 static int finalize_sample_default (int s, sensors_event_t* data)
294 {
295         /* Swap fields if we have a custom channel ordering on this sensor */
296         if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
297                 reorder_fields(data->data, sensor[s].order);
298
299         sensor[s].event_count++;
300
301         switch (sensor[s].type) {
302                 case SENSOR_TYPE_ACCELEROMETER:
303                         /* Always consider the accelerometer accurate */
304                         data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
305                         if (sensor[s].quirks & QUIRK_BIASED)
306                                 calibrate_accel(s, data);
307                         denoise(s, data);
308                         break;
309
310                 case SENSOR_TYPE_MAGNETIC_FIELD:
311                         calibrate_compass (s, data);
312                         denoise(s, data);
313                         break;
314
315                 case SENSOR_TYPE_GYROSCOPE:
316
317                         /* Report medium accuracy by default ; higher accuracy levels will be reported once, and if, we achieve  calibration. */
318                         data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
319
320                         /*
321                          * We're only trying to calibrate data from continuously firing gyroscope drivers, as motion based ones use
322                          * movement thresholds that may lead us to incorrectly estimate bias.
323                          */
324                         if (sensor[s].selected_trigger !=
325                                 sensor[s].motion_trigger_name)
326                                         calibrate_gyro(s, data);
327
328                         /*
329                          * For noisy sensors drop a few samples to make sure we have at least GYRO_MIN_SAMPLES events in the
330                          * filtering queue. This improves mean and std dev.
331                          */
332                         if (sensor[s].filter_type) {
333                                 if (sensor[s].selected_trigger !=
334                                     sensor[s].motion_trigger_name &&
335                                     sensor[s].event_count < GYRO_MIN_SAMPLES)
336                                                 return 0;
337
338                                 denoise(s, data);
339                         }
340
341                         /* Clamp near zero moves to (0,0,0) if appropriate */
342                         clamp_gyro_readings_to_zero(s, data);
343                         break;
344
345                 case SENSOR_TYPE_PROXIMITY:
346                         /*
347                          * See iio spec for in_proximity* - depending on the device
348                          * this value is either in meters either unit-less and cannot
349                          * be translated to SI units. Where the translation is not possible
350                          * lower values indicate something is close and higher ones indicate distance.
351                          */
352                         if (data->data[0] > PROXIMITY_THRESHOLD)
353                                 data->data[0] = PROXIMITY_THRESHOLD;
354
355                         /* ... fall through ... */
356                 case SENSOR_TYPE_LIGHT:
357                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
358                 case SENSOR_TYPE_TEMPERATURE:
359                 case SENSOR_TYPE_INTERNAL_ILLUMINANCE:
360                 case SENSOR_TYPE_INTERNAL_INTENSITY:
361                         /* Only keep two decimals for these readings */
362                         data->data[0] = 0.01 * ((int) (data->data[0] * 100));
363
364                         /* These are on change sensors ; drop the sample if it has the same value as the previously reported one. */
365                         if (data->data[0] == sensor[s].prev_val.data)
366                                 return 0;
367
368                         sensor[s].prev_val.data = data->data[0];
369                         break;
370                 case SENSOR_TYPE_STEP_COUNTER:
371                         if (data->u64.step_counter == sensor[s].prev_val.data64)
372                                 return 0;
373                         sensor[s].prev_val.data64 = data->u64.data[0];
374                         break;
375
376         }
377
378         /* If there are active virtual sensors depending on this one - process the event */
379         if (sensor[s].ref_count)
380                 process_event(s, data);
381
382
383         return 1; /* Return sample to Android */
384 }
385
386
387 static float transform_sample_default (int s, int c, unsigned char* sample_data)
388 {
389         datum_info_t* sample_type = &sensor[s].channel[c].type_info;
390         int64_t s64 = sample_as_int64(sample_data, sample_type);
391         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
392
393         /* In case correction has been requested using properties, apply it */
394         scale *= sensor[s].channel[c].opt_scale;
395
396         /* Apply default scaling rules */
397         return (sensor[s].offset + s64) * scale;
398 }
399
400
401 static int finalize_sample_ISH (int s, sensors_event_t* data)
402 {
403         float pitch, roll, yaw;
404
405         /* Swap fields if we have a custom channel ordering on this sensor */
406         if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
407                 reorder_fields(data->data, sensor[s].order);
408
409         if (sensor[s].type == SENSOR_TYPE_ORIENTATION) {
410
411                 pitch = data->data[0];
412                 roll = data->data[1];
413                 yaw = data->data[2];
414
415                 data->data[0] = 360.0 - yaw;
416                 data->data[1] = -pitch;
417                 data->data[2] = -roll;
418         }
419
420         /* Add this event to our global records, for filtering purposes */
421         record_sample(s, data);
422
423         return 1; /* Return sample to Android */
424 }
425
426
427 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
428 {
429         datum_info_t* sample_type = &sensor[s].channel[c].type_info;
430         int val         = (int) sample_as_int64(sample_data, sample_type);
431         float correction;
432         int data_bytes  = (sample_type->realbits)/8;
433         int exponent    = sensor[s].offset;
434
435         /* In case correction has been requested using properties, apply it */
436         correction = sensor[s].channel[c].opt_scale;
437
438         switch (sensor_desc[s].type) {
439                 case SENSOR_TYPE_ACCELEROMETER:
440                         switch (c) {
441                                 case 0:
442                                         return correction * CONVERT_A_G_VTF16E14_X(data_bytes, exponent, val);
443
444                                 case 1:
445                                         return correction * CONVERT_A_G_VTF16E14_Y(data_bytes, exponent, val);
446
447                                 case 2:
448                                         return  correction * CONVERT_A_G_VTF16E14_Z(data_bytes, exponent, val);
449                         }
450                         break;
451
452                 case SENSOR_TYPE_GYROSCOPE:
453                         switch (c) {
454                                 case 0:
455                                         return correction * CONVERT_G_D_VTF16E14_X(data_bytes, exponent, val);
456
457                                 case 1:
458                                         return correction * CONVERT_G_D_VTF16E14_Y(data_bytes, exponent, val);
459
460                                 case 2:
461                                         return  correction * CONVERT_G_D_VTF16E14_Z(data_bytes, exponent, val);
462                         }
463                         break;
464
465                 case SENSOR_TYPE_MAGNETIC_FIELD:
466                         switch (c) {
467                                 case 0:
468                                         return correction * CONVERT_M_MG_VTF16E14_X(data_bytes, exponent, val);
469
470                                 case 1:
471                                         return correction * CONVERT_M_MG_VTF16E14_Y(data_bytes, exponent, val);
472
473                                 case 2:
474                                         return correction * CONVERT_M_MG_VTF16E14_Z(data_bytes, exponent, val);
475                         }
476                         break;
477
478                 case SENSOR_TYPE_LIGHT:
479                         return (float) val;
480
481                 case SENSOR_TYPE_ORIENTATION:
482                         return correction * convert_from_vtf_format(data_bytes, exponent, val);
483
484                 case SENSOR_TYPE_ROTATION_VECTOR:
485                         return correction * convert_from_vtf_format(data_bytes, exponent, val);
486         }
487
488         return 0;
489 }
490
491
492 void select_transform (int s)
493 {
494         char prop_name[PROP_NAME_MAX];
495         char prop_val[PROP_VALUE_MAX];
496         int i                   = sensor[s].catalog_index;
497         const char *prefix      = sensor_catalog[i].tag;
498
499         sprintf(prop_name, PROP_BASE, prefix, "transform");
500
501         if (property_get(prop_name, prop_val, ""))
502                 if (!strcmp(prop_val, "ISH")) {
503                         ALOGI(  "Using Intel Sensor Hub semantics on %s\n", sensor[s].friendly_name);
504
505                         sensor[s].ops.transform = transform_sample_ISH;
506                         sensor[s].ops.finalize = finalize_sample_ISH;
507                         return;
508                 }
509
510         sensor[s].ops.transform = transform_sample_default;
511         sensor[s].ops.finalize = finalize_sample_default;
512 }
513
514
515 float acquire_immediate_float_value (int s, int c)
516 {
517         char sysfs_path[PATH_MAX];
518         float val;
519         int ret;
520         int dev_num = sensor[s].dev_num;
521         int i = sensor[s].catalog_index;
522         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
523         const char* input_path = sensor_catalog[i].channel[c].input_path;
524         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
525         float offset = sensor[s].offset;
526         int sensor_type = sensor_catalog[i].type;
527         float correction;
528
529         /* In case correction has been requested using properties, apply it */
530         correction = sensor[s].channel[c].opt_scale;
531
532         /* Acquire a sample value for sensor s / channel c through sysfs */
533
534         if (sensor[s].channel[c].input_path_present) {
535                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
536                 ret = sysfs_read_float(sysfs_path, &val);
537
538                 if (!ret)
539                         return val * correction;
540         }
541
542         if (!sensor[s].channel[c].raw_path_present)
543                 return 0;
544
545         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
546         ret = sysfs_read_float(sysfs_path, &val);
547
548         if (ret == -1)
549                 return 0;
550
551         /*
552          * There is no transform ops defined yet for raw sysfs values.
553          * Use this function to perform transformation as well.
554          */
555         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
556                 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) * correction;
557
558         return (val + offset) * scale * correction;
559 }
560
561 uint64_t acquire_immediate_uint64_value (int s, int c)
562 {
563         char sysfs_path[PATH_MAX];
564         uint64_t val;
565         int ret;
566         int dev_num = sensor[s].dev_num;
567         int i = sensor[s].catalog_index;
568         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
569         const char* input_path = sensor_catalog[i].channel[c].input_path;
570         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
571         float offset = sensor[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[s].channel[c].opt_scale;
577
578         /* Acquire a sample value for sensor s / channel c through sysfs */
579
580         if (sensor[s].channel[c].input_path_present) {
581                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
582                 ret = sysfs_read_uint64(sysfs_path, &val);
583
584                 if (!ret)
585                         return val * correction;
586         };
587
588         if (!sensor[s].channel[c].raw_path_present)
589                 return 0;
590
591         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
592         ret = sysfs_read_uint64(sysfs_path, &val);
593
594         if (ret == -1)
595                 return 0;
596
597         return (val + offset) * scale * correction;
598 }