android滤波方位传感器
我使用方向传感器测量相对于代码中定义的校准点的坡度。当平板电脑处于要测量的稳定点时,输出相当跳跃+/-~.35。我想在传感器的输出上加上某种滤波器,但我不确定如何真正做到这一点 以下是我目前的代码: 在onCreateView中android滤波方位传感器,android,android-sensors,Android,Android Sensors,我使用方向传感器测量相对于代码中定义的校准点的坡度。当平板电脑处于要测量的稳定点时,输出相当跳跃+/-~.35。我想在传感器的输出上加上某种滤波器,但我不确定如何真正做到这一点 以下是我目前的代码: 在onCreateView中 sensorCalibrate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view
sensorCalibrate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calibratedPitch = pitch;
adjustedRoll = roll;
// adjPitchDisplay.setText(Float.toString(Math.abs(calibratedPitch)) + " ");
Toast.makeText(getActivity(), "Sensor Calibrated!", Toast.LENGTH_SHORT).show();
}
});
还有一些方法
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mRotationSensor) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
update(truncatedRotationVector);
} else {
update(event.values);
}
}
}
private void update(float[] vectors) {
final NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(5);
formatter.setMaximumFractionDigits(5);
float rads_to_Degrees = 57.29578F;
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
int worldAxisX = SensorManager.AXIS_X;
int worldAxisZ = SensorManager.AXIS_Z;
float[] adjustedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
float[] orientation = new float[3];
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
pitch = orientation[1]*rads_to_Degrees;
roll = orientation[2]*rads_to_Degrees; // roll will be implemented later on
pitch = Math.abs(pitch);
roll = Math.abs(roll);
outputPitch = (calibratedPitch - pitch);
final double tanPitch = Math.tan(Math.toRadians(outputPitch));
outputGrade = tanPitch * 100D;
outputGradeDisplay.setText("Current grade " + String.format("%.2f",outputGrade) + "%");
}
您可以尝试快速且脏的低通滤波器:
private static final double K = 0.9;
private void update (..) {
...
pitch = K*lastPitch + (1.0 - K)*(Math.abs(orientation[1] * rads_to_Degrees);
lastPitch = pitch;
...
}
其中0.0