Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I eventually figured out how to work this out, simply using the z axis to work out whether the motion is towards the ground or not.</p> <p>The z axis data is fed into the z array (minus the force of gravity). When acceleration goes over a certain level, we record the highest number, highZ and the lowest number, lowZ. These mark out the range of the hand movement between high and low points. When the acceleration goes below 2, we check to see if the latest bit of data in the Z array is equal to the high point or the low point, and this tells us whether the movement was a flick or a pull.</p> <p>It's probably not the most efficient way to work this out, but it's working now so I'm happy. Thanks for all your help, everyone.</p> <p>Here's my finished code:</p> <pre><code>public class MainActivity extends Activity implements SensorEventListener { private float mAccelNoGrav; private float mAccelWithGrav; private float mLastAccelWithGrav; ArrayList&lt;Float&gt; z = new ArrayList&lt;Float&gt;(); public static float finalZ; public static boolean shakeIsHappening; public static int beatnumber = 0; public static float highZ; public static float lowZ; public static boolean flick; public static boolean pull; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); results = (TextView) findViewById(R.id.results); clickresults = (TextView) findViewById(R.id.clickresults); SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor accelerometer = manager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); if (!manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST)) { builder.append("Problem with Accelerometer - Shaking will not work"); } ; mAccelNoGrav = 0.00f; mAccelWithGrav = SensorManager.GRAVITY_EARTH; mLastAccelWithGrav = SensorManager.GRAVITY_EARTH; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; z.add((event.values[2])-SensorManager.GRAVITY_EARTH); mLastAccelWithGrav = mAccelWithGrav; mAccelWithGrav = android.util.FloatMath.sqrt(x * x + y * y + z.indexOf(z.size()-1) * z.indexOf(z.size()-1)); float delta = mAccelWithGrav - mLastAccelWithGrav; mAccelNoGrav = mAccelNoGrav * 0.9f + delta; // Low-cut filter if (mAccelNoGrav &gt; 8.5) { shakeIsHappening = true; z.clear(); if (z.indexOf(z.size()-2) &gt; z.indexOf(z.size()-1)) { clickresults.append(" Z shrinking" + z); } else if (z.indexOf(z.size()-2) &lt; z.indexOf(z.size()-1)) { clickresults.append(" Z growing" + z); } } if (shakeIsHappening == true &amp;&amp; mAccelNoGrav &lt; 2) { finalZ = z.get(z.size()-1); highZ= z.get(z.size()-1); lowZ= z.get(z.size()-1); for (int i = 0; i &lt; z.size(); i++) { if (z.get(i) &gt; highZ) { highZ = z.get(i); } else if ((z.get(i) &lt; lowZ)) { lowZ = z.get(i); } if (highZ==finalZ) { flick = true; pull = false; } else if (lowZ==finalZ) { flick = false; pull = true; } if (flick) { beatnumber++; clickresults.append(" click number: " + beatnumber + "\n" + "PA: " + mLastAccelWithGrav + " CA:" + mAccelNoGrav + "\n " + "Lz " + z.indexOf(z.size()-2) +"z " + z.indexOf(z.size()-1) + "\n" + "\n"); shakeIsHappening = false; } z.clear(); } } } @Override protected void onResume() { super.onResume(); // YOU DO NEED TO TRY AND REREGISTER IT NOW } @Override protected void onPause() { // YOU DO NEED TO TRY AND UNREGISTER IT NOW super.onPause(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload