首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >陀螺仪/罗盘和器件方向

陀螺仪/罗盘和器件方向
EN

Stack Overflow用户
提问于 2012-12-27 06:09:23
回答 1查看 2.1K关注 0票数 0

我正在为Android制作一个指南针应用程序,我已经设法通过使用陀螺仪、加速计和磁力计(参见我使用的教程的here )来捕捉方位角。不幸的是,只有当设备与地面平行放置(例如,平放在桌子上/我的手掌等)时,指南针读数才能正常工作。每当我将设备倾斜到完全直立的位置(屏幕对着我),指南针就会给出错误的读数。我假设这是因为当设备本身的方向改变时,轴也会改变。如何防止这种情况发生?我希望指南针在这两个位置都工作:平行于地面和完全直立的位置。我该怎么做呢?如果有人能给我一个正确的方向,那将是非常有帮助的。

谢谢你的帮助!:D

EN

回答 1

Stack Overflow用户

发布于 2013-04-14 06:07:55

在SensorFusionActivity中添加mDisplay变量,如下所示:

代码语言:javascript
复制
private Display mDisplay = null;

然后,在onCreate方法中添加以下代码:

代码语言:javascript
复制
mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

最后,按如下方式修改calculateAccMagOrientation:

代码语言:javascript
复制
// calculates orientation angles from accelerometer and magnetometer output
public void calculateAccMagOrientation() {
    if(SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet)) {

        int rotation = mDisplay.getRotation();

        if(rotation == Surface.ROTATION_0)
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, rotationMatrix);
        else if(rotation == Surface.ROTATION_180)
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, rotationMatrix);
        else if(rotation == Surface.ROTATION_270)
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, rotationMatrix);

        SensorManager.getOrientation(rotationMatrix, accMagOrientation);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14046846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档