Can anyone give me some android accelerometer example code? - android

I am developing a application which needs a new acceleration datum every 10 millisecond. And i need to save the data into a files. Can anyone give me some android accelerometer example codes as reference? Thanks very much.

Read up on the SensorManager to learn how to get accelerometer data:
http://developer.android.com/reference/android/hardware/SensorManager.html
You can access the SensorManager straight from your application context.
SensorManager manager = (SensorManager) context.getSystemService(Context.SensorManager);
Then you register a SensorEventListener, the sensor you want to listen to, and the rate. The SensorManager class provides constants for the refresh rate. For your purposes, SENSOR_DELAY_GAME might be fast enough, but I feel like SENSOR_DELAY_FASTEST is a safer bet. To my knowledge, there's no hard numbers on the rates of the defaults, so just see what works best.
Sensor accel = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
manager.registerListener(new MySensorEventListener(), accel, SensorManager.SENSOR_DELAY_GAME);
Then, every time a new event is polled from the accelerometer, it will call the listener's onSensorChanged method:
public class MySensorEventListener implements SensorEventListener {
#Override
public void onSensorChanged(SensorEvent event) {
// do what you need to do with the data from event
}
}

Related

Android Wear: Gyroscope data unchanged when logging both lin_accl and gyroscope

I am trying to read both gyroscope and linear_acclerometer data in the highest sample rate on my Moto 360 . I will press a button on the handheld, which will register sensors on the watch and start loggging all the sensor data, and when I press another button, the watch will unregister these sensors and stop logging.
However, the sensor value will become a constant value sometimes. I found if I restart the watch, the sensor will start working and generate meaningful sensor value again.
Does anyone has encounter the similar problem or have some ideas on why this may happen ?
private void startSensorListeners() {
Log.d(TAG, "startSensorListeners");
isCollecting = true;
//Register the motion Sensor Listener
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SENSOR_DELAY);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SENSOR_DELAY);
}
public void stopSensorListeners() {
isCollecting = false;
mSensorManager.unregisterListener(this);
}
public final void onSensorChanged(SensorEvent event) {
if(!isCollecting){
mSensorManager.unregisterListener(this, event.sensor);
return ;
}
// Save sensordata into local files, I also output the values[] in event on the logcat to monitor the sensor values in the realtime.
saveData(event);
}
Thanks
I don't have an actual answer for this. I'm just sharing my problem because I feel like they are very similar. The problem occurs on a Moto 360 and was not tested on another device.
I use the Magnetic sensor.
mMagneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
I register my Fragment as a listener to new values.
mSensorManager.registerListener(this, mMagneticSensor, SensorManager.SENSOR_DELAY_NORMAL);
I was developing an application. At the beginning, they were changing as expected but, after ~an hour, the values from the sensor just froze, remaining exactly the same no matter what.
I suspect two things: battery level and data rate renewal.
Maybe the battery dropped below some acceptable level (was around 25% when it stopped working)... it would be weird but it would not a big surprise.
Secondly, I remember having changed SensorManager.SENSOR_DELAY_NORMAL) to SensorManager.SENSOR_DELAY_UI) at some point, not long before the values froze. Perhaps it's a coincidence. The SensorManager.SENSOR_DELAY_UI is a faster data rate renewal than SensorManager.SENSOR_DELAY_NORMAL and as I was modifying a bitmap each time I got a new value. Maybe a buffer was full and wasn't emptied...maybe
Anyway, if I got some news on this subject I will update this post.

Getting 3D Android device orientation

Most smartphones feature a 3-axis Gyroscope, a 3-axis magnetometer and a 3-axis accelerometer. Combining these sensors' measurements in a clever way (Kalman filter) should yield a fast, accurate, noise free and absolute device orientation. This could be stored as a quaternion or yaw/pitch/roll values.
What is the simplest way to achieve this in Android? Does the operating system already do those calculations for you? Are there open source implementations that one could use?
Most examples/tutorials I could find only focus on one type of sensor. But for my use case only the fusion of all three sensors via proper filtering would be of interest...
You need to implements SensorEventListener interface.
and in your class you must declare these :
Sensor sensor;
SensorManager sensorManager;
and override these methods:
public void onAccuracyChanged(Sensor sensor,int accuracy)
{
}
public void onSensorChanged(SensorEvent event)
{
}
and in constructor of your class(Activity or...)
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_GAME);

Dynamic Detection of Hand Movement using Accelerometer

I want to detect the movement of the hand dynamically and modify sound accordingly. The phone is in the hand in a fixed orientation which does not change. For Example, I am holding the phone in my stretched hand and as it moves to the right or left, my music volume changes dynamically; If I move up and down the speed of playing changes and moving at some intermediate angle changes both speed and volume accordingly. I charted the accelerometer data while doing these motions and there seems to be some pattern but I am not sure how to filter those. I have looked at a lot of posts - High Pass/Low Pass filters, Kalman Filters, Gesture Recognizers but it is difficult to understand what is the appropriate method. Most of the posts don't seem to detect dynamically - but only when a certain gesture is finished. I only need to use accelerometer and not gyroscope and any other sensor. What is the correct approach here? Are there any existing libraries that do this?
if your activity implements
SensorEventListener
and you use the variables
private SensorManager sensorManager;
private Sensor mAccelerometer;
in your OnCreate() you instantiate them and register the listener like this:
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_FASTEST);
and then use this method to get the accelerometer data
public void onSensorChanged(SensorEvent event) {
double x = event.values[0];
double y = event.values[1];
double z = event.values[2];
}
and you can take it from there depending on the values you get from the accelerometer, make the changes to the volume or whatever

how to start make app like clinometer in android

i want to get and move needle on co-ordinates as per user rotates or move device as compass does also as this app is doing already.you can see what i want to say on this link https://play.google.com/store/apps/details?id=com.plaincode.clinometer&hl=en but now at this time i am not able to understand on this context. i am unable to find hint for how to start..is their is any api to get and make it work or need to implement accelerometer on a very extensive level to get it done. please guide me if u have any code or link regarding this..thank in advance
I was looking for this solution more than a year ago and for that somehow i managed to do this similar this way. and source for that which i managed to do is -- link to source
You should start creating an activity that implements SensorEventListener, including the two following methods:
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// add your code
// event.values[] contains the 3 accelerometer values
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
// add your code
}
As second step, start with the implementation of accelerometers into the Activity:
private SensorManager mSensorManager;
private Sensor mRotationSensor;
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Then register a sensor listener on it (you could do in onResume):
mSensorManager.registerListener(this, mRotationSensor, 16000);
// 16000 microseconds
and unregister it (in onPause):
mSensorManager.unregisterListener(this);
Please note that, if you want a very accurate measurement of angles and slopes in the 3 dimensions, you should implement a calibration routine of the onboard accelerometers, that usually need a small compensation in gain, offset and angle.
You can read a simple but complete usage of Accelerometers on the free and opensource BasicAirData Clinometer: https://github.com/BasicAirData/Clinometer
The app includes also a calibration routine.

First Android App - How to Access the Compass

I'm making my first Android application. As a toy problem to learn the system I want to make a simple app that display as text which direction the phone is pointing using the built in compass.
How do I access the compass from my code, and have my code be aware of direction changes?
I believe I'll need the SensorManager class but I'm confused how to use it. How do I tell it I want the compass sensor? How do I tell it to do an action (update text) on a direction change?
// First, get an instance of the SensorManager
SensorManager sMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// Second, get the sensor you're interested in
Sensor magnetField = sMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
// Third, implement a SensorEventListener class
SensorEventListener magnetListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// do things if you're interested in accuracy changes
}
public void onSensorChanged(SensorEvent event) {
// implement what you want to do here
}
};
// Finally, register your listener
sMan.registerListener(magnetListener, magnetField, SensorManager.SENSOR_DELAY_NORMAL);
However, please note that this is actually a magnetic sensor; therefore if you have magnetic interference around you, it may be pointing to the wrong direction. Also, you need to know the difference between True North and Magnetic North. Since this code uses magnetic sensor, you obtain the Magnetic North, but if you need to calculate the True North, you would need to do some adjustments with GeomagneticField.getDeclination().
Have a look at the API demos. There is an application that has already been written which access the compass and accelerometer. Maybe that will give you a better idea on how you can go about your task.
you shall find it in:
/android-sdk-linux_86/samples/android-8/ApiDemos/src/com/example/android/apis/os/sensor.java
hope it helps.
First you should check if a compass sensor is present on the system
PackageManager m = getPackageManager();
if(!m.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)) {
Log.d("COMPASS_SENSOR", "Device has no compass");
}

Categories

Resources