SensorFusion.cpp revision 3301542828febc768e1df42892cfac4992c35474
1984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian/* 2984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Copyright (C) 2011 The Android Open Source Project 3984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * 4984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * you may not use this file except in compliance with the License. 6984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * You may obtain a copy of the License at 7984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * 8984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * 10984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Unless required by applicable law or agreed to in writing, software 11984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * See the License for the specific language governing permissions and 14984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * limitations under the License. 15984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian */ 16984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 17984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorDevice.h" 18984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorFusion.h" 19984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorService.h" 20984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 21984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiannamespace android { 22984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 23984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 24984826cc158193e61e3a00359ef4f6699c7d748aMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(SensorFusion) 25984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 26984826cc158193e61e3a00359ef4f6699c7d748aMathias AgopianSensorFusion::SensorFusion() 27984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian : mSensorDevice(SensorDevice::getInstance()), 283301542828febc768e1df42892cfac4992c35474Mathias Agopian mEnabled(false), mGyroTime(0) 29984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian{ 30984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian sensor_t const* list; 31984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian size_t count = mSensorDevice.getSensorList(&list); 32984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian for (size_t i=0 ; i<count ; i++) { 33984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (list[i].type == SENSOR_TYPE_ACCELEROMETER) { 34984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mAcc = Sensor(list + i); 35984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 36984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (list[i].type == SENSOR_TYPE_MAGNETIC_FIELD) { 37984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mMag = Sensor(list + i); 38984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 39984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (list[i].type == SENSOR_TYPE_GYROSCOPE) { 40984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mGyro = Sensor(list + i); 41984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // 200 Hz for gyro events is a good compromise between precision 42984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // and power/cpu usage. 433301542828febc768e1df42892cfac4992c35474Mathias Agopian mGyroRate = 200; 443301542828febc768e1df42892cfac4992c35474Mathias Agopian mTargetDelayNs = 1000000000LL/mGyroRate; 45984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 46984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mFusion.init(); 48984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 49984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 50984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianvoid SensorFusion::process(const sensors_event_t& event) { 513301542828febc768e1df42892cfac4992c35474Mathias Agopian if (event.type == SENSOR_TYPE_GYROSCOPE) { 52984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (mGyroTime != 0) { 53984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const float dT = (event.timestamp - mGyroTime) / 1000000000.0f; 54984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const float freq = 1 / dT; 553301542828febc768e1df42892cfac4992c35474Mathias Agopian if (freq >= 100 && freq<1000) { // filter values obviously wrong 563301542828febc768e1df42892cfac4992c35474Mathias Agopian const float alpha = 1 / (1 + dT); // 1s time-constant 573301542828febc768e1df42892cfac4992c35474Mathias Agopian mGyroRate = freq + (mGyroRate - freq)*alpha; 583301542828febc768e1df42892cfac4992c35474Mathias Agopian } 59984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 60984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mGyroTime = event.timestamp; 61984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mFusion.handleGyro(vec3_t(event.data), 1.0f/mGyroRate); 62984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } else if (event.type == SENSOR_TYPE_MAGNETIC_FIELD) { 63984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const vec3_t mag(event.data); 643301542828febc768e1df42892cfac4992c35474Mathias Agopian mFusion.handleMag(mag); 65984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } else if (event.type == SENSOR_TYPE_ACCELEROMETER) { 66984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const vec3_t acc(event.data); 673301542828febc768e1df42892cfac4992c35474Mathias Agopian mFusion.handleAcc(acc); 683301542828febc768e1df42892cfac4992c35474Mathias Agopian mAttitude = mFusion.getAttitude(); 69984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 70984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 71984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 72984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiantemplate <typename T> inline T min(T a, T b) { return a<b ? a : b; } 73984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiantemplate <typename T> inline T max(T a, T b) { return a>b ? a : b; } 74984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 75984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t SensorFusion::activate(void* ident, bool enabled) { 76984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 77984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian LOGD_IF(DEBUG_CONNECTIONS, 78984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "SensorFusion::activate(ident=%p, enabled=%d)", 79984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian ident, enabled); 80984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 81984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const ssize_t idx = mClients.indexOf(ident); 82984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (enabled) { 83984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (idx < 0) { 84984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mClients.add(ident); 85984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 86984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } else { 87984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (idx >= 0) { 88984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mClients.removeItemsAt(idx); 89984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 90984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 91984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 92984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mSensorDevice.activate(ident, mAcc.getHandle(), enabled); 93984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mSensorDevice.activate(ident, mMag.getHandle(), enabled); 943301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.activate(ident, mGyro.getHandle(), enabled); 95984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 96984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const bool newState = mClients.size() != 0; 97984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (newState != mEnabled) { 98984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mEnabled = newState; 99984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (newState) { 100984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mFusion.init(); 1013301542828febc768e1df42892cfac4992c35474Mathias Agopian mGyroTime = 0; 102984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 103984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 104984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return NO_ERROR; 105984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 106984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 107984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t SensorFusion::setDelay(void* ident, int64_t ns) { 1083301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.setDelay(ident, mAcc.getHandle(), ns); 1093301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.setDelay(ident, mMag.getHandle(), ms2ns(20)); 1103301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.setDelay(ident, mGyro.getHandle(), mTargetDelayNs); 111984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return NO_ERROR; 112984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 113984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 114984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 115984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianfloat SensorFusion::getPowerUsage() const { 1163301542828febc768e1df42892cfac4992c35474Mathias Agopian float power = mAcc.getPowerUsage() + 1173301542828febc768e1df42892cfac4992c35474Mathias Agopian mMag.getPowerUsage() + 1183301542828febc768e1df42892cfac4992c35474Mathias Agopian mGyro.getPowerUsage(); 119984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return power; 120984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 121984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 122984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianint32_t SensorFusion::getMinDelay() const { 123984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return mAcc.getMinDelay(); 124984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 125984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 126984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianvoid SensorFusion::dump(String8& result, char* buffer, size_t SIZE) { 127984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const Fusion& fusion(mFusion); 1283301542828febc768e1df42892cfac4992c35474Mathias Agopian snprintf(buffer, SIZE, "9-axis fusion %s (%d clients), gyro-rate=%7.2fHz, " 1293301542828febc768e1df42892cfac4992c35474Mathias Agopian "q=< %g, %g, %g, %g > (%g), " 1303301542828febc768e1df42892cfac4992c35474Mathias Agopian "b=< %g, %g, %g >\n", 131984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mEnabled ? "enabled" : "disabled", 132984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mClients.size(), 133984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mGyroRate, 134984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getAttitude().x, 135984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getAttitude().y, 136984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getAttitude().z, 1373301542828febc768e1df42892cfac4992c35474Mathias Agopian fusion.getAttitude().w, 1383301542828febc768e1df42892cfac4992c35474Mathias Agopian length(fusion.getAttitude()), 139984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getBias().x, 140984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getBias().y, 141984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getBias().z); 142984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian result.append(buffer); 143984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 144984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 145984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 146984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; // namespace android 147