The sensor in Android --- gyroscope sensor

The gyroscope sensor is called Gyro-sensor. The gyroscope measures the rad/s rotation rate around the x, y, and z axes of the device, and returns the angular velocity data of the x, y, and z axes.

The unit of angular velocity is radians/second.

The coordinate system of the sensor is the same  as that used for the acceleration sensor . Counterclockwise rotation is positive. That is, if an observer looks at a device at the origin from some positive position on the x, y, or z axis, the observer will report a positive rotation if the device appears to be rotating counterclockwise. This is the standard mathematical definition of positive rotation, as opposed to the definition of roll used by the orientation sensor.

Typically, the output of a gyroscope is integrated over time to compute a rotation that describes the change in angle over time steps. Standard gyroscopes provide raw rotation data without any filtering or correction for noise and drift (bias). In practice, gyroscope noise and drift introduce errors that need to be compensated for. Typically, you can determine drift (bias) and noise by monitoring other sensors such as gravity sensors or accelerometers.


public class MagneticFieldActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor sensor;
    private TextView mMagneticField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magnetic_field);
        mMagneticField=findViewById(R.id.tv_magnetic_field);
        sensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor=sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE );
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (sensorManager!=null){
            sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_UI);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this,sensor);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // 传感器返回的数据
        float x=event.values[0];
        float y=event.values[1];
        float z=event.values[2];
        StringBuffer buffer = new StringBuffer();
        buffer.append("X:").append(String.format("%.2f", x)).append("\n");
        buffer.append("Y:").append(String.format("%.2f", y)).append("\n");
        buffer.append("Z:").append(String.format("%.2f", z)).append("\n");
        mMagneticField.setText(buffer);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

For more mobile phone sensor usage, please see Sensors in Android (total)

Guess you like

Origin blog.csdn.net/lanrenxiaowen/article/details/108236348