我正在开发一个使用react-native-sensors磁力计的指南针应用程序。我得到了正确的值,指南针工作得很好,主要问题是指南针更新太快,方向变化太频繁,变化是+-5度。我想做一个平滑的指南针。
_angle = (magnetometer) => {
if (magnetometer) {
let { x, y, z } = magnetometer
if (Math.atan2(y, x) >= 0) {
angle = Math.atan2(y, x) * (180 / Math.PI)
} else {
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI)
}
}
return Math.round(angle)
}
//Inside ComponentDidMount
magnetometer.subscribe(({ x, y, z, timestamp }) =>
this.setState({ sensorValue: this._angle({ x, y, z }) })发布于 2019-08-02 20:11:51
找到了一个听起来像SamuelPS's答案的答案,我使用了LPF:Low Pass Filter for JavaScript它只是更优化,更流畅。
constructor(props) {
super(props)
LPF.init([])
}
_angle = (magnetometer) => {
if (magnetometer) {
let { x, y, z } = magnetometer
if (Math.atan2(y, x) >= 0) {
angle = Math.atan2(y, x) * (180 / Math.PI)
} else {
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI)
}
}
return Math.round(LPF.next(angle))
}发布于 2019-08-01 20:33:01
我会提出两件事。
不要用磁力计的每一个输出更新你的状态。取而代之的是,对数据进行某种过滤。一个简单的例子可以是减少采样。假设磁力计为您提供了1000个样本/秒(我虚构了数据)。视图每秒更新1000次太多了,而不是创建一个包含200个样本的缓冲区,并在每次它满的时候设置这200个样本的平均值的状态。在这种情况下,你每秒只有5次更新,大大减少了震动的感觉。在这里使用不同的值进行一些实验,直到找到所需的输出。如果你想要更平滑的东西,重叠的缓冲区也可以工作: 200个样本缓冲区,但不是每次都重置缓冲区,你只需删除第一个100个。因此,样本减少了1/10,但每个输出都是100个新样本和100个已经影响输出的样本之间的平均值。
第二件事是不要将指南针直接设置在磁力计的位置上,否则,看起来就像是指针在跳跃(零点平滑)。若要创建过渡动画以在更改位置时产生平滑移动,请执行以下操作。
有了这两件事,它应该可以顺利地工作。我希望这些信息是有用的,祝你的指南针好运!
发布于 2020-04-10 06:46:34
添加到阿卜杜拉·叶海亚的回答中,安装并导入LPF模块。设置LPF平滑值,并检查波动是否仍然存在。
import LPF from "lpf";
constructor() {
super();
LPF.init([]);
LPF.smoothing = 0.2;
}
_angle = magnetometer => {
let angle = 0;
if (magnetometer) {
let {x, y} = magnetometer;
if (Math.atan2(y, x) >= 0) {
angle = Math.atan2(y, x) * (180 / Math.PI);
} else {
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
}
}
return Math.round(LPF.next(angle));
};有关详细信息,请参阅此repo - react-native-compass。
https://stackoverflow.com/questions/57308560
复制相似问题