简体   繁体   English

确定以地平线为轴的设备的角度-android

[英]determining angle of device with horizon as axis -android

I have an interesting problem. 我有一个有趣的问题。 I am trying to trace out the angle of a users phone regardless of whether they are holding it in landscape or portrait or any where in between.. 我试图找出用户手机的角度,而不管他们是横向还是纵向握住手机,还是介于两者之间。

I am currently using SENSOR_ORIENTATION and getting YAW, ROLL, and Pitch. 我目前正在使用SENSOR_ORIENTATION并获得YAW,ROLL和Pitch。 I was also trying to get the angle of the users view port on the zaxis using yaw, and to correct for the users roll (landscape, portrait, or in between) i did some simple math..: 我还试图使用偏航角来获取用户在z轴上的视口的角度,并纠正用户的侧倾(横向,纵向或中间),我做了一些简单的数学运算..:

double roll = phoneOri.getRoll();
double yaw = phoneOri.getYaw()+roll;

     if (yaw>=360){
                yaw = (yaw - 360);
            }
            if (yaw<0){
                yaw = (yaw+360);    
            }

this code corrects the yaw and gives me the proper angle no matter what the roll position is.. 该代码可以校正偏航,并且无论滚动位置在什么位置都可以给我合适的角度。
but this sort of math doesn't correct for pitch it seems. 但是这种数学方法似乎无法解决音高问题。 if i hold my phone perpendicular to the floor i want to always get a pitch of 0 regardless of roll or yaw, if i face the back of the phone straight up to the sky and parallel to the ground i want to get 90, back to the ground -90; 如果我将手机垂直于地板放置,则无论滚动或偏航,我都希望始终将其倾斜度设置为0,如果我将手机背面直接面对天空并平行于地面,则我想将其倾斜90度,地面-90;

here is how i am getting pitch roll and yaw: 这是我如何变桨和偏航的方法:

    double yaw;
    double pitch;
    double roll;

public void onSensorChanged(SensorEvent evt) {
            int type=evt.sensor.getType();
            if(type == Sensor.TYPE_ORIENTATION){
                yaw = evt.values[0];
                pitch = evt.values[1];
                roll = evt.values[2];
            }
            if (type == Sensor.TYPE_MAGNETIC_FIELD) {
                orientation[0]=(orientation[0]*1+evt.values[0])*0.5f;
                orientation[1]=(orientation[1]*1+evt.values[1])*0.5f;
                orientation[2]=(orientation[2]*1+evt.values[2])*0.5f;
            } else if (type == Sensor.TYPE_ACCELEROMETER) {
                acceleration[0]=(acceleration[0]*2+evt.values[0])*0.33334f;
                acceleration[1]=(acceleration[1]*2+evt.values[1])*0.33334f;
                acceleration[2]=(acceleration[2]*2+evt.values[2])*0.33334f;
            }
            if ((type==Sensor.TYPE_MAGNETIC_FIELD) || (type==Sensor.TYPE_ACCELEROMETER)) {
                float newMat[]=new float[16];

                //Toast toast = Toast.makeText(ctx.getApplicationContext(), "accel", Toast.LENGTH_SHORT);
                //toast.show();
                SensorManager.getRotationMatrix(newMat, null, acceleration, orientation);
                if(displayOri==0||displayOri==2){
                    SensorManager.remapCoordinateSystem(newMat,SensorManager.AXIS_X*-1, SensorManager.AXIS_MINUS_Y*-1,newMat);
                }else{
                    SensorManager.remapCoordinateSystem(newMat,SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,newMat);
                }

                matrix=newMat;
                SensorManager.getOrientation (newMat, orientationValues); 
            }
        }

currently i get the following results: 目前我得到以下结果:

pitch = -90 roll = 0 ; 俯仰= -90横滚= 0 ;

在此处输入图片说明

pitch = 0 roll = 90 ; 俯仰= 0滚= 90 ;

在此处输入图片说明

pitch = 90 roll = -20 ; 俯仰= 90滚= -20 ;

在此处输入图片说明

pitch = 0 roll = -90 ; 俯仰= 0滚= -90 ;

I am trying to get a pitch of 0 in any of these positions and then get a pitch between -90 and 90 respectively as the user tilts the phone on the HORIZON axis? 我试图在这些位置中的任何一个位置将音高设置为0,然后在用户将手机沿HORIZON轴倾斜时分别获得-90和90之间的音高?

any ideas? 有任何想法吗? oh and i should add that my application is locked to landscape mode.. 哦,我应该补充一点,我的应用程序已锁定为横向模式。

Those constants probably have cumlative errors. 这些常数可能有累积误差。 Try using the Math functions instead... 尝试改用数学函数...

Interesting problem, 有趣的问题

have you tried getting the absolute x,y and z values (irrespective of the orientation of the phone) as mentioned here , using that, you should get the absolute y values that you are looking for 您是否尝试过获取此处所述的x,y和z的绝对值(与手机的方向无关),使用该方法,您应该获取想要的y绝对值

public abstract void onSensorChanged (int sensor, float[] values) 公共抽象无效onSensorChanged(int传感器,float []值)

> IMPORTANT NOTE: The axis are swapped when the device's screen orientation changes. >重要说明:当设备的屏幕方向改变时,轴将被交换。 To access the unswapped values, use indices 3, 4 and 5 in values[]. 要访问未交换的值,请在values []中使用索引3、4和5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM