SensorFusion.h revision 984826cc158193e61e3a00359ef4f6699c7d748a
1984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian/* 2984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Copyright (C) 2011 The Android Open Source Project 3984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * 4984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * you may not use this file except in compliance with the License. 6984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * You may obtain a copy of the License at 7984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * 8984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * 10984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Unless required by applicable law or agreed to in writing, software 11984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * See the License for the specific language governing permissions and 14984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * limitations under the License. 15984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian */ 16984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 17984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#ifndef ANDROID_SENSOR_FUSION_H 18984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#define ANDROID_SENSOR_FUSION_H 19984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 20984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <stdint.h> 21984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <sys/types.h> 22984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 23984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <utils/SortedVector.h> 24984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <utils/Singleton.h> 25984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <utils/String8.h> 26984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 27984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <gui/Sensor.h> 28984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 29984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "Fusion.h" 30984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SecondOrderLowPassFilter.h" 31984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 32984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 33984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 34984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiannamespace android { 35984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 36984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 37984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianclass SensorDevice; 38984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 39984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianclass SensorFusion : public Singleton<SensorFusion> { 40984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian friend class Singleton<SensorFusion>; 41984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 42984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorDevice& mSensorDevice; 43984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian Sensor mAcc; 44984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian Sensor mMag; 45984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian Sensor mGyro; 46984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian Fusion mFusion; 47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian bool mEnabled; 48984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian bool mHasGyro; 49984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian float mGyroRate; 50984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian nsecs_t mTargetDelayNs; 51984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian nsecs_t mGyroTime; 52984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mat33_t mRotationMatrix; 53984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SecondOrderLowPassFilter mLowPass; 54984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian BiquadFilter<vec3_t> mAccData; 55984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian vec3_t mFilteredMag; 56984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian vec3_t mFilteredAcc; 57984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SortedVector<void*> mClients; 58984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 59984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorFusion(); 60984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 61984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianpublic: 62984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian void process(const sensors_event_t& event); 63984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 64984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian bool isEnabled() const { return mEnabled; } 65984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian bool hasGyro() const { return mHasGyro; } 66984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian bool hasEstimate() const { return !mHasGyro || mFusion.hasEstimate(); } 67984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mat33_t getRotationMatrix() const { return mRotationMatrix; } 68984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian vec3_t getGyroBias() const { return mFusion.getBias(); } 69984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian float getEstimatedRate() const { return mGyroRate; } 70984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 71984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian status_t activate(void* ident, bool enabled); 72984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian status_t setDelay(void* ident, int64_t ns); 73984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 74984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian float getPowerUsage() const; 75984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian int32_t getMinDelay() const; 76984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 77984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian void dump(String8& result, char* buffer, size_t SIZE); 78984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; 79984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 80984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 81984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 82984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; // namespace android 83984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 84984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#endif // ANDROID_SENSOR_FUSION_H 85