2011-05-18 05:54:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SensorDevice.h"
|
|
|
|
#include "SensorFusion.h"
|
|
|
|
#include "SensorService.h"
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
ANDROID_SINGLETON_STATIC_INSTANCE(SensorFusion)
|
|
|
|
|
|
|
|
SensorFusion::SensorFusion()
|
|
|
|
: mSensorDevice(SensorDevice::getInstance()),
|
2011-05-28 01:18:13 +00:00
|
|
|
mEnabled(false), mGyroTime(0)
|
2011-05-18 05:54:42 +00:00
|
|
|
{
|
|
|
|
sensor_t const* list;
|
2013-05-11 02:32:39 +00:00
|
|
|
Sensor uncalibratedGyro;
|
2011-07-14 23:39:46 +00:00
|
|
|
ssize_t count = mSensorDevice.getSensorList(&list);
|
|
|
|
if (count > 0) {
|
|
|
|
for (size_t i=0 ; i<size_t(count) ; i++) {
|
|
|
|
if (list[i].type == SENSOR_TYPE_ACCELEROMETER) {
|
|
|
|
mAcc = Sensor(list + i);
|
|
|
|
}
|
|
|
|
if (list[i].type == SENSOR_TYPE_MAGNETIC_FIELD) {
|
|
|
|
mMag = Sensor(list + i);
|
|
|
|
}
|
|
|
|
if (list[i].type == SENSOR_TYPE_GYROSCOPE) {
|
|
|
|
mGyro = Sensor(list + i);
|
|
|
|
}
|
2013-05-11 02:32:39 +00:00
|
|
|
if (list[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
|
|
|
|
uncalibratedGyro = Sensor(list + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the uncalibrated gyroscope for sensor fusion when available
|
|
|
|
if (uncalibratedGyro.getType() == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
|
|
|
|
mGyro = uncalibratedGyro;
|
2011-05-18 05:54:42 +00:00
|
|
|
}
|
2013-05-11 02:32:39 +00:00
|
|
|
|
|
|
|
// 200 Hz for gyro events is a good compromise between precision
|
|
|
|
// and power/cpu usage.
|
2013-05-30 21:18:23 +00:00
|
|
|
mEstimatedGyroRate = 200;
|
|
|
|
mTargetDelayNs = 1000000000LL/mEstimatedGyroRate;
|
2011-07-14 23:39:46 +00:00
|
|
|
mFusion.init();
|
2011-05-18 05:54:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorFusion::process(const sensors_event_t& event) {
|
2013-05-11 02:32:39 +00:00
|
|
|
if (event.type == mGyro.getType()) {
|
2011-05-18 05:54:42 +00:00
|
|
|
if (mGyroTime != 0) {
|
|
|
|
const float dT = (event.timestamp - mGyroTime) / 1000000000.0f;
|
2013-05-30 21:18:23 +00:00
|
|
|
mFusion.handleGyro(vec3_t(event.data), dT);
|
|
|
|
// here we estimate the gyro rate (useful for debugging)
|
2011-05-18 05:54:42 +00:00
|
|
|
const float freq = 1 / dT;
|
2011-05-28 01:18:13 +00:00
|
|
|
if (freq >= 100 && freq<1000) { // filter values obviously wrong
|
|
|
|
const float alpha = 1 / (1 + dT); // 1s time-constant
|
2013-05-30 21:18:23 +00:00
|
|
|
mEstimatedGyroRate = freq + (mEstimatedGyroRate - freq)*alpha;
|
2011-05-28 01:18:13 +00:00
|
|
|
}
|
2011-05-18 05:54:42 +00:00
|
|
|
}
|
|
|
|
mGyroTime = event.timestamp;
|
|
|
|
} else if (event.type == SENSOR_TYPE_MAGNETIC_FIELD) {
|
|
|
|
const vec3_t mag(event.data);
|
2011-05-28 01:18:13 +00:00
|
|
|
mFusion.handleMag(mag);
|
2011-05-18 05:54:42 +00:00
|
|
|
} else if (event.type == SENSOR_TYPE_ACCELEROMETER) {
|
|
|
|
const vec3_t acc(event.data);
|
2011-05-28 01:18:13 +00:00
|
|
|
mFusion.handleAcc(acc);
|
|
|
|
mAttitude = mFusion.getAttitude();
|
2011-05-18 05:54:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> inline T min(T a, T b) { return a<b ? a : b; }
|
|
|
|
template <typename T> inline T max(T a, T b) { return a>b ? a : b; }
|
|
|
|
|
|
|
|
status_t SensorFusion::activate(void* ident, bool enabled) {
|
|
|
|
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS,
|
2011-05-18 05:54:42 +00:00
|
|
|
"SensorFusion::activate(ident=%p, enabled=%d)",
|
|
|
|
ident, enabled);
|
|
|
|
|
|
|
|
const ssize_t idx = mClients.indexOf(ident);
|
|
|
|
if (enabled) {
|
|
|
|
if (idx < 0) {
|
|
|
|
mClients.add(ident);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (idx >= 0) {
|
|
|
|
mClients.removeItemsAt(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mSensorDevice.activate(ident, mAcc.getHandle(), enabled);
|
|
|
|
mSensorDevice.activate(ident, mMag.getHandle(), enabled);
|
2011-05-28 01:18:13 +00:00
|
|
|
mSensorDevice.activate(ident, mGyro.getHandle(), enabled);
|
2011-05-18 05:54:42 +00:00
|
|
|
|
|
|
|
const bool newState = mClients.size() != 0;
|
|
|
|
if (newState != mEnabled) {
|
|
|
|
mEnabled = newState;
|
|
|
|
if (newState) {
|
|
|
|
mFusion.init();
|
2011-05-28 01:18:13 +00:00
|
|
|
mGyroTime = 0;
|
2011-05-18 05:54:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorFusion::setDelay(void* ident, int64_t ns) {
|
2011-05-28 01:18:13 +00:00
|
|
|
mSensorDevice.setDelay(ident, mAcc.getHandle(), ns);
|
|
|
|
mSensorDevice.setDelay(ident, mMag.getHandle(), ms2ns(20));
|
|
|
|
mSensorDevice.setDelay(ident, mGyro.getHandle(), mTargetDelayNs);
|
2011-05-18 05:54:42 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float SensorFusion::getPowerUsage() const {
|
2011-05-28 01:18:13 +00:00
|
|
|
float power = mAcc.getPowerUsage() +
|
|
|
|
mMag.getPowerUsage() +
|
|
|
|
mGyro.getPowerUsage();
|
2011-05-18 05:54:42 +00:00
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t SensorFusion::getMinDelay() const {
|
|
|
|
return mAcc.getMinDelay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorFusion::dump(String8& result, char* buffer, size_t SIZE) {
|
|
|
|
const Fusion& fusion(mFusion);
|
2011-05-28 01:18:13 +00:00
|
|
|
snprintf(buffer, SIZE, "9-axis fusion %s (%d clients), gyro-rate=%7.2fHz, "
|
|
|
|
"q=< %g, %g, %g, %g > (%g), "
|
|
|
|
"b=< %g, %g, %g >\n",
|
2011-05-18 05:54:42 +00:00
|
|
|
mEnabled ? "enabled" : "disabled",
|
|
|
|
mClients.size(),
|
2013-05-30 21:18:23 +00:00
|
|
|
mEstimatedGyroRate,
|
2011-05-18 05:54:42 +00:00
|
|
|
fusion.getAttitude().x,
|
|
|
|
fusion.getAttitude().y,
|
|
|
|
fusion.getAttitude().z,
|
2011-05-28 01:18:13 +00:00
|
|
|
fusion.getAttitude().w,
|
|
|
|
length(fusion.getAttitude()),
|
2011-05-18 05:54:42 +00:00
|
|
|
fusion.getBias().x,
|
|
|
|
fusion.getBias().y,
|
|
|
|
fusion.getBias().z);
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|