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; 317b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian ssize_t count = mSensorDevice.getSensorList(&list); 327b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (count > 0) { 337b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 347b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (list[i].type == SENSOR_TYPE_ACCELEROMETER) { 357b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mAcc = Sensor(list + i); 367b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian } 377b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (list[i].type == SENSOR_TYPE_MAGNETIC_FIELD) { 387b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mMag = Sensor(list + i); 397b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian } 407b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (list[i].type == SENSOR_TYPE_GYROSCOPE) { 417b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mGyro = Sensor(list + i); 427b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // 200 Hz for gyro events is a good compromise between precision 437b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // and power/cpu usage. 447b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mGyroRate = 200; 457b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mTargetDelayNs = 1000000000LL/mGyroRate; 467b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian } 47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 487b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mFusion.init(); 49984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 50984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 51984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 52984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianvoid SensorFusion::process(const sensors_event_t& event) { 533301542828febc768e1df42892cfac4992c35474Mathias Agopian if (event.type == SENSOR_TYPE_GYROSCOPE) { 54984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (mGyroTime != 0) { 55984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const float dT = (event.timestamp - mGyroTime) / 1000000000.0f; 56984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const float freq = 1 / dT; 573301542828febc768e1df42892cfac4992c35474Mathias Agopian if (freq >= 100 && freq<1000) { // filter values obviously wrong 583301542828febc768e1df42892cfac4992c35474Mathias Agopian const float alpha = 1 / (1 + dT); // 1s time-constant 593301542828febc768e1df42892cfac4992c35474Mathias Agopian mGyroRate = freq + (mGyroRate - freq)*alpha; 603301542828febc768e1df42892cfac4992c35474Mathias Agopian } 61984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 62984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mGyroTime = event.timestamp; 63984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mFusion.handleGyro(vec3_t(event.data), 1.0f/mGyroRate); 64984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } else if (event.type == SENSOR_TYPE_MAGNETIC_FIELD) { 65984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const vec3_t mag(event.data); 663301542828febc768e1df42892cfac4992c35474Mathias Agopian mFusion.handleMag(mag); 67984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } else if (event.type == SENSOR_TYPE_ACCELEROMETER) { 68984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const vec3_t acc(event.data); 693301542828febc768e1df42892cfac4992c35474Mathias Agopian mFusion.handleAcc(acc); 703301542828febc768e1df42892cfac4992c35474Mathias Agopian mAttitude = mFusion.getAttitude(); 71984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 72984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 73984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 74984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiantemplate <typename T> inline T min(T a, T b) { return a<b ? a : b; } 75984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiantemplate <typename T> inline T max(T a, T b) { return a>b ? a : b; } 76984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 77984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t SensorFusion::activate(void* ident, bool enabled) { 78984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 79a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD_IF(DEBUG_CONNECTIONS, 80984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "SensorFusion::activate(ident=%p, enabled=%d)", 81984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian ident, enabled); 82984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 83984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const ssize_t idx = mClients.indexOf(ident); 84984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (enabled) { 85984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (idx < 0) { 86984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mClients.add(ident); 87984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 88984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } else { 89984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (idx >= 0) { 90984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mClients.removeItemsAt(idx); 91984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 92984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 93984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 94984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mSensorDevice.activate(ident, mAcc.getHandle(), enabled); 95984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mSensorDevice.activate(ident, mMag.getHandle(), enabled); 963301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.activate(ident, mGyro.getHandle(), enabled); 97984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 98984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const bool newState = mClients.size() != 0; 99984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (newState != mEnabled) { 100984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mEnabled = newState; 101984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (newState) { 102984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mFusion.init(); 1033301542828febc768e1df42892cfac4992c35474Mathias Agopian mGyroTime = 0; 104984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 105984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 106984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return NO_ERROR; 107984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 108984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 109984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t SensorFusion::setDelay(void* ident, int64_t ns) { 1103301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.setDelay(ident, mAcc.getHandle(), ns); 1113301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.setDelay(ident, mMag.getHandle(), ms2ns(20)); 1123301542828febc768e1df42892cfac4992c35474Mathias Agopian mSensorDevice.setDelay(ident, mGyro.getHandle(), mTargetDelayNs); 113984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return NO_ERROR; 114984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 115984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 116984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 117984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianfloat SensorFusion::getPowerUsage() const { 1183301542828febc768e1df42892cfac4992c35474Mathias Agopian float power = mAcc.getPowerUsage() + 1193301542828febc768e1df42892cfac4992c35474Mathias Agopian mMag.getPowerUsage() + 1203301542828febc768e1df42892cfac4992c35474Mathias Agopian mGyro.getPowerUsage(); 121984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return power; 122984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 123984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 124984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianint32_t SensorFusion::getMinDelay() const { 125984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return mAcc.getMinDelay(); 126984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 127984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 128984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianvoid SensorFusion::dump(String8& result, char* buffer, size_t SIZE) { 129984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const Fusion& fusion(mFusion); 1303301542828febc768e1df42892cfac4992c35474Mathias Agopian snprintf(buffer, SIZE, "9-axis fusion %s (%d clients), gyro-rate=%7.2fHz, " 1313301542828febc768e1df42892cfac4992c35474Mathias Agopian "q=< %g, %g, %g, %g > (%g), " 1323301542828febc768e1df42892cfac4992c35474Mathias Agopian "b=< %g, %g, %g >\n", 133984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mEnabled ? "enabled" : "disabled", 134984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mClients.size(), 135984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mGyroRate, 136984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getAttitude().x, 137984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getAttitude().y, 138984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getAttitude().z, 1393301542828febc768e1df42892cfac4992c35474Mathias Agopian fusion.getAttitude().w, 1403301542828febc768e1df42892cfac4992c35474Mathias Agopian length(fusion.getAttitude()), 141984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getBias().x, 142984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getBias().y, 143984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.getBias().z); 144984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian result.append(buffer); 145984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 146984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 147984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 148984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; // namespace android 149