0
방위 센서를 사용하여 코드 내에 정의 된 교정 점을 기준으로 기울기를 측정합니다. 태블릿이 측정 할 안정적인 지점에있을 때, 출력은 다소 불안정합니다 (+/- ~ 0.35). 나는 센서의 출력에 일종의 필터를 달기를 원하지만 실제로 어떻게해야하는지 확신 할 수 없다.android - 방위각 센서 필터링
여기 내 코드는 지금까지의 : onCreateView() 내에서
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) + "%");
}