使用加速度计和陀螺仪动态更新对象的位置

时间:2015-06-03 08:03:51

标签: android accelerometer gyroscope

我是android编程的初学者。下面是使用加速度计和陀螺仪读数更新物体当前位置的程序。我的目标是更新物体在屏幕上的位置。目前程序的输出是它只是显示对象并且它不会更新它的位置。你能告诉我我哪里出错了。

Mainactivity.java

公共类MainActivity扩展Activity实现SensorEventListener {

    CustomDrawableView mCustomDrawableView = null;
    ShapeDrawable mDrawable = new ShapeDrawable();

    int UPDATE_THRESHOLD=20;
    Sensor accelerometer,gyroscope;
    SensorManager sm,sm1;
    TextView acceleration,rotation,c_x,c_y;
    long mLastUpdate;

    public static int x;
    public static int y;


    double GRAVITY_THRESH = 12;

    double one_step_freeze = 0;
    double SENSOR_INTERVAL = 0.02;
    double STEP_INTERVAL_THRESH = 0.4;
    double init_x = 1;
    double init_y = 0;
    double init_theta = 0;
    static int ACCE_FILTER_DATA_MIN_TIME = 20; // 1000ms
    long lastSaved = System.currentTimeMillis();
    double step_counter = 0;
    double current_x,current_y;


    double one_step_happend = 0;
    double accumulated_rad = 0;
    double trace_x = init_x;
    double trace_x_particle = init_x;
    double trace_y = init_y;
    double trace_y_particle = init_y;
    double last_x = init_x;
    double last_y = init_y;
    double last_theta = init_theta;

    double z_gy;

    float acc_z,acc_x,acc_y;
    SensorEventListener mSensorListenerAcc,mSensorListenerGyro;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         //setContentView(R.layout.activity_main);
         sm=(SensorManager)getSystemService(SENSOR_SERVICE);
         sm1=(SensorManager)getSystemService(SENSOR_SERVICE);

         mCustomDrawableView = new CustomDrawableView(this);
         setContentView(mCustomDrawableView);

         accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
         gyroscope=sm1.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
         sm.registerListener(this, accelerometer,     SensorManager.SENSOR_DELAY_NORMAL );
               sm1.registerListener(this,gyroscope,SensorManager.SENSOR_DELAY_NORMAL);
    //acceleration=(TextView)findViewById(R.id.acceleration);
    //rotation=(TextView)findViewById(R.id.rotation);
   // c_x=(TextView)findViewById(R.id.c_x);
    //c_y=(TextView)findViewById(R.id.c_y);
}
/*@Override
protected void onResume()
{
    super.onResume();
    //sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
    sm.registerListener(mSensorListenerAcc, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
    sm1.registerListener(mSensorListenerGyro, gyroscope,SensorManager.SENSOR_DELAY_FASTEST);
    //mLastUpdate=System.currentTimeMillis();
}

@Override
protected void onPause()
{
    sm.unregisterListener(this);
}*/




@Override
public void onSensorChanged(SensorEvent event) {
  /*  long actualTime = System.currentTimeMillis();
    if(actualTime-mLastUpdate > UPDATE_THRESHOLD){
        mLastUpdate=actualTime;

    }*/
    if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {

        /*rotation.setText("Xg: " + event.values[0] +
                "\nYg: " + event.values[1] +
                "\nZg: " + event.values[2]);*/
        z_gy= event.values[2];


    }

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {



       /* acceleration.setText("X: " + event.values[0] +
                "\nY: " + event.values[1] +
                "\nZ: " + event.values[2]);*/
        acc_x=event.values[1];
        acc_y=event.values[2];
        acc_z = event.values[2];
        System.out.println("acc_z:" + Float.toString(acc_z));
        if (acc_z > GRAVITY_THRESH && one_step_freeze == 0) {
            System.out.println("entering1oop1");
            one_step_happend = 1;
            step_counter = step_counter + 1;
            System.out.println("stepcounter: " + Double.toString(step_counter) + "==========================");
            one_step_freeze = STEP_INTERVAL_THRESH / SENSOR_INTERVAL;
            /*System.out.println("onestepfreeze_in_firstloop_aft_div: " + Double.toString(one_step_freeze));
            System.out.println("exitingloop1");*/
        }

        if (one_step_freeze > 0) {
                System.out.println("stepcounterin 2 loop: " + Double.toString(step_counter) + "==========================");
                if ((System.currentTimeMillis() - lastSaved) > ACCE_FILTER_DATA_MIN_TIME) {
                    lastSaved = System.currentTimeMillis();
                    one_step_freeze = one_step_freeze - 1;
                   /* System.out.println("onestepfreeze2:" + Double.toString(one_step_freeze));
                    System.out.println("exitin 2loop");*/

                }
            }
        if(one_step_happend==1){
           // System.out.println("entered 3 loop:");
            last_theta=accumulated_rad+last_theta;
           // System.out.println("last_theta value: "+last_theta);
            current_x=last_x+STRIDE_LENGTH*Math.cos(last_theta);
            System.out.println("current_x value: " + current_x);
            current_y=last_y+STRIDE_LENGTH*Math.sin(last_theta);
            //System.out.println("current_y value: " + current_y);
           // System.out.println("processing c_x");
            //c_x.setText("c_x:" + current_x);
          //  System.out.println("processing c_y");
            //c_y.setText("c_y:" + current_y);
            last_x = current_x;
           // System.out.println("last_x value: " + last_x);

            last_y = current_y;
           // System.out.println("last_y value: "+last_y);
            accumulated_rad = 0;
            one_step_happend = 0;

        }else{
            accumulated_rad= accumulated_rad + z_gy * SENSOR_INTERVAL;
            System.out.println("accumulatedrad: "+accumulated_rad);
        }
        }




       /*if ((acc_z > GRAVITY_THRESH) && (one_step_freeze == 0)) {
           one_step_happend = 1;
           step_counter = step_counter + 1;
           System.out.println("stepcounter: " + Double.toString(step_counter) + "==========================");
           one_step_freeze = STEP_INTERVAL_THRESH / SENSOR_INTERVAL;
           //System.out.println("onestepfreeze2:"+Double.toString(one_step_freeze)+"==========================");
               /*spcounter.setText((int) step_counter);
       }*/
       /*if (one_step_freeze > 0) {
           if ((System.currentTimeMillis() - lastSaved) > ACCE_FILTER_DATA_MIN_TIME) {
               lastSaved = System.currentTimeMillis();
               one_step_freeze = one_step_freeze - 1;
               System.out.println("onestepfreeze2:" + Double.toString(one_step_freeze));
           }
       }
       if (one_step_happend == 1) {

           last_theta = accumulated_rad + last_theta;
           double current_x = last_x + STRIDE_LENGTH * Math.cos(last_theta);
           double current_y = last_y + STRIDE_LENGTH * Math.sin(last_theta);
           c_x.setText((int) current_x);
           c_y.setText((int)current_y);
                /*trace_x = [trace_x current_x];
                trace_y = [trace_y current_y];
           last_x = current_x;
           //System.out.println("last_x"+last_x);
           last_y = current_y;
           //System.out.println("last_y"+last_y);
           accumulated_rad = 0;
           one_step_happend = 0;
       } *//*else {
           accumulated_rad = accumulated_rad + z_gy * SENSOR_INTERVAL;
           //System.out.println(accumulated_rad);
              /*  calculate(accumulated_rad);

       }
   }

* /

}
@Override
protected void onResume()
{
    super.onResume();
    // Register this class as a listener for the accelerometer sensor
    sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
    // ...and the orientation sensor
    sm1.registerListener(this, sm1.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop()
{
    // Unregister the listener
    sm.unregisterListener(this);
    super.onStop();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
public class CustomDrawableView extends View
{
    static final int width = 50;
    static final int height = 50;

    public CustomDrawableView(Context context)
    {
        super(context);

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds((int) current_x, (int) current_y, (int) current_x + width, (int) current_y + height);
        //mDrawable.setBounds((int)acc_x, (int)acc_y, (int)acc_x + width,(int) acc_y + height);
    }

    protected void onDraw(Canvas canvas)
    {
       /* x= (int)current_x;
        y= (int)current_y;*/
        RectF oval = new RectF(MainActivity.x, MainActivity.y, MainActivity.x + width, MainActivity.y
                + height); // set bounds of rectangle
        Paint p = new Paint(); // set some paint options
        p.setColor(Color.BLUE);
        canvas.drawOval(oval, p);
        invalidate();
    }
}

}

0 个答案:

没有答案