Android: Using the accelerometer to detect a flick -
i'm trying develop app user can hit invisible drum using motion of phone. when phone flicked downwards drum sounded @ end of flick.
i have managed 90% of working, detecting when large, quick movement stops. although drum being sounded after flick (good) it's being sounded @ end of pull (not desirable).
by flick mean phone being flicked forwards , downwards, if striking drum it, , pull mean returning arm starting position.
does know efficient way of determining when flick occurs not push?
any ideas gratefully received.
thanks
existing code:
package com.example.testaccelerometer; import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.hardware.sensorlistener; import android.hardware.sensormanager; import android.os.bundle; import android.app.activity; import android.content.context; import android.view.menu; import android.widget.textview; public class mainactivity extends activity implements sensoreventlistener{ public static textview results; public static textview clickresults; stringbuilder builder = new stringbuilder(); private float maccelnograv; private float maccelwithgrav; private float mlastaccelwithgrav; public static boolean shakeishappening; public static int beatnumber = 0; @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 accelerometer - shaking not work"); }; maccelnograv = 0.00f; maccelwithgrav = sensormanager.gravity_earth; mlastaccelwithgrav = sensormanager.gravity_earth; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. 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) { builder.setlength(0); builder.append("x " + event.values[0] + "\ny " + event.values[1] + "\nz " + event.values[2]); results.settext(builder.tostring()); float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; mlastaccelwithgrav = maccelwithgrav; maccelwithgrav = android.util.floatmath.sqrt(x*x + y*y + z*z); float delta = maccelwithgrav - mlastaccelwithgrav; maccelnograv = maccelnograv * 0.9f + delta; if (maccelnograv >8.5) { shakeishappening = true; //clickresults.append(" click " + maccel); } if (shakeishappening == true && maccelnograv <2) { beatnumber++; clickresults.append(" click number: " + beatnumber + "\n" + "pa: " + mlastaccelwithgrav + " ca:" + maccelnograv + "\n "); shakeishappening = false; } } @override protected void onresume() { super.onresume(); // need try , reregister } @override protected void onpause() { // need try , unregister super.onpause(); } }
you can use geo-magnet sensor in conjunction accelerometer sensor determine edge of device facing toward ground, , exclude acceleration events in opposite direction
http://developer.android.com/reference/android/hardware/sensormanager.html#getorientation(float[], float[])
Comments
Post a Comment