androidaccelerometergyroscope

gyroscope and accelerometer are inverted


I'm using both gyroscope and accelerometer in an Android. What I do is only displaying values from both sensors. I don't get why to track device acceleration, I have to use gyroscope and why the device orientation is given by accelerometer sensor. I have test this code on 2 tablets, 3 phones and result are the same.

Listeners :

// gyroscope sensor
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);

// accelerometer sensor
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

And to get the result I implement SensorEventListener :

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    switch (sensorEvent.sensor.getStringType()){
        case Sensor.STRING_TYPE_ACCELEROMETER:
            (TextView)findViewById(R.id.sensor_accel_data_textView)).setText(String.valueOf(sensorEvent.values[0]));
            ((TextView)findViewById(R.id.sensor_accel_data_textView2)).setText(String.valueOf(sensorEvent.values[1]));
            ((TextView)findViewById(R.id.sensor_accel_data_textView3)).setText(String.valueOf(sensorEvent.values[2]));
            break;
        case Sensor.STRING_TYPE_GYROSCOPE:
            ((TextView)findViewById(R.id.sensor_gyro_data_textView)).setText(String.valueOf(sensorEvent.values[0]));
            ((TextView)findViewById(R.id.sensor_gyro_data_textView2)).setText(String.valueOf(sensorEvent.values[1]));
            ((TextView)findViewById(R.id.sensor_gyro_data_textView3)).setText(String.valueOf(sensorEvent.values[2]));
            break;
    }
}

Solution

  • This question is almost a year old, but I just stumbled onto it and got the impression that the root of the question is a misunderstanding of what each sensor actually measures.

    So, hopefully the following explanation clarifies a bit that the sensors are not switched, but that in fact accelerometers are used to detect orientation and that the (imho badly named) gyroscope does not provide an absolute orientation.

    Accelerometer

    You should imagine an accelerometer as a sample mass, which is held by some springs, because that is what an accelerometer is (you can search for MEMS accelerometer to find examples on how this is actually implemented on a micrometer scale). If you accelerate the device, the mass will push against the springs because of inertia and the tiny deflection of the mass is measured to detect the acceleration. However, the mass is not only deflected by an actual acceleration but also by gravitational pull. So, if your phone is resting on the table, the mass is still deflected towards the ground.

    So, the "-10" to "10" you see is earth's acceleration at 9.81 m/s². From a physics perspective, this is confusing because the resting device is obviously not being accelerated while the sensor still shows 9.81 m/s², so we get the device acceleration plus earth's acceleration from the sensor. While this is the nightmare of any physics teacher, it is extremely helpful because it tells you where "down" is and hence gives you the orientation of the device (except for rotations around the axis of gravity).

    Gyroscope

    The sensor called "gyroscope" is another sample mass in your device, which is actively driven to vibrate. Because of the vibration movement it is subject to the Coriolis force (in its frame of reference) and gets deflected when rotating (searching for "MEMS gyroscope" yields even more astonishing devices, especially if you think about the fact that they can detect the Coriolis force on that scale).

    However, the deflection does not allow you to determine an absolute angle (as an old-fashioned gyroscope would), but instead it is proportional to the angular velocity (rate of rotation). Therefore, you can expect a reading of zero on all axes for a resting device and will only see a reading for any axis as long as you are rotating the device about this axis.