Different accelerometer values ​​across multiple devices

I am making a game that uses a device’s accelerometer to populate a progress bar. On my note 2, it took me about 20 seconds, shaking the phone up and down to fill the panel, however, I tried using ZTE Blade, and it took me 4 seconds.

Is there a way to calibrate accelerometers in my code? This is what I do:

@Override public void onSensorChanged(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; if (!mInitialized) { mLastX = x; mLastY = y; mLastZ = z; mInitialized = true; } else { float deltaX = Math.abs(mLastX - x); float deltaY = Math.abs(mLastY - y); float deltaZ = Math.abs(mLastZ - z); if (deltaX < NOISE) deltaX = (float)0.0; if (deltaY < NOISE) deltaY = (float)0.0; if (deltaZ < NOISE) deltaZ = (float)0.0; mLastX = x; mLastY = y; mLastZ = z; if(deltaY == 0){ return; } sProgress += deltaY; pb.setProgress(sProgress); } } 
+4
source share
2 answers

The problem may be with the frequency of the accelerometer. Do not use below android constants when registering.

 mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); 

These icons are different for different devices.

 int SENSOR_DELAY_FASTEST get sensor data as fast as possible int SENSOR_DELAY_GAME rate suitable for games int SENSOR_DELAY_NORMAL rate (default) suitable for screen orientation changes int SENSOR_DELAY_UI rate suitable for the user interface 

Use hard-coded values ​​in microseconds, for example, for a frequency of 1 Hz

 mSensorManager.registerListener(this, mAccelerometer,1000000); 

Hope he solves.

0
source

Another solution for ensuring frequency equivalence is the following solution.

 @Override public void onSensorChanged(SensorEvent sensorEvent) { if(sensorEventTooQuick(sensorEvent.timestamp)) { return; } mLastSensorEventTime = sensorEvent.timestamp; // process sensor } private boolean sensorEventTooQuick(long sensorTime) { long diff = sensorTime - mLastSensorEventTime; return diff < MINIMUM_SENSOR_EVENT_GAP; } 

What to consider:

  • You want you to choose MINIMUM_SENSOR_EVENT_GAP to be small enough not to lose information. The accelerometer is very fast, so this is usually not a problem, but it can be a problem for other sensors depending on the application.
  • Be sure to check the different values ​​on different devices.

I do this because it ensures that you will not receive events faster than your limit.

0
source

All Articles