Detect direction of shake movement in SensorListener

by TsEMaNN » Fri, 14 Jan 2011 21:36:23 GMT


Sponsored Links
 Hello developers,

I have got a problem concerning the SensorListener. At the moment I
can already detect if the phone got shaken. Addtionally I now want to
detect in which direction the phone is shaken. Do you maybe know which
values I have to consider to detect this?

My code at the moment looks like this:


public class ShakeActivity extends Activity implements SensorListener
{
   // For shake motion detection.
   private SensorManager sensorMgr;
   private long lastUpdate = -1;
   private float x, y, z;
   private float last_x, last_y, last_z;
   private float raw_x, last_raw_x;
   private static final int SHAKE_THRESHOLD = 800;
   private final float shakeThreshold = 1.5f;

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);
      // start motion detection
      sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
      boolean accelSupported = sensorMgr.registerListener(this,
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);

      if (!accelSupported) {
         // on accelerometer on this device
         sensorMgr.unregisterListener(this,
               SensorManager.SENSOR_ACCELEROMETER);
      }
   }

   protected void onPause() {
      if (sensorMgr != null) {
         sensorMgr.unregisterListener(this,
               SensorManager.SENSOR_ACCELEROMETER);
         sensorMgr = null;
      }
      super.onPause();
   }

   public void onAccuracyChanged(int arg0, int arg1) {
      // TODO Auto-generated method stub
   }

   public void onSensorChanged(int sensor, float[] values) {
      Log.d("sensor", "onSensorChanged: " + sensor);
      if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
         long curTime = System.currentTimeMillis();
         // only allow one update every 100ms.
         if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            x = values[SensorManager.DATA_X];
            y = values[SensorManager.DATA_Y];
            z = values[SensorManager.DATA_Z];
            raw_x = SensorManager.GRAVITY_EARTH;

            float xChange = Math.abs(x - last_x);
            float speed = Math.abs(x + y + z - last_x - last_y -
last_z)
                  / diffTime * 10000;

            float deltaX = Math.abs(last_x - x);
            float deltaY = Math.abs(last_y - y);
            float deltaZ = Math.abs(last_z - y);
            float delta_raw_x = raw_x - last_raw_x;
            // Log.d("sensor", "diff: " + diffTime + " - speed: " +
speed);
            if (speed > SHAKE_THRESHOLD) {
               Log.d("sensor", "shake detected w/ speed: " + speed);
               Toast.makeText(
                     this,
                     "shake detected w/ speed: " + speed + "; accelX:
"
                           + xChange, Toast.LENGTH_SHORT).show();
               // now left,right,up,down movement has to be detected
               // somehow
            }
            last_x = x;
            last_y = y;
            last_z = z;
            last_raw_x = raw_x;
         }
      }
   }
}


Best regards,
tsemann

-- 



Re: Detect direction of shake movement in SensorListener

by Hari Edo » Sat, 15 Jan 2011 03:33:47 GMT


 What is your code giving you that's unsatisfactory?

Depending on your definition of "shake", you could probably
tell the difference between "side to side" vs "up and down"
but you will likely get very inconsistent results if you try to
distinguish "leftward" vs "rightward".  Your hand often
anticipates a little, moving left before a big rightward jerk.
(Google "anticipation animation" for more.)




-- 


Sponsored Links


Re: Detect direction of shake movement in SensorListener

by bklik » Mon, 21 Feb 2011 03:53:39 GMT


 I think what you're after is the difference in values between X (left
and right), Y (up and down), Z (toward you and away).

My code looks something like this:

public void onSensorChanged(SensorEvent event) {
    long curTime = System.currentTimeMillis();

    x = Math.abs(event.values[0]);
    y = Math.abs(event.values[1]);
    z = Math.abs(event.values[2]);

    if((x+y+z) > ACCEL_THRESHOLD){
        shakeCount++;
        shakeLevel += (x+y+z);
        if(shakeCount >= 3){
            shakeCount = 0;
            shakeLevel = 0;
        }

        if(shakeLevel > SHAKE_THRESHOLD){
            // Do something because the phone has been shaken.
        }
    }
}

What I'm doing is looking for repetitive amounts of movement in any
direction, over a certain threshold, over time. You could break this
down into counts for X, Y, and Z rather then adding them all together
like I am. Then, just play with different thresholds that trigger when
you expect.

Brenton





-- 



Other Threads

1. CTS case: testPerformFiltering always timeout, even on Nexus One

Hi All

I'm testing the CTS on our device, and always met the
testPerformFiltering timeout issue durning AutoCompleteTextViewTest.
We also have tested such case on Nexus One, it timeouts too.

I have debug that, the testPerformFiltering test case function did not
run at all. The test runner must stuck there.
Can anybody give some advise about this? Why it's timeout?

We're using the Android r2.1_r1 CTS release and android source code
with widget code untouched.
Here's the CTS log:

start --plan CTS -p android.widget.cts.AutoCompleteTextViewTest
start java package android.widget.cts.AutoCompleteTextViewTest
==============================================================
Test java package contained in test package android.widget:
android.widget.cts.AutoCompleteTextViewTest
android.widget.cts.AutoCompleteTextViewTest#testAccessAdapter...(pass)
android.widget.cts.AutoCompleteTextViewTest#testAccessDropDownAnchor...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testAccessDropDownWidth...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testAccessItemClickListener...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testAccessItemSelectedListener...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testAccessListSelection...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testAccessValidater...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testConstructor...(pass)
android.widget.cts.AutoCompleteTextViewTest#testConvertSelectionToString...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testEnoughToFilter...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testGetThreshold...(pass)
android.widget.cts.AutoCompleteTextViewTest#testOnAttachedToWindow...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testOnCommitCompletion...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testOnDetachedFromWindow...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testOnFilterComplete....
(pass)
android.widget.cts.AutoCompleteTextViewTest#testOnKeyPreIme...(pass)
android.widget.cts.AutoCompleteTextViewTest#testOnTextChanged...(pass)
android.widget.cts.AutoCompleteTextViewTest#testOnWindowFocusChanged...
(pass)
android.widget.cts.AutoCompleteTextViewTest#testPerformCompletion....
(pass)
android.widget.cts.AutoCompleteTextViewTest#testPerformFiltering........................................................................................................................................................
(timeout)
android.widget.cts.AutoCompleteTextViewTest#testPopupWindow....(pass)
android.widget.cts.AutoCompleteTextViewTest#testReplaceText....(pass)
android.widget.cts.AutoCompleteTextViewTest#testSetFrame....(pass)
android.widget.cts.AutoCompleteTextViewTest#testPerformValidation....
(pass)
android.widget.cts.AutoCompleteTextViewTest#testSetCompletionHint....
(pass)
==============================================================

Best Regards
Xinyu Chen

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Android Hardware Requirements

Hello,

is there any information about hardware requirements for an android
phone? I spent some time searching the web and this group but
unfortunately could not find anything useful.

Is there such an information, covering the following questions
- required screensize? (qvga [320x240] afaik)
- required/optional hardware components
  - bluetooth, wifi, compass, motion sensors, etc?

Any help is welcome.

-- 

3. last modified time of a Contact item

4. Android Browser Plugins

5. How to scan Access Points and select strongest signal?

6. Retrieving Shoutcast Meta-Data from MediaPlayer not possible. Any alternative ?

7. How to go back from 2.2 to 2.1update (ERE27) on nexus one