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