【问题标题】:How to convert Yaw,Roll & Pitch values to (x,y) coordinates?如何将 Yaw、Roll 和 Pitch 值转换为 (x,y) 坐标?
【发布时间】:2013-06-07 18:32:52
【问题描述】:

嗨,在我的应用程序中,我需要创建一个陀螺仪。因此,我正在使用以下代码。

public class AccessGyroscope extends Activity implements SensorEventListener
{
    private TextView tv;
    private SensorManager sManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.tv);
        sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
    }

    @Override
    protected void onStop() 
    {
        sManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) 
    {
    }

    @Override
    public void onSensorChanged(SensorEvent event) 
    {
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        {
            return;
        }

        tv.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
                   "Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
                   "Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
    }
}

我可以获得 Yaw、Roll 和 Pitch 值,但我想在 2d 平面上显示它。 基本上我想要这样的视图。

红点应根据设备的方向移动。我可以在画布上工作以准备该视图。但我真正想要的是那些与 Yaw、Roll 和 Pitch 值相关的坐标。

请帮忙

【问题讨论】:

    标签: android canvas android-sensors


    【解决方案1】:

    我终于找到了答案。

    首先我没有使用 TYPE_ORIENTATION,而是使用了 TYPE_GRAVITY。

    这是我的完整代码。

    我的自定义视图的代码是..

    public class GyroscopeView extends View
    {
        private final static String TAG = "GyroscopeView";
    
        private float bearing;
        float pitch = 0;
        float roll = 0;
    
        private Paint paintOuter;
        private Paint paintInner;
        private Paint paintDot;
    
        float pointX, pointY;
        float dotX, dotY;
        int radius;
    
        public GyroscopeView(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
            initCompassView();
        }
    
        public GyroscopeView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            initCompassView();
        }
    
        public GyroscopeView(Context context)
        {
            super(context);
            // TODO Auto-generated constructor stub
            initCompassView();
        }
    
        protected void initCompassView()
        {
            setFocusable(true);
    
            Resources r = this.getResources();
    
            paintOuter = new Paint(Paint.ANTI_ALIAS_FLAG);
            paintOuter.setColor(Color.WHITE);
            paintOuter.setStrokeWidth(1);
            paintOuter.setStyle(Paint.Style.FILL_AND_STROKE);
    
            paintInner = new Paint(Paint.ANTI_ALIAS_FLAG);
            paintInner.setColor(Color.BLUE);
            paintInner.setStrokeWidth(1);
            paintInner.setStyle(Paint.Style.STROKE);
    
            paintDot = new Paint(Paint.ANTI_ALIAS_FLAG);
            paintDot.setColor(Color.RED);
            paintDot.setStrokeWidth(1);
            paintDot.setStyle(Paint.Style.FILL_AND_STROKE);
    
        }
    
        @Override
        protected void onDraw(Canvas canvas)
        {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
    
            int px = getMeasuredWidth() / 2;
            int py = getMeasuredHeight() / 2;
            radius = Math.min(px, py);
    
            pointX = px;
            pointY = py;
    
            canvas.drawCircle(pointX, pointY, radius, paintOuter);
    
            canvas.drawCircle(pointX, pointY, 40, paintInner);
            canvas.drawCircle(dotX, dotY, 5, paintDot);
    
        }
    
        void update(float z, float yy, float xx)
        {
    
            if (yy > 0)
            {
                dotY = pointY - ((1 - yy) * z);
            } else
            {
                dotY = pointY + ((1 - yy) * z);
            }
            if (xx > 0)
            {
                dotX = pointX - ((1 - xx) * z);
            } else
            {
                dotX = pointX + ((1 - xx) * z);
            }
            invalidate();
        }
    

    这是我的活动代码。

    public class GyroscopeActivity extends Activity
    {
        GyroscopeView gyroscopeView;
        SensorManager sensorManager;
    
        float[] gValues = new float[3];
    
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gyroscope);
    
             gyroscopeView = (GyroscopeView) findViewById(R.id.gyroscope_view);
            initSensor();
        }
    
        void initSensor()
        {
            sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        }
    
        @Override
        protected void onResume()
        {
            super.onResume();
            Sensor sensorGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
            sensorManager.registerListener(sensorGyroListener, sensorGyroscope, SensorManager.SENSOR_DELAY_UI);
        }
    
        @Override
        protected void onStop()
        {
            sensorManager.unregisterListener(sensorGyroListener);
            super.onStop();
        }
    
        private final SensorEventListener sensorGyroListener = new SensorEventListener()
        {
            public void onSensorChanged(SensorEvent event)
            {
                if (event.sensor.getType() == Sensor.TYPE_GRAVITY)
                    gValues = event.values;
    
                gyroscopeView.update(gValues[0], gValues[1], gValues[2]);
            }
    
            public void onAccuracyChanged(Sensor sensor, int accuracy)
            {
            }
        };
    

    希望这会对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2013-06-02
      • 1970-01-01
      • 2021-05-10
      • 2012-02-26
      • 2013-12-25
      相关资源
      最近更新 更多