1

現在、「Sensor.TYPE_ORIENTATION」は廃止されたため、置き換えようとしています。そのため、 Android ドキュメントには、その方法に関するほとんどの情報が欠けています。ここでデバッグと掘り下げを行っているときに、 OrientationSensorazimuthによって提供されていたを計算する方法を見つけました。私はこのようにします:

float accelerometerVals[] = new float[3];
float magneticVals[] = new float[3];

float orientationSensorVals[] = new float[3]; 

float rotationMatrix[] = new float[16];
float orientation[] = new float[3];

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
             System.arraycopy(event.values, 0, accelerometerVals, 0, 3);
             break;
        case Sensor.TYPE_MAGNETIC_FIELD:
              System.arraycopy(event.values, 0, magneticVals, 0, 3);
              break;
        case Sensor.TYPE_ORIENTATION:
              System.arraycopy(event.values, 0, orientationSensorVals, 0, 3);
              break;
        default:
              break;
        }

        if (null != magneticVals && null != accelerometerVals){
            SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerVals, magneticVals)
            SensorManager.getOrientation(rotationMatrix, orientation);
            float azimuth = Math.toDegrees(orientation[0])
            //this calculation gives me the Azimuth in the same format that OrientationSensor
            azimuth += (azimuth >= 0) ? 0 : 360
            float FALSE_PITCH = Math.toDegrees(orientation[1])
            float FALSE_ROLL = Math.toDegrees(orientation[2])
        }    

変数 FALSE_PITCH && FALSE_ROLL に注意してください。OrientationSensor 内で以前と同じ出力を得るために、この値を「正規化」する方法がわかりません (値は +10 から -10 まで異なります)。

4

2 に答える 2

6

私は同じ問題を経験しましたが、私にとってはそうであるように、あなたのロールが正しくないことに驚いています. これが私が使用したものです:

//Let values[] be your orientation[] variable once in degrees.

// Azimuth scaling
if (values[0]<0) values[0] += 360;

// Pitch scaling
if (values[1]<-90) values[1] += (-2*(90+values[1]));
else if (values[1]>90) values[1] += (2*(90-values[1]));

// Roll scaling
// NOT NEEDED

方向センサーと角度の方向[]変数の両方の出力を投稿できますか?

于 2012-04-18T08:08:51.227 に答える
1

多分それはあなたを助けるでしょう!!

SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
boolean orientok = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);

//test if orientation sensor exist on the device
if (!orientok){
        mSensorManager.unregisterListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
        ((TextView)findViewById(R.id.orient)).setText("no orientation sensor");
    }

public void onSensorChanged(SensorEvent event) {
    switch(event.sensor.getType()){
    case Sensor.TYPE_ORIENTATION:
        onOrientChanged(event);
        break;
                    ...// add other sensor type if you want and create for each one his function like the example below
}

 public void onOrientChanged(SensorEvent event) {
    float azimuth,pitch,roll;
    azimuth = event.values[0];
    pitch = event.values[1];
    roll = event.values[2];
    ((TextView)findViewById(R.id.azimuth)).setText("Axe x "+azimuth);
    ((TextView)findViewById(R.id.pitch)).setText("Axe y "+pitch);
    ((TextView)findViewById(R.id.roll)).setText("Axe z "+roll);
}
于 2012-04-18T14:25:58.173 に答える