javascript - 如何计算给定 (x,y,z) 磁力计和加速度计数据的航向 (N/W/S/E)?

标签 javascript geolocation core-location sensors cllocation

我正在使用 react-native-sensor从这些传感器获取原始数据。

import {magnetometer, acclerometer} from 'react-native-sensors';
const subscription = accelerometer.subscribe(({ x, y, z, timestamp }) =>
    console.log({ x, y, z, timestamp })
    this.setState({ accelerometer: { x, y, z, timestamp } })
);
const subscription = magnetometer.subscribe(({ x, y, z, timestamp }) => 
    console.log({ x, y, z })
    this.setState({ magnetometer: { x, y, z, timestamp } })
);

给定这6个数据点,我怎样才能得到度数和方向呢?什么是合适的算法?

我不明白这个answer中的算法.这个答案利用了 alpha、beta、gamma……与“x、y、z”相同吗?为什么只使用 3 个数据点而不是 6 个?为什么一些其他答案说需要加速度计数据(用于倾斜调整?)。为什么没有利用所有 6 个数据点的答案?

(注意:文档中有“magenetometer”的拼写错误)

最佳答案

Background

磁力计测量地球磁场。此信息与手机内部的加速器相结合。加速器获取有关手机在空间中位置的信息。它能够通过手机内的固态传感器精确定位手机的位置,这些传感器可以测量手机的倾斜度和运动。根据算法软件开发公司 Sensor Platforms 的说法,这些设备提供的信息意味着无论手机处于哪个方向,指南针应用程序都可以显示主要方向。

类似project: compass-react-native-non-expoMIT License 下仅使用设备内置的磁力计传感器,使用包 react-native-sensors 识别方向并计算 Angular ,使用来自磁力计的 3 个数据点:

subscribe = async () => {
    new Magnetometer({
      updateInterval: 100
    })
    .then(magnetometerObservable => {
      this._subscription = magnetometerObservable;
      this._subscription.subscribe(sensorData => {
        console.log(sensorData);
        this.setState({magnetometer: this._angle(sensorData)});
      });
    })
    .catch(error => {
      console.log("The sensor is not available");
    });
  };

  _unsubscribe = () => {
    this._subscription && this._subscription.stop();
    this._subscription = null;
  };

  _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);
  };

  _direction = (degree) => {
    if (degree >= 22.5 && degree < 67.5) {
      return 'NE';
    }
    else if (degree >= 67.5 && degree < 112.5) {
      return 'E';
    }
    else if (degree >= 112.5 && degree < 157.5) {
      return 'SE';
    }
    else if (degree >= 157.5 && degree < 202.5) {
      return 'S';
    }
    else if (degree >= 202.5 && degree < 247.5) {
      return 'SW';
    }
    else if (degree >= 247.5 && degree < 292.5) {
      return 'W';
    }
    else if (degree >= 292.5 && degree < 337.5) {
      return 'NW';
    }
    else {
      return 'N';
    }
  };

  // Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
  _degree = (magnetometer) => {
    return magnetometer - 90 >= 0 ? magnetometer - 90 : magnetometer + 271;
};

另一个project: react-native-sensor-manager使用来自磁力计和加速度计的6个数据点来计算方向:

 float[] mGravity;
float[] mGeomagnetic;

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
  Sensor mySensor = sensorEvent.sensor;
  WritableMap map = mArguments.createMap();

  if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER)
    mGravity = sensorEvent.values;
  if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    mGeomagnetic = sensorEvent.values;
  if (mGravity != null && mGeomagnetic != null) {
    float R[] = new float[9];
    float I[] = new float[9];
    boolean success = mSensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
    if (success) {
      long curTime = System.currentTimeMillis();
      float orientation[] = new float[3];
      mSensorManager.getOrientation(R, orientation);

      float heading = (float)((Math.toDegrees(orientation[0])) % 360.0f);
      float pitch = (float)((Math.toDegrees(orientation[1])) % 360.0f);
      float roll = (float)((Math.toDegrees(orientation[2])) % 360.0f);

      if (heading < 0) {
        heading = 360 - (0 - heading);
      }

      if (pitch < 0) {
        pitch = 360 - (0 - pitch);
      }

      if (roll < 0) {
        roll = 360 - (0 - roll);
      }

      map.putDouble("azimuth", heading);
      map.putDouble("pitch", pitch);
      map.putDouble("roll", roll);
      sendEvent("Orientation", map);
      lastUpdate = curTime;
    }
  }
}

others too .

关于javascript - 如何计算给定 (x,y,z) 磁力计和加速度计数据的航向 (N/W/S/E)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55034145/

相关文章:

iphone - iPhone上的 "X would like to use your current location"背后的规则是什么?

javascript - Deftjs promise 完成()不工作

javascript - W/网络核心(2733): Can't get the viewWidth after the first layout

javascript - 根据条件查找mongodb(在mongoose+node.JS中)记录

iPhone核心位置: distance filter how does it work?

ios - 在用户拒绝使用定位服务后,如何提示用户打开定位服务

javascript - 如何根据 JSON 文件中的 bool 值自动更改 img

javascript - 从 Node 使用 mocha js 中的全局窗口变量

ios - 如何避免 XCode 每次运行时都重置我的 iPhone 上的位置

django - 如何使用地理位置 API 向用户位置提供默认点域值,无法将字符串转换为 float :