2 * Copyright (C) 2014 Intel Corporation.
9 #include <hardware/sensors.h>
11 #include "calibration.h"
13 static void reset (struct gyro_cal* cal_data)
17 cal_data->bias_x = cal_data->bias_y = cal_data->bias_z = 0;
19 cal_data->min_x = cal_data->min_y = cal_data->min_z = 1.0;
20 cal_data->max_x = cal_data->max_y = cal_data->max_z =-1.0;
24 void gyro_cal_init (struct sensor_info_t* info)
27 struct gyro_cal* cal_data = (struct gyro_cal*) info->cal_data;
37 data_file = fopen (GYRO_CALIBRATION_PATH, "r");
38 if (data_file == NULL)
41 ret = fscanf(data_file, "%f %f %f",
42 &cal_data->bias_x, &cal_data->bias_y, &cal_data->bias_z);
46 void gyro_store_data (struct sensor_info_t* info)
49 struct gyro_cal* cal_data = (struct gyro_cal*) info->cal_data;
55 data_file = fopen (GYRO_CALIBRATION_PATH, "w");
57 if (data_file == NULL)
60 ret = fprintf(data_file, "%f %f %f",
61 cal_data->bias_x, cal_data->bias_y, cal_data->bias_z);
64 ALOGE ("Gyro calibration - store data failed!");
70 static int gyro_collect (float x, float y, float z, struct gyro_cal* cal_data)
72 /* Analyze gyroscope data */
74 if (fabs(x) >= 1 || fabs(y) >= 1 || fabs(z) >= 1) {
76 /* We're supposed to be standing still ; start over */
79 return 0; /* Uncalibrated */
82 if (cal_data->count < GYRO_DS_SIZE) {
84 if (x < cal_data->min_x)
87 if (y < cal_data->min_y)
90 if (z < cal_data->min_z)
93 if (x > cal_data->max_x)
96 if (y > cal_data->max_y)
99 if (z > cal_data->max_z)
102 if (fabs(cal_data->max_x - cal_data->min_x) <= GYRO_MAX_ERR &&
103 fabs(cal_data->max_y - cal_data->min_y) <= GYRO_MAX_ERR &&
104 fabs(cal_data->max_z - cal_data->min_z) <= GYRO_MAX_ERR)
105 cal_data->count++; /* One more conformant sample */
107 reset(cal_data); /* Out of spec sample ; start over */
109 return 0; /* Still uncalibrated */
112 /* We got enough stable samples to estimate gyroscope bias */
113 cal_data->bias_x = (cal_data->max_x + cal_data->min_x) / 2;
114 cal_data->bias_y = (cal_data->max_y + cal_data->min_y) / 2;
115 cal_data->bias_z = (cal_data->max_z + cal_data->min_z) / 2;
117 return 1; /* Calibrated! */
120 void calibrate_gyro(struct sensors_event_t* event, struct sensor_info_t* info)
122 struct gyro_cal* cal_data = (struct gyro_cal*) info->cal_data;
124 if (cal_data == NULL)
127 /* Attempt gyroscope calibration if we have not reached this state */
128 if (info->cal_level == 0)
129 info->cal_level = gyro_collect(event->data[0], event->data[1],
130 event->data[2], cal_data);
133 event->gyro.status = SENSOR_STATUS_ACCURACY_HIGH;
135 switch (event->type) {
136 case SENSOR_TYPE_GYROSCOPE:
137 /* For the gyroscope apply the bias */
138 event->data[0] = event->data[0] - cal_data->bias_x;
139 event->data[1] = event->data[1] - cal_data->bias_y;
140 event->data[2] = event->data[2] - cal_data->bias_z;
143 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
145 * For the uncalibrated gyroscope don't apply the bias,
146 * but tell he Android framework what we think it is.
148 event->uncalibrated_gyro.bias[0] = cal_data->bias_x;
149 event->uncalibrated_gyro.bias[1] = cal_data->bias_y;
150 event->uncalibrated_gyro.bias[2] = cal_data->bias_z;