5

and so I needed to know when the android-based phone was rotated the x,y,z directions for a tilt-based program and then I used the well circulated code that uses magnetism and acceleration to find orientation since the orientation detector is unreliable and this was well and good in the x,z planes but lets say a person was on a circularly moving bus holding their phones in a fixed landscape position and then the android registers y-direction movement even though the user didnt move their phones in the y direction and so the program fails but just using acceleration alone also isn/t accurate and i am wondering how to solve this problem of detecting y- motion relative to the user?

public class MotionListener {
        String service_name = Context.SENSOR_SERVICE;
        SensorManager sensorManager;
        Sensor sensor;

        public MotionListener(Context context) {
            sensorManager = (SensorManager) context.getSystemService(service_name);
            SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
            Sensor aSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            Sensor mfSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
            sm.registerListener(myAccelerometerListener, aSensor, SensorManager.SENSOR_DELAY_GAME);
            sm.registerListener(myMagneticFieldListener, mfSensor, SensorManager.SENSOR_DELAY_GAME);
        }

        float[] accelerometerValues;
        float[] magneticFieldValues;
        final SensorEventListener myAccelerometerListener = new SensorEventListener() {
            public void onSensorChanged(SensorEvent sensorEvent) {
                if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                    accelerometerValues = sensorEvent.values;
                float[] values = new float[3];
                float[] R = new float[9];
                try {
                    SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues);
                    SensorManager.getOrientation(R, values);
                    values[0] = (float) ((float) values[0] * 180 / Math.PI);
                    values[1] = (float) ((float) values[1] * 180 / Math.PI);
                    values[2] = (float) ((float) values[2] * 180 / Math.PI);
                    System.out.println((int)values[0] + "   " + (int)values[1] + "        " + (int)values[2]);
                    setTiltCoordinates(values);
                } catch (NullPointerException e) {
                }
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }
        };
2
  • 3
    not possible - without feeding the orientation of the user to the device - which is a completely different question
    – KevinDTimm
    Mar 12, 2012 at 20:26
  • Nope. Can't do it. Frames of reference, etc. As a user, trying to use such an app under those conditions is an exercise in futility, anyway. Mar 22, 2012 at 3:59

1 Answer 1

4
+25

Well, if you are just looking at the accelerometer data, all you have is three values in x, y and z directions. One of these will have the greatest pull, and you can usually take this to mean that is the direction 'down' is.

There can be issues with movement, such as if the user spins on the spot, making the phone feel acceleration that is not due to gravity, if this is great enough, it might start to look that direction is 'down', how ever, you would have to be spinning fast enough to create close to one g of acceleration, which I very much doubt a bus would be able to do.

You next big problem, is that the x, y and z data values are just three perpendicular axis to which each handset could be orientated to differently. On one phone, 'y' (what is normally considered the up and down axis) might be through the screen, with the screen on the zx, plane. Another phone might have y at an angle to the screen.

So, from just accelerometer data you can't tell what direction orientation the phone is. Though, perhaps the Android SDK accounts for this and 'adjusts' the real accelerometer data to allow all phones to see the same data.

3
  • I should have been more clear, the app moves an object around via tilting. Lets say the user wishes to perpetually move the object west, then they would need to keep pumping the phone if acceleration was the only sensor. This doesn't work. Mar 23, 2012 at 15:30
  • you mean you want to do something like tilt a ball around on the phone whilst it is held flat...
    – thecoshman
    Mar 23, 2012 at 15:37
  • Or do you want orientation data around an axis? Mar 23, 2012 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.