首页 > 解决方案 > 当设备安装在移动的车辆上时,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

    }

标签: androidandroid-studioandroid-layoutandroid-sensors

解决方案


我找到了解决上述问题的方法,通过实现复合 Rotation_Vector 传感器,通过将向量值解析为“.getRoationMatrixFromVector”方法来检索旋转矩阵,如下所示。

SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

来自上述替代过程的rotationMatrix 值似乎与地球坐标系相关,而不是静止时设备的坐标系。


推荐阅读