我正在使用Sensortag CC2541(连接到 Nexus 手机)
我正在使用这个传感器的磁力计来制作一个简单的指南针。我正在使用以下等式来查找北、南、西和东。
Direction (y>0) = 90 - [arcTAN(x/y)]*180/pi
Direction (y<0) = 270 - [arcTAN(x/y)]*180/pi
Direction (y=0, x<0) = 180.0
Direction (y=0, x>0) = 0.0
这是我在updateMagnetometer方法中使用的代码
@Override
public void onUpdateMagnetometer(SensorTagManager mgr, Point3D b) {
super.onUpdateMagnetometer(mgr, b);
double m=0;
if(b.x < 0.0 && (b.y < 0.01 && b.y > 0.0))
m = 180;
else if(b.x > 0.0 && (b.y < 0.01 && b.y > 0.0))
m = 0;
else if(b.y > 0.0)
m = 90 - (Math.atan(b.x/b.y))*(180/Math.PI);
else if(b.y < 0.0)
m = 270 - (Math.atan(b.x/b.y))*(180/Math.PI);
final float rot = (float) m;
runOnUiThread(new Runnable() {
@Override
public void run() {
pointer.setRotation(-rot); //pointer is the image of my compass
}
});
}
但是当我运行它时,无论我移动、旋转或改变传感器的位置多少,指南针总是卡在北方和西方之间。
我可能会犯什么错误?
而且,我在网上看了很多指南针的帮助。但我什么也找不到。任何编码技巧、伪代码或使用磁力计、加速度计和/或陀螺仪制作指南针的提示???
额外信息
所以这是我的主要课程。这些方法每秒钟调用一次x
以更新读数。PS:我的传感器有 2 个按钮(左右)
mStManager.enableSensor(Sensor.MAGNETOMETER,MAGNETOMETER_UPDATE_PERIOD);
mStManager.enableSensor(Sensor.ACCELEROMETER,MAGNETOMETER_UPDATE_PERIOD);
@Override
public void onUpdateAmbientTemperature(SensorTagManager mgr, double temp) {
super.onUpdateAmbientTemperature(mgr, temp);
}
@Override
public void onUpdateAccelerometer(SensorTagManager mgr, Point3D acc) {
super.onUpdateAccelerometer(mgr, acc);
}
@Override
public void onUpdateBarometer(SensorTagManager mgr, double pressure, double height) {
super.onUpdateBarometer(mgr, pressure, height);
}
@Override
public void onUpdateGyroscope(SensorTagManager mgr, Point3D ang) {
super.onUpdateGyroscope(mgr, ang);
}
@Override
public void onUpdateHumidity(SensorTagManager mgr, double rh) {
super.onUpdateHumidity(mgr, rh);
}
@Override
public void onUpdateInfraredTemperature(SensorTagManager mgr, double temp) {
super.onUpdateInfraredTemperature(mgr, temp);
}
@Override
public void onUpdateKeys(SensorTagManager mgr, boolean left, boolean right) {
super.onUpdateKeys(mgr, left, right);
if (right) {
mgr.calibrateMagnetometer();
}
}
@Override
public void onUpdateMagnetometer(SensorTagManager mgr, Point3D b) {
super.onUpdateMagnetometer(mgr, b);
}
}
所以基本上在onUpdateMagnetometer
方法中,b
包含了我的阅读。它属于Point3D
如下所示的类
public class Point3D {
public double x, y, z;
public Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double norm() {
return Math.sqrt(x*x + y*y + z*z);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(z);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point3D other = (Point3D) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
return false;
if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z))
return false;
return true;
}
public String toString() {
return "[" + this.x + ", " + this.y + ", " + this.z + "]";
}
}