OSDN Git Service

Reinstate monotonic timestamp in data acquisition threads
[android-x86/hardware-intel-libsensors.git] / transform.c
index b68f316..90834be 100644 (file)
@@ -21,8 +21,8 @@
 #define GRAVITY 9.80665f
 
 /* 720 LSG = 1G */
-#define LSG                         (1024.0f)
-#define NUMOFACCDATA                (8.0f)
+#define LSG                         (1024.0)
+#define NUMOFACCDATA                (8.0)
 
 /* conversion of acceleration data to SI units (m/s^2) */
 #define CONVERT_A                   (GRAVITY_EARTH / LSG / NUMOFACCDATA)
 #define CONVERT_A_Z(x)              ((float(x)/1000) * (GRAVITY * 1.0))
 
 /* conversion of magnetic data to uT units */
-#define CONVERT_M                   (1.0f/6.6f)
+#define CONVERT_M                   (1.0/6.6)
 #define CONVERT_M_X                 (-CONVERT_M)
 #define CONVERT_M_Y                 (-CONVERT_M)
 #define CONVERT_M_Z                 (CONVERT_M)
 
-#define CONVERT_GAUSS_TO_MICROTESLA(x)        ( (x) * 100 )
+#define CONVERT_GAUSS_TO_MICROTESLA(x)        ((x) * 100 )
 
 /* conversion of orientation data to degree units */
-#define CONVERT_O                   (1.0f/64.0f)
+#define CONVERT_O                   (1.0/64)
 #define CONVERT_O_A                 (CONVERT_O)
 #define CONVERT_O_P                 (CONVERT_O)
 #define CONVERT_O_R                 (-CONVERT_O)
 
 /*conversion of gyro data to SI units (radian/sec) */
-#define CONVERT_GYRO                ((2000.0f/32767.0f)*((float)M_PI / 180.0f))
+#define CONVERT_GYRO                (2000.0/32767*M_PI/180)
 #define CONVERT_GYRO_X              (-CONVERT_GYRO)
 #define CONVERT_GYRO_Y              (-CONVERT_GYRO)
 #define CONVERT_GYRO_Z              (CONVERT_GYRO)
@@ -86,7 +86,7 @@ inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
     }
 }
 
-// Platform sensor orientation
+/* Platform sensor orientation */
 #define DEF_ORIENT_ACCEL_X                   -1
 #define DEF_ORIENT_ACCEL_Y                   -1
 #define DEF_ORIENT_ACCEL_Z                   -1
@@ -95,7 +95,7 @@ inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
 #define DEF_ORIENT_GYRO_Y                   1
 #define DEF_ORIENT_GYRO_Z                   1
 
-// G to m/s2
+/* G to m/s2 */
 #define CONVERT_FROM_VTF16(s,d,x)      (convert_from_vtf_format(s,d,x))
 #define CONVERT_A_G_VTF16E14_X(s,d,x)  (DEF_ORIENT_ACCEL_X *\
                                         convert_from_vtf_format(s,d,x)*GRAVITY)
@@ -104,18 +104,18 @@ inline float convert_from_vtf_format(int size, int exponent, unsigned int value)
 #define CONVERT_A_G_VTF16E14_Z(s,d,x)  (DEF_ORIENT_ACCEL_Z *\
                                         convert_from_vtf_format(s,d,x)*GRAVITY)
 
-// Degree/sec to radian/sec
+/* Degree/sec to radian/sec */
 #define CONVERT_G_D_VTF16E14_X(s,d,x)  (DEF_ORIENT_GYRO_X *\
                                         convert_from_vtf_format(s,d,x) * \
-                                        ((float)M_PI/180.0f))
+                                        M_PI/180)
 #define CONVERT_G_D_VTF16E14_Y(s,d,x)  (DEF_ORIENT_GYRO_Y *\
                                         convert_from_vtf_format(s,d,x) * \
-                                        ((float)M_PI/180.0f))
+                                        M_PI/180)
 #define CONVERT_G_D_VTF16E14_Z(s,d,x)  (DEF_ORIENT_GYRO_Z *\
                                         convert_from_vtf_format(s,d,x) * \
-                                        ((float)M_PI/180.0f))
+                                        M_PI/180)
 
-// Milli gauss to micro tesla
+/* Milli gauss to micro tesla */
 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x)/10)
 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x)/10)
 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x)/10)
@@ -189,78 +189,6 @@ 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)
-{
-       /*
-        * 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.
-        */
-
-       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));
-               }
-       }
-
-       if (!si->history || !si->history_sum)
-               return; /* Unlikely, but still... */
-
-       /* Update initialized samples count */
-       if (si->history_entries < si->history_size)
-               si->history_entries++;
-       else
-               history_full = 1;
-
-       /* 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];
-
-               si->history[si->history_index * num_fields + f] = data->data[f];
-               si->history_sum[f] += data->data[f];
-
-               /* For now simply compute a mobile mean for each field */
-               /* and output filtered data */
-               data->data[f] = si->history_sum[f] / si->history_entries;
-       }
-
-       /* Update our rolling index (next evicted cell) */
-       si->history_index = (si->history_index + 1) % si->history_size;
-}
-
-
 static void clamp_gyro_readings_to_zero (int s, struct sensors_event_t* data)
 {
        float x, y, z;
@@ -329,13 +257,13 @@ static int finalize_sample_default (int s, struct sensors_event_t* data)
                        /* 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());
+                       calibrate_compass (data, &sensor_info[s]);
                        if (sensor_info[s].quirks & QUIRK_NOISY)
-                               denoise(&sensor_info[s], data, 3, 100);
+                               denoise(s, data);
                        break;
 
                case SENSOR_TYPE_GYROSCOPE:
@@ -361,17 +289,18 @@ static int finalize_sample_default (int s, struct sensors_event_t* data)
                                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.
+                       /*
+                        * For noisy sensors drop a few samples to make sure we
+                        * have at least GYRO_MIN_SAMPLES events in the
+                        * filtering queue. This improves mean and std dev.
                         */
                        if (sensor_info[s].quirks & QUIRK_NOISY) {
-                               denoise_median(&sensor_info[s], data, 3);
-                               if((sensor_info[s].selected_trigger !=
-                                       sensor_info[s].motion_trigger_name) &&
-                                       sensor_info[s].event_count < MIN_SAMPLES)
+                               if (sensor_info[s].selected_trigger !=
+                                   sensor_info[s].motion_trigger_name &&
+                                   sensor_info[s].event_count<GYRO_MIN_SAMPLES)
                                                return 0;
+
+                               denoise(s, data);
                        }
 
                        /* Clamp near zero moves to (0,0,0) if appropriate */
@@ -398,9 +327,6 @@ static int finalize_sample_default (int s, struct sensors_event_t* data)
                        break;
        }
 
-       /* Add this event to our global records, for filtering purposes */
-       record_sample(s, data);
-
        return 1; /* Return sample to Android */
 }