OSDN Git Service

GMIN-3015: Initialize accuracy flag on accel and gyro samples
[android-x86/hardware-intel-libsensors.git] / transform.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <math.h>
7 #include <utils/Log.h>
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
10 #include "calibration.h"
11 #include "common.h"
12 #include "description.h"
13 #include "transform.h"
14 #include "utils.h"
15
16 /*----------------------------------------------------------------------------*/
17
18 /* Macros related to Intel Sensor Hub */
19
20 #define GRAVITY 9.80665f
21
22 /* 720 LSG = 1G */
23 #define LSG                         (1024.0f)
24 #define NUMOFACCDATA                (8.0f)
25
26 /* conversion of acceleration data to SI units (m/s^2) */
27 #define CONVERT_A                   (GRAVITY_EARTH / LSG / NUMOFACCDATA)
28 #define CONVERT_A_X(x)              ((float(x)/1000) * (GRAVITY * -1.0))
29 #define CONVERT_A_Y(x)              ((float(x)/1000) * (GRAVITY * 1.0))
30 #define CONVERT_A_Z(x)              ((float(x)/1000) * (GRAVITY * 1.0))
31
32 /* conversion of magnetic data to uT units */
33 #define CONVERT_M                   (1.0f/6.6f)
34 #define CONVERT_M_X                 (-CONVERT_M)
35 #define CONVERT_M_Y                 (-CONVERT_M)
36 #define CONVERT_M_Z                 (CONVERT_M)
37
38 #define CONVERT_GAUSS_TO_MICROTESLA(x)        ( (x) * 100 )
39
40 /* conversion of orientation data to degree units */
41 #define CONVERT_O                   (1.0f/64.0f)
42 #define CONVERT_O_A                 (CONVERT_O)
43 #define CONVERT_O_P                 (CONVERT_O)
44 #define CONVERT_O_R                 (-CONVERT_O)
45
46 /*conversion of gyro data to SI units (radian/sec) */
47 #define CONVERT_GYRO                ((2000.0f/32767.0f)*((float)M_PI / 180.0f))
48 #define CONVERT_GYRO_X              (-CONVERT_GYRO)
49 #define CONVERT_GYRO_Y              (-CONVERT_GYRO)
50 #define CONVERT_GYRO_Z              (CONVERT_GYRO)
51
52 #define BIT(x) (1 << (x))
53
54 inline unsigned int set_bit_range(int start, int end)
55 {
56     int i;
57     unsigned int value = 0;
58
59     for (i = start; i < end; ++i)
60         value |= BIT(i);
61     return value;
62 }
63
64 inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
65 {
66     int divider=1;
67     int i;
68     float sample;
69     int mul = 1.0;
70
71     value = value & set_bit_range(0, size*8);
72     if (value & BIT(size*8-1)) {
73         value =  ((1LL << (size*8)) - value);
74         mul = -1.0;
75     }
76     sample = value * 1.0;
77     if (exponent < 0) {
78         exponent = abs(exponent);
79         for (i = 0; i < exponent; ++i) {
80             divider = divider*10;
81         }
82         return mul * sample/divider;
83     } else {
84         return mul * sample * pow(10.0, exponent);
85     }
86 }
87
88 // Platform sensor orientation
89 #define DEF_ORIENT_ACCEL_X                   -1
90 #define DEF_ORIENT_ACCEL_Y                   -1
91 #define DEF_ORIENT_ACCEL_Z                   -1
92
93 #define DEF_ORIENT_GYRO_X                   1
94 #define DEF_ORIENT_GYRO_Y                   1
95 #define DEF_ORIENT_GYRO_Z                   1
96
97 // G to m/s2
98 #define CONVERT_FROM_VTF16(s,d,x)      (convert_from_vtf_format(s,d,x))
99 #define CONVERT_A_G_VTF16E14_X(s,d,x)  (DEF_ORIENT_ACCEL_X *\
100                                         convert_from_vtf_format(s,d,x)*GRAVITY)
101 #define CONVERT_A_G_VTF16E14_Y(s,d,x)  (DEF_ORIENT_ACCEL_Y *\
102                                         convert_from_vtf_format(s,d,x)*GRAVITY)
103 #define CONVERT_A_G_VTF16E14_Z(s,d,x)  (DEF_ORIENT_ACCEL_Z *\
104                                         convert_from_vtf_format(s,d,x)*GRAVITY)
105
106 // Degree/sec to radian/sec
107 #define CONVERT_G_D_VTF16E14_X(s,d,x)  (DEF_ORIENT_GYRO_X *\
108                                         convert_from_vtf_format(s,d,x) * \
109                                         ((float)M_PI/180.0f))
110 #define CONVERT_G_D_VTF16E14_Y(s,d,x)  (DEF_ORIENT_GYRO_Y *\
111                                         convert_from_vtf_format(s,d,x) * \
112                                         ((float)M_PI/180.0f))
113 #define CONVERT_G_D_VTF16E14_Z(s,d,x)  (DEF_ORIENT_GYRO_Z *\
114                                         convert_from_vtf_format(s,d,x) * \
115                                         ((float)M_PI/180.0f))
116
117 // Milli gauss to micro tesla
118 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x)/10)
119 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x)/10)
120 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x)/10)
121
122
123 /*----------------------------------------------------------------------------*/
124
125 static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
126 {
127         uint64_t u64;
128         int i;
129         int zeroed_bits = type->storagebits - type->realbits;
130         uint64_t sign_mask;
131         uint64_t value_mask;
132
133         u64 = 0;
134
135         if (type->endianness == 'b')
136                 for (i=0; i<type->storagebits/8; i++)
137                         u64 = (u64 << 8) | sample[i];
138         else
139                 for (i=type->storagebits/8 - 1; i>=0; i--)
140                         u64 = (u64 << 8) | sample[i];
141
142         u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
143
144         if (type->sign == 'u')
145                 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
146
147         /* Signed integer */
148
149         switch (type->realbits) {
150                 case 0 ... 1:
151                         return 0;
152
153                 case 8:
154                         return (int64_t) (int8_t) u64;
155
156                 case 16:
157                         return (int64_t) (int16_t) u64;
158
159                 case 32:
160                         return (int64_t) (int32_t) u64;
161
162                 case 64:
163                         return (int64_t) u64;
164
165                 default:
166                         sign_mask = 1 << (type->realbits-1);
167                         value_mask = sign_mask - 1;
168
169                         if (u64 & sign_mask)
170                                 /* Negative value: return 2-complement */
171                                 return - ((~u64 & value_mask) + 1);
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 denoise (struct sensor_info_t* si, struct sensors_event_t* data,
192                      int num_fields, int max_samples)
193 {
194         /*
195          * Smooth out incoming data using a moving average over a number of
196          * samples. We accumulate one second worth of samples, or max_samples,
197          * depending on which is lower.
198          */
199
200         int i;
201         float total;
202         int f;
203         int sampling_rate = (int) si->sampling_rate;
204         int history_size;
205
206         /* Don't denoise anything if we have less than two samples per second */
207         if (sampling_rate < 2)
208                 return;
209
210         /* Restrict window size to the min of sampling_rate and max_samples */
211         if (sampling_rate > max_samples)
212                 history_size = max_samples;
213         else
214                 history_size = sampling_rate;
215
216         /* Reset history if we're operating on an incorrect window size */
217         if (si->history_size != history_size) {
218                 si->history_size = history_size;
219                 si->history_entries = 0;
220                 si->history_index = 0;
221                 si->history = (float*) realloc(si->history,
222                                 si->history_size * num_fields * sizeof(float));
223         }
224
225         if (!si->history)
226                 return; /* Unlikely, but still... */
227
228         /* Update initialized samples count */
229         if (si->history_entries < si->history_size)
230                 si->history_entries++;
231
232         /* Record new sample */
233         for (f=0; f < num_fields; f++)
234                 si->history[si->history_index * num_fields + f] = data->data[f];
235
236         /* Update our rolling index (next evicted cell) */
237         si->history_index = (si->history_index + 1) % si->history_size;
238
239         /* For now simply compute a mobile mean for each field */
240         for (f=0; f < num_fields; f++) {
241                 total = 0;
242
243                 for (i=0; i < si->history_entries; i++)
244                                 total += si->history[i * num_fields + f];
245
246                 /* Output filtered data */
247                 data->data[f] = total / si->history_entries;
248         }
249 }
250
251
252 static int finalize_sample_default(int s, struct sensors_event_t* data)
253 {
254         int i           = sensor_info[s].catalog_index;
255         int sensor_type = sensor_catalog[i].type;
256
257         /* Swap fields if we have a custom channel ordering on this sensor */
258         if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
259                 reorder_fields(data->data, sensor_info[s].order);
260
261         switch (sensor_type) {
262                 case SENSOR_TYPE_ACCELEROMETER:
263                         /* Always consider the accelerometer accurate */
264                         data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
265                         if (sensor_info[s].quirks & QUIRK_NOISY)
266                                 denoise(&sensor_info[s], data, 3, 20);
267                         break;
268
269                 case SENSOR_TYPE_MAGNETIC_FIELD:
270                         calibrate_compass (data, &sensor_info[s], get_timestamp());
271                         if (sensor_info[s].quirks & QUIRK_NOISY)
272                                 denoise(&sensor_info[s], data, 3, 100);
273                         break;
274
275                 case SENSOR_TYPE_GYROSCOPE:
276                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
277                         calibrate_gyro(data, &sensor_info[s]);
278                         if (sensor_info[s].quirks & QUIRK_NOISY)
279                                 denoise(&sensor_info[s], data, 3, 20);
280                         break;
281
282                 case SENSOR_TYPE_LIGHT:
283                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
284                 case SENSOR_TYPE_TEMPERATURE:
285                         /* Only keep two decimals for these readings */
286                         data->data[0] = 0.01 * ((int) (data->data[0] * 100));
287
288                         /* ... fall through ... */
289
290                 case SENSOR_TYPE_PROXIMITY:
291                         /*
292                          * These are on change sensors ; drop the sample if it
293                          * has the same value as the previously reported one.
294                          */
295                         if (data->data[0] == sensor_info[s].prev_val)
296                                 return 0;
297
298                         sensor_info[s].prev_val = data->data[0];
299                         break;
300         }
301
302         return 1; /* Return sample to Android */
303 }
304
305
306 static float transform_sample_default(int s, int c, unsigned char* sample_data)
307 {
308         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
309         int64_t              s64 = sample_as_int64(sample_data, sample_type);
310         float scale = sensor_info[s].scale ?
311                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
312
313         /* In case correction has been requested using properties, apply it */
314         scale *= sensor_info[s].channel[c].opt_scale;
315
316         /* Apply default scaling rules */
317         return (sensor_info[s].offset + s64) * scale;
318 }
319
320
321 static int finalize_sample_ISH(int s, struct sensors_event_t* data)
322 {
323         int i           = sensor_info[s].catalog_index;
324         int sensor_type = sensor_catalog[i].type;
325         float pitch, roll, yaw;
326
327         /* Swap fields if we have a custom channel ordering on this sensor */
328         if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
329                 reorder_fields(data->data, sensor_info[s].order);
330
331         if (sensor_type == SENSOR_TYPE_ORIENTATION) {
332
333                 pitch = data->data[0];
334                 roll = data->data[1];
335                 yaw = data->data[2];
336
337                 data->data[0] = 360.0 - yaw;
338                 data->data[1] = -pitch;
339                 data->data[2] = -roll;
340         }
341
342         return 1; /* Return sample to Android */
343 }
344
345
346 static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
347 {
348         struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
349         int val         = (int) sample_as_int64(sample_data, sample_type);
350         int i           = sensor_info[s].catalog_index;
351         int sensor_type = sensor_catalog[i].type;
352         float correction;
353         int data_bytes  = (sample_type->realbits)/8;
354         int exponent    = sensor_info[s].offset;
355
356         /* In case correction has been requested using properties, apply it */
357         correction = sensor_info[s].channel[c].opt_scale;
358
359         switch (sensor_type) {
360                 case SENSOR_TYPE_ACCELEROMETER:
361                         switch (c) {
362                                 case 0:
363                                         return  correction *
364                                                 CONVERT_A_G_VTF16E14_X(
365                                                 data_bytes, exponent, val);
366
367                                 case 1:
368                                         return  correction *
369                                                 CONVERT_A_G_VTF16E14_Y(
370                                                 data_bytes, exponent, val);
371
372                                 case 2:
373                                         return  correction *
374                                                 CONVERT_A_G_VTF16E14_Z(
375                                                 data_bytes, exponent, val);
376                         }
377                         break;
378
379
380                 case SENSOR_TYPE_GYROSCOPE:
381                         switch (c) {
382                                 case 0:
383                                         return  correction *
384                                                 CONVERT_G_D_VTF16E14_X(
385                                                 data_bytes, exponent, val);
386
387                                 case 1:
388                                         return  correction *
389                                                 CONVERT_G_D_VTF16E14_Y(
390                                                 data_bytes, exponent, val);
391
392                                 case 2:
393                                         return  correction *
394                                                 CONVERT_G_D_VTF16E14_Z(
395                                                 data_bytes, exponent, val);
396                         }
397                         break;
398
399                 case SENSOR_TYPE_MAGNETIC_FIELD:
400                         switch (c) {
401                                 case 0:
402                                         return  correction *
403                                                 CONVERT_M_MG_VTF16E14_X(
404                                                 data_bytes, exponent, val);
405
406                                 case 1:
407                                         return  correction *
408                                                 CONVERT_M_MG_VTF16E14_Y(
409                                                 data_bytes, exponent, val);
410
411                                 case 2:
412                                         return  correction *
413                                                 CONVERT_M_MG_VTF16E14_Z(
414                                                 data_bytes, exponent, val);
415                         }
416                         break;
417
418                 case SENSOR_TYPE_LIGHT:
419                                 return (float) val;
420
421                 case SENSOR_TYPE_ORIENTATION:
422                         return  correction * convert_from_vtf_format(
423                                                 data_bytes, exponent, val);
424
425                 case SENSOR_TYPE_ROTATION_VECTOR:
426                         return  correction * convert_from_vtf_format(
427                                                 data_bytes, exponent, val);
428         }
429
430         return 0;
431 }
432
433
434 void select_transform (int s)
435 {
436         char prop_name[PROP_NAME_MAX];
437         char prop_val[PROP_VALUE_MAX];
438         int i                   = sensor_info[s].catalog_index;
439         const char *prefix      = sensor_catalog[i].tag;
440
441         sprintf(prop_name, PROP_BASE, prefix, "transform");
442
443         if (property_get(prop_name, prop_val, "")) {
444                 if (!strcmp(prop_val, "ISH")) {
445                         ALOGI(  "Using Intel Sensor Hub semantics on %s\n",
446                                 sensor_info[s].friendly_name);
447
448                         sensor_info[s].ops.transform = transform_sample_ISH;
449                         sensor_info[s].ops.finalize = finalize_sample_ISH;
450                         return;
451                 }
452         }
453
454         sensor_info[s].ops.transform = transform_sample_default;
455         sensor_info[s].ops.finalize = finalize_sample_default;
456 }
457
458
459 float acquire_immediate_value(int s, int c)
460 {
461         char sysfs_path[PATH_MAX];
462         float val;
463         int ret;
464         int dev_num = sensor_info[s].dev_num;
465         int i = sensor_info[s].catalog_index;
466         const char* raw_path = sensor_catalog[i].channel[c].raw_path;
467         const char* input_path = sensor_catalog[i].channel[c].input_path;
468         float scale = sensor_info[s].scale ?
469                         sensor_info[s].scale : sensor_info[s].channel[c].scale;
470         float offset = sensor_info[s].offset;
471         int sensor_type = sensor_catalog[i].type;
472         float correction;
473
474         /* In case correction has been requested using properties, apply it */
475         correction = sensor_info[s].channel[c].opt_scale;
476
477         /* Acquire a sample value for sensor s / channel c through sysfs */
478
479         if (input_path[0]) {
480                 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
481                 ret = sysfs_read_float(sysfs_path, &val);
482
483                 if (!ret) {
484                         return val * correction;
485                 }
486         };
487
488         if (!raw_path[0])
489                 return 0;
490
491         sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
492         ret = sysfs_read_float(sysfs_path, &val);
493
494         if (ret == -1)
495                 return 0;
496
497         /*
498         There is no transform ops defined yet for Raw sysfs values
499         Use this function to perform transformation as well.
500         */
501         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
502                 return  CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
503                         correction;
504
505         return (val + offset) * scale * correction;
506 }