如何以固定频率(例如每 20 毫秒、40 毫秒或 60 毫秒)从 Android 加速计传感器获取 x、y、z 值

发布于 2024-11-28 12:08:34 字数 355 浏览 6 评论 0原文

我正在做一个Android项目,遇到了以下情况:

  1. 现在我们需要固定频率的加速度计值,例如20ms、40ms或60ms

  2. 现在我们处于 SENSOR_DELAY_GAME,但我们发现不同的设备对此参数有不同的间隔。例如,G2 使用 40ms,G7 使用 60ms,Nexus S 使用 20ms。

  3. 我尝试设置定时器或使用thread.sleep,但由于Java的GC问题,他们不能让系统定期获取值。

这是非常烦人的,如果有人有任何想法说 Android SDK 中是否有一种适当的方法可以让我以常规频率获取加速度计值,那将非常有帮助!

多谢!

Im working on an Android project and met the situation below:

  1. Now we are needing the accelerometer value on a regular frequency, such as 20ms, 40ms or 60ms

  2. Now we are SENSOR_DELAY_GAME right now but we found different devices are having different intervals for this parameter. For instance, the G2 is using 40ms, G7 is using 60ms and Nexus S is using 20ms.

  3. I tried to set timer or used thread.sleep but because of the GC problem of Java, they can not let the system to get the value on a regular frequency.

This is very annoying and if any one has any idea to say if inside Android SDK there is a proper method to allow me get the accelerometer values on a regular frequency, that will be very helpful!

Thanks a lot!

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

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

发布评论

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

评论(3

潇烟暮雨 2024-12-05 12:08:34

我通过简单地丢弃比我想要的更早的值来做到这一点。从电池消耗的角度来看,这并不理想,因为我需要让传感器比我需要的更频繁地给我供电,但至少我可以控制它们定期进入。

像这样的东西:

static final int ACCEL_SENSOR_DELAY = 100;  // the number of milisecs to wait before accepting another reading from accelerometer sensor
long lastAccelSensorChange = 0; // the last time an accelerometer reading was processed
@Override
public void onSensorChanged(SensorEvent sensorEvent) {


    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)  return;

    long now = System.currentTimeMillis();
    if (now-ACCEL_SENSOR_DELAY > lastAccelSensorChange) {
        lastAccelSensorChange = now;
        mCompassValues = event.values.clone();
        //... do your stuff
    }

I've done this by simply throwing away values that are sooner than I want them. Not ideal from a battery consumption standpoint as I need to have the sensors feed me more often than I need but at least then I can control that they come in on a regular interval.

Something like:

static final int ACCEL_SENSOR_DELAY = 100;  // the number of milisecs to wait before accepting another reading from accelerometer sensor
long lastAccelSensorChange = 0; // the last time an accelerometer reading was processed
@Override
public void onSensorChanged(SensorEvent sensorEvent) {


    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)  return;

    long now = System.currentTimeMillis();
    if (now-ACCEL_SENSOR_DELAY > lastAccelSensorChange) {
        lastAccelSensorChange = now;
        mCompassValues = event.values.clone();
        //... do your stuff
    }
北风几吹夏 2024-12-05 12:08:34

我构建了一个代码,可以让您在任何设备上获得准确的频率。

您可以在此处下载该项目并获取一些说明。

在代码中,您可以尝试不同的费率。例如,我的 Galaxy S2 上的正常模式是 5Hz。

I have built a code that allows you to get the exact frequency on any device.

You can download here the project and get some explanations.

In the code, you can try the different rates. For example, the normal mode on my Galaxy S2 is 5Hz.

凌乱心跳 2024-12-05 12:08:34

通过设置采样周期来使用 registerListener,如下所示:

boolean registerListener (SensorEventListener listener, Sensor sensor, int samplingPeriodUs)

来源

警告samplingPeriodUs参数只是对系统的一个提示。在使用这个之前先测试一下。

Use registerListener by setting the sampling period as below:

boolean registerListener (SensorEventListener listener, Sensor sensor, int samplingPeriodUs)

Source

Word of caution: The samplingPeriodUs argument is only a hint to the system. Test it before using this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文