blob: 4ec0c8cbd28cf5cbb95f37543075770ff2ef1358 [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;
31 size_t count = mSensorDevice.getSensorList(&list);
32 for (size_t i=0 ; i<count ; i++) {
33 if (list[i].type == SENSOR_TYPE_ACCELEROMETER) {
34 mAcc = Sensor(list + i);
35 }
36 if (list[i].type == SENSOR_TYPE_MAGNETIC_FIELD) {
37 mMag = Sensor(list + i);
38 }
39 if (list[i].type == SENSOR_TYPE_GYROSCOPE) {
40 mGyro = Sensor(list + i);
41 // 200 Hz for gyro events is a good compromise between precision
42 // and power/cpu usage.
Mathias Agopian33015422011-05-27 18:18:13 -070043 mGyroRate = 200;
44 mTargetDelayNs = 1000000000LL/mGyroRate;
Mathias Agopian984826c2011-05-17 22:54:42 -070045 }
46 }
47 mFusion.init();
Mathias Agopian984826c2011-05-17 22:54:42 -070048}
49
50void SensorFusion::process(const sensors_event_t& event) {
Mathias Agopian33015422011-05-27 18:18:13 -070051 if (event.type == SENSOR_TYPE_GYROSCOPE) {
Mathias Agopian984826c2011-05-17 22:54:42 -070052 if (mGyroTime != 0) {
53 const float dT = (event.timestamp - mGyroTime) / 1000000000.0f;
54 const float freq = 1 / dT;
Mathias Agopian33015422011-05-27 18:18:13 -070055 if (freq >= 100 && freq<1000) { // filter values obviously wrong
56 const float alpha = 1 / (1 + dT); // 1s time-constant
57 mGyroRate = freq + (mGyroRate - freq)*alpha;
58 }
Mathias Agopian984826c2011-05-17 22:54:42 -070059 }
60 mGyroTime = event.timestamp;
61 mFusion.handleGyro(vec3_t(event.data), 1.0f/mGyroRate);
62 } else if (event.type == SENSOR_TYPE_MAGNETIC_FIELD) {
63 const vec3_t mag(event.data);
Mathias Agopian33015422011-05-27 18:18:13 -070064 mFusion.handleMag(mag);
Mathias Agopian984826c2011-05-17 22:54:42 -070065 } else if (event.type == SENSOR_TYPE_ACCELEROMETER) {
66 const vec3_t acc(event.data);
Mathias Agopian33015422011-05-27 18:18:13 -070067 mFusion.handleAcc(acc);
68 mAttitude = mFusion.getAttitude();
Mathias Agopian984826c2011-05-17 22:54:42 -070069 }
70}
71
72template <typename T> inline T min(T a, T b) { return a<b ? a : b; }
73template <typename T> inline T max(T a, T b) { return a>b ? a : b; }
74
75status_t SensorFusion::activate(void* ident, bool enabled) {
76
77 LOGD_IF(DEBUG_CONNECTIONS,
78 "SensorFusion::activate(ident=%p, enabled=%d)",
79 ident, enabled);
80
81 const ssize_t idx = mClients.indexOf(ident);
82 if (enabled) {
83 if (idx < 0) {
84 mClients.add(ident);
85 }
86 } else {
87 if (idx >= 0) {
88 mClients.removeItemsAt(idx);
89 }
90 }
91
92 mSensorDevice.activate(ident, mAcc.getHandle(), enabled);
93 mSensorDevice.activate(ident, mMag.getHandle(), enabled);
Mathias Agopian33015422011-05-27 18:18:13 -070094 mSensorDevice.activate(ident, mGyro.getHandle(), enabled);
Mathias Agopian984826c2011-05-17 22:54:42 -070095
96 const bool newState = mClients.size() != 0;
97 if (newState != mEnabled) {
98 mEnabled = newState;
99 if (newState) {
100 mFusion.init();
Mathias Agopian33015422011-05-27 18:18:13 -0700101 mGyroTime = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700102 }
103 }
104 return NO_ERROR;
105}
106
107status_t SensorFusion::setDelay(void* ident, int64_t ns) {
Mathias Agopian33015422011-05-27 18:18:13 -0700108 mSensorDevice.setDelay(ident, mAcc.getHandle(), ns);
109 mSensorDevice.setDelay(ident, mMag.getHandle(), ms2ns(20));
110 mSensorDevice.setDelay(ident, mGyro.getHandle(), mTargetDelayNs);
Mathias Agopian984826c2011-05-17 22:54:42 -0700111 return NO_ERROR;
112}
113
114
115float SensorFusion::getPowerUsage() const {
Mathias Agopian33015422011-05-27 18:18:13 -0700116 float power = mAcc.getPowerUsage() +
117 mMag.getPowerUsage() +
118 mGyro.getPowerUsage();
Mathias Agopian984826c2011-05-17 22:54:42 -0700119 return power;
120}
121
122int32_t SensorFusion::getMinDelay() const {
123 return mAcc.getMinDelay();
124}
125
126void SensorFusion::dump(String8& result, char* buffer, size_t SIZE) {
127 const Fusion& fusion(mFusion);
Mathias Agopian33015422011-05-27 18:18:13 -0700128 snprintf(buffer, SIZE, "9-axis fusion %s (%d clients), gyro-rate=%7.2fHz, "
129 "q=< %g, %g, %g, %g > (%g), "
130 "b=< %g, %g, %g >\n",
Mathias Agopian984826c2011-05-17 22:54:42 -0700131 mEnabled ? "enabled" : "disabled",
132 mClients.size(),
133 mGyroRate,
134 fusion.getAttitude().x,
135 fusion.getAttitude().y,
136 fusion.getAttitude().z,
Mathias Agopian33015422011-05-27 18:18:13 -0700137 fusion.getAttitude().w,
138 length(fusion.getAttitude()),
Mathias Agopian984826c2011-05-17 22:54:42 -0700139 fusion.getBias().x,
140 fusion.getBias().y,
141 fusion.getBias().z);
142 result.append(buffer);
143}
144
145// ---------------------------------------------------------------------------
146}; // namespace android