OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/sensors/master' into gmin...
[android-x86/hardware-intel-libsensors.git] / transform.c
index 8b525d1..cc53f39 100644 (file)
@@ -189,107 +189,122 @@ static void reorder_fields(float* data, unsigned char map[MAX_CHANNELS])
 }
 
 
-static void denoise (struct sensor_info_t* si, struct sensors_event_t* data,
-                    int num_fields, int max_samples)
+static void clamp_gyro_readings_to_zero (int s, struct sensors_event_t* data)
 {
-       /*
-        * Smooth out incoming data using a moving average over a number of
-        * samples. We accumulate one second worth of samples, or max_samples,
-        * depending on which is lower.
-        */
+       float x, y, z;
+       float near_zero;
 
-       int i;
-       int f;
-       int sampling_rate = (int) si->sampling_rate;
-       int history_size;
-       int history_full = 0;
-
-       /* Don't denoise anything if we have less than two samples per second */
-       if (sampling_rate < 2)
-               return;
-
-       /* Restrict window size to the min of sampling_rate and max_samples */
-       if (sampling_rate > max_samples)
-               history_size = max_samples;
-       else
-               history_size = sampling_rate;
-
-       /* Reset history if we're operating on an incorrect window size */
-       if (si->history_size != history_size) {
-               si->history_size = history_size;
-               si->history_entries = 0;
-               si->history_index = 0;
-               si->history = (float*) realloc(si->history,
-                               si->history_size * num_fields * sizeof(float));
-               if (si->history) {
-                       si->history_sum = (float*) realloc(si->history_sum,
-                               num_fields * sizeof(float));
-                       if (si->history_sum)
-                               memset(si->history_sum, 0, num_fields * sizeof(float));
-               }
-       }
+       switch (sensor_info[s].type) {
+               case SENSOR_TYPE_GYROSCOPE:
+                       x = data->data[0];
+                       y = data->data[1];
+                       z = data->data[2];
+                       break;
 
-       if (!si->history || !si->history_sum)
-               return; /* Unlikely, but still... */
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                       x = data->data[0] - data->uncalibrated_gyro.bias[0];
+                       y = data->data[1] - data->uncalibrated_gyro.bias[1];
+                       z = data->data[2] - data->uncalibrated_gyro.bias[2];
+                       break;
 
-       /* Update initialized samples count */
-       if (si->history_entries < si->history_size)
-               si->history_entries++;
-       else
-               history_full = 1;
+               default:
+                       return;
+       }
 
-       /* Record new sample and calculate the moving sum */
-       for (f=0; f < num_fields; f++) {
-               /**
-                * A field is going to be overwritten if
-                * history is full, so decrease the history sum
-                */
-               if (history_full)
-                       si->history_sum[f] -=
-                               si->history[si->history_index * num_fields + f];
+       /* If we're calibrated, don't filter out as much */
+       if (sensor_info[s].cal_level > 0)
+               near_zero = 0.02; /* rad/s */
+       else
+               near_zero = 0.1;
 
-               si->history[si->history_index * num_fields + f] = data->data[f];
-               si->history_sum[f] += data->data[f];
+       /* If motion on all axes is small enough */
+       if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
 
-               /* For now simply compute a mobile mean for each field */
-               /* and output filtered data */
-               data->data[f] = si->history_sum[f] / si->history_entries;
+               /*
+                * Report that we're not moving at all... but not exactly zero
+                * as composite sensors (orientation, rotation vector) don't
+                * seem to react very well to it.
+                */
+               switch (sensor_info[s].type) {
+                       case SENSOR_TYPE_GYROSCOPE:
+                               data->data[0] *= 0.000001;
+                               data->data[1] *= 0.000001;
+                               data->data[2] *= 0.000001;
+                               break;
+
+                       case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                               data->data[0]= data->uncalibrated_gyro.bias[0]
+                                               + 0.000001 * x;
+                               data->data[1]= data->uncalibrated_gyro.bias[1]
+                                               + 0.000001 * y;
+                               data->data[2]= data->uncalibrated_gyro.bias[2]
+                                               + 0.000001 * z;
+                               break;
+               }
        }
-
-       /* Update our rolling index (next evicted cell) */
-       si->history_index = (si->history_index + 1) % si->history_size;
 }
 
 
-static int finalize_sample_default(int s, struct sensors_event_t* data)
+static int finalize_sample_default (int s, struct sensors_event_t* data)
 {
-       int i           = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[i].type;
-
        /* Swap fields if we have a custom channel ordering on this sensor */
        if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
                reorder_fields(data->data, sensor_info[s].order);
 
-       switch (sensor_type) {
+       sensor_info[s].event_count++;
+       switch (sensor_info[s].type) {
                case SENSOR_TYPE_ACCELEROMETER:
                        /* Always consider the accelerometer accurate */
                        data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
                        if (sensor_info[s].quirks & QUIRK_NOISY)
-                               denoise(&sensor_info[s], data, 3, 20);
+                               denoise(s, data);
                        break;
 
                case SENSOR_TYPE_MAGNETIC_FIELD:
                        calibrate_compass (data, &sensor_info[s], get_timestamp());
                        if (sensor_info[s].quirks & QUIRK_NOISY)
-                               denoise(&sensor_info[s], data, 3, 100);
+                               denoise(s, data);
                        break;
 
                case SENSOR_TYPE_GYROSCOPE:
+
+                       /*
+                        * Report medium accuracy by default ; higher accuracy
+                        * levels will be reported once, and if, we achieve
+                        * calibration.
+                        */
+                       data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+                       /* ... fall through */
+
                case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
-                       if (!(sensor_info[s].quirks & QUIRK_TERSE_DRIVER))
-                               calibrate_gyro(data, &sensor_info[s]);
-                       if (sensor_info[s].quirks & QUIRK_NOISY)
-                               denoise_median(data, &sensor_info[s]);
+
+                       /*
+                        * We're only trying to calibrate data from continuously
+                        * firing gyroscope drivers, as motion based ones use
+                        * movement thresholds that may lead us to incorrectly
+                        * estimate bias.
+                        */
+                       if (sensor_info[s].selected_trigger !=
+                               sensor_info[s].motion_trigger_name)
+                                       calibrate_gyro(data, &sensor_info[s]);
+
+                       /* For noisy sensors we'll drop a very few number
+                        * of samples to make sure we have at least MIN_SAMPLES events
+                        * in the filtering queue. This is to make sure we are not sending
+                        * events that can disturb our mean or stddev.
+                        */
+                       if (sensor_info[s].quirks & QUIRK_NOISY) {
+                               if((sensor_info[s].selected_trigger !=
+                                       sensor_info[s].motion_trigger_name) &&
+                                       sensor_info[s].event_count < MIN_SAMPLES)
+                                               return 0;
+
+                               denoise(s, data);
+                       }
+
+                       /* Clamp near zero moves to (0,0,0) if appropriate */
+                       clamp_gyro_readings_to_zero(s, data);
                        break;
 
                case SENSOR_TYPE_LIGHT:
@@ -331,17 +346,15 @@ static float transform_sample_default(int s, int c, unsigned char* sample_data)
 }
 
 
-static int finalize_sample_ISH(int s, struct sensors_event_t* data)
+static int finalize_sample_ISH (int s, struct sensors_event_t* data)
 {
-       int i           = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[i].type;
        float pitch, roll, yaw;
 
        /* Swap fields if we have a custom channel ordering on this sensor */
        if (sensor_info[s].quirks & QUIRK_FIELD_ORDERING)
                reorder_fields(data->data, sensor_info[s].order);
 
-       if (sensor_type == SENSOR_TYPE_ORIENTATION) {
+       if (sensor_info[s].type == SENSOR_TYPE_ORIENTATION) {
 
                pitch = data->data[0];
                roll = data->data[1];
@@ -352,16 +365,17 @@ static int finalize_sample_ISH(int s, struct sensors_event_t* data)
                data->data[2] = -roll;
        }
 
+       /* Add this event to our global records, for filtering purposes */
+       record_sample(s, data);
+
        return 1; /* Return sample to Android */
 }
 
 
-static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
+static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
 {
        struct datum_info_t* sample_type = &sensor_info[s].channel[c].type_info;
        int val         = (int) sample_as_int64(sample_data, sample_type);
-       int i           = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[i].type;
        float correction;
        int data_bytes  = (sample_type->realbits)/8;
        int exponent    = sensor_info[s].offset;
@@ -369,7 +383,7 @@ static float transform_sample_ISH(int s, int c, unsigned char* sample_data)
        /* In case correction has been requested using properties, apply it */
        correction = sensor_info[s].channel[c].opt_scale;
 
-       switch (sensor_type) {
+       switch (sensor_info[s].type) {
                case SENSOR_TYPE_ACCELEROMETER:
                        switch (c) {
                                case 0: