我正在尝试使设备倾斜(设备沿 y 轴旋转,但不幸的是我无法实现我的目标。我尝试了很多使用 TYPE_ACCELEROMETER 和 TYPE_MAGNETIC_FIELD 作为组合传感器(传感器融合)的东西。我也关注了运动传感器
我想要的是?
我想获得安装在车辆上的设备(手机)的倾斜度。比方说,我在汽车上安装了一个设备,而汽车是静止的。所以倾角是0度。Lator on,当车辆通过下通道或天桥时,倾角应该是相应的。我试图计算这个,这是我的代码:
...
...
private static float ALPHA = 0.005f
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv = (TextView) findViewById(R.id.tv);
edtAlpha = (EditText) findViewById(R.id.alpha);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
if (accelerometer == null) {
Toast.makeText(this, "Oh not found", Toast.LENGTH_SHORT).show();
}
if (magnetometer == null) {
Toast.makeText(this, "Oh magnetometer not found",
Toast.LENGTH_SHORT).show();
}
}
protected float[] lowPass(float[] input, float[] output) {
if (output == null)
return input;
String s = edtAlpha.getText().toString();
if (s != null && s.length() > 0) {
try {
ALPHA = Float.valueOf(s);
} catch (NumberFormatException e) {
ALPHA = 0.005f;
}
} else {
ALPHA = 0.005f;
}
for (int i = 0; i < input.length; i++) {
output[i] = output[i] + ALPHA * (input[i] - output[i]);
}
return output;
}
public int getRotation(final Activity activity) {
int result = 1;
Method mDefaultDisplay_getRotation;
try {
mDefaultDisplay_getRotation = Display.class.getMethod(
"getRotation", new Class[] {});
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
Object retObj = mDefaultDisplay_getRotation.invoke(display);
if (retObj != null) {
result = (Integer) retObj;
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
public void onSensorChanged(SensorEvent event) {
Log.d(tag, "onSensorChanged");
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
gravSensorVals = lowPass(event.values.clone(), gravSensorVals);
} else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
magSensorVals = lowPass(event.values.clone(), magSensorVals);
}
if (gravSensorVals != null && magSensorVals != null) {
SensorManager.getRotationMatrix(RTmp, I, gravSensorVals,
magSensorVals);
int rotation = getRotation(this);
if (rotation == 1) {
SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_X,
SensorManager.AXIS_MINUS_Z, Rot);
} else {
SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_Y,
SensorManager.AXIS_MINUS_Z, Rot);
}
SensorManager.getOrientation(Rot, results);
float azimuth = (float) (((results[0] * 180) / Math.PI) + 180);
float pitch = (float) (((results[1] * 180 / Math.PI)) + 90);
float roll = (float) (((results[2] * 180 / Math.PI)));
tv.setText("Azimuth : " + df.format(azimuth) + "\nPitch : "
+ df.format(pitch) + "\nRoll : " + df.format(roll));
}
Log.d(tag, "Sensor type : " + event.sensor.getType());
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer,
SensorManager.SENSOR_DELAY_UI);
}
...
...
我的代码有什么问题?
当我快速加速/减速我的车时,角度会迅速增加/减少,但它不应该。换句话说,当我加速/减速车辆时,不应该对角度有任何影响。我也尝试遵循这些教程: Link 1 Link 2等。