CorrectedGyroSensor.cpp revision 0cc8f809924706c7d683da30605f432635dd5bb6
1/*
2 * Copyright (C) 2011 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 "CorrectedGyroSensor.h"
26#include "SensorDevice.h"
27#include "SensorFusion.h"
28
29namespace android {
30// ---------------------------------------------------------------------------
31
32CorrectedGyroSensor::CorrectedGyroSensor(sensor_t const* list, size_t count)
33    : mSensorDevice(SensorDevice::getInstance()),
34      mSensorFusion(SensorFusion::getInstance())
35{
36    for (size_t i=0 ; i<count ; i++) {
37        if (list[i].type == SENSOR_TYPE_GYROSCOPE) {
38            mGyro = Sensor(list + i);
39            break;
40        }
41    }
42
43    sensor_t hwSensor;
44    hwSensor.name       = "Corrected Gyroscope Sensor";
45    hwSensor.vendor     = "AOSP";
46    hwSensor.version    = 1;
47    hwSensor.handle     = '_cgy';
48    hwSensor.type       = SENSOR_TYPE_GYROSCOPE;
49    hwSensor.maxRange   = mGyro.getMaxValue();
50    hwSensor.resolution = mGyro.getResolution();
51    hwSensor.power      = mSensorFusion.getPowerUsage();
52    hwSensor.minDelay   = mGyro.getMinDelay();
53    mSensor = Sensor(&hwSensor);
54}
55
56bool CorrectedGyroSensor::process(sensors_event_t* outEvent,
57        const sensors_event_t& event)
58{
59    if (event.type == SENSOR_TYPE_GYROSCOPE) {
60        const vec3_t bias(mSensorFusion.getGyroBias());
61        *outEvent = event;
62        outEvent->data[0] -= bias.x;
63        outEvent->data[1] -= bias.y;
64        outEvent->data[2] -= bias.z;
65        outEvent->sensor = '_cgy';
66        return true;
67    }
68    return false;
69}
70
71status_t CorrectedGyroSensor::activate(void* ident, bool enabled) {
72    mSensorDevice.activate(ident, mGyro.getHandle(), enabled);
73    return mSensorFusion.activate(FUSION_9AXIS, ident, enabled);
74}
75
76status_t CorrectedGyroSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
77    mSensorDevice.setDelay(ident, mGyro.getHandle(), ns);
78    return mSensorFusion.setDelay(FUSION_9AXIS, ident, ns);
79}
80
81const Sensor& CorrectedGyroSensor::getSensor() const {
82    return mSensor;
83}
84
85// ---------------------------------------------------------------------------
86}; // namespace android
87
88