blob: 4837b97ae588324501586055b980a66122afe145 [file] [log] [blame]
Mathias Agopian984826c2011-05-17 22:54:42 -07001/*
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 "SensorDevice.h"
18#include "SensorFusion.h"
19#include "SensorService.h"
20
21namespace android {
22// ---------------------------------------------------------------------------
23
24ANDROID_SINGLETON_STATIC_INSTANCE(SensorFusion)
25
26SensorFusion::SensorFusion()
27 : mSensorDevice(SensorDevice::getInstance()),
Mathias Agopian33015422011-05-27 18:18:13 -070028 mEnabled(false), mGyroTime(0)
Mathias Agopian984826c2011-05-17 22:54:42 -070029{
30 sensor_t const* list;
Mathias Agopian03193062013-05-10 19:32:39 -070031 Sensor uncalibratedGyro;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070032 ssize_t count = mSensorDevice.getSensorList(&list);
33 if (count > 0) {
34 for (size_t i=0 ; i<size_t(count) ; i++) {
35 if (list[i].type == SENSOR_TYPE_ACCELEROMETER) {
36 mAcc = Sensor(list + i);
37 }
38 if (list[i].type == SENSOR_TYPE_MAGNETIC_FIELD) {
39 mMag = Sensor(list + i);
40 }
41 if (list[i].type == SENSOR_TYPE_GYROSCOPE) {
42 mGyro = Sensor(list + i);
Mathias Agopian03193062013-05-10 19:32:39 -070043 }
44 if (list[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
45 uncalibratedGyro = Sensor(list + i);
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070046 }
Mathias Agopian984826c2011-05-17 22:54:42 -070047 }
Mathias Agopian03193062013-05-10 19:32:39 -070048
49 // Use the uncalibrated gyroscope for sensor fusion when available
50 if (uncalibratedGyro.getType() == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
51 mGyro = uncalibratedGyro;
52 }
53
54 // 200 Hz for gyro events is a good compromise between precision
55 // and power/cpu usage.
56 mGyroRate = 200;
57 mTargetDelayNs = 1000000000LL/mGyroRate;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070058 mFusion.init();
Mathias Agopian984826c2011-05-17 22:54:42 -070059 }
Mathias Agopian984826c2011-05-17 22:54:42 -070060}
61
62void SensorFusion::process(const sensors_event_t& event) {
Mathias Agopian03193062013-05-10 19:32:39 -070063 if (event.type == mGyro.getType()) {
Mathias Agopian984826c2011-05-17 22:54:42 -070064 if (mGyroTime != 0) {
65 const float dT = (event.timestamp - mGyroTime) / 1000000000.0f;
66 const float freq = 1 / dT;
Mathias Agopian33015422011-05-27 18:18:13 -070067 if (freq >= 100 && freq<1000) { // filter values obviously wrong
68 const float alpha = 1 / (1 + dT); // 1s time-constant
69 mGyroRate = freq + (mGyroRate - freq)*alpha;
70 }
Mathias Agopian984826c2011-05-17 22:54:42 -070071 }
72 mGyroTime = event.timestamp;
73 mFusion.handleGyro(vec3_t(event.data), 1.0f/mGyroRate);
74 } else if (event.type == SENSOR_TYPE_MAGNETIC_FIELD) {
75 const vec3_t mag(event.data);
Mathias Agopian33015422011-05-27 18:18:13 -070076 mFusion.handleMag(mag);
Mathias Agopian984826c2011-05-17 22:54:42 -070077 } else if (event.type == SENSOR_TYPE_ACCELEROMETER) {
78 const vec3_t acc(event.data);
Mathias Agopian33015422011-05-27 18:18:13 -070079 mFusion.handleAcc(acc);
80 mAttitude = mFusion.getAttitude();
Mathias Agopian984826c2011-05-17 22:54:42 -070081 }
82}
83
84template <typename T> inline T min(T a, T b) { return a<b ? a : b; }
85template <typename T> inline T max(T a, T b) { return a>b ? a : b; }
86
87status_t SensorFusion::activate(void* ident, bool enabled) {
88
Steve Blocka5512372011-12-20 16:23:08 +000089 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopian984826c2011-05-17 22:54:42 -070090 "SensorFusion::activate(ident=%p, enabled=%d)",
91 ident, enabled);
92
93 const ssize_t idx = mClients.indexOf(ident);
94 if (enabled) {
95 if (idx < 0) {
96 mClients.add(ident);
97 }
98 } else {
99 if (idx >= 0) {
100 mClients.removeItemsAt(idx);
101 }
102 }
103
104 mSensorDevice.activate(ident, mAcc.getHandle(), enabled);
105 mSensorDevice.activate(ident, mMag.getHandle(), enabled);
Mathias Agopian33015422011-05-27 18:18:13 -0700106 mSensorDevice.activate(ident, mGyro.getHandle(), enabled);
Mathias Agopian984826c2011-05-17 22:54:42 -0700107
108 const bool newState = mClients.size() != 0;
109 if (newState != mEnabled) {
110 mEnabled = newState;
111 if (newState) {
112 mFusion.init();
Mathias Agopian33015422011-05-27 18:18:13 -0700113 mGyroTime = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700114 }
115 }
116 return NO_ERROR;
117}
118
119status_t SensorFusion::setDelay(void* ident, int64_t ns) {
Mathias Agopian33015422011-05-27 18:18:13 -0700120 mSensorDevice.setDelay(ident, mAcc.getHandle(), ns);
121 mSensorDevice.setDelay(ident, mMag.getHandle(), ms2ns(20));
122 mSensorDevice.setDelay(ident, mGyro.getHandle(), mTargetDelayNs);
Mathias Agopian984826c2011-05-17 22:54:42 -0700123 return NO_ERROR;
124}
125
126
127float SensorFusion::getPowerUsage() const {
Mathias Agopian33015422011-05-27 18:18:13 -0700128 float power = mAcc.getPowerUsage() +
129 mMag.getPowerUsage() +
130 mGyro.getPowerUsage();
Mathias Agopian984826c2011-05-17 22:54:42 -0700131 return power;
132}
133
134int32_t SensorFusion::getMinDelay() const {
135 return mAcc.getMinDelay();
136}
137
138void SensorFusion::dump(String8& result, char* buffer, size_t SIZE) {
139 const Fusion& fusion(mFusion);
Mathias Agopian33015422011-05-27 18:18:13 -0700140 snprintf(buffer, SIZE, "9-axis fusion %s (%d clients), gyro-rate=%7.2fHz, "
141 "q=< %g, %g, %g, %g > (%g), "
142 "b=< %g, %g, %g >\n",
Mathias Agopian984826c2011-05-17 22:54:42 -0700143 mEnabled ? "enabled" : "disabled",
144 mClients.size(),
145 mGyroRate,
146 fusion.getAttitude().x,
147 fusion.getAttitude().y,
148 fusion.getAttitude().z,
Mathias Agopian33015422011-05-27 18:18:13 -0700149 fusion.getAttitude().w,
150 length(fusion.getAttitude()),
Mathias Agopian984826c2011-05-17 22:54:42 -0700151 fusion.getBias().x,
152 fusion.getBias().y,
153 fusion.getBias().z);
154 result.append(buffer);
155}
156
157// ---------------------------------------------------------------------------
158}; // namespace android