使用加速度计无输出速度表 [英] speedometer using accelerometer no output

查看:161
本文介绍了使用加速度计无输出速度表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了使用加速度计来计算装置速度的测速仪的应用程序。这里是code: `包com.exacmple.speedo;

i have created an speedometer application which uses accelerometer to calculate device speed. here is the code: `package com.exacmple.speedo;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class Speedometer extends Activity {
SensorManager sensorManager;
TextView myTextView;
float appliedAcceleration = 0;
float currentAcceleration = 0;
float velocity = 0;
Date lastUpdate;
Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myTextView = (TextView) findViewById(R.id.myTextView);
    lastUpdate = new Date(System.currentTimeMillis());
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sensorManager.registerListener(sensorListener,
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_FASTEST);
    Timer updateTimer = new Timer("velocityUpdate");
    updateTimer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            updateGUI();
        }
    }, 0, 1000);
}

private void updateGUI() {
    // Convert from m/s to mph
    final double mph = (Math.round(100 * velocity / 1.6 * 3.6)) / 100;
    // Update the GUI
    handler.post(new Runnable() {
        public void run() {
            myTextView.setText(String.valueOf(mph) + "mph");
        }
    });
}

private void updateVelocity() {
    // Calculate how long this acceleration has been applied.
    Date timeNow = new Date(System.currentTimeMillis());
    long timeDelta = timeNow.getTime() - lastUpdate.getTime();
    lastUpdate.setTime(timeNow.getTime());
    // Calculate the change in velocity at the
    // current acceleration since the last update.
    float deltaVelocity = appliedAcceleration * (timeDelta / 1000);
    appliedAcceleration = currentAcceleration;
    // Add the velocity change to the current velocity.
    velocity += deltaVelocity;
    // Convert from meters per second to miles per hour.
    double mph = (Math.round(velocity / 1.6 * 3.6));
    myTextView.setText(String.valueOf(mph) + "mph");
}

private final SensorListener sensorListener = new SensorListener() {
    double calibration;
    public void onSensorChanged(int sensor, float[] values) {
        double x = values[SensorManager.DATA_X];
        double y = values[SensorManager.DATA_Y];
        double z = values[SensorManager.DATA_Z];
        double a = -1
                * Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)
                        + Math.pow(z, 2));
        if (calibration == Double.NaN)
            calibration = a;
        else {
            updateVelocity();
            currentAcceleration = (float) a;
        }
    }
    public void onAccuracyChanged(int sensor, int accuracy) {
    }
};

}` 我面临的问题是,它并没有显示在初始速度为0.0公里加速我的情况的任何变化。我测试过它的logcat,它显示了他们的速度,但不会显示在用户界面的任何增量。请帮忙。

}` the problem i am facing is that it doesn't show any change in initial speed that is 0.0 mph in my case. i had checked it on logcat, it shows speed their but does not show any increments on UI. please help.

推荐答案

这不就是code将只是不断地积聚在〜9.8米/ SS?也许你想给组件切线仅集成到地球表面。

Isn't that code going to just constantly accumulate at ~9.8m/ss ? Maybe you want to only integrate the component tangent to the earth surface.

这篇关于使用加速度计无输出速度表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆