OSDN Git Service

Reverse the default orientation of accelerometer
[android-x86/hardware-intel-libsensors.git] / gyro-calibration.c
index a9aa92c..6a2170f 100644 (file)
@@ -1,6 +1,18 @@
 /*
- * Copyright (C) 2014 Intel Corporation.
- */
+// Copyright (c) 2015 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
 
 #include <stdio.h>
 #include <math.h>
 #include "common.h"
 #include "calibration.h"
 
-static void reset (struct gyro_cal* cal_data)
+
+/* Gyro defines */
+#define GYRO_MAX_ERR 0.05
+#define GYRO_DS_SIZE 100
+#define GYRO_CALIBRATION_PATH "/data/gyro.conf"
+#define GYRO_CAL_VERSION 1.0
+
+
+static void reset (gyro_cal_t* cal_data)
 {
        cal_data->count = 0;
 
@@ -21,16 +41,61 @@ static void reset (struct gyro_cal* cal_data)
 }
 
 
-void gyro_cal_init (struct sensor_info_t* info)
+void gyro_cal_init (int s)
+{
+       int ret;
+       gyro_cal_t* cal_data = (gyro_cal_t*) sensor[s].cal_data;
+       FILE* data_file;
+       float version;
+
+       sensor[s].cal_level = 0;
+
+       if (cal_data == NULL)
+               return;
+
+       reset(cal_data);
+
+       data_file = fopen (GYRO_CALIBRATION_PATH, "r");
+       if (data_file == NULL)
+               return;
+
+       ret = fscanf(data_file, "%f %f %f %f", &version,
+                       &cal_data->bias_x, &cal_data->bias_y, &cal_data->bias_z);
+
+       if (ret != 4 || version != GYRO_CAL_VERSION) {
+               reset(cal_data);
+               ALOGE("Gyro calibration - init failed!\n");
+       }
+
+       fclose(data_file);
+}
+
+
+void gyro_store_data (int s)
 {
-       info->cal_level = 0;
+       int ret;
+       gyro_cal_t* cal_data = (gyro_cal_t*) sensor[s].cal_data;
+       FILE* data_file;
+
+       if (cal_data == NULL)
+               return;
+
+       data_file = fopen (GYRO_CALIBRATION_PATH, "w");
+
+       if (data_file == NULL)
+               return;
+
+       ret = fprintf(data_file, "%f %f %f %f", GYRO_CAL_VERSION,
+               cal_data->bias_x, cal_data->bias_y, cal_data->bias_z);
 
-       if (info->cal_data)
-               reset((struct gyro_cal*) info->cal_data);
+       if (ret < 0)
+               ALOGE ("Gyro calibration - store data failed!");
+
+       fclose(data_file);
 }
 
 
-static int gyro_collect (float x, float y, float z, struct gyro_cal* cal_data)
+static int gyro_collect (float x, float y, float z, gyro_cal_t* cal_data)
 {
        /* Analyze gyroscope data */
 
@@ -80,39 +145,24 @@ static int gyro_collect (float x, float y, float z, struct gyro_cal* cal_data)
        return 1; /* Calibrated! */
 }
 
-void calibrate_gyro(struct sensors_event_t* event, struct sensor_info_t* info)
-{
-       struct gyro_cal* cal_data = (struct gyro_cal*) info->cal_data;
 
-       event->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+void calibrate_gyro (int s, sensors_event_t* event)
+{
+       gyro_cal_t* cal_data = (gyro_cal_t*) sensor[s].cal_data;
 
        if (cal_data == NULL)
                return;
 
        /* Attempt gyroscope calibration if we have not reached this state */
-       if (info->cal_level == 0)
-               info->cal_level = gyro_collect(event->data[0], event->data[1],
+       if (sensor[s].cal_level == 0)
+               sensor[s].cal_level = gyro_collect(event->data[0], event->data[1],
                                               event->data[2], cal_data);
 
-       if (info->cal_level)
-               event->gyro.status = SENSOR_STATUS_ACCURACY_HIGH;
-
-       switch (event->type) {
-               case SENSOR_TYPE_GYROSCOPE:
-                       /* For the gyroscope apply the bias */
-                       event->data[0] = event->data[0] - cal_data->bias_x;
-                       event->data[1] = event->data[1] - cal_data->bias_y;
-                       event->data[2] = event->data[2] - cal_data->bias_z;
-                       break;
-
-               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
-                       /*
-                        * For the uncalibrated gyroscope don't apply the bias,
-                        * but tell he Android framework what we think it is.
-                        */
-                       event->uncalibrated_gyro.bias[0] = cal_data->bias_x;
-                       event->uncalibrated_gyro.bias[1] = cal_data->bias_y;
-                       event->uncalibrated_gyro.bias[2] = cal_data->bias_z;
-                       break;
-       }
+
+       event->data[0] = event->data[0] - cal_data->bias_x;
+       event->data[1] = event->data[1] - cal_data->bias_y;
+       event->data[2] = event->data[2] - cal_data->bias_z;
+
+       if (sensor[s].cal_level)
+              event->gyro.status = SENSOR_STATUS_ACCURACY_HIGH;
 }