| 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 |  | 
|  | 21 | namespace android { | 
|  | 22 | // --------------------------------------------------------------------------- | 
|  | 23 |  | 
|  | 24 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorFusion) | 
|  | 25 |  | 
|  | 26 | SensorFusion::SensorFusion() | 
|  | 27 | : mSensorDevice(SensorDevice::getInstance()), | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 28 | mEnabled(false), mGyroTime(0) | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 29 | { | 
|  | 30 | sensor_t const* list; | 
| Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 31 | Sensor uncalibratedGyro; | 
| Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 32 | 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 Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 43 | } | 
|  | 44 | if (list[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) { | 
|  | 45 | uncalibratedGyro = Sensor(list + i); | 
| Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 46 | } | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 47 | } | 
| Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 48 |  | 
|  | 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. | 
| Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 56 | mEstimatedGyroRate = 200; | 
|  | 57 | mTargetDelayNs = 1000000000LL/mEstimatedGyroRate; | 
| Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 58 | mFusion.init(); | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 59 | } | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | void SensorFusion::process(const sensors_event_t& event) { | 
| Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 63 | if (event.type == mGyro.getType()) { | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 64 | if (mGyroTime != 0) { | 
|  | 65 | const float dT = (event.timestamp - mGyroTime) / 1000000000.0f; | 
| Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 66 | mFusion.handleGyro(vec3_t(event.data), dT); | 
|  | 67 | // here we estimate the gyro rate (useful for debugging) | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 68 | const float freq = 1 / dT; | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 69 | if (freq >= 100 && freq<1000) { // filter values obviously wrong | 
|  | 70 | const float alpha = 1 / (1 + dT); // 1s time-constant | 
| Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 71 | mEstimatedGyroRate = freq + (mEstimatedGyroRate - freq)*alpha; | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 72 | } | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 73 | } | 
|  | 74 | mGyroTime = event.timestamp; | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 75 | } else if (event.type == SENSOR_TYPE_MAGNETIC_FIELD) { | 
|  | 76 | const vec3_t mag(event.data); | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 77 | mFusion.handleMag(mag); | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 78 | } else if (event.type == SENSOR_TYPE_ACCELEROMETER) { | 
|  | 79 | const vec3_t acc(event.data); | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 80 | mFusion.handleAcc(acc); | 
|  | 81 | mAttitude = mFusion.getAttitude(); | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 82 | } | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | template <typename T> inline T min(T a, T b) { return a<b ? a : b; } | 
|  | 86 | template <typename T> inline T max(T a, T b) { return a>b ? a : b; } | 
|  | 87 |  | 
|  | 88 | status_t SensorFusion::activate(void* ident, bool enabled) { | 
|  | 89 |  | 
| Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 90 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 91 | "SensorFusion::activate(ident=%p, enabled=%d)", | 
|  | 92 | ident, enabled); | 
|  | 93 |  | 
|  | 94 | const ssize_t idx = mClients.indexOf(ident); | 
|  | 95 | if (enabled) { | 
|  | 96 | if (idx < 0) { | 
|  | 97 | mClients.add(ident); | 
|  | 98 | } | 
|  | 99 | } else { | 
|  | 100 | if (idx >= 0) { | 
|  | 101 | mClients.removeItemsAt(idx); | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | mSensorDevice.activate(ident, mAcc.getHandle(), enabled); | 
|  | 106 | mSensorDevice.activate(ident, mMag.getHandle(), enabled); | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 107 | mSensorDevice.activate(ident, mGyro.getHandle(), enabled); | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | const bool newState = mClients.size() != 0; | 
|  | 110 | if (newState != mEnabled) { | 
|  | 111 | mEnabled = newState; | 
|  | 112 | if (newState) { | 
|  | 113 | mFusion.init(); | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 114 | mGyroTime = 0; | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 115 | } | 
|  | 116 | } | 
|  | 117 | return NO_ERROR; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | status_t SensorFusion::setDelay(void* ident, int64_t ns) { | 
| Aravind Akella | ebff73c | 2013-12-05 16:24:59 -0800 | [diff] [blame] | 121 | // Call batch with timeout zero instead of setDelay(). | 
|  | 122 | mSensorDevice.batch(ident, mAcc.getHandle(), 0, ns, 0); | 
|  | 123 | mSensorDevice.batch(ident, mMag.getHandle(), 0, ms2ns(20), 0); | 
|  | 124 | mSensorDevice.batch(ident, mGyro.getHandle(), 0, mTargetDelayNs, 0); | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 125 | return NO_ERROR; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 |  | 
|  | 129 | float SensorFusion::getPowerUsage() const { | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 130 | float power =   mAcc.getPowerUsage() + | 
|  | 131 | mMag.getPowerUsage() + | 
|  | 132 | mGyro.getPowerUsage(); | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 133 | return power; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | int32_t SensorFusion::getMinDelay() const { | 
|  | 137 | return mAcc.getMinDelay(); | 
|  | 138 | } | 
|  | 139 |  | 
| Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 140 | void SensorFusion::dump(String8& result) { | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 141 | const Fusion& fusion(mFusion); | 
| Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 142 | result.appendFormat("9-axis fusion %s (%zd clients), gyro-rate=%7.2fHz, " | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 143 | "q=< %g, %g, %g, %g > (%g), " | 
|  | 144 | "b=< %g, %g, %g >\n", | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 145 | mEnabled ? "enabled" : "disabled", | 
|  | 146 | mClients.size(), | 
| Mathias Agopian | 2e2a560 | 2013-05-30 14:18:23 -0700 | [diff] [blame] | 147 | mEstimatedGyroRate, | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 148 | fusion.getAttitude().x, | 
|  | 149 | fusion.getAttitude().y, | 
|  | 150 | fusion.getAttitude().z, | 
| Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 151 | fusion.getAttitude().w, | 
|  | 152 | length(fusion.getAttitude()), | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 153 | fusion.getBias().x, | 
|  | 154 | fusion.getBias().y, | 
|  | 155 | fusion.getBias().z); | 
| Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
|  | 158 | // --------------------------------------------------------------------------- | 
|  | 159 | }; // namespace android |