OSDN Git Service

Enable Gauss-2-uT conversion for interrupt mode magneto
[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         float correction = sensor[s].channel[c].opt_scale;
408
409         /* Correlated with "acquire_immediate_value" method */
410         if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
411                 return CONVERT_GAUSS_TO_MICROTESLA((sensor[s].offset + s64) * scale) * correction;
412
413         /* Apply default scaling rules */
414         return (sensor[s].offset + s64) * scale * correction;
415 }
416
417
418 static int finalize_sample_ISH (int s, sensors_event_t* data)
419 {
420         float pitch, roll, yaw;
421
422         /* Swap fields if we have a custom channel ordering on this sensor */
423         if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
424                 reorder_fields(data->data, sensor[s].order);
425
426         if (sensor[s].type == SENSOR_TYPE_ORIENTATION) {
427
428                 pitch = data->data[0];
429                 roll = data->data[1];
430                 yaw = data->data[2];
431
432                 data->data[0] = 360.0 - yaw;
433                 data->data[1] = -pitch;
434                 data->data[2] = -roll;
435         }
436
437         /* Add this event to our global records, for filtering purposes */
438         record_sample(s, data);
439
440         return 1; /* Return sample to Android */
441 }
442
443
444 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
445 {
446         datum_info_t* sample_type = &sensor[s].channel[c].type_info;
447         int val         = (int) sample_as_int64(sample_data, sample_type);
448         float correction;
449         int data_bytes  = (sample_type->realbits)/8;
450         int exponent    = sensor[s].offset;
451
452         /* In case correction has been requested using properties, apply it */
453         correction = sensor[s].channel[c].opt_scale;
454
455         switch (sensor_desc[s].type) {
456                 case SENSOR_TYPE_ACCELEROMETER:
457                         switch (c) {
458                                 case 0:
459                                         return correction * CONVERT_A_G_VTF16E14_X(data_bytes, exponent, val);
460
461                                 case 1:
462                                         return correction * CONVERT_A_G_VTF16E14_Y(data_bytes, exponent, val);
463
464                                 case 2:
465                                         return  correction * CONVERT_A_G_VTF16E14_Z(data_bytes, exponent, val);
466                         }
467                         break;
468
469                 case SENSOR_TYPE_GYROSCOPE:
470                         switch (c) {
471                                 case 0:
472                                         return correction * CONVERT_G_D_VTF16E14_X(data_bytes, exponent, val);
473
474                                 case 1:
475                                         return correction * CONVERT_G_D_VTF16E14_Y(data_bytes, exponent, val);
476
477                                 case 2:
478                                         return  correction * CONVERT_G_D_VTF16E14_Z(data_bytes, exponent, val);
479                         }
480                         break;
481
482                 case SENSOR_TYPE_MAGNETIC_FIELD:
483                         switch (c) {
484                                 case 0:
485                                         return correction * CONVERT_M_MG_VTF16E14_X(data_bytes, exponent, val);
486
487                                 case 1:
488                                         return correction * CONVERT_M_MG_VTF16E14_Y(data_bytes, exponent, val);
489
490                                 case 2:
491                                         return correction * CONVERT_M_MG_VTF16E14_Z(data_bytes, exponent, val);
492                         }
493                         break;
494
495                 case SENSOR_TYPE_LIGHT:
496                         return (float) val;
497
498                 case SENSOR_TYPE_ORIENTATION:
499                         return correction * convert_from_vtf_format(data_bytes, exponent, val);
500
501                 case SENSOR_TYPE_ROTATION_VECTOR:
502                         return correction * convert_from_vtf_format(data_bytes, exponent, val);
503         }
504
505         return 0;
506 }
507
508
509 void select_transform (int s)
510 {
511         char prop_name[PROP_NAME_MAX];
512         char prop_val[PROP_VALUE_MAX];
513         int i                   = sensor[s].catalog_index;
514         const char *prefix      = sensor_catalog[i].tag;
515
516         sprintf(prop_name, PROP_BASE, prefix, "transform");
517
518         if (property_get(prop_name, prop_val, ""))
519                 if (!strcmp(prop_val, "ISH")) {
520                         ALOGI(  "Using Intel Sensor Hub semantics on %s\n", sensor[s].friendly_name);
521
522                         sensor[s].ops.transform = transform_sample_ISH;
523                         sensor[s].ops.finalize = finalize_sample_ISH;
524                         return;
525                 }
526
527         sensor[s].ops.transform = transform_sample_default;
528         sensor[s].ops.finalize = finalize_sample_default;
529 }
530
531
532 float acquire_immediate_float_value (int s, int c)
533 {
534         char sysfs_path[PATH_MAX];
535         float val;
536         int ret;
537         int dev_num = sensor[s].dev_num;
538         int i = sensor[s].catalog_index;
539         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
540         const char* input_path = sensor_catalog[i].channel[c].input_path;
541         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
542         float offset = sensor[s].offset;
543         float correction;
544
545         /* In case correction has been requested using properties, apply it */
546         correction = sensor[s].channel[c].opt_scale;
547
548         /* Acquire a sample value for sensor s / channel c through sysfs */
549
550         if (sensor[s].channel[c].input_path_present) {
551                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
552                 ret = sysfs_read_float(sysfs_path, &val);
553
554                 if (!ret)
555                         return val * correction;
556         }
557
558         if (!sensor[s].channel[c].raw_path_present)
559                 return 0;
560
561         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
562         ret = sysfs_read_float(sysfs_path, &val);
563
564         if (ret == -1)
565                 return 0;
566
567         /*
568          * There is no transform ops defined yet for raw sysfs values.
569          * Use this function to perform transformation as well.
570          */
571         if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
572                 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) * correction;
573
574         return (val + offset) * scale * correction;
575 }
576
577 uint64_t acquire_immediate_uint64_value (int s, int c)
578 {
579         char sysfs_path[PATH_MAX];
580         uint64_t val;
581         int ret;
582         int dev_num = sensor[s].dev_num;
583         int i = sensor[s].catalog_index;
584         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
585         const char* input_path = sensor_catalog[i].channel[c].input_path;
586         float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
587         float offset = sensor[s].offset;
588         int sensor_type = sensor_catalog[i].type;
589         float correction;
590
591         /* In case correction has been requested using properties, apply it */
592         correction = sensor[s].channel[c].opt_scale;
593
594         /* Acquire a sample value for sensor s / channel c through sysfs */
595
596         if (sensor[s].channel[c].input_path_present) {
597                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
598                 ret = sysfs_read_uint64(sysfs_path, &val);
599
600                 if (!ret)
601                         return val * correction;
602         };
603
604         if (!sensor[s].channel[c].raw_path_present)
605                 return 0;
606
607         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
608         ret = sysfs_read_uint64(sysfs_path, &val);
609
610         if (ret == -1)
611                 return 0;
612
613         return (val + offset) * scale * correction;
614 }