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 :
- FASTEST
- GAME
- NORMAL
- UI
On my Galaxy S2, I get the next frequencies for the accelerometer :
- FASTEST : 100 Hz
- GAME : 50 Hz
- NORMAL : 5 Hz
- 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.
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(<code>nbElements </code> * 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.
Articles similaires :
Comment installer BackTrack 5 R1 en Live USB ? En effet, une façon rap...
Dans le précédent tutoriel, nous avons vu comment transformer une appl...
Lorsque vous créez l’espace disque virtuel pour une machine virtuelle,...
BackTrack 5 R3 est sorti le 13 aout 2012, apportant avec lui des corre...


4 commentaires
1 ping
Passer au formulaire de commentaire ↓
Malcolm Gyasi
27 mars 2012 à 16 h 35 min (UTC 2) Lier vers ce commentaire
Hi. I tried importing the accelerometer code above but its not working. I dont think all the files are made available in the download
Thomas
27 mars 2012 à 19 h 39 min (UTC 2) Lier vers ce commentaire
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.
Dávid Lakatos
27 août 2012 à 13 h 47 min (UTC 2) Lier vers ce commentaire
is that possible to set the accelerometer in a number what i choose?
Thomas
12 octobre 2012 à 20 h 52 min (UTC 2) Lier vers ce commentaire
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.
Impossibility to change the rate of the accelerometer | PHP Developer Resource
24 mai 2012 à 23 h 09 min (UTC 2) Lier vers ce commentaire
[...] 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 [...]