如何以固定速率记录来自 Android 运动传感器的数据 [英] How to log data from Android Motion Sensors at a fixed rate

查看:32
本文介绍了如何以固定速率记录来自 Android 运动传感器的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Android 编程的基础知识.

I'm learning the Basics of Android programming.

我有一个简单的 android 测试应用程序,我将加速度计、磁力计和方向数据记录到一个外部文件中,同时还显示它.我通过调用方法 initLogger,在单击 Start 按钮(相关传感器的 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

问题:

  1. 如何确保 SensorChanged 事件以恒定速率触发?
  2. Java 中的 TimerTask 类在这种情况下有帮助吗?

这里的专家.请帮助我:)

Experts out here in SO.Please help me :)

推荐答案

既然您知道如果没有触发 SensorChanged 事件,则没有变化,您可以使用旧值.当您要求在特定时间间隔的 LOG 数据时,我不会在 onSensorChanged 方法中做任何输出,只是将新数据克隆到您的加速度计数据变量.然后每 40 毫秒记录一次加速度计数据的值.这样即使数据没有改变,你也会每 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 Answer 的说法,似乎也可以在特定时间间隔内交付"传感器数据.但是由于这些交付"与 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.

这篇关于如何以固定速率记录来自 Android 运动传感器的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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