«

»

nov 19

Imprimer ceci Article

Accelerometer’s frequency on Android with code

Important!

Encore un article en anglais, mais il est très facile à comprendre. Si vous avez besoin d’aide, utilisez les commentaires.

English textEnglish text


Some values for accelerometer’s frequency

After researchs on the Internet with my friend Google, as I did not find out the frequency of Samsung Galaxy S2 Accelerometer on Android, I decided to build my own code to know the frequency !

On Android, you can use one of the four next rates for the accelerometer :

  1. FASTEST
  2. GAME
  3. NORMAL
  4. UI
Get accelerometer's frequency on android

Get accelerometer’s frequency on android

On my Galaxy S2, I get the next frequencies for the accelerometer :

  1. FASTEST    : 100 Hz
  2. GAME          : 50   Hz
  3. NORMAL    : 5      Hz
  4. UI                 : 16    Hz

This the Java code to test the frequency of your accelerometer. You can also download all the code and import the project in Eclipse for example.

 

Get Frequency

Files Sources from Eclipse project.
Size 54.7Ko
42039 téléchargements.

Code

package samoht.tuto.frequency;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/**
 *
 * @author Samoht
 * http://samoht.fr
 * Do what you want with this code, if you let this disclaimer
 * 19/11/2011 18:00
 *
 */
public class GetFrequencyActivity extends Activity implements SensorEventListener {
	private static final double nbElements = 30;
	private SensorManager sensorManager;
	private Sensor accelerometer;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		boolean accelSupported = sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
		accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		if (!accelSupported) {
			sensorManager.unregisterListener(this, accelerometer);
			((TextView) findViewById(R.id.acc)).setText("Accelerometer not detected");
		}
	}
	/**
	 * Always make sure to disable sensors you don't need, especially when your
	 * activity is paused. Failing to do so can drain the battery in just a few
	 * hours. Note that the system will not disable sensors automatically when
	 * the screen turns off.
	 **/
	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
	}
	protected void onPause() {
		super.onPause();
		sensorManager.unregisterListener(this);
	}
	protected void onDestroy() {
		super.onDestroy();
		sensorManager.unregisterListener(this);
	}
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// Nothing to do
	}
	long now = 0;
	long time = 0;
	int temp = 0;
	@Override
	public void onSensorChanged(SensorEvent event) {
		long tS;
		// only if the event is from the accelerometer
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			float x, y, z;
			x = event.values[0];
			y = event.values[1];
			z = event.values[2];
			// Get timestamp of the event
			tS = event.timestamp;
			((TextView) this.findViewById(R.id.axex)).setText("X : " + x);
			((TextView) this.findViewById(R.id.axey)).setText("Y : " + y);
			((TextView) this.findViewById(R.id.axez)).setText("Z : " + z);
			// Get the mean frequency for "nbElements" (=30) elements
			if (now != 0) {
				temp++;
				if (temp == nbElements) {
					time = tS - now;
					((TextView) findViewById(R.id.acc)).setText("Frequency : " + (nbElements * 1000000000 / time)
							+ " Hz");
					temp = 0;
				}
			}
			// To set up now on the first event and do not change it while we do not have "nbElements" events
			if (temp == 0) {
				now = tS;
			}
		}
	}
	public void onClickQuit(View v) {
		finish();
	}
}

Remarks

If you want to limit the number of digits in the frequency, use a mask :

	String mask= new String("#0.##");
	DecimalFormat form = new DecimalFormat(mask);
        ...
((TextView) findViewById(R.id.acc)).setText("Frequency : " + form.format(nbElements * 1000000000 / time) + " Hz");
....

Then, if you want a more precise frequency,change the value of nbElements. Instead of 30, set 50 or 100. The greater the number is, the slower the refresh time is.

Other usefull information, the timestamp is in nanosecond, that’s why I multiplie by 10 to power of 9, to change the result in seconds.

Important!

If you have questions or improvements (because this code does only its job, use comments. Thanks

 

 

Article intéressant ? Alors partagez-le avec vos amis et la terre entière en cliquant sur les boutons ci-dessous :

Articles similaires :

Lien Permanent pour cet article : http://samoht.fr/tuto/accelerometers-frequency-on-android-with-code

6 comments

1 ping

Passer au formulaire de commentaire

  1. Malcolm Gyasi

    Hi. I tried importing the accelerometer code above but its not working. I dont think all the files are made available in the download

  2. Thomas

    To import the code just copy the folder Frequency (with the 2 folders and the file) to the examples’ folder of Android (f.i : C:Program Files (x86)Androidandroid-sdksamplesandroid-8).

    Then in Eclipse click on the icon « Opens a wizzard to help Create a new Android Project »,
    then select « create from existing example »,
    « Next »,
    « Android 2.2″,
    « Next » and
    select the folder named « Frequency ».

    That’s all.

    Hope it’ll be helpful.

  3. Dávid Lakatos

    is that possible to set the accelerometer in a number what i choose?

    1. Thomas

       Hi, I don’t really understand what you whant to do.

      In fact, the value of the accelerometer is « setted » by the inclinaison of your phone. You CAN NOT set your own value to a sensor.

  4. Jeffrey Rocks

    This is very helpfull for my research!

    I was also looking on the internet for sensor refresh rates for my Galaxy S2. I almost wanted to do the test myself, but now i don’t have to. Saves me a lot of time!

    Thank you!

    Yours sincerely,
    Jeffrey Rocks

    1. Thomas Peyrard

      Hi Jeffrey !
      You’re welcome and thanks for having taken time to comment :)

  1. Impossibility to change the rate of the accelerometer | PHP Developer Resource

    […] So I decided to install an application found on the Internet which tests the rate of the accelerometer : http://samoht.fr/tuto/accelerometers-frequency-on-android-with-code […]

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *


*

Vous pouvez utiliser les balises HTML suivantes : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>