Thanks,but your result is the accurate rotation angles of axises. I just want to know how to get the accelerometer values translated into the world coordinate. Any thoughts?
On 3月2日, 下午7时49分, mscwd01 <[email protected]> wrote: > This is my Sensor Listener Class, I hope it helps: > > private class AccelMagListener implements SensorEventListener { > private static final int matrix_size = 16; > float[] R = new float[matrix_size]; > float[] outR = new float[matrix_size]; > float[] I = new float[matrix_size]; > float[] values = new float[3]; > > public void onAccuracyChanged(Sensor arg0, int arg1) { > } > > public void onSensorChanged(SensorEvent event) { > > if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) > return; > > switch (event.sensor.getType()) { > case Sensor.TYPE_MAGNETIC_FIELD: > mags = event.values.clone(); > break; > case Sensor.TYPE_ACCELEROMETER: > accels = event.values.clone(); > break; > } > > if (mags != null && accels != null) { > > SensorManager.getRotationMatrix(R, I, accels, mags); > > // Correct if screen is in Landscape > SensorManager.remapCoordinateSystem(R, > SensorManager.AXIS_X, > SensorManager.AXIS_Z, outR); > > SensorManager.getOrientation(outR, values); > > azimuth = Convert.radToDeg(values[0]); > pitch = Convert.radToDeg(values[1]); > roll = Convert.radToDeg(values[2]); > > } > > } > > }; > > On Mar 2, 9:39 am, Lotuz <[email protected]> wrote: > > > > > 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. > > > lotuz -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

