Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 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 | #ifndef ANDROID_SENSOR_FUSION_H |
| 18 | #define ANDROID_SENSOR_FUSION_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <utils/SortedVector.h> |
| 24 | #include <utils/Singleton.h> |
| 25 | #include <utils/String8.h> |
| 26 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 27 | #include <sensor/Sensor.h> |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 28 | |
| 29 | #include "Fusion.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 30 | |
| 31 | // --------------------------------------------------------------------------- |
| 32 | |
| 33 | namespace android { |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | class SensorDevice; |
| 37 | |
| 38 | class SensorFusion : public Singleton<SensorFusion> { |
| 39 | friend class Singleton<SensorFusion>; |
| 40 | |
| 41 | SensorDevice& mSensorDevice; |
| 42 | Sensor mAcc; |
| 43 | Sensor mMag; |
| 44 | Sensor mGyro; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 45 | |
| 46 | Fusion mFusions[NUM_FUSION_MODE]; // normal, no_mag, no_gyro |
| 47 | |
| 48 | bool mEnabled[NUM_FUSION_MODE]; |
| 49 | |
| 50 | vec4_t &mAttitude; |
| 51 | vec4_t mAttitudes[NUM_FUSION_MODE]; |
| 52 | |
| 53 | SortedVector<void*> mClients[3]; |
| 54 | |
Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 55 | float mEstimatedGyroRate; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 56 | nsecs_t mTargetDelayNs; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 57 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 58 | nsecs_t mGyroTime; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 59 | nsecs_t mAccTime; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 60 | |
| 61 | SensorFusion(); |
| 62 | |
| 63 | public: |
| 64 | void process(const sensors_event_t& event); |
| 65 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 66 | bool isEnabled() const { |
| 67 | return mEnabled[FUSION_9AXIS] || |
| 68 | mEnabled[FUSION_NOMAG] || |
| 69 | mEnabled[FUSION_NOGYRO]; |
| 70 | } |
| 71 | |
| 72 | bool hasEstimate(int mode = FUSION_9AXIS) const { |
| 73 | return mFusions[mode].hasEstimate(); |
| 74 | } |
| 75 | |
| 76 | mat33_t getRotationMatrix(int mode = FUSION_9AXIS) const { |
| 77 | return mFusions[mode].getRotationMatrix(); |
| 78 | } |
| 79 | |
| 80 | vec4_t getAttitude(int mode = FUSION_9AXIS) const { |
| 81 | return mAttitudes[mode]; |
| 82 | } |
| 83 | |
| 84 | vec3_t getGyroBias() const { return mFusions[FUSION_9AXIS].getBias(); } |
Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 85 | float getEstimatedRate() const { return mEstimatedGyroRate; } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 86 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 87 | status_t activate(int mode, void* ident, bool enabled); |
| 88 | status_t setDelay(int mode, void* ident, int64_t ns); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 89 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 90 | float getPowerUsage(int mode=FUSION_9AXIS) const; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 91 | int32_t getMinDelay() const; |
| 92 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 93 | void dump(String8& result) const; |
| 94 | void dump(util::ProtoOutputStream* proto) const; |
| 95 | void dumpFusion(FUSION_MODE mode, util::ProtoOutputStream* proto) const; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | |
| 99 | // --------------------------------------------------------------------------- |
| 100 | }; // namespace android |
| 101 | |
| 102 | #endif // ANDROID_SENSOR_FUSION_H |