机器人:为什么getrotationmatrix返回false? [英] android: why getrotationmatrix return false?

查看:467
本文介绍了机器人:为什么getrotationmatrix返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要得到我的手机的方向,我已经使用这个code,我觉得很多人使用它。这是code

I want to get the orientation of my phone and I have using this code that I find lot of people using it. this is the code

    public void onSensorChanged(SensorEvent event) {
    //if the data sensor is unreliabel
    if(event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        return;

    //gets the value
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        gravity = event.values.clone();
        break;

    case Sensor.TYPE_MAGNETIC_FIELD:
        geomag = event.values.clone();
        break;
    }
    getOrientation();
}

private void getOrientation(){

    //if gravity n geomag have value, find the rotation matrix
            if(gravity != null && geomag != null){

                //check the rotation matrix found
                boolean success = SensorManager.getRotationMatrix(inR, I, gravity, geomag);

                if(success){
                    SensorManager.getOrientation(inR, orientVals);
                    azimuth = Math.toDegrees(orientVals[0]);

                    TextView azi = (TextView) findViewById(R.id.textAzi);
                    azi.setText("azi : " + azimuth);

                }
                TextView cek = (TextView) findViewById(R.id.cek);
                cek.setText("rotation: "+success);
            }   

但为什么getrotationmatrix随时返回假的? 问题出在哪里?

but why the getrotationmatrix always return to false? where is the problem?

推荐答案

我再试一次,并获得解决方案 但我改变code变成像下面的

I've try again and get the solution but I change the code to become like below

    public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    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;
    }

    if(!ready)
        return;

    boolean cek = SensorManager.getRotationMatrix(inR, inclineMatrix, accelValues, compassValues);

    if(cek){
        SensorManager.getOrientation(inR, prefValues);
        mInclination = SensorManager.getInclination(inclineMatrix);

        //display every 30th values
        if(counter++ % 30 == 0){
            //do your code, what you want to do to the result
            counter = 1;
        }
    }

和一切工作得很好

这篇关于机器人:为什么getrotationmatrix返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆