android - 使用 react-native-sensors 的磁力计的平滑方向指南针

标签 android react-native 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'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/

相关文章:

android - 使用 AsyncHttpClient loopj 返回值

android - 在android中将音频文件与捕获的视频合并

javascript - 在给定(x,y,z)陀螺仪的情况下,如何检测设备旋转 360 度?

react-native - 如何从 React Native 传感器中的加速度计值中减去重力

java - 有没有办法使用 for 循环获取 RES > value > strings.xml 项值?

android - 使用 OpenStreetMap 获得最高速度

facebook - returnKeyType "next"不起作用

javascript - React Native - TypeScript 中带有 ref 回调的高阶组件抛出编译错误

javascript - 当使用react-native-router-flux和react-redux时, Prop 没有被传递到组件中?