How do we get the accelerometer values?
private SensorManager mgr=null;
mgr=(SensorManager)ctxt.getSystemService(Context.SENSOR_SERVICE);
mgr.registerListener(listener,
mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
private SensorEventListener listener=new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
//Total acceleration will be sqrt(x^2+y^2+z^2)
double netForce=e.values[0]*e.values[0]; //X axis
netForce+=e.values[1]*e.values[1]; //Y axis
netForce+=(e.values[2])*(e.values[2]); //Z axis (upwards)
netForce = Math.sqrt(netForce) - SensorManager.GRAVITY_EARTH; //Take the square root, minus gravity
Log.d("ForceCalculator", "Net force:"+netForce+"");
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// unused
}
};
This will create and register an event listener for the accelerometer. This listener then calculates the net force.
This calculation is pretty self-explanatory. It involves three components - X axis, Y axis, and Z axis. To calculate the magnitude of these vector components, we simply take the square root of their combined squares:
netForce = sqrt( x^2 + y^2 + z^2 )
The only point to note is that we then subtract the acceleration due to gravity (approx. 9.8m/s^2). Otherwise we could have a resting acceleration of +9.8.
Full code will be coming later, when I've got some more time to play around with it. For now, this should show you how to get net force using the accelerometer. Still to come is code to calculate maximum force over a specified period of time, with threshold starting and finishing velocities.
6 comments:
Hello,
I have a question regarding net force.
What force are you trying to measure using this code? Is it really force or is it acceleration ?
Thanks,
Krushnaal
You're right Krushnaal - this is actually measuring the net acceleration. I've mistakenly been using the word "force" interchangeably. Since the phone is a fixed mass however, a change in acceleration will always be due to a proportional force being applied, so it's still relevant.
Cheers,
David
Is there a way to determine the force applied on the phone by swinging it in a particular direction?
Thanks,
Krushnaal
If you really want to find the force, you'll need to find (or estimate) the mass of the phone first. From standard physics, Force=Mass*Acceleration - we can find the acceleration using the code above, so if you have the mass of the phone you can apply this simple calculation to determine the force applied.
Of course, depending on what you're using this value for you may simply want to use the acceleration value instead.
Cheers,
David
P.S. For more info on the calculations involved, check out this site.
how to find angular momentum in s=android of swinging balls?
I think that if you are adjusting for gravity you would have to know the orientation of the device wrt/ gravity and subract each component before computing the absolute value.
Depending on how the gravity is oriented along the accelerometer you will get different values of total acceleration.
Post a Comment