虽然我没有在Android上使用指南针,但下面显示的基本处理(在JavaScript中)可能适用于您.
它基于加速度计上的低通滤波器,它是由Windows Phone team推荐的,适用于罗盘(每360°循环行为)进行修改.
我假设罗盘读数是以度为单位,浮点数在0-360之间,输出应该相似.
你想在过滤器中完成2件事情:
If the change is small, to prevent gitter, gradually turn to that direction.
If the change is big, to prevent lag, turn to that direction immediatly (and it can be canceled if you want the compass to move only in a smooth way).
为此,我们将有2个常量:
The easing float that defines how smooth the movement will be (1 is no smoothing and 0 is never updating, my default is 0.5). We will call it SmoothFactorCompass.
The threshold in which the distance is big enough to turn immediatly (0 is jump always, 360 is never jumping, my default is 30). We will call it SmoothThresholdCompass.
我们在调用中保存了一个变量,一个称为oldCompass的float,它是算法的结果.
所以可变防御是:
var SmoothFactorCompass = 0.5;
var SmoothThresholdCompass = 30.0;
var oldCompass = 0.0;
并且函数接收newCompass,并返回oldCompass作为结果.
if (Math.abs(newCompass - oldCompass) < 180) {
if (Math.abs(newCompass - oldCompass) > SmoothThresholdCompass) {
oldCompass = newCompass;
}
else {
oldCompass = oldCompass + SmoothFactorCompass * (newCompass - oldCompass);
}
}
else {
if (360.0 - Math.abs(newCompass - oldCompass) > SmoothThresholdCompass) {
oldCompass = newCompass;
}
else {
if (oldCompass > newCompass) {
oldCompass = (oldCompass + SmoothFactorCompass * ((360 + newCompass - oldCompass) % 360) + 360) % 360;
}
else {
oldCompass = (oldCompass - SmoothFactorCompass * ((360 - newCompass + oldCompass) % 360) + 360) % 360;
}
}
}
我看到这个问题在5个月前已经开放了,可能还没有关系,但我确定其他程序员可能会发现它很有用.
Oded Elyada.