2 * Copyright (C) 2014-2015 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"
18 #define GYRO_MIN_SAMPLES 5 /* Drop first few gyro samples after enable */
21 /*----------------------------------------------------------------------------*/
24 /* Macros related to Intel Sensor Hub */
26 #define GRAVITY 9.80665
30 #define NUMOFACCDATA 8.0
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))
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)
44 #define CONVERT_GAUSS_TO_MICROTESLA(x) ((x) * 100)
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)
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)
58 #define BIT(x) (1 << (x))
60 inline unsigned int set_bit_range (int start, int end)
63 unsigned int value = 0;
65 for (i = start; i < end; ++i)
71 inline float convert_from_vtf_format (int size, int exponent, unsigned int value)
78 value = value & set_bit_range(0, size * 8);
80 if (value & BIT(size*8-1)) {
81 value = ((1LL << (size * 8)) - value);
88 exponent = abs(exponent);
89 for (i = 0; i < exponent; ++i)
90 divider = divider * 10;
92 return mul * sample/divider;
95 return mul * sample * pow(10.0, exponent);
98 /* Platform sensor orientation */
99 #define DEF_ORIENT_ACCEL_X -1
100 #define DEF_ORIENT_ACCEL_Y -1
101 #define DEF_ORIENT_ACCEL_Z -1
103 #define DEF_ORIENT_GYRO_X 1
104 #define DEF_ORIENT_GYRO_Y 1
105 #define DEF_ORIENT_GYRO_Z 1
108 #define CONVERT_FROM_VTF16(s,d,x) convert_from_vtf_format(s,d,x)
109 #define CONVERT_A_G_VTF16E14_X(s,d,x) (DEF_ORIENT_ACCEL_X * convert_from_vtf_format(s,d,x) * GRAVITY)
110 #define CONVERT_A_G_VTF16E14_Y(s,d,x) (DEF_ORIENT_ACCEL_Y * convert_from_vtf_format(s,d,x) * GRAVITY)
111 #define CONVERT_A_G_VTF16E14_Z(s,d,x) (DEF_ORIENT_ACCEL_Z * convert_from_vtf_format(s,d,x) * GRAVITY)
113 /* Degree/sec to radian/sec */
114 #define CONVERT_G_D_VTF16E14_X(s,d,x) (DEF_ORIENT_GYRO_X * convert_from_vtf_format(s,d,x) * M_PI / 180)
115 #define CONVERT_G_D_VTF16E14_Y(s,d,x) (DEF_ORIENT_GYRO_Y * convert_from_vtf_format(s,d,x) * M_PI / 180)
116 #define CONVERT_G_D_VTF16E14_Z(s,d,x) (DEF_ORIENT_GYRO_Z * convert_from_vtf_format(s,d,x) * M_PI / 180)
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 static int64_t sample_as_int64 (unsigned char* sample, datum_info_t* type)
128 int zeroed_bits = type->storagebits - type->realbits;
134 if (type->endianness == 'b')
135 for (i=0; i<type->storagebits/8; i++)
136 u64 = (u64 << 8) | sample[i];
138 for (i=type->storagebits/8 - 1; i>=0; i--)
139 u64 = (u64 << 8) | sample[i];
141 u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
143 if (type->sign == 'u')
144 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
148 switch (type->realbits) {
153 return (int64_t) (int8_t) u64;
156 return (int64_t) (int16_t) u64;
159 return (int64_t) (int32_t) u64;
162 return (int64_t) u64;
165 sign_mask = 1 << (type->realbits-1);
166 value_mask = sign_mask - 1;
169 return - ((~u64 & value_mask) + 1); /* Negative value: return 2-complement */
171 return (int64_t) u64; /* Positive value */
176 static void reorder_fields (float* data, unsigned char map[MAX_CHANNELS])
179 float temp[MAX_CHANNELS];
181 for (i=0; i<MAX_CHANNELS; i++)
182 temp[i] = data[map[i]];
184 for (i=0; i<MAX_CHANNELS; i++)
189 static void clamp_gyro_readings_to_zero (int s, sensors_event_t* data)
198 /* If we're calibrated, don't filter out as much */
199 if (sensor[s].cal_level > 0)
200 near_zero = 0.02; /* rad/s */
204 /* If motion on all axes is small enough */
205 if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
208 * Report that we're not moving at all... but not exactly zero as composite sensors (orientation, rotation vector) don't
209 * seem to react very well to it.
212 data->data[0] *= 0.000001;
213 data->data[1] *= 0.000001;
214 data->data[2] *= 0.000001;
219 static void process_event_gyro_uncal (int s, int i, sensors_event_t* data)
221 gyro_cal_t* gyro_data;
223 if (sensor[s].type == SENSOR_TYPE_GYROSCOPE) {
224 gyro_data = (gyro_cal_t*) sensor[s].cal_data;
226 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
228 sensor[i].sample.type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
229 sensor[i].sample.sensor = s;
231 sensor[i].sample.data[0] = data->data[0] + gyro_data->bias_x;
232 sensor[i].sample.data[1] = data->data[1] + gyro_data->bias_y;
233 sensor[i].sample.data[2] = data->data[2] + gyro_data->bias_z;
235 sensor[i].sample.uncalibrated_gyro.bias[0] = gyro_data->bias_x;
236 sensor[i].sample.uncalibrated_gyro.bias[1] = gyro_data->bias_y;
237 sensor[i].sample.uncalibrated_gyro.bias[2] = gyro_data->bias_z;
239 sensor[i].report_pending = 1;
243 static void process_event_magn_uncal (int s, int i, sensors_event_t* data)
245 compass_cal_t* magn_data;
247 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD) {
248 magn_data = (compass_cal_t*) sensor[s].cal_data;
250 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
252 sensor[i].sample.type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
253 sensor[i].sample.sensor = s;
255 sensor[i].sample.data[0] = data->data[0] + magn_data->offset[0][0];
256 sensor[i].sample.data[1] = data->data[1] + magn_data->offset[1][0];
257 sensor[i].sample.data[2] = data->data[2] + magn_data->offset[2][0];
259 sensor[i].sample.uncalibrated_magnetic.bias[0] = magn_data->offset[0][0];
260 sensor[i].sample.uncalibrated_magnetic.bias[1] = magn_data->offset[1][0];
261 sensor[i].sample.uncalibrated_magnetic.bias[2] = magn_data->offset[2][0];
263 sensor[i].report_pending = 1;
267 static void process_event (int s, sensors_event_t* data)
270 * This gets the real event (post process - calibration, filtering & co.) and makes it into a virtual one.
271 * The specific processing function for each sensor will populate the necessary fields and set up the report pending flag.
276 /* Go through out virtual sensors and check if we can use this event */
277 for (i = 0; i < sensor_count; i++)
278 switch (sensor[i].type) {
279 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
280 process_event_gyro_uncal(s, i, data);
282 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
283 process_event_magn_uncal(s, i, data);
291 static int finalize_sample_default (int s, sensors_event_t* data)
293 /* Swap fields if we have a custom channel ordering on this sensor */
294 if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
295 reorder_fields(data->data, sensor[s].order);
297 sensor[s].event_count++;
298 switch (sensor[s].type) {
299 case SENSOR_TYPE_ACCELEROMETER:
300 /* Always consider the accelerometer accurate */
301 data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
302 if (sensor[s].quirks & QUIRK_BIASED)
303 calibrate_accel(s, data);
307 case SENSOR_TYPE_MAGNETIC_FIELD:
308 calibrate_compass (s, data);
312 case SENSOR_TYPE_GYROSCOPE:
314 /* Report medium accuracy by default ; higher accuracy levels will be reported once, and if, we achieve calibration. */
315 data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
318 * We're only trying to calibrate data from continuously firing gyroscope drivers, as motion based ones use
319 * movement thresholds that may lead us to incorrectly estimate bias.
321 if (sensor[s].selected_trigger !=
322 sensor[s].motion_trigger_name)
323 calibrate_gyro(s, data);
326 * For noisy sensors drop a few samples to make sure we have at least GYRO_MIN_SAMPLES events in the
327 * filtering queue. This improves mean and std dev.
329 if (sensor[s].filter_type) {
330 if (sensor[s].selected_trigger !=
331 sensor[s].motion_trigger_name &&
332 sensor[s].event_count < GYRO_MIN_SAMPLES)
338 /* Clamp near zero moves to (0,0,0) if appropriate */
339 clamp_gyro_readings_to_zero(s, data);
342 case SENSOR_TYPE_LIGHT:
343 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
344 case SENSOR_TYPE_TEMPERATURE:
345 /* Only keep two decimals for these readings */
346 data->data[0] = 0.01 * ((int) (data->data[0] * 100));
348 /* ... fall through ... */
350 case SENSOR_TYPE_PROXIMITY:
351 /* These are on change sensors ; drop the sample if it has the same value as the previously reported one. */
352 if (data->data[0] == sensor[s].prev_val.data)
355 sensor[s].prev_val.data = data->data[0];
357 case SENSOR_TYPE_STEP_COUNTER:
358 if (data->u64.step_counter == sensor[s].prev_val.data64)
360 sensor[s].prev_val.data64 = data->u64.data[0];
364 /* If there are active virtual sensors depending on this one - process the event */
365 if (sensor[s].ref_count)
366 process_event(s, data);
369 return 1; /* Return sample to Android */
373 static float transform_sample_default (int s, int c, unsigned char* sample_data)
375 datum_info_t* sample_type = &sensor[s].channel[c].type_info;
376 int64_t s64 = sample_as_int64(sample_data, sample_type);
377 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
379 /* In case correction has been requested using properties, apply it */
380 scale *= sensor[s].channel[c].opt_scale;
382 /* Apply default scaling rules */
383 return (sensor[s].offset + s64) * scale;
387 static int finalize_sample_ISH (int s, sensors_event_t* data)
389 float pitch, roll, yaw;
391 /* Swap fields if we have a custom channel ordering on this sensor */
392 if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
393 reorder_fields(data->data, sensor[s].order);
395 if (sensor[s].type == SENSOR_TYPE_ORIENTATION) {
397 pitch = data->data[0];
398 roll = data->data[1];
401 data->data[0] = 360.0 - yaw;
402 data->data[1] = -pitch;
403 data->data[2] = -roll;
406 /* Add this event to our global records, for filtering purposes */
407 record_sample(s, data);
409 return 1; /* Return sample to Android */
413 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
415 datum_info_t* sample_type = &sensor[s].channel[c].type_info;
416 int val = (int) sample_as_int64(sample_data, sample_type);
418 int data_bytes = (sample_type->realbits)/8;
419 int exponent = sensor[s].offset;
421 /* In case correction has been requested using properties, apply it */
422 correction = sensor[s].channel[c].opt_scale;
424 switch (sensor[s].type) {
425 case SENSOR_TYPE_ACCELEROMETER:
428 return correction * CONVERT_A_G_VTF16E14_X(data_bytes, exponent, val);
431 return correction * CONVERT_A_G_VTF16E14_Y(data_bytes, exponent, val);
434 return correction * CONVERT_A_G_VTF16E14_Z(data_bytes, exponent, val);
438 case SENSOR_TYPE_GYROSCOPE:
441 return correction * CONVERT_G_D_VTF16E14_X(data_bytes, exponent, val);
444 return correction * CONVERT_G_D_VTF16E14_Y(data_bytes, exponent, val);
447 return correction * CONVERT_G_D_VTF16E14_Z(data_bytes, exponent, val);
451 case SENSOR_TYPE_MAGNETIC_FIELD:
454 return correction * CONVERT_M_MG_VTF16E14_X(data_bytes, exponent, val);
457 return correction * CONVERT_M_MG_VTF16E14_Y(data_bytes, exponent, val);
460 return correction * CONVERT_M_MG_VTF16E14_Z(data_bytes, exponent, val);
464 case SENSOR_TYPE_LIGHT:
467 case SENSOR_TYPE_ORIENTATION:
468 return correction * convert_from_vtf_format(data_bytes, exponent, val);
470 case SENSOR_TYPE_ROTATION_VECTOR:
471 return correction * convert_from_vtf_format(data_bytes, exponent, val);
478 void select_transform (int s)
480 char prop_name[PROP_NAME_MAX];
481 char prop_val[PROP_VALUE_MAX];
482 int i = sensor[s].catalog_index;
483 const char *prefix = sensor_catalog[i].tag;
485 sprintf(prop_name, PROP_BASE, prefix, "transform");
487 if (property_get(prop_name, prop_val, ""))
488 if (!strcmp(prop_val, "ISH")) {
489 ALOGI( "Using Intel Sensor Hub semantics on %s\n", sensor[s].friendly_name);
491 sensor[s].ops.transform = transform_sample_ISH;
492 sensor[s].ops.finalize = finalize_sample_ISH;
496 sensor[s].ops.transform = transform_sample_default;
497 sensor[s].ops.finalize = finalize_sample_default;
501 float acquire_immediate_float_value (int s, int c)
503 char sysfs_path[PATH_MAX];
506 int dev_num = sensor[s].dev_num;
507 int i = sensor[s].catalog_index;
508 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
509 const char* input_path = sensor_catalog[i].channel[c].input_path;
510 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
511 float offset = sensor[s].offset;
512 int sensor_type = sensor_catalog[i].type;
515 /* In case correction has been requested using properties, apply it */
516 correction = sensor[s].channel[c].opt_scale;
518 /* Acquire a sample value for sensor s / channel c through sysfs */
520 if (sensor[s].channel[c].input_path_present) {
521 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
522 ret = sysfs_read_float(sysfs_path, &val);
525 return val * correction;
528 if (!sensor[s].channel[c].raw_path_present)
531 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
532 ret = sysfs_read_float(sysfs_path, &val);
538 * There is no transform ops defined yet for raw sysfs values.
539 * Use this function to perform transformation as well.
541 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
542 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) * correction;
544 return (val + offset) * scale * correction;
547 uint64_t acquire_immediate_uint64_value (int s, int c)
549 char sysfs_path[PATH_MAX];
552 int dev_num = sensor[s].dev_num;
553 int i = sensor[s].catalog_index;
554 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
555 const char* input_path = sensor_catalog[i].channel[c].input_path;
556 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
557 float offset = sensor[s].offset;
558 int sensor_type = sensor_catalog[i].type;
561 /* In case correction has been requested using properties, apply it */
562 correction = sensor[s].channel[c].opt_scale;
564 /* Acquire a sample value for sensor s / channel c through sysfs */
566 if (sensor[s].channel[c].input_path_present) {
567 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
568 ret = sysfs_read_uint64(sysfs_path, &val);
571 return val * correction;
574 if (!sensor[s].channel[c].raw_path_present)
577 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
578 ret = sysfs_read_uint64(sysfs_path, &val);
583 return (val + offset) * scale * correction;