2 * Copyright (C) 2014 Intel Corporation.
9 #include <hardware/sensors.h>
11 #include "calibration.h"
14 #define GYRO_MAX_ERR 0.05
15 #define GYRO_DS_SIZE 100
16 #define GYRO_CALIBRATION_PATH "/data/gyro.conf"
18 static void reset (struct gyro_cal* cal_data)
22 cal_data->bias_x = cal_data->bias_y = cal_data->bias_z = 0;
24 cal_data->min_x = cal_data->min_y = cal_data->min_z = 1.0;
25 cal_data->max_x = cal_data->max_y = cal_data->max_z =-1.0;
29 void gyro_cal_init (struct sensor_info_t* info)
32 struct gyro_cal* cal_data = (struct gyro_cal*) info->cal_data;
42 data_file = fopen (GYRO_CALIBRATION_PATH, "r");
43 if (data_file == NULL)
46 ret = fscanf(data_file, "%f %f %f",
47 &cal_data->bias_x, &cal_data->bias_y, &cal_data->bias_z);
51 void gyro_store_data (struct sensor_info_t* info)
54 struct gyro_cal* cal_data = (struct gyro_cal*) info->cal_data;
60 data_file = fopen (GYRO_CALIBRATION_PATH, "w");
62 if (data_file == NULL)
65 ret = fprintf(data_file, "%f %f %f",
66 cal_data->bias_x, cal_data->bias_y, cal_data->bias_z);
69 ALOGE ("Gyro calibration - store data failed!");
75 static int gyro_collect (float x, float y, float z, struct gyro_cal* cal_data)
77 /* Analyze gyroscope data */
79 if (fabs(x) >= 1 || fabs(y) >= 1 || fabs(z) >= 1) {
81 /* We're supposed to be standing still ; start over */
84 return 0; /* Uncalibrated */
87 if (cal_data->count < GYRO_DS_SIZE) {
89 if (x < cal_data->min_x)
92 if (y < cal_data->min_y)
95 if (z < cal_data->min_z)
98 if (x > cal_data->max_x)
101 if (y > cal_data->max_y)
104 if (z > cal_data->max_z)
107 if (fabs(cal_data->max_x - cal_data->min_x) <= GYRO_MAX_ERR &&
108 fabs(cal_data->max_y - cal_data->min_y) <= GYRO_MAX_ERR &&
109 fabs(cal_data->max_z - cal_data->min_z) <= GYRO_MAX_ERR)
110 cal_data->count++; /* One more conformant sample */
112 reset(cal_data); /* Out of spec sample ; start over */
114 return 0; /* Still uncalibrated */
117 /* We got enough stable samples to estimate gyroscope bias */
118 cal_data->bias_x = (cal_data->max_x + cal_data->min_x) / 2;
119 cal_data->bias_y = (cal_data->max_y + cal_data->min_y) / 2;
120 cal_data->bias_z = (cal_data->max_z + cal_data->min_z) / 2;
122 return 1; /* Calibrated! */
125 void calibrate_gyro(struct sensors_event_t* event, struct sensor_info_t* info)
127 struct gyro_cal* cal_data = (struct gyro_cal*) info->cal_data;
129 if (cal_data == NULL)
132 /* Attempt gyroscope calibration if we have not reached this state */
133 if (info->cal_level == 0)
134 info->cal_level = gyro_collect(event->data[0], event->data[1],
135 event->data[2], cal_data);
137 switch (event->type) {
138 case SENSOR_TYPE_GYROSCOPE:
139 /* For the gyroscope apply the bias */
140 event->data[0] = event->data[0] - cal_data->bias_x;
141 event->data[1] = event->data[1] - cal_data->bias_y;
142 event->data[2] = event->data[2] - cal_data->bias_z;
145 event->gyro.status = SENSOR_STATUS_ACCURACY_HIGH;
148 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
150 * For the uncalibrated gyroscope don't apply the bias,
151 * but tell he Android framework what we think it is.
153 event->uncalibrated_gyro.bias[0] = cal_data->bias_x;
154 event->uncalibrated_gyro.bias[1] = cal_data->bias_y;
155 event->uncalibrated_gyro.bias[2] = cal_data->bias_z;