views:

19

answers:

0

I'm trying to translate the accelerometer values from the device coordinates to the world coordinates. It's not clear how to do this. I guess getRotationMatrix method can do sth about this,and I use the matrix R the method generated multiply accelerometer values but I did not get what I expected.

Here is my code:

public void onSensorChanged(SensorEvent event) {

            if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
                    t1.setText("X:"+event.values[0]);
                    t2.setText("Y:"+event.values[1]);
                    t3.setText("Z:"+event.values[2]);
                    e1=event.values.clone();
                    b=true;

            }
            if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){
                    t8.setText("X:"+event.values[0]);
                    t9.setText("Y:"+event.values[1]);
                    t10.setText("Z:"+event.values[2]);
                    e2=event.values.clone();
                    b=true;
            }
            if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){
                    t11.setText("X:"+event.values[0]);
                    t12.setText("Y:"+event.values[1]);
                    t13.setText("Z:"+event.values[2]);

            }
            if(e1!=null&e2!=null&b==true){
                    float[] R=new float[9];
                    float[] I=new float[9];
                    float[] outR=new float[9];
                    float[] e3=new float[3];
                    boolean b=SensorManager.getRotationMatrix(R,I, e1, e2);
                    float[] result=new float[3];
                    result[0]=R[0]*e1[0]+R[1]*e1[1]+R[2]*e1[2];
                    result[1]=R[3]*e1[0]+R[4]*e1[1]+R[5]*e1[2];
                    result[2]=R[6]*e1[0]+R[7]*e1[1]+R[8]*e1[2];

                    t19.setText("X:"+e3[0]);
                    t20.setText("Y:"+e3[1]);
                    t21.setText("Z:"+e3[2]);

                    if(result[0]>10.0f|result[1]>10.0f){
                            Log.i("aaa", "X:"+result[0]+" Y:"+result[1]+" Z:"+result[2]);
                    }
                    b=false;
            }
    }

The above code seems can do sth expected.When the device stays statically,result[0] and result[1] approach 0 and result[2] approaches standard gravity.But when you give the device a acceleration in any direction,result[0] and result[1] are still 0 while result[2] varies a lot. Any help would be appreciated.

KYLE