blob: 541fad26f069017b2ac2369a394de05bdf349f6d [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18#include <math.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22
23#include <hardware/sensors.h>
24
25#include "GravitySensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070026#include "SensorDevice.h"
27#include "SensorFusion.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080028
29namespace android {
30// ---------------------------------------------------------------------------
31
32GravitySensor::GravitySensor(sensor_t const* list, size_t count)
33 : mSensorDevice(SensorDevice::getInstance()),
Mathias Agopian984826c2011-05-17 22:54:42 -070034 mSensorFusion(SensorFusion::getInstance()),
Mathias Agopian50b66762010-11-29 17:26:51 -080035 mAccTime(0),
Mathias Agopian87c9dbb2010-11-11 17:58:51 -080036 mLowPass(M_SQRT1_2, 1.5f),
Mathias Agopianf001c922010-11-11 17:58:51 -080037 mX(mLowPass), mY(mLowPass), mZ(mLowPass)
Mathias Agopianf001c922010-11-11 17:58:51 -080038{
39 for (size_t i=0 ; i<count ; i++) {
40 if (list[i].type == SENSOR_TYPE_ACCELEROMETER) {
41 mAccelerometer = Sensor(list + i);
42 break;
43 }
44 }
45}
46
47bool GravitySensor::process(sensors_event_t* outEvent,
48 const sensors_event_t& event)
49{
50 const static double NS2S = 1.0 / 1000000000.0;
51 if (event.type == SENSOR_TYPE_ACCELEROMETER) {
Mathias Agopian984826c2011-05-17 22:54:42 -070052 vec3_t g;
53 if (mSensorFusion.hasGyro()) {
54 if (!mSensorFusion.hasEstimate())
55 return false;
56 const mat33_t R(mSensorFusion.getRotationMatrix());
57 // FIXME: we need to estimate the length of gravity because
58 // the accelerometer may have a small scaling error. This
59 // translates to an offset in the linear-acceleration sensor.
60 g = R[2] * GRAVITY_EARTH;
Mathias Agopianf001c922010-11-11 17:58:51 -080061 } else {
Mathias Agopian984826c2011-05-17 22:54:42 -070062 const double now = event.timestamp * NS2S;
63 if (mAccTime == 0) {
64 g.x = mX.init(event.acceleration.x);
65 g.y = mY.init(event.acceleration.y);
66 g.z = mZ.init(event.acceleration.z);
67 } else {
68 double dT = now - mAccTime;
69 mLowPass.setSamplingPeriod(dT);
70 g.x = mX(event.acceleration.x);
71 g.y = mY(event.acceleration.y);
72 g.z = mZ(event.acceleration.z);
73 }
74 g *= (GRAVITY_EARTH / length(g));
75 mAccTime = now;
Mathias Agopianf001c922010-11-11 17:58:51 -080076 }
Mathias Agopianf001c922010-11-11 17:58:51 -080077 *outEvent = event;
Mathias Agopian984826c2011-05-17 22:54:42 -070078 outEvent->data[0] = g.x;
79 outEvent->data[1] = g.y;
80 outEvent->data[2] = g.z;
Mathias Agopianf001c922010-11-11 17:58:51 -080081 outEvent->sensor = '_grv';
82 outEvent->type = SENSOR_TYPE_GRAVITY;
83 return true;
84 }
85 return false;
86}
Mathias Agopian984826c2011-05-17 22:54:42 -070087
Mathias Agopianf001c922010-11-11 17:58:51 -080088status_t GravitySensor::activate(void* ident, bool enabled) {
Mathias Agopian984826c2011-05-17 22:54:42 -070089 status_t err;
90 if (mSensorFusion.hasGyro()) {
91 err = mSensorFusion.activate(this, enabled);
92 } else {
93 err = mSensorDevice.activate(this, mAccelerometer.getHandle(), enabled);
94 if (err == NO_ERROR) {
95 if (enabled) {
96 mAccTime = 0;
97 }
Mathias Agopianf001c922010-11-11 17:58:51 -080098 }
99 }
100 return err;
101}
102
103status_t GravitySensor::setDelay(void* ident, int handle, int64_t ns)
104{
Mathias Agopian984826c2011-05-17 22:54:42 -0700105 if (mSensorFusion.hasGyro()) {
106 return mSensorFusion.setDelay(this, ns);
107 } else {
108 return mSensorDevice.setDelay(this, mAccelerometer.getHandle(), ns);
109 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800110}
111
112Sensor GravitySensor::getSensor() const {
113 sensor_t hwSensor;
114 hwSensor.name = "Gravity Sensor";
115 hwSensor.vendor = "Google Inc.";
Mathias Agopian984826c2011-05-17 22:54:42 -0700116 hwSensor.version = mSensorFusion.hasGyro() ? 3 : 2;
Mathias Agopianf001c922010-11-11 17:58:51 -0800117 hwSensor.handle = '_grv';
118 hwSensor.type = SENSOR_TYPE_GRAVITY;
Mathias Agopian984826c2011-05-17 22:54:42 -0700119 hwSensor.maxRange = GRAVITY_EARTH * 2;
Mathias Agopianf001c922010-11-11 17:58:51 -0800120 hwSensor.resolution = mAccelerometer.getResolution();
Mathias Agopian984826c2011-05-17 22:54:42 -0700121 hwSensor.power = mSensorFusion.hasGyro() ?
122 mSensorFusion.getPowerUsage() : mAccelerometer.getPowerUsage();
123 hwSensor.minDelay = mSensorFusion.hasGyro() ?
124 mSensorFusion.getMinDelay() : mAccelerometer.getMinDelay();
Mathias Agopianf001c922010-11-11 17:58:51 -0800125 Sensor sensor(&hwSensor);
126 return sensor;
127}
128
129// ---------------------------------------------------------------------------
130}; // namespace android
131