android 指南针不稳定,Android指南针方向不可靠(低通滤波器)

虽然我没有在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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值