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

标签 android distance accelerometer

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

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) 很吵。这意味着你会有很多小错误,这些错误会很快累积起来

2)它可以最大化。如果你加速太猛,它会达到上限并且不会报告任何更高的东西。

如果您想计算距离,最好使用 GPS 和平滑算法。加速度计总是会给出糟糕的结果。

关于android - 使用 Android 加速度传感器进行距离测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52068688/

相关文章:

Android:使用加速度计创建一个简单的 maraca 应用程序

android - OpenGL 与 OpenGL ES(GLchar 等)

android - 大屏电脑全屏,宽度超过最大宽度,出现灰色背景

android - 返回 Android Studio 代码编辑器的快捷方式

r - 将距离矩阵转换并保存为特定格式

php - 使用php和mysql计算两点之间的距离

android - 加速度计值表现得像倾斜仪?

java - Arraylist 元素未完全接收

java - onPictureTaken() 错误,问题是无法将 RGB 转换为灰色,程序停止从 cvtcolor() 函数运行。?

android - Android 中的 "gravity"和 "acceleration"传感器有什么区别?