Android Gyroscope verse Accelerometer

夙愿已清 提交于 2021-02-07 09:21:47

问题


I am building an Android game and I want to figure out whether the user tilts the device to the left or the right (Similar to how Temple Run works when you move the man from side to side).

I have read many tutorials and examples and I made sample applications but the amount of data I get back from both the Gyroscope and the Accelerometer are overwhelming. Would I need both sets of hardware to work out whether the user tilts the device and in which direction?

My current application is detecting every slight movement and that is obviously not correct.

public class Main extends Activity {

  private SensorManager mSensorManager;
  private float mAccel; // acceleration apart from gravity
  private float mAccelCurrent; // current acceleration including gravity
  private float mAccelLast; // last acceleration including gravity
  private RelativeLayout background;
  private Boolean isleft = true;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.background = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;

   /* float x1, x2, y1, y2;
    String direction;
    switch(event.getAction()) {
            case(MotionEvent.ACTION_DOWN):
                x1 = event.getX();
                y1 = event.getY();
                break;
            case(MotionEvent.ACTION_UP) {
                x2 = event.getX();
                y2 = event.getY();
                float dx = x2-x1;
                float dy = y2-y1;

                    // Use dx and dy to determine the direction
                if(Math.abs(dx) > Math.abs(dy)) {
                    if(dx>0) directiion = "right";
                    else direction = "left";
                } else {
                    if(dy>0) direction = "down";
                    else direction = "up";
                }
            }
            }*/
}

private final SensorEventListener mSensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent se) {

      float x = se.values[0];
      float y = se.values[1];
      float z = se.values[2];

      if((mAccelLast<mAccelCurrent)&&(isleft == true)){
          background.setBackgroundResource(R.drawable.bg_right);
          isleft = false;
      }
      if((mAccelLast>mAccelCurrent)&&(isleft == false)){
          background.setBackgroundResource(R.drawable.bg_left);
          isleft = true;
      } 
      mAccelLast = mAccelCurrent;
      mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
      float delta = mAccelCurrent - mAccelLast;
      Log.d("FB", "delta : "+delta);
      mAccel = mAccel * 0.9f + delta; // perform low-cut filter
     // Log.d("FB", "mAccel : "+mAccel);

    }

Would I be better off using just the accelerometer, just the gyroscope or would I need both?


回答1:


This post links to the differences between the two: Android accelerometer and gyroscope

http://diydrones.com/profiles/blogs/faq-whats-the-difference

http://answers.oreilly.com/topic/1751-mobile-accelerometers-and-gyroscopes-explained/

The documentation will also help: http://developer.android.com/guide/topics/sensors/sensors_motion.html

From my VERY limited experience, the gyro constantly measures the x, y, z rotation and keeps updating. Useful for steering a car/plane/character in a game. The accelerometer is a little more like a wii-mote, for swinging around or picking up a shake gesture.




回答2:


From my experience with accelerometer using the gravity method, you can use it for x, y and z rotation. Just type in google "vector method accelerometer". I have used this method with a compass for correcting the coordinates due to tilt.



来源:https://stackoverflow.com/questions/12140521/android-gyroscope-verse-accelerometer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!