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 | #include "SensorDevice.h" |
| 18 | #include "SensorFusion.h" |
| 19 | #include "SensorService.h" |
| 20 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 21 | #include <android/util/ProtoOutputStream.h> |
Pontus Lidman | d0e68d2 | 2023-07-20 18:31:53 +0000 | [diff] [blame^] | 22 | #include <cutils/properties.h> |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 23 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> |
| 24 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | // --------------------------------------------------------------------------- |
| 27 | |
| 28 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorFusion) |
| 29 | |
| 30 | SensorFusion::SensorFusion() |
| 31 | : mSensorDevice(SensorDevice::getInstance()), |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 32 | mAttitude(mAttitudes[FUSION_9AXIS]), |
| 33 | mGyroTime(0), mAccTime(0) |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 34 | { |
| 35 | sensor_t const* list; |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 36 | Sensor uncalibratedGyro; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 37 | ssize_t count = mSensorDevice.getSensorList(&list); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 38 | |
| 39 | mEnabled[FUSION_9AXIS] = false; |
| 40 | mEnabled[FUSION_NOMAG] = false; |
| 41 | mEnabled[FUSION_NOGYRO] = false; |
| 42 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 43 | if (count > 0) { |
| 44 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 45 | if (list[i].type == SENSOR_TYPE_ACCELEROMETER) { |
| 46 | mAcc = Sensor(list + i); |
| 47 | } |
| 48 | if (list[i].type == SENSOR_TYPE_MAGNETIC_FIELD) { |
| 49 | mMag = Sensor(list + i); |
| 50 | } |
| 51 | if (list[i].type == SENSOR_TYPE_GYROSCOPE) { |
| 52 | mGyro = Sensor(list + i); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 53 | } |
| 54 | if (list[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) { |
| 55 | uncalibratedGyro = Sensor(list + i); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 56 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 57 | } |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 58 | |
| 59 | // Use the uncalibrated gyroscope for sensor fusion when available |
| 60 | if (uncalibratedGyro.getType() == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) { |
| 61 | mGyro = uncalibratedGyro; |
| 62 | } |
| 63 | |
Pontus Lidman | d0e68d2 | 2023-07-20 18:31:53 +0000 | [diff] [blame^] | 64 | // Wearable devices will want to tune this parameter |
| 65 | // to 100 (Hz) in device.mk to save some power. |
| 66 | int32_t value = property_get_int32( |
| 67 | "sensors.aosp_low_power_sensor_fusion.maximum_rate", 200); |
| 68 | mEstimatedGyroRate = static_cast<float>(value); |
| 69 | mTargetDelayNs = 1000000000LL / mEstimatedGyroRate; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 70 | |
| 71 | for (int i = 0; i<NUM_FUSION_MODE; ++i) { |
| 72 | mFusions[i].init(i); |
| 73 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 74 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void SensorFusion::process(const sensors_event_t& event) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 78 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 79 | if (event.type == mGyro.getType()) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 80 | float dT; |
| 81 | if ( event.timestamp - mGyroTime> 0 && |
| 82 | event.timestamp - mGyroTime< (int64_t)(5e7) ) { //0.05sec |
| 83 | |
| 84 | dT = (event.timestamp - mGyroTime) / 1000000000.0f; |
Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 85 | // here we estimate the gyro rate (useful for debugging) |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 86 | const float freq = 1 / dT; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 87 | if (freq >= 100 && freq<1000) { // filter values obviously wrong |
| 88 | const float alpha = 1 / (1 + dT); // 1s time-constant |
Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 89 | mEstimatedGyroRate = freq + (mEstimatedGyroRate - freq)*alpha; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 90 | } |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 91 | |
| 92 | const vec3_t gyro(event.data); |
| 93 | for (int i = 0; i<NUM_FUSION_MODE; ++i) { |
| 94 | if (mEnabled[i]) { |
| 95 | // fusion in no gyro mode will ignore |
| 96 | mFusions[i].handleGyro(gyro, dT); |
| 97 | } |
| 98 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 99 | } |
| 100 | mGyroTime = event.timestamp; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 101 | } else if (event.type == SENSOR_TYPE_MAGNETIC_FIELD) { |
| 102 | const vec3_t mag(event.data); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 103 | for (int i = 0; i<NUM_FUSION_MODE; ++i) { |
| 104 | if (mEnabled[i]) { |
| 105 | mFusions[i].handleMag(mag);// fusion in no mag mode will ignore |
| 106 | } |
| 107 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 108 | } else if (event.type == SENSOR_TYPE_ACCELEROMETER) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 109 | float dT; |
| 110 | if ( event.timestamp - mAccTime> 0 && |
| 111 | event.timestamp - mAccTime< (int64_t)(1e8) ) { //0.1sec |
| 112 | dT = (event.timestamp - mAccTime) / 1000000000.0f; |
| 113 | |
| 114 | const vec3_t acc(event.data); |
| 115 | for (int i = 0; i<NUM_FUSION_MODE; ++i) { |
| 116 | if (mEnabled[i]) { |
| 117 | mFusions[i].handleAcc(acc, dT); |
| 118 | mAttitudes[i] = mFusions[i].getAttitude(); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | mAccTime = event.timestamp; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | template <typename T> inline T min(T a, T b) { return a<b ? a : b; } |
| 127 | template <typename T> inline T max(T a, T b) { return a>b ? a : b; } |
| 128 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 129 | status_t SensorFusion::activate(int mode, void* ident, bool enabled) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 130 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 131 | ALOGD_IF(DEBUG_CONNECTIONS, |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 132 | "SensorFusion::activate(mode=%d, ident=%p, enabled=%d)", |
| 133 | mode, ident, enabled); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 134 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 135 | const ssize_t idx = mClients[mode].indexOf(ident); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 136 | if (enabled) { |
| 137 | if (idx < 0) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 138 | mClients[mode].add(ident); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 139 | } |
| 140 | } else { |
| 141 | if (idx >= 0) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 142 | mClients[mode].removeItemsAt(idx); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | const bool newState = mClients[mode].size() != 0; |
| 147 | if (newState != mEnabled[mode]) { |
| 148 | mEnabled[mode] = newState; |
| 149 | if (newState) { |
| 150 | mFusions[mode].init(mode); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | mSensorDevice.activate(ident, mAcc.getHandle(), enabled); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 155 | if (mode != FUSION_NOMAG) { |
| 156 | mSensorDevice.activate(ident, mMag.getHandle(), enabled); |
| 157 | } |
| 158 | if (mode != FUSION_NOGYRO) { |
| 159 | mSensorDevice.activate(ident, mGyro.getHandle(), enabled); |
| 160 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 161 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 162 | return NO_ERROR; |
| 163 | } |
| 164 | |
| 165 | status_t SensorFusion::setDelay(int mode, void* ident, int64_t ns) { |
| 166 | // Call batch with timeout zero instead of setDelay(). |
| 167 | if (ns > (int64_t)5e7) { |
| 168 | ns = (int64_t)(5e7); |
| 169 | } |
| 170 | mSensorDevice.batch(ident, mAcc.getHandle(), 0, ns, 0); |
| 171 | if (mode != FUSION_NOMAG) { |
Grigory Dzhavadyan | 4998c16 | 2016-10-31 17:02:32 -0700 | [diff] [blame] | 172 | mSensorDevice.batch(ident, mMag.getHandle(), 0, ms2ns(10), 0); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 173 | } |
| 174 | if (mode != FUSION_NOGYRO) { |
| 175 | mSensorDevice.batch(ident, mGyro.getHandle(), 0, mTargetDelayNs, 0); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 176 | } |
| 177 | return NO_ERROR; |
| 178 | } |
| 179 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 180 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 181 | float SensorFusion::getPowerUsage(int mode) const { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 182 | float power = mAcc.getPowerUsage() + |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 183 | ((mode != FUSION_NOMAG) ? mMag.getPowerUsage() : 0) + |
| 184 | ((mode != FUSION_NOGYRO) ? mGyro.getPowerUsage() : 0); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 185 | return power; |
| 186 | } |
| 187 | |
| 188 | int32_t SensorFusion::getMinDelay() const { |
| 189 | return mAcc.getMinDelay(); |
| 190 | } |
| 191 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 192 | void SensorFusion::dump(String8& result) const { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 193 | const Fusion& fusion_9axis(mFusions[FUSION_9AXIS]); |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 194 | result.appendFormat("9-axis fusion %s (%zd clients), gyro-rate=%7.2fHz, " |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 195 | "q=< %g, %g, %g, %g > (%g), " |
| 196 | "b=< %g, %g, %g >\n", |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 197 | mEnabled[FUSION_9AXIS] ? "enabled" : "disabled", |
| 198 | mClients[FUSION_9AXIS].size(), |
Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 199 | mEstimatedGyroRate, |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 200 | fusion_9axis.getAttitude().x, |
| 201 | fusion_9axis.getAttitude().y, |
| 202 | fusion_9axis.getAttitude().z, |
| 203 | fusion_9axis.getAttitude().w, |
| 204 | length(fusion_9axis.getAttitude()), |
| 205 | fusion_9axis.getBias().x, |
| 206 | fusion_9axis.getBias().y, |
| 207 | fusion_9axis.getBias().z); |
| 208 | |
| 209 | const Fusion& fusion_nomag(mFusions[FUSION_NOMAG]); |
| 210 | result.appendFormat("game fusion(no mag) %s (%zd clients), " |
| 211 | "gyro-rate=%7.2fHz, " |
| 212 | "q=< %g, %g, %g, %g > (%g), " |
| 213 | "b=< %g, %g, %g >\n", |
| 214 | mEnabled[FUSION_NOMAG] ? "enabled" : "disabled", |
| 215 | mClients[FUSION_NOMAG].size(), |
| 216 | mEstimatedGyroRate, |
| 217 | fusion_nomag.getAttitude().x, |
| 218 | fusion_nomag.getAttitude().y, |
| 219 | fusion_nomag.getAttitude().z, |
| 220 | fusion_nomag.getAttitude().w, |
| 221 | length(fusion_nomag.getAttitude()), |
| 222 | fusion_nomag.getBias().x, |
| 223 | fusion_nomag.getBias().y, |
| 224 | fusion_nomag.getBias().z); |
| 225 | |
| 226 | const Fusion& fusion_nogyro(mFusions[FUSION_NOGYRO]); |
| 227 | result.appendFormat("geomag fusion (no gyro) %s (%zd clients), " |
| 228 | "gyro-rate=%7.2fHz, " |
| 229 | "q=< %g, %g, %g, %g > (%g), " |
| 230 | "b=< %g, %g, %g >\n", |
| 231 | mEnabled[FUSION_NOGYRO] ? "enabled" : "disabled", |
| 232 | mClients[FUSION_NOGYRO].size(), |
| 233 | mEstimatedGyroRate, |
| 234 | fusion_nogyro.getAttitude().x, |
| 235 | fusion_nogyro.getAttitude().y, |
| 236 | fusion_nogyro.getAttitude().z, |
| 237 | fusion_nogyro.getAttitude().w, |
| 238 | length(fusion_nogyro.getAttitude()), |
| 239 | fusion_nogyro.getBias().x, |
| 240 | fusion_nogyro.getBias().y, |
| 241 | fusion_nogyro.getBias().z); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 244 | void SensorFusion::dumpFusion(FUSION_MODE mode, util::ProtoOutputStream* proto) const { |
| 245 | using namespace service::SensorFusionProto::FusionProto; |
| 246 | const Fusion& fusion(mFusions[mode]); |
| 247 | proto->write(ENABLED, mEnabled[mode]); |
| 248 | proto->write(NUM_CLIENTS, (int)mClients[mode].size()); |
| 249 | proto->write(ESTIMATED_GYRO_RATE, mEstimatedGyroRate); |
| 250 | proto->write(ATTITUDE_X, fusion.getAttitude().x); |
| 251 | proto->write(ATTITUDE_Y, fusion.getAttitude().y); |
| 252 | proto->write(ATTITUDE_Z, fusion.getAttitude().z); |
| 253 | proto->write(ATTITUDE_W, fusion.getAttitude().w); |
| 254 | proto->write(ATTITUDE_LENGTH, length(fusion.getAttitude())); |
| 255 | proto->write(BIAS_X, fusion.getBias().x); |
| 256 | proto->write(BIAS_Y, fusion.getBias().y); |
| 257 | proto->write(BIAS_Z, fusion.getBias().z); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Dump debugging information as android.service.SensorFusionProto protobuf message using |
| 262 | * ProtoOutputStream. |
| 263 | * |
| 264 | * See proto definition and some notes about ProtoOutputStream in |
| 265 | * frameworks/base/core/proto/android/service/sensor_service.proto |
| 266 | */ |
| 267 | void SensorFusion::dump(util::ProtoOutputStream* proto) const { |
| 268 | uint64_t token = proto->start(service::SensorFusionProto::FUSION_9AXIS); |
| 269 | dumpFusion(FUSION_9AXIS, proto); |
| 270 | proto->end(token); |
| 271 | |
| 272 | token = proto->start(service::SensorFusionProto::FUSION_NOMAG); |
| 273 | dumpFusion(FUSION_NOMAG, proto); |
| 274 | proto->end(token); |
| 275 | |
| 276 | token = proto->start(service::SensorFusionProto::FUSION_NOGYRO); |
| 277 | dumpFusion(FUSION_NOGYRO, proto); |
| 278 | proto->end(token); |
| 279 | } |
| 280 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 281 | // --------------------------------------------------------------------------- |
| 282 | }; // namespace android |