【问题标题】:Distance measurement using Android acceleration sensor使用 Android 加速度传感器进行距离测量
【发布时间】:2018-12-31 22:51:00
【问题描述】:

我想使用 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);

}

}

【问题讨论】:

    标签: android distance accelerometer


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 谢谢。您不能使用 GPS,因为它会在建筑物内传输距离信息。
    • 如果您正在为特定建筑物执行此操作,请考虑使用蓝牙信标。但是加速度计太不准确了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多