我發現了很多關於如何處理設備旋轉,運動方向和位置傳感器的線索。 我想創建一個應用程序,我將在我的車中使用,首先我想測量汽車的旋轉度。 所以我把我的手機放在手機殼裏,例如當我左轉車時,我想看看汽車在電話上的轉彎程度。使用Android手機測量汽車的旋轉度
磁性和加速度計可以嗎?
我發表了一個代碼,首先我認爲沒關係。 (比方說,我認爲我的手機「波泰特」模式,因此不會對景觀第一)
private static SensorManager sensorService;
//magnetic
private Sensor mSensor;
//accelerometer
private Sensor gSensor;
private float[] mValuesMagnet = new float[3];
private float[] mValuesAccel = new float[3];
private float[] mValuesOrientation = new float[3];
private float[] mRotationMatrix = new float[9];
@Override
public void onCreate(Bundle savedInstanceState) {
nf.setMaximumFractionDigits(2);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
this.mSensor = sensorService.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
this.gSensor = sensorService.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorService.registerListener(this, gSensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorService.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
break;
}
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
// double azimuth = Math.toDegrees(mValuesOrientation[0]); //azimuth, rotation around the Z axis.
// double pitch = Math.toDegrees(mValuesOrientation[1]); // pitch, rotation around the X axis.
double roll = Math.toDegrees(mValuesOrientation[2]); //roll, rotation around the Y axis.
//normalize
// azimuth = azimuth>0?azimuth:azimuth+360;
roll = roll>0?roll:roll+360;
String txt = "roll= "+Math.round(roll);
((EditText)findViewById(R.id.szog)).setText(txt);
}
問題: - 如何準確將這個應用在汽車? (我能做些什麼才能更準確?) - 當我將手機放在「橫向」模式下時應該怎麼做? 從方位角度還是可以的?
請注意,這是第一次嘗試,所以有很多事情要做! 但首先我想看看如何實現,
謝謝!
所以看起來像重力/磁場是一個更好的組合,我會試試看。這個組合可以用於非常慢的速度測量嗎? (3-5公里/小時) – user1552747 2013-04-06 11:32:50
我認爲這與測量速度無關 – Stochastically 2013-04-06 12:28:52
謝謝,我認爲對於我的問題,您的回答是OK(現在我的應用程序更好!)。如果你有時間請告訴我,什麼傳感器適合低速測量?謝謝! – user1552747 2013-04-06 13:38:38