【发布时间】:2015-04-15 19:18:33
【问题描述】:
也许我的问题看起来很愚蠢,但我是 Android 编码的新手。我有一个陀螺仪的代码,如果我将手机转动 60 度,它就会振动。因此,如果我将手机转动超过 -30 度和 +30 度,手机就会振动。但是我有噪音问题,我的传感器不能正常工作,如果我快速移动手机,即使没有完成 +-30 度的限制,手机也会振动。
下面是我的代码,如果有人可以帮助我,如何在该代码中实现卡尔曼滤波器?
我是否必须添加一些新类并以某种方式调用它?我会很感激举个例子,如果有人有的话。
public class AccessGyroscope extends Activity implements SensorEventListener
{
private TextView tv;
//the Sensor Manager
private SensorManager sManager;
private float valueX, valueY, valueZ;
public Vibrator v;
private float vibrateThreshold = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the TextView from the layout file
tv = (TextView) findViewById(R.id.tv);
//get a hook to the sensor service
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
vibrateThreshold = 30;
//initialize vibration
v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
* callbacks defined in this class, and gather the sensor information as
* quick as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing
}
@Override
public void onSensorChanged(SensorEvent event)
{
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
valueX = event.values[2];
valueY = event.values[1];
valueZ = event.values[0];
//else it will output the Roll, Pitch and Yawn values
tv.setText("Orientation X (Roll) :"+ Float.toString(valueX) +"\n"+
"Orientation Y (Pitch) :"+ Float.toString(valueY) +"\n"+
"Orientation Z (Yaw) :"+ Float.toString(valueZ));
vibrate(); //here I'm calling vibration calculation
}
public void vibrate(){
if(valueX > vibrateThreshold || valueX < -vibrateThreshold){
v.vibrate(50);
}
}
}
【问题讨论】:
标签: android implementation kalman-filter