2016-08-28 117 views
3

我寫了下面的代碼,其中加速度計的值在旋轉期間以x,y,z顯示。加速度計值爲度數

public class MainActivity extends AppCompatActivity implements SensorEventListener { 

private TextView xText,yText,zText; 
private Sensor mySensor; 
private SensorManager SM; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Creating the Sensor Manager 
    SM = (SensorManager)getSystemService(SENSOR_SERVICE); 

    // Accelerometer Sensor 
    mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 

    // Register sensor Listener 
    SM.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL); 

    // Assign TextView 
    xText = (TextView)findViewById(R.id.xText); 
    yText = (TextView)findViewById(R.id.yText); 
    zText = (TextView)findViewById(R.id.zText); 


} 

@Override 
public void onSensorChanged(SensorEvent sensorEvent) { 



    xText.setText("X: " + sensorEvent.values[0]); 
    yText.setText("Y: " + sensorEvent.values[1]); 
    zText.setText("Z: " + sensorEvent.values[2]); 
} 

@Override 
public void onAccuracyChanged(Sensor sensor, int i) { 

    } 
} 

現在我想將我從SensorEvents獲得的值轉換爲度數。我在這裏看到各種問題,但我感到困惑。

double x = sensorEvent.values[0]; 
    double y = sensorEvent.values[1]; 
    double z = sensorEvent.values[2]; 

應該有一個公式,它取上述值並將它們轉換成度。

任何想法?

謝謝,

泰奧。

編輯

@Override 
public void onSensorChanged(SensorEvent sensorEvent) { 
    //xText.setText("X: " + sensorEvent.values[0]); 
    //yText.setText("Y: " + sensorEvent.values[1]); 
    //zText.setText("Z: " + sensorEvent.values[2]); 

    double x = sensorEvent.values[0]; 
    double y = sensorEvent.values[1]; 
    double z = sensorEvent.values[2]; 

    double pitch = Math.atan(x/Math.sqrt(Math.pow(y,2) + Math.pow(z,2))); 
    double roll = Math.atan(y/Math.sqrt(Math.pow(x,2) + Math.pow(z,2))); 
    //convert radians into degrees 
    pitch = pitch * (180.0/3.14); 
    roll = roll * (180.0/3.14) ; 

    yText.setText(String.valueOf(pitch)); 
    zText.setText(String.valueOf(roll)); 
} 
+0

你有沒有運氣? –

回答

0

剎那間需要爲TYPE_ACCELEROMETER註冊,同時也爲TYPE_MAGNETIC_FIELD比你可以利用SensorManager內置的方法對你有所幫助:

public void onSensorChanged(SensorEvent event) { 
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
     gravity = event.values; 
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) 
     geomagnetic = event.values; 
    if (mGravity != null && geomagnetic != null) { 
     float R[] = new float[9]; 
     float I[] = new float[9]; 
     boolean success = SensorManager.getRotationMatrix(R, I, gravity, geomagnetic); 
     if (success) { 
     float orientation[] = new float[3]; 
     SensorManager.getOrientation(R, orientation); 
     myAzimut = orientation[0]; // myAzimut is The geomagnetic inclination angle in radians. 
     } 
    } 
} 

你可以學習所有附加信息通過閱讀SensorManager源代碼評論。

+0

好的。感謝你的回答。但如何顯示度數?如果我把xText.setText(String.valueOf(myAzimut));我什麼也沒得到:( – Theo

+0

你加了android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION?因爲看起來你得到了空值。 –

+0

oops。我沒有。讓我添加它們並告訴你... – Theo

1

現在我想我從SensorEvents去度值轉換

你TYPE_ACCELEROMETER獲得價值的單位是米/秒^ 2,因此試圖轉換到度不合理。

你的俯仰和俯仰計算似乎不正確。對於正確的計算看到DSensorEventProcessor類中的方法processSensorData(DProcessedSensorEvent.DProcessedSensorEventBuilder builder)https://github.com/hoananguyen/dsensor/blob/master/dsensor/src/main/java/com/hoan/dsensor_master/DSensorEventProcessor.java

要轉換俯仰和滾動使用度Math.toDegrees(valueToConvert)

0

你的代碼似乎是正確。使用Math.PI中的(180.0/3.14)可以獲得更準確的結果。