使用Android加速度传感器进行距离测量

时间:2018-08-29 02:33:29

标签: android distance accelerometer

我想使用Android加速度传感器来测量距离。我修改了stackoverflow中的代码。街道信息很奇怪。你能告诉我怎么了吗?当按下按钮时,传感器被激活;如果未按下按钮,则计算传感器。我是韩国人,并且使用过Google翻译。

public class MainActivity extends AppCompatActivity {

SensorManager sm;
SensorEventListener acc;
Sensor accSensor;
TextView x,y,z,dist_total,time,dist_last,ax,az,ay;
float currentAcc, lastAcc=0.0f, effectiveAcc;
float distance = 0, totalDistance = 0;
long time_elapsed;
long tt;
long startTime;
long Endtime;
float xx;
float dX = 0;

public float accelXValue, accelYValue,accelZValue = 0.0f;
Button startButton,stopButton, resetButton;

Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    sm = (SensorManager)getSystemService(SENSOR_SERVICE);
    accSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    acc = new accListener();


    stopButton = (Button) findViewById(R.id.stop);
    resetButton =(Button) findViewById(R.id.reset);

    x = (TextView)findViewById(R.id.x);
    y = (TextView)findViewById(R.id.y);
    z = (TextView)findViewById(R.id.z);

    ax = (TextView)findViewById(R.id.ax);
    ay = (TextView)findViewById(R.id.ay);
    az = (TextView)findViewById(R.id.az);


    dist_last = (TextView)findViewById(R.id.text_last_dist);
    time = (TextView)findViewById(R.id.text_time);



    findViewById(R.id.start).setOnTouchListener(new View.OnTouchListener(){

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){

                case MotionEvent.ACTION_DOWN:
                    sm.registerListener(acc, accSensor, SensorManager.SENSOR_DELAY_UI);
                    displayValues();
                    startTime = System.currentTimeMillis();

                    break;

                case MotionEvent.ACTION_UP:
                    sm.unregisterListener(acc);
                    calculate();
                    stopCalculation();
                    Endtime = System.currentTimeMillis();
                    System.out.println("**********Endtime**********"+(Endtime/1000));
                    break;

            }
            return false;
        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



        }

    });

    resetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            resetFields();
            Toast.makeText(getApplicationContext(),"모든 정보를 초기화를 했습니다.",Toast.LENGTH_SHORT).show();
        }
    });
}


@Override
public void onPause(){
super.onPause();
Log.e("LOG", "onPause()");
sm.unregisterListener(acc);
}

@Override
public void onDestroy(){
    super.onDestroy();
    Log.e("LOG", "onDestroy()");
    sm.unregisterListener(acc);
}

@Override
protected void onResume() {
    super.onResume();
    sm.registerListener(acc,accSensor,SensorManager.SENSOR_DELAY_GAME);
}

private class accListener implements SensorEventListener {
    public void onSensorChanged(SensorEvent event){





        x.setText(Float.toString(event.values[0]));
        y.setText(Float.toString(event.values[1]));
        z.setText(Float.toString(event.values[2]));

   if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        accelXValue = event.values[0];
        accelYValue = event.values[1];
        accelZValue = event.values[2];

        Log.e("LOG", "ACCELOMETER           [X]:" + String.format("%.4f", event.values[0])
                + "           [Y]:" + String.format("%.4f", event.values[1])
                + "           [Z]:" + String.format("%.4f", event.values[2]));



    }
    public void onAccuracyChanged(Sensor sensor,int accuracy){

    }

}

public void calculate() {


        time_elapsed = (Endtime-startTime)/1000;
        tt = Math.abs(time_elapsed);
        currentAcc = (float) Math.sqrt(Math.pow(accelXValue,2)+Math.pow(accelZValue,2)+Math.pow(accelYValue,2));
        effectiveAcc = currentAcc - lastAcc;
        distance = Math.abs(effectiveAcc) * 0.5f * tt * tt ;
        totalDistance += distance;
        lastAcc = currentAcc;






    System.out.println("totalDistance : "+totalDistance);
    System.out.println("tt : "+tt);
    System.out.println("currentAcc : "  +currentAcc);
    System.out.println("effectiveAcc : "+effectiveAcc);
    System.out.println("distance : "+distance);
    System.out.println("lastAcc : "+lastAcc);

}

private void stopCalculation() {

    dist_last.setText(String.format("Distance: " + "%s" + " m", 
 Float.toString(distance)));

}

public void displayValues(){


    ax.setText(String.format("Acceleration X: " + "%s" , accelXValue));
    ay.setText(String.format("Acceleration Y: " + "%s" , accelYValue));
    az.setText(String.format("Acceleration Z: " + "%s" , accelZValue));


}

public void resetFields(){
    totalDistance = 0.0f;
    distance = 0.0f;
    tt =0;
    currentAcc = 0.0f;
    lastAcc = 0.0f;
    effectiveAcc =0.0f;

    dist_last.setText(String.format("Distance: " + "%s" + " m",Float.toString(distance)));

    System.out.println("TotalDistance"+totalDistance);
    System.out.println("distance"+distance);
    System.out.println("tt"+tt);
    System.out.println("currentAcc"+currentAcc);
    System.out.println("effectiveAcc"+effectiveAcc);
    System.out.println("lastAcc"+lastAcc);

}

}

1 个答案:

答案 0 :(得分:2)

无论您做什么,这都无法很好地工作。加速度计有两个问题:

1)很吵。这意味着您会有很多小错误,这些错误会很快加起来

2)它可以最大化。如果您加速得太厉害,它就会消失并且不会报告更高的值。

如果要进行距离测量,最好使用GPS和平滑算法。加速度计将始终给出糟糕的结果。