我怎样才能在android中获得旋转矢量传感器的值

时间:2011-07-23 09:19:23

标签: android

我的目标是从Android设备计算几乎所有传感器的值..我已经完成了除了旋转矢量传感器..但我也想知道我的代码的真实性..是所有值显示正确..这里是我的代码..

public void onSensorChanged(SensorEvent sensorEvent){         synchronized(this){

        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            long curTime = System.currentTimeMillis();
            if (lastUpdate == -1 || (curTime - lastUpdate) > 500) {
                lastUpdate = curTime;

                gravity[0] = alpha * gravity[0] + (1 - alpha) * sensorEvent.values[0];
                gravity[1] = alpha * gravity[1] + (1 - alpha) * sensorEvent.values[1];
                gravity[2] = alpha * gravity[2] + (1 - alpha) * sensorEvent.values[2];

                linear_acceleration[0] = sensorEvent.values[0] - gravity[0];
                linear_acceleration[1] = sensorEvent.values[1] - gravity[1];
                linear_acceleration[2] = sensorEvent.values[2] - gravity[2];

                acceleration[0] = gravity[0] + linear_acceleration[0];
                acceleration[1] = gravity[1] + linear_acceleration[1];
                acceleration[2] = gravity[2] + linear_acceleration[2];

                AccX.setText( "TYPE_ACCELEROMETER Sensor Values \n " +
                              "X = "+Float.toString( sensorEvent.values[0])+"  m/s^2");
                AccY.setText( "Y = "+Float.toString( sensorEvent.values[1])+"  m/s^2");
                AccZ.setText( "Z = "+Float.toString( sensorEvent.values[2])+"  m/s^2 \n");


                GravX.setText( "TYPE_GRAVITY Sensor Values \n " +
                               "X = "+Float.toString(gravity[0])+"  m/s^2");
                GravY.setText( "Y = "+Float.toString(gravity[1])+"  m/s^2");
                GravZ.setText( "Z = "+Float.toString(gravity[2])+"  m/s^2\n");

                LAccX.setText( "TYPE_LINEAR_ACCELERATION Sensor Values \n" +
                               "X = "+Float.toString(linear_acceleration[0])+"  m/s^2");
                LAccY.setText( "Y = "+Float.toString(linear_acceleration[1])+"  m/s^2 ");
                LAccZ.setText( "Z = "+Float.toString(linear_acceleration[2])+"  m/s^2\n ");
            }
        }

        if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            long curTime = System.currentTimeMillis();
            if (lastUpdate == -1 || (curTime - lastUpdate) > 500) {
                lastUpdate = curTime;

                OriX.setText("TYPE_ORIENTATION Values \n " +
                             "X = "+Float.toString( sensorEvent.values[0])+"  degree");
                OriY.setText("Y = "+Float.toString( sensorEvent.values[1])+"  degree");
                OriZ.setText("Z = "+ Float.toString( sensorEvent.values[2])+"  degree \n");
            }
        }

        if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            long curTime = System.currentTimeMillis();
            if (lastUpdate == -1 || (curTime - lastUpdate) > 500) {
                lastUpdate = curTime;

                magneticX.setText( "TYPE_MAGNETIC_FIELD Sensor Values \n " +
                                   "X = "+ Float.toString( sensorEvent.values[0])+"  micro-Tesla (uT) ");
                magneticY.setText( "Y = "+Float.toString( sensorEvent.values[1])+"  micro-Tesla (uT) ");
                magneticZ.setText( "Z = "+Float.toString( sensorEvent.values[2])+"  micro-Tesla (uT) \n");
            }
        }

        if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            long curTime = System.currentTimeMillis();
            if (lastUpdate == -1 || (curTime - lastUpdate) > 500) {
                lastUpdate = curTime;
                GyrX.setText( "TYPE_GYROSCOPE Sensor Values \n X = "+ Float.toString( sensorEvent.values[0])+"  ");
                GyrY.setText( "Y = "+Float.toString( sensorEvent.values[1])+"  ");
                GyrZ.setText( "Z = "+Float.toString( sensorEvent.values[2])+"  ");
            }
        }


    }

}

请告诉我如何显示旋转vcctor传感器值..

0 个答案:

没有答案