2 * Copyright (C) 2014 Intel Corporation.
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
10 #include "calibration.h"
12 #include "description.h"
13 #include "transform.h"
15 #include "filtering.h"
17 /*----------------------------------------------------------------------------*/
19 /* Macros related to Intel Sensor Hub */
21 #define GRAVITY 9.80665f
25 #define NUMOFACCDATA (8.0f)
27 /* conversion of acceleration data to SI units (m/s^2) */
28 #define CONVERT_A (GRAVITY_EARTH / LSG / NUMOFACCDATA)
29 #define CONVERT_A_X(x) ((float(x)/1000) * (GRAVITY * -1.0))
30 #define CONVERT_A_Y(x) ((float(x)/1000) * (GRAVITY * 1.0))
31 #define CONVERT_A_Z(x) ((float(x)/1000) * (GRAVITY * 1.0))
33 /* conversion of magnetic data to uT units */
34 #define CONVERT_M (1.0f/6.6f)
35 #define CONVERT_M_X (-CONVERT_M)
36 #define CONVERT_M_Y (-CONVERT_M)
37 #define CONVERT_M_Z (CONVERT_M)
39 #define CONVERT_GAUSS_TO_MICROTESLA(x) ( (x) * 100 )
41 /* conversion of orientation data to degree units */
42 #define CONVERT_O (1.0f/64.0f)
43 #define CONVERT_O_A (CONVERT_O)
44 #define CONVERT_O_P (CONVERT_O)
45 #define CONVERT_O_R (-CONVERT_O)
47 /*conversion of gyro data to SI units (radian/sec) */
48 #define CONVERT_GYRO ((2000.0f/32767.0f)*((float)M_PI / 180.0f))
49 #define CONVERT_GYRO_X (-CONVERT_GYRO)
50 #define CONVERT_GYRO_Y (-CONVERT_GYRO)
51 #define CONVERT_GYRO_Z (CONVERT_GYRO)
53 #define BIT(x) (1 << (x))
55 inline unsigned int set_bit_range(int start, int end)
58 unsigned int value = 0;
60 for (i = start; i < end; ++i)
65 inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
72 value = value & set_bit_range(0, size*8);
73 if (value & BIT(size*8-1)) {
74 value = ((1LL << (size*8)) - value);
79 exponent = abs(exponent);
80 for (i = 0; i < exponent; ++i) {
83 return mul * sample/divider;
85 return mul * sample * pow(10.0, exponent);
89 // Platform sensor orientation
90 #define DEF_ORIENT_ACCEL_X -1
91 #define DEF_ORIENT_ACCEL_Y -1
92 #define DEF_ORIENT_ACCEL_Z -1
94 #define DEF_ORIENT_GYRO_X 1
95 #define DEF_ORIENT_GYRO_Y 1
96 #define DEF_ORIENT_GYRO_Z 1
99 #define CONVERT_FROM_VTF16(s,d,x) (convert_from_vtf_format(s,d,x))
100 #define CONVERT_A_G_VTF16E14_X(s,d,x) (DEF_ORIENT_ACCEL_X *\
101 convert_from_vtf_format(s,d,x)*GRAVITY)
102 #define CONVERT_A_G_VTF16E14_Y(s,d,x) (DEF_ORIENT_ACCEL_Y *\
103 convert_from_vtf_format(s,d,x)*GRAVITY)
104 #define CONVERT_A_G_VTF16E14_Z(s,d,x) (DEF_ORIENT_ACCEL_Z *\
105 convert_from_vtf_format(s,d,x)*GRAVITY)
107 // Degree/sec to radian/sec
108 #define CONVERT_G_D_VTF16E14_X(s,d,x) (DEF_ORIENT_GYRO_X *\
109 convert_from_vtf_format(s,d,x) * \
110 ((float)M_PI/180.0f))
111 #define CONVERT_G_D_VTF16E14_Y(s,d,x) (DEF_ORIENT_GYRO_Y *\
112 convert_from_vtf_format(s,d,x) * \
113 ((float)M_PI/180.0f))
114 #define CONVERT_G_D_VTF16E14_Z(s,d,x) (DEF_ORIENT_GYRO_Z *\
115 convert_from_vtf_format(s,d,x) * \
116 ((float)M_PI/180.0f))
118 // Milli gauss to micro tesla
119 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x)/10)
120 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x)/10)
121 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x)/10)
124 /*----------------------------------------------------------------------------*/
126 static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
130 int zeroed_bits = type->storagebits - type->realbits;
136 if (type->endianness == 'b')
137 for (i=0; i<type->storagebits/8; i++)
138 u64 = (u64 << 8) | sample[i];
140 for (i=type->storagebits/8 - 1; i>=0; i--)
141 u64 = (u64 << 8) | sample[i];
143 u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
145 if (type->sign == 'u')
146 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
150 switch (type->realbits) {
155 return (int64_t) (int8_t) u64;
158 return (int64_t) (int16_t) u64;
161 return (int64_t) (int32_t) u64;
164 return (int64_t) u64;
167 sign_mask = 1 << (type->realbits-1);
168 value_mask = sign_mask - 1;
171 /* Negative value: return 2-complement */
172 return - ((~u64 & value_mask) + 1);
174 return (int64_t) u64; /* Positive value */
179 static void reorder_fields(float* data, unsigned char map[MAX_CHANNELS])
182 float temp[MAX_CHANNELS];
184 for (i=0; i<MAX_CHANNELS; i++)
185 temp[i] = data[map[i]];
187 for (i=0; i<MAX_CHANNELS; i++)
192 static void clamp_gyro_readings_to_zero (int s, struct sensors_event_t* data)
197 switch (sensor_info[s].type) {
198 case SENSOR_TYPE_GYROSCOPE:
204 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
205 x = data->data[0] - data->uncalibrated_gyro.bias[0];
206 y = data->data[1] - data->uncalibrated_gyro.bias[1];
207 z = data->data[2] - data->uncalibrated_gyro.bias[2];
214 /* If we're calibrated, don't filter out as much */
215 if (sensor_info[s].cal_level > 0)
216 near_zero = 0.02; /* rad/s */
220 /* If motion on all axes is small enough */
221 if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
224 * Report that we're not moving at all... but not exactly zero
225 * as composite sensors (orientation, rotation vector) don't
226 * seem to react very well to it.
228 switch (sensor_info[s].type) {
229 case SENSOR_TYPE_GYROSCOPE:
230 data->data[0] *= 0.000001;
231 data->data[1] *= 0.000001;
232 data->data[2] *= 0.000001;
235 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
236 data->data[0]= data->uncalibrated_gyro.bias[0]
238 data->data[1]= data->uncalibrated_gyro.bias[1]
240 data->data[2]= data->uncalibrated_gyro.bias[2]
248 static int finalize_sample_default (int s, struct sensors_event_t* data)
250 /* Swap fields if we have a custom channel ordering on this sensor */
251 if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
252 reorder_fields(data->data, sensor_info[s].order);
254 sensor_info[s].event_count++;
255 switch (sensor_info[s].type) {
256 case SENSOR_TYPE_ACCELEROMETER:
257 /* Always consider the accelerometer accurate */
258 data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
259 if (sensor_info[s].quirks & QUIRK_NOISY)
260 denoise_average(&sensor_info[s], data, 3, 20);
263 case SENSOR_TYPE_MAGNETIC_FIELD:
264 calibrate_compass (data, &sensor_info[s], get_timestamp());
265 if (sensor_info[s].quirks & QUIRK_NOISY)
266 denoise_average(&sensor_info[s], data, 3, 30);
269 case SENSOR_TYPE_GYROSCOPE:
272 * Report medium accuracy by default ; higher accuracy
273 * levels will be reported once, and if, we achieve
276 data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
278 /* ... fall through */
280 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
283 * We're only trying to calibrate data from continuously
284 * firing gyroscope drivers, as motion based ones use
285 * movement thresholds that may lead us to incorrectly
288 if (sensor_info[s].selected_trigger !=
289 sensor_info[s].motion_trigger_name)
290 calibrate_gyro(data, &sensor_info[s]);
292 /* For noisy sensors we'll drop a very few number
293 * of samples to make sure we have at least MIN_SAMPLES events
294 * in the filtering queue. This is to make sure we are not sending
295 * events that can disturb our mean or stddev.
297 if (sensor_info[s].quirks & QUIRK_NOISY) {
298 denoise_median(&sensor_info[s], data, 3);
299 if((sensor_info[s].selected_trigger !=
300 sensor_info[s].motion_trigger_name) &&
301 sensor_info[s].event_count < MIN_SAMPLES)
305 /* Clamp near zero moves to (0,0,0) if appropriate */
306 clamp_gyro_readings_to_zero(s, data);
309 case SENSOR_TYPE_LIGHT:
310 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
311 case SENSOR_TYPE_TEMPERATURE:
312 /* Only keep two decimals for these readings */
313 data->data[0] = 0.01 * ((int) (data->data[0] * 100));
315 /* ... fall through ... */
317 case SENSOR_TYPE_PROXIMITY:
319 * These are on change sensors ; drop the sample if it
320 * has the same value as the previously reported one.
322 if (data->data[0] == sensor_info[s].prev_val)
325 sensor_info[s].prev_val = data->data[0];
329 /* Add this event to our global records, for filtering purposes */
330 record_sample(s, data);
332 return 1; /* Return sample to Android */
336 static float transform_sample_default(int s, int c, unsigned char* sample_data)
338 struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
339 int64_t s64 = sample_as_int64(sample_data, sample_type);
340 float scale = sensor_info[s].scale ?
341 sensor_info[s].scale : sensor_info[s].channel[c].scale;
343 /* In case correction has been requested using properties, apply it */
344 scale *= sensor_info[s].channel[c].opt_scale;
346 /* Apply default scaling rules */
347 return (sensor_info[s].offset + s64) * scale;
351 static int finalize_sample_ISH (int s, struct sensors_event_t* data)
353 float pitch, roll, yaw;
355 /* Swap fields if we have a custom channel ordering on this sensor */
356 if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
357 reorder_fields(data->data, sensor_info[s].order);
359 if (sensor_info[s].type == SENSOR_TYPE_ORIENTATION) {
361 pitch = data->data[0];
362 roll = data->data[1];
365 data->data[0] = 360.0 - yaw;
366 data->data[1] = -pitch;
367 data->data[2] = -roll;
370 /* Add this event to our global records, for filtering purposes */
371 record_sample(s, data);
373 return 1; /* Return sample to Android */
377 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
379 struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
380 int val = (int) sample_as_int64(sample_data, sample_type);
382 int data_bytes = (sample_type->realbits)/8;
383 int exponent = sensor_info[s].offset;
385 /* In case correction has been requested using properties, apply it */
386 correction = sensor_info[s].channel[c].opt_scale;
388 switch (sensor_info[s].type) {
389 case SENSOR_TYPE_ACCELEROMETER:
393 CONVERT_A_G_VTF16E14_X(
394 data_bytes, exponent, val);
398 CONVERT_A_G_VTF16E14_Y(
399 data_bytes, exponent, val);
403 CONVERT_A_G_VTF16E14_Z(
404 data_bytes, exponent, val);
409 case SENSOR_TYPE_GYROSCOPE:
413 CONVERT_G_D_VTF16E14_X(
414 data_bytes, exponent, val);
418 CONVERT_G_D_VTF16E14_Y(
419 data_bytes, exponent, val);
423 CONVERT_G_D_VTF16E14_Z(
424 data_bytes, exponent, val);
428 case SENSOR_TYPE_MAGNETIC_FIELD:
432 CONVERT_M_MG_VTF16E14_X(
433 data_bytes, exponent, val);
437 CONVERT_M_MG_VTF16E14_Y(
438 data_bytes, exponent, val);
442 CONVERT_M_MG_VTF16E14_Z(
443 data_bytes, exponent, val);
447 case SENSOR_TYPE_LIGHT:
450 case SENSOR_TYPE_ORIENTATION:
451 return correction * convert_from_vtf_format(
452 data_bytes, exponent, val);
454 case SENSOR_TYPE_ROTATION_VECTOR:
455 return correction * convert_from_vtf_format(
456 data_bytes, exponent, val);
463 void select_transform (int s)
465 char prop_name[PROP_NAME_MAX];
466 char prop_val[PROP_VALUE_MAX];
467 int i = sensor_info[s].catalog_index;
468 const char *prefix = sensor_catalog[i].tag;
470 sprintf(prop_name, PROP_BASE, prefix, "transform");
472 if (property_get(prop_name, prop_val, "")) {
473 if (!strcmp(prop_val, "ISH")) {
474 ALOGI( "Using Intel Sensor Hub semantics on %s\n",
475 sensor_info[s].friendly_name);
477 sensor_info[s].ops.transform = transform_sample_ISH;
478 sensor_info[s].ops.finalize = finalize_sample_ISH;
483 sensor_info[s].ops.transform = transform_sample_default;
484 sensor_info[s].ops.finalize = finalize_sample_default;
488 float acquire_immediate_value(int s, int c)
490 char sysfs_path[PATH_MAX];
493 int dev_num = sensor_info[s].dev_num;
494 int i = sensor_info[s].catalog_index;
495 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
496 const char* input_path = sensor_catalog[i].channel[c].input_path;
497 float scale = sensor_info[s].scale ?
498 sensor_info[s].scale : sensor_info[s].channel[c].scale;
499 float offset = sensor_info[s].offset;
500 int sensor_type = sensor_catalog[i].type;
503 /* In case correction has been requested using properties, apply it */
504 correction = sensor_info[s].channel[c].opt_scale;
506 /* Acquire a sample value for sensor s / channel c through sysfs */
509 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
510 ret = sysfs_read_float(sysfs_path, &val);
513 return val * correction;
520 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
521 ret = sysfs_read_float(sysfs_path, &val);
527 There is no transform ops defined yet for Raw sysfs values
528 Use this function to perform transformation as well.
530 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
531 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) *
534 return (val + offset) * scale * correction;