其實就是整合磁力計和加速度計...然後呼叫
SensorManager.getRotationMatrix(R, null,
accelerometer_values, magnitude_values);
SensorManager.getOrientation(R, values);
最後得到方位角度....
但是Dma4412 只有mpu6050....沒有磁力計 hmcxxxx...
所以 得到的 magnitude_values :0.0 0.0 0.0
所以 rotation matrix 為零....
方位腳為零
public class MainActivity extends Activity {
private static final String TAG = "OrientationDemo";
private SensorManager sensorManager;
private TextView tvMessage;
private float[] accelerometer_values = null;
private float[] magnitude_values = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
findViews();
}
private void findViews() {
tvMessage = (TextView) findViewById(R.id.tvMessage);
}
SensorEventListener listener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
accelerometer_values = event.values.clone();
//Log.i(TAG, "ACCELEROMETER :" + accelerometer_values);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
magnitude_values = event.values.clone();
//Log.i(TAG, "MAGNETIC_FIELD" + magnitude_values);
break;
default:
break;
}
if (magnitude_values == null || accelerometer_values == null) {
return;
}
Log.i(TAG, "magnitude_values :"+ magnitude_values[0] + " " + magnitude_values[1] + " " + magnitude_values[2]
);
Log.i(TAG, "accelerometer_values :"+ accelerometer_values[0] + " " + accelerometer_values[1] + " " + accelerometer_values[2]
);
float[] R = new float[9];
float[] values = new float[3];
SensorManager.getRotationMatrix(R, null,
accelerometer_values, magnitude_values);
SensorManager.getOrientation(R, values);
Log.i(TAG, "rotation matrix :"+ R[0] + " " + R[1] + " " + R[2]+
" "+ R[3] + " " + R[4] + " " + R[5]+
" "+ R[6] + " " + R[7] + " " + R[8]);
String sensorInfo = "";
for (int i = 0; i < values.length; i++)
sensorInfo += "-values[" + i + "] = " + Math.toDegrees(values[i]) + "\n";
tvMessage.setText(sensorInfo);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Called when the accuracy of a sensor has changed.
}
};
@Override
protected void onResume() {
super.onResume();
boolean enable = sensorManager.registerListener(listener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI)
&&
sensorManager.registerListener(listener,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_UI);
if (!enable) {
sensorManager.unregisterListener(listener);
Log.e(TAG, getString(R.string.msg_SensorNotSupported));
}
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
沒有留言:
張貼留言