문제
Android 용 틸트 앱에서 작업하고 있습니다.초상화 및 가로 모드로 문제가 발생합니다.피치가 90도 (폰 끝에 전화)이 있고 롤의 물리적 변화가 없을 때 롤 가치가 미친 듯이 가기 전에 롤 가치가 미친 경우.나는이 문제에 대한 해결책을 찾을 수 없었습니다.누구든지 올바른 방향으로 나를 가리킬 수 있다면, 그것은 감사 할 것입니다.
여기에는 짧은 코드 덤프가 있으므로 가속도계 오류가 아닙니다.
final SensorEventListener mEventListener = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
setListners(sensorManager, mEventListener);
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
synchronized (this) {
switch (event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
long actualTime = System.currentTimeMillis();
//Sensitivity delay
if (actualTime - lastUpdate < 250) {
return;
}
else {
sysAzimuth = (int)Math.toDegrees(mValuesOrientation[0]);
sysPitch = (int)Math.toDegrees(mValuesOrientation[1]);
sysRoll = (int)Math.toDegrees(mValuesOrientation[2]);
//invert direction with -1
pitch = (sysPitch - pitchCal)*-1;
roll = (sysRoll - rollCal);
azimuth = sysAzimuth;
lastUpdate = actualTime;
}
. 해결책
나는 내가 찾고있는 것을 발견했다, 회전 행렬.
나는 피치와 롤에 대한 오일러 각 (롤, 피치, 요)을 사용하고 있었다.전화가 90도 끝나면 X와 Z 평원이 동일하고 전화기가 미친 듯이 가기 때문에 오일러 각도가있는 기본 결함이 있습니다.
getrotationMatrix 를 통해 회전 행렬을 사용하여 피치와 롤 각도를 가져와야합니다.
여기 모든 것;)
XML :
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/main.xml -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/update" android:text="Update Values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doUpdate" />
<Button android:id="@+id/show" android:text="Show Me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doShow" android:layout_toRightOf="@id/update" />
<TextView android:id="@+id/preferred" android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/update" />
<TextView android:id="@+id/orientation" android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/preferred" />
</RelativeLayout>
.
코드 :
package YOURPACKAGE;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
public class YOURCLASS extends Activity implements SensorEventListener {
private static final String TAG = "VirtualJax";
private SensorManager mgr;
private Sensor accel;
private Sensor compass;
private Sensor orient;
private TextView preferred;
private TextView orientation;
private boolean ready = false;
private float[] accelValues = new float[3];
private float[] compassValues = new float[3];
private float[] inR = new float[9];
private float[] inclineMatrix = new float[9];
private float[] orientationValues = new float[3];
private float[] prefValues = new float[3];
private float mAzimuth;
private double mInclination;
private int counter;
private int mRotation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preferred = (TextView)findViewById(R.id.preferred);
orientation = (TextView)findViewById(R.id.orientation);
mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
accel = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
compass = mgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
orient = mgr.getDefaultSensor(Sensor.TYPE_ORIENTATION);
WindowManager window = (WindowManager) this.getSystemService(WINDOW_SERVICE);
int apiLevel = Integer.parseInt(Build.VERSION.SDK);
if(apiLevel <8) {
mRotation = window.getDefaultDisplay().getOrientation();
}
else {
mRotation = window.getDefaultDisplay().getRotation();
}
}
@Override
protected void onResume() {
mgr.registerListener(this, accel, SensorManager.SENSOR_DELAY_GAME);
mgr.registerListener(this, compass, SensorManager.SENSOR_DELAY_GAME);
mgr.registerListener(this, orient, SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
@Override
protected void onPause() {
mgr.unregisterListener(this, accel);
mgr.unregisterListener(this, compass);
mgr.unregisterListener(this, orient);
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// ignore
}
public void onSensorChanged(SensorEvent event) {
// Need to get both accelerometer and compass
// before we can determine our orientationValues
switch(event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
for(int i=0; i<3; i++) {
accelValues[i] = event.values[i];
}
if(compassValues[0] != 0)
ready = true;
break;
case Sensor.TYPE_MAGNETIC_FIELD:
for(int i=0; i<3; i++) {
compassValues[i] = event.values[i];
}
if(accelValues[2] != 0)
ready = true;
break;
case Sensor.TYPE_ORIENTATION:
for(int i=0; i<3; i++) {
orientationValues[i] = event.values[i];
}
break;
}
if(!ready)
return;
if(SensorManager.getRotationMatrix(inR, inclineMatrix, accelValues, compassValues)) {
// got a good rotation matrix
SensorManager.getOrientation(inR, prefValues);
mInclination = SensorManager.getInclination(inclineMatrix);
// Display every 10th value
if(counter++ % 10 == 0) {
doUpdate(null);
counter = 1;
}
}
}
public void doUpdate(View view) {
if(!ready)
return;
mAzimuth = (float) Math.toDegrees(prefValues[0]);
if(mAzimuth < 0) {
mAzimuth += 360.0f;
}
String msg = String.format(
"Preferred:\nazimuth (Z): %7.3f \npitch (X): %7.3f\nroll (Y): %7.3f",
mAzimuth, Math.toDegrees(prefValues[1]),
Math.toDegrees(prefValues[2]));
preferred.setText(msg);
msg = String.format(
"Orientation Sensor:\nazimuth (Z): %7.3f\npitch (X): %7.3f\nroll (Y): %7.3f",
orientationValues[0],
orientationValues[1],
orientationValues[2]);
orientation.setText(msg);
preferred.invalidate();
orientation.invalidate();
}
public void doShow(View view) {
// google.streetview:cbll=30.32454,-81.6584&cbp=1,yaw,,pitch,1.0
// yaw = degrees clockwise from North
// For yaw we can use either mAzimuth or orientationValues[0].
//
// pitch = degrees up or down. -90 is looking straight up,
// +90 is looking straight down
// except that pitch doesn't work properly
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(
"google.streetview:cbll=30.32454,-81.6584&cbp=1," +
Math.round(orientationValues[0]) + ",,0,1.0"
));
startActivity(intent);
return;
}
. 다른 팁
나는 euler angles (롤, 피치, 요)를 사용하지 않을 것입니다.이미 알아 차리면서 앱의 안정성을 훨씬 망칩니다.
여기를 참조하십시오. 안드로이드 오리엔테이션 센서로 이상한 동작 .
실험을 통해 초상화에서 가로 모드로 전환 할 때 회전 행렬이 변경되지 않지만 OpenGL과 함께 사용할 수 있도록 수동으로 변경해야한다는 것을 알았습니다.
copyMat(mRotationMatrixP, mRotationMatrix);
// permute and negate columns 0, 1
mRotationMatrixP[0] = -mRotationMatrix[1];
mRotationMatrixP[4] = -mRotationMatrix[5];
mRotationMatrixP[8] = -mRotationMatrix[9];
// permute 1, 0
mRotationMatrixP[1] = mRotationMatrix[0];
mRotationMatrixP[5] = mRotationMatrix[4];
mRotationMatrixP[9] = mRotationMatrix[8];
.
또한 첫 번째 장소에서 올바르게 회전 행렬을 올바르게 획득하시기 바랍니다.
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(
mRotationMatrix , event.values);
SensorManager.getOrientation (mRotationMatrix, values);
. 당신이 설명하는 것은 짐벌 잠금이라고합니다.피치 +/- 90에서는 요 - (+) 롤이 완전히 정의되지 않습니다.피치 +/- 90 근처에서는 작은 NOIS / 오류가 태도의 오류가 발생할 수 있으며 실제 방향이 크게 변화가 없더라도 개별적으로 개별적으로 변동될 수 있습니다.여기 요, 피치 롤에 대한 훌륭한 쓰기가 있습니다 (많은 플랫폼에 잘 구현되지 않음) :
http://www.sensorplatforms.com/understanding-ientation-conventions.- 모브 - 플랫폼 /