Android 如何使用SensorManager.getOrientation()计算“俯仰”和“滚动”
我目前正在尝试替换我的“Sensor.TYPE_ORIENTATION”,因为它已被弃用。 因此缺乏关于如何做到这一点的大部分信息。在调试和diggin的过程中,我找到了如何计算方位角的方法,方位角曾经是由方位传感器提供的。 我是这样做的:Android 如何使用SensorManager.getOrientation()计算“俯仰”和“滚动”,android,algorithm,sensors,android-sensors,Android,Algorithm,Sensors,Android Sensors,我目前正在尝试替换我的“Sensor.TYPE_ORIENTATION”,因为它已被弃用。 因此缺乏关于如何做到这一点的大部分信息。在调试和diggin的过程中,我找到了如何计算方位角的方法,方位角曾经是由方位传感器提供的。 我是这样做的: float accelerometerVals[] = new float[3]; float magneticVals[] = new float[3]; float orientationSensorVals[] = new float[3]; f
float accelerometerVals[] = new float[3];
float magneticVals[] = new float[3];
float orientationSensorVals[] = new float[3];
float rotationMatrix[] = new float[16];
float orientation[] = new float[3];
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, accelerometerVals, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, magneticVals, 0, 3);
break;
case Sensor.TYPE_ORIENTATION:
System.arraycopy(event.values, 0, orientationSensorVals, 0, 3);
break;
default:
break;
}
if (null != magneticVals && null != accelerometerVals){
SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerVals, magneticVals)
SensorManager.getOrientation(rotationMatrix, orientation);
float azimuth = Math.toDegrees(orientation[0])
//this calculation gives me the Azimuth in the same format that OrientationSensor
azimuth += (azimuth >= 0) ? 0 : 360
float FALSE_PITCH = Math.toDegrees(orientation[1])
float FALSE_ROLL = Math.toDegrees(orientation[2])
}
注意变量FALSE\U PITCH和FALSE\U ROLL。我不知道如何将+10到-10之间的值进行归一化,以获得与我以前在方向传感器内相同的输出,我也遇到过同样的问题,但我很惊讶你的滚动不正确,因为这对我来说是错误的。以下是我使用的:
//Let values[] be your orientation[] variable once in degrees.
// Azimuth scaling
if (values[0]<0) values[0] += 360;
// Pitch scaling
if (values[1]<-90) values[1] += (-2*(90+values[1]));
else if (values[1]>90) values[1] += (2*(90-values[1]));
// Roll scaling
// NOT NEEDED
您能发布方向传感器和度数方向[]变量的一些输出吗?我也遇到过同样的问题,但我很惊讶您的滚动不正确,因为这对我来说是错误的。以下是我使用的:
//Let values[] be your orientation[] variable once in degrees.
// Azimuth scaling
if (values[0]<0) values[0] += 360;
// Pitch scaling
if (values[1]<-90) values[1] += (-2*(90+values[1]));
else if (values[1]>90) values[1] += (2*(90-values[1]));
// Roll scaling
// NOT NEEDED
您能发布方向传感器和度数方向[]变量的一些输出吗?可能会对您有所帮助
SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
boolean orientok = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
//test if orientation sensor exist on the device
if (!orientok){
mSensorManager.unregisterListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
((TextView)findViewById(R.id.orient)).setText("no orientation sensor");
}
public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()){
case Sensor.TYPE_ORIENTATION:
onOrientChanged(event);
break;
...// add other sensor type if you want and create for each one his function like the example below
}
public void onOrientChanged(SensorEvent event) {
float azimuth,pitch,roll;
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Axe x "+azimuth);
((TextView)findViewById(R.id.pitch)).setText("Axe y "+pitch);
((TextView)findViewById(R.id.roll)).setText("Axe z "+roll);
}
也许它会帮助你
SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
boolean orientok = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
//test if orientation sensor exist on the device
if (!orientok){
mSensorManager.unregisterListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
((TextView)findViewById(R.id.orient)).setText("no orientation sensor");
}
public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()){
case Sensor.TYPE_ORIENTATION:
onOrientChanged(event);
break;
...// add other sensor type if you want and create for each one his function like the example below
}
public void onOrientChanged(SensorEvent event) {
float azimuth,pitch,roll;
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Axe x "+azimuth);
((TextView)findViewById(R.id.pitch)).setText("Axe y "+pitch);
((TextView)findViewById(R.id.roll)).setText("Axe z "+roll);
}
您能解释一下为什么不需要对卷进行标准化吗?您能解释一下为什么不需要对卷进行标准化吗?类型_方向不受欢迎类型_方向不受欢迎