如何在一个固定的速度记录来自Android的运动传感器的数据传感器、速度、数据、如何在

2023-09-05 07:06:46 作者:还给你的自由

我正在学习的Andr​​oid编程的基础知识。

I'm learning the Basics of Android programming.

我有一个简单的Andr​​oid应用程序的测试中,我登录了加速计,磁力计和方向数据到外部文件,同时也显示它。我通过调用一个方法 initLogger 启动上点击一个开始按钮(registerListener的相关传感器)的记录过程。

I have a simple android test application in which i log the accelerometer,magnetometer and the orientation data to an external file while also displaying it. I initiate the logging process on click of a Start button (registerListener for relevant sensors) by calling a method initLogger.

这类似于这样的东西...

Which looks something similar to this...

public void initLogger(View view)
{
    boolean bFlag = false;

    Button btnStart = (Button)findViewById(R.id.btnStartLog);
    Button btnStop = (Button)findViewById(R.id.btnStopLog);

    btnStart.setEnabled(bFlag);
    btnStop.setEnabled(!bFlag);

    bEnableLogging = true;
    //Start reading the sensor values
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI);
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);

   //so on.... 

还有一个停止按钮,将停止记录过程(并最终通过调用unregisterListener每个传感器注销)

There is also a Stop button, which shall stop the logging process (and finally unregister by calling unregisterListener for each sensor)

数据检索过程发生在 onSensorChanged 处理程序,该应检索相关传感器的数据,将值设置到相应的UI元素,并最终将数据记录到外部.csv文件。

The data retrieval process happens inside the onSensorChanged handler which shall retrieve the data from the relevant sensors, sets the value to the respective UI elements and finally log the data to an external .csv file.

onSensorChanged 事件处理程序看起来像这样...

onSensorChanged eventhandler looks something like this ...

public void onSensorChanged(SensorEvent event) {


    // TODO Auto-generated method stub
    // accelerometer
    TextView tAX = (TextView) findViewById(R.id.txtViewAxValue);
    TextView tAY = (TextView) findViewById(R.id.txtViewAyValue);
    TextView tAZ = (TextView) findViewById(R.id.txtViewAzValue);

    // magnetic field
    TextView tMX = (TextView) findViewById(R.id.txtViewMx);
    TextView tMY = (TextView) findViewById(R.id.txtViewMy);
    TextView tMZ = (TextView) findViewById(R.id.txtViewMz);

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

            accelerometerdata = event.values.clone();

            tAX.setText(Double.toString(accelerometerdata[0]));
            tAY.setText(Double.toString(accelerometerdata[1]));
            tAZ.setText(Double.toString(accelerometerdata[2]));


        }

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

            magneticmatrixdata = event.values.clone();

            tMX.setText(Double.toString(magneticmatrixdata[0]));
            tMY.setText(Double.toString(magneticmatrixdata[1]));
            tMZ.setText(Double.toString(magneticmatrixdata[2]));

        }

               // so on ....

虽然我收到的所有配置的传感器的数据,我没有过把接收到的数据的速率控制。即

Although i receive the data from all the configured sensors, i do not have the control over the rate at which the data is received. i.e

我知道SensorChanged事件触发的,当传感器数据被改变。但是我想这个事件在一个固定的速度被解雇。 对于前:每40毫秒

I know SensorChanged event is fired as and when the Sensor data is changed. However i want this event to be fired at a fixed rate. For ex: every 40ms

问:

如何保证SensorChanged事件被触发以恒定的速率? 是类的TimerTask的任何帮助Java在这种情况下?

专家在这里在SO.Please帮我:)

Experts out here in SO.Please help me :)

推荐答案

既然你知道,如果没有SensorChanged触发的事件,也没有改变,你可以用你的旧值。 当你要求在特定的时间间隔的日志数据,我不会做的onSensorChanged方法的任何输出只是新的数据复制到您的accelerometerdata变量。 而不是记录每40毫秒accelerometerdata的价值。这样,你正在登录的实际值每40毫秒,即使数据没有改变......

Since you know that if there was no SensorChanged Event fired, there was no change, you can just use your old value. As you asked for LOG data in specific intervals, i would not do any output in the onSensorChanged Method just clone the new data to your accelerometerdata variable. And than log the value of accelerometerdata every 40ms. This way you are logging the actual value every 40ms even if the data didnt change....

注:根据Ridcullys答案也似乎是可能得到的传感器数据在特定的时间间隔交付。但由于没有对这些交付一如既往地与Android上的传感器数据的延迟,与我的解决方案,你会更准确地在40毫秒的时间间隔。另一方面是可能发生的,如果在该时刻的传感器数据的变化登录,可能会发生您延迟新的数据一间隔。 我猜(不知道这一点) - 自其只是记录,而不是关于某事像把它尽可能快地实时,所以这不是一个要求 - 定时器,解决方案将导致更少的CPU负载

Note: According to Ridcullys Answer it also seem to be possible to get Sensor data "delivered" in specific time intervals. But since there is an delay on these "Deliveries" as always with sensor-data on Android, with my solution you will be more exactly on the 40ms interval. On the other hand it could happen that if the sensor data changes in the moment you log, it might happen that you delay the new data for one interval. And i guess (not sure about this point) - since its just about logging and not about sth like "get it as fast as possible in realtime", so this is not an requirement - the Timer-Solution causes less CPU-Load.