File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes Problem in detecting a shake movement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Problem in detecting a shake movement " Watch "Problem in detecting a shake movement " New topic
Author

Problem in detecting a shake movement

ganesh gupthag
Greenhorn

Joined: Mar 05, 2009
Posts: 5
Hi group ,
I use OpenIntents SensorSimulator to detect the shake movement
In code i wrote

public float accelerometer_shake_threshold = 1.2f; // what does this 1.2 means to this program
static final int ACCELEROMETER_FLOAT_TO_INT = 1024; // is this a conversion factor
int mACCELEROMETER_SHAKE_THRESHOLD_SQUARE;

public void onSensorChanged(int sensor, float[] values){
if( sensor == SensorManager.SENSOR_ACCELEROMETER){
int t = (int)(ACCELEROMETER_FLOAT_TO_INT * accelerometer_shake_threshold);
mACCELEROMETER_SHAKE_THRESHOLD_SQUARE = t * t;


android.util.Log.i("Answer",String.valueOf(values[0]));
android.util.Log.i("Answer",String.valueOf(values[1]));
android.util.Log.i("Answer",String.valueOf(values[2]));

int ax = (int)(ACCELEROMETER_FLOAT_TO_INT * values[0]);
int ay = (int)(ACCELEROMETER_FLOAT_TO_INT * values[1]);
int az = (int)(ACCELEROMETER_FLOAT_TO_INT * values[2]);

int len2 = ax * ax + ay * ay + az * az;
android.util.Log.i("LEN is",Integer.toString(len2));

if (len2 < mACCELEROMETER_SHAKE_THRESHOLD_SQUARE) {

Log.v(TAG,"Force Detected");
TextView tv=(TextView)findViewById(R.id.found);
tv.setText("ViolentForce Detected");

}
I could not get the desired result ,the value of len2 is seems to be constant for any movement I give through the sensor simulator (Yaw,Roll and Pitch).If the len2 is constant value then what could be the purpose of the if statement .
I just blindly followed the code snippet given under" Detecting "Shake" With The Accelerometer"(http://www.anddev.org/example_detecting_-und-quotshake-und-quot_with_the_accelerometer-t4451.html
) .I don't no where i went wrong
In other few examples which i came across has len2 value compared with cube root of gravity(being a newb its Greek to me ) Help me out to know the concept to detect a violent shake .What kind of physics is used to detect this sense .
Thanks
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Accelerometer gives acceleration(raw data) along X-Y-Z axis in SI units. To detect a shake, you need to define it first and then apply a suitable model(can require good understanding of physics).

The algorithm for detecting a shake could be as simple as:



The way you combine your accelerometer readings and the threshold value will define the "strength" and type of "shake" that your code can sense(Perhaps providing this in API would have been more convenient).

Maybe someone with a physics degree can help here.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
ganesh gupthag
Greenhorn

Joined: Mar 05, 2009
Posts: 5
Thanks Tripathi for your post
What do you mean by applying a model ,Can't i use OpenIntents Sensor Simulator to pass a shake movement (by just sliding the Yaw or,Roll or Pitch vigorously )
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

By model, I meant -

{formula(designed using accelerometer readings) or
predicates on the readings}
that will describe shake for your algorithm.

For e.g: In you code, you chose sum of squares and compared it with some large value:
[ "X^2 + Y^2+Z^2" > "threshold^2"].
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem in detecting a shake movement