2014-09-15 3 views
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) + "%"); 
} 

답변

0

당신은 신속하고 더러운 로우 패스 필터 시도 할 수 :

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 < = K < = 1.0입니다.

K 값이 1.0에 가까울수록 안정적이지만 변화에 반응하는 속도가 느려지므로 K 값이 0.0에 가까울수록 더 빠르게 응답하지만 더 불안정한 결과를 얻을 수 있습니다.