Android NDK中的陀螺仪和磁场传感器事件

3

当在Android上本地访问传感器时,如何访问磁场和陀螺仪传感器事件的值:

if(event.type == ASENSOR_TYPE_ACCELEROMETER) {
        float x = event.acceleration.x;
            ...
    }
else if(event.type == ASENSOR_TYPE_GYROSCOPE) {
        ???
    }
else if(event.type == ASENSOR_TYPE_MAGNETIC_FIELD) {
        ???
    }

谢谢


数值存储在event.values[]数组中 - 全部解释在这里:http://developer.android.com/guide/topics/sensors/sensors_overview.html - pootle
我认为那不太对。ASensorEvent结构如下所示:http://mobilepearls.com/labs/native-android-api/include/android/sensor.h - user2830438
啊!抱歉,我是用Java编写的,如果您使用的是C语言,则它们似乎在加速度或磁力联合体中 - 这将导致其为event.magnetic.x或event.acceleration.azimuth - 但它们在底层都是相同的东西。 - pootle
看,联合体中没有陀螺仪字段。而且,加速度计肯定不能给你角度测量。你是指甚至磁场方位角吗?我只是试图弄清楚联合体中哪个字段对应哪个传感器。除了加速度计外,我找不到任何文档资料。 - user2830438
好的,我会读取前三个浮点数并查看它们是否有意义。Android文档各不相同,但一定会有相关内容,所以请仔细查找。 - pootle
2个回答

3

看一下传感器的头文件:

ANDROID-NDK-ROOT-DIR/platforms/android-/arch-arm/usr/include/android/sensor.h

typedef struct ASensorVector {
    union {
        float v[3];
        struct {
            float x;
            float y;
            float z;
        };
        struct {
            float azimuth;
            float pitch;
            float roll;
        };
    };
    int8_t status;
    uint8_t reserved[3];
} ASensorVector;

typedef struct ASensorEvent {
    int32_t version; /* sizeof(struct ASensorEvent) */
    int32_t sensor;
    int32_t type;
    int32_t reserved0;
    int64_t timestamp;
    union {
        float           data[16];
        ASensorVector   vector;
        ASensorVector   acceleration;
        ASensorVector   magnetic;
        float           temperature;
        float           distance;
        float           light;
        float           pressure;
    };
    int32_t reserved1[4];
} ASensorEvent;

顺便说一下,我找到了这个例子:

https://github.com/Uroc327Mirrors/pixellight/blob/43a661e762034054b47766d7e38d94baf22d2038/Base/PLInput/src/Backend/Android/AndroidSensorManagerDevice.cpp


0
根据这个 https://developer.android.com/guide/topics/sensors/sensors_motion ,你可以通过字段 data 获取任何事件的数据:
if(event.type == ASENSOR_TYPE_GYROSCOPE) 
{
    float x=event.data[0];//Rate of rotation around the x axis
    float y=event.data[1];//Rate of rotation around the y axis
    float z=event.data[2];//Rate of rotation around the z axis
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接