我怎样才能得到磁场矢量,独立于设备的旋转? [英] How can I get the magnetic field vector, independent of the device rotation?

查看:106
本文介绍了我怎样才能得到磁场矢量,独立于设备的旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想archieve是一种磁性指纹的位置的。我用的是MAGNETIC_FIELD传感器和在事件中,我得到了 3个值为(不幸的是没有进一步解释)X,Y和Z轴。

What I want to archieve is a sort of "magnetic fingerprint" of a location. I use the MAGNETIC_FIELD sensor and in the event I get the 3 values for the (unfortunately not further explained) X, Y and Z axis.

但问题是,这些值改变,因为我旋转设备,所以我猜的3轴相对于该设备。什么我需要的是补偿装置的旋转,使我得到同样的3价值观,无论设备如何旋转。

Problem is, that the values change as I rotate the device, so I guess the 3 axis are relative to the device. What I'd need is to compensate the device rotation so that I get the same 3 values, regardless of how the device is rotated.

我试图与旋转矩阵相乘(我知道怎么弄的),试图乘倾斜度矩阵等,但没有任何工程。不管我怎么努力,还是当我旋转设备的值的变化。

I tried to multiply with the rotation matrix (I know how to get that), tried to multiply with the inclination matrix and so on, but nothing works. Regardless of what I try, still the values change when I rotate the device.

因此​​,没有人知道该怎么做是正确的? preferrably与code,因为我读了很多的东西,如'那么,你就必须在使用旋转矩阵来补偿,但没有找到一个具体的,工作的例子。

So does anyone know how to do it right? Preferrably with code, because I read a lot of stuff like 'well then you'll have to compensate that using rotation matrix' but did not find a single concrete, working example.

推荐答案

做到这一点

private static final int TEST_GRAV = Sensor.TYPE_ACCELEROMETER;
private static final int TEST_MAG = Sensor.TYPE_MAGNETIC_FIELD;
private final float alpha = (float) 0.8;
private float gravity[] = new float[3];
private float magnetic[] = new float[3];

public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;
    if (sensor.getType() == TEST_GRAV) {
            // Isolate the force of gravity with the low-pass filter.
              gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
              gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
              gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
    } else if (sensor.getType() == TEST_MAG) {

            magnetic[0] = event.values[0];
            magnetic[1] = event.values[1];
            magnetic[2] = event.values[2];

            float[] R = new float[9];
            float[] I = new float[9];
            SensorManager.getRotationMatrix(R, I, gravity, magnetic);
            float [] A_D = event.values.clone();
            float [] A_W = new float[3];
            A_W[0] = R[0] * A_D[0] + R[1] * A_D[1] + R[2] * A_D[2];
            A_W[1] = R[3] * A_D[0] + R[4] * A_D[1] + R[5] * A_D[2];
            A_W[2] = R[6] * A_D[0] + R[7] * A_D[1] + R[8] * A_D[2];

            Log.d("Field","\nX :"+A_W[0]+"\nY :"+A_W[1]+"\nZ :"+A_W[2]);

        }
    }

这篇关于我怎样才能得到磁场矢量,独立于设备的旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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