我正在使用 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 }) })
最佳答案
找到一个听起来类似于 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))
}
关于android - 使用 react-native-sensors 的磁力计的平滑方向指南针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308560/