android - 使用 react-native-sensors 的磁力计的平滑定向指南针
问题描述
我正在使用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 的答案相似,我使用了 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))
}
推荐阅读
- multilingual - 多语种 Ghost 具有相同的蛞蝓
- android - 使用 ajax 发布到 IP 在 Cordova 中不起作用,但在手机浏览器中可以正常工作
- java - 我在 Excel 表中的单元格迭代器和空白单元格类型方面面临问题。如何动态处理空白单元格和空白列调整
- android - 如何在 NodeJS+Android 中处理 Google 登录
- java - 私有的、受保护的访问修饰符和抽象之间的区别
- java - 可以使用什么方法来获得该地区的准确日期和时间?
- java - 尝试使用 MouseListener 和 MouseMotionListener 创建圈子——我做错了什么?
- python - ImageDataGenerator 不喜欢我的 fashionMNIST 数据集。它需要哪个输入?
- android - 在通知上打开网站点击 Android
- python - 在 Mac 上安装 pyfastext 时出错:命令“cc”失败,退出状态为 1