android - 当设备安装在移动的车辆上时,Compass App Roll/Y 值不变
问题描述
提前感谢任何可以帮助解决这个问题的人。
我是 Android Dev 的新手,并创建了一个 Compass 应用程序来帮助使用设备传感器确定航向。当设备本身旋转(静止时)时,指南针返回正确的值,但当它安装在车辆中时,旋转值保持不变。我检查了 Compass 应用程序的其他实现,似乎没有任何我遗漏的东西。
关于可能是什么问题的任何见解?
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager mySensorManager;
private float[] accelVals = new float[3];
private float[] magVals = new float[3];
private float[] rotationMatrix = new float[16];
private float[] orientationVals = new float[3];
int Pose;
TextView displayData2;
Sensor myMagneticSensor, myAccelerometer;
// System display. Need this for determining rotation.
private Display mDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayData2 = (TextView) findViewById(R.id.data2);
mySensorManager = (SensorManager) getSystemService(
Context.SENSOR_SERVICE);
myAccelerometer = mySensorManager.getDefaultSensor(
Sensor.TYPE_ACCELEROMETER);
myMagneticSensor = mySensorManager.getDefaultSensor(
Sensor.TYPE_MAGNETIC_FIELD);
}
@Override
protected void onStart() {
super.onStart();
// Listeners for the sensors are registered in this callback and
// can be unregistered in onStop().
//
// Check to ensure sensors are available before registering listeners.
// Both listeners are registered with a "normal" amount of delay
// (SENSOR_DELAY_NORMAL).
if (myAccelerometer != null) {
mySensorManager.registerListener(this, myAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
if (myMagneticSensor != null) {
mySensorManager.registerListener(this, myMagneticSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onStop() {
super.onStop();
// Unregister all sensor listeners in this callback so they don't
// continue to use resources when the app is stopped.
mySensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER: {
accelVals = event.values.clone();
break;
}
case Sensor.TYPE_MAGNETIC_FIELD: {
magVals = event.values.clone();
break;
}
}
SensorManager.getRotationMatrix(rotationMatrix, null, accelVals,
magVals);
SensorManager.getOrientation(rotationMatrix, orientationVals);
// Convert the result from radians to degrees
orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
orientationVals[2] = (float) (Math.toDegrees(orientationVals[2]) + 360) % 360 ;
Pose = Math.round(orientationVals[2]);
//Print Data
displayData2.setText("Pose: " + Pose);
// displayData2.setText("Z: " + Float.toString(orientationVals[2]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
解决方案
我找到了解决上述问题的方法,通过实现复合 Rotation_Vector 传感器,通过将向量值解析为“.getRoationMatrixFromVector”方法来检索旋转矩阵,如下所示。
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
来自上述替代过程的rotationMatrix 值似乎与地球坐标系相关,而不是静止时设备的坐标系。
推荐阅读
- c++ - 在矩阵模板类中重载运算符 *= 和 Operator+
- java - 键入字符时 onKeyReleased 不起作用
- c++ - Assimp - 如何使用任何文件格式导入带有纹理的网格?
- apache-ranger - Ranger 增量 AD 同步是否每次都会覆盖或保留以前同步的用户(例如,如果过滤器更改)?Unix用户重新创建?
- bash - 如何使用 bash 将包含“姓氏,名字”的 csv 列分成两个不同的 csv 列?
- java - 开始活动(意图);似乎总是使应用程序崩溃
- javascript - 在 react-table 中,我结合了基本的排序和调整大小的示例,但想在调整列大小时抑制排序
- string - Mule 3.8.4 中的 for 循环 数据编织 1.0
- asp.net - 如何在 IIS 上提供已编译的 Web 服务?
- ruby-on-rails - 如何获取`reject_if`的对象?