Android 传感器同时记录值

发布于 2024-11-19 15:39:05 字数 125 浏览 4 评论 0原文

我正在构建一个应用程序,该应用程序必须将各种传感器值同时记录到 .csv 文件中(如果传感器可用)。任何人都可以建议如何将值插入到 csv 文件(表格形式)中。是否可以记录所有单个传感器更改事件中的 .csv 值。

安瓦

Im building an application which has to record various sensor values simulatneously into a .csv file(if sensors are available).Can any one suggest ways to keep inserting values into the csv file(of a table form).Is it possible to record all the .csv values in a single sensor change event.

Anva

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2024-11-26 15:39:05

您一次只能接收来自 1 个传感器的数据,并且传感器可能以不同的时间间隔报告数据。

有几种方法可以解决这个问题。

最简单的方法是缓存每个传感器的值,然后每当您获取任何传感器的新数据时,就更新该传感器的缓存值,然后使用所有缓存的传感器值写入一个新的 CSV 行。这一新的 CSV 行仅与您刚刚更新数据的 1 个传感器的前一行有所不同。

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    //Note: in practice, you need to copy the values out of the event.values array
    //using, for example, System.arraycopy
    cached_values[event.sensor.getType()] = event.values; 
    writeCsvLine(cached_values);
}

或者,您可以缓存传感器值,直到获得“重复”数据,即您已经拥有未写入数据的传感器数据,而不是每次获取数据时都写入一行 CSV。然后,您将为缓存值写出一个 CSV 行,重置每个传感器的“is_writing”标志,并更新刚刚获得的传感器的缓存值。这样,您的 CSV 文件中就不会出现太多重复值,但仍然保证不会错过任何报告的传感器值。

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    if (!cached_value_written[event.sensor.getType()]) {
       writeCsvLine(cached_values);
       cached_values_written.reset(); //reset all flags to true
    }
    cached_values[event.sensor.getType()] = event.values; 
    cached_value_written[event.sensor.getType()] = false;
}

另一种方法是在获取传感器值时对其进行缓存,然后在单独的线程中,每隔(例如,1 秒)获取“当前”值,并将其写出。在这种方法中,您需要小心同步对缓存值的访问,因为您将在单独的线程中进行读取和写入。

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    synchronized(cached_values) {
        cached_values[event.sensor.getType()] = event.values;
    }
}

//on a separate thread:
run() {
   while (true) {
       Thread.sleep(1000);
       synchronized(cached_values) {
           writeCsvLine(cached_values);               
       }
   }
}

或者,您可以存储上次写入值的时间,而不是使用一个单独的线程每 1 秒检查一次,然后当您输入新值时,检查是否为 1 秒(或您想要使用的任何时间间隔)自上次写入数据以来已经过去了,如果是这样,请使用当前缓存的值写入一个新的 CSV 行。

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    long currentTime = System.currentTimeMillis();
    cached_values[event.sensor.getType()] = event.values;
    if (currentTime - lastWrittenTime >= 1000) {
        writeCsvLine(cached_values);
        lastWrittenTime = currentTime;
    }
}

You can only receive data from 1 sensor at a time, and the sensors may be reporting data at different time intervals.

There are several ways you could approach this.

The simplest would be to cache the values for each sensor, and then whenever you get new data for any sensor, you update the cached value for that sensor and then write a new CSV line with all of the cached sensor values. This new line of CSV will only differ from the previous for the 1 sensor whose data you just updated.

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    //Note: in practice, you need to copy the values out of the event.values array
    //using, for example, System.arraycopy
    cached_values[event.sensor.getType()] = event.values; 
    writeCsvLine(cached_values);
}

Alternatively, instead of writing a line of CSV every time you get a piece of data, you could cache the sensor values until you get a "duplicate" - i.e. data for a sensor that you already have unwritten data for. Then you would write out a CSV line for the cached values, reset your "is_written" flags for each sensor, and update the cached value for the sensor you just got. This way, you don't have as many duplicate values in the CSV file, but are still guaranteed to not miss any reported sensor value.

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    if (!cached_value_written[event.sensor.getType()]) {
       writeCsvLine(cached_values);
       cached_values_written.reset(); //reset all flags to true
    }
    cached_values[event.sensor.getType()] = event.values; 
    cached_value_written[event.sensor.getType()] = false;
}

Yet another approach would be to cache the sensor values as you get them, and then in a separate thread, you would grab the "current" values every, say, 1 second, and write them out. In this approach, you need to be careful to synchronize access to the cached values, because you would be reading and writing in separate threads.

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    synchronized(cached_values) {
        cached_values[event.sensor.getType()] = event.values;
    }
}

//on a separate thread:
run() {
   while (true) {
       Thread.sleep(1000);
       synchronized(cached_values) {
           writeCsvLine(cached_values);               
       }
   }
}

Or, instead of having a separate thread that checks every 1 second, you could store the time at which you last wrote the values, and then when you get in a new value, check if 1 second (or whatever interval you want to use) has elapsed since the last time you wrote data, and if so, write a new CSV line with the currently cached values.

(Pseudo-code)
onSensorChanged(SensorEvent event) {
    long currentTime = System.currentTimeMillis();
    cached_values[event.sensor.getType()] = event.values;
    if (currentTime - lastWrittenTime >= 1000) {
        writeCsvLine(cached_values);
        lastWrittenTime = currentTime;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文