基于磁力计和陀螺仪的北方计算 [英] North calculation based on magnetometer and gyroscope

查看:117
本文介绍了基于磁力计和陀螺仪的北方计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算面对(如果它将基于真正的北方或磁性而无关紧要)。在iOS设备上可以看到 CLLocationManager 返回的 CLHeading 对象为我们提供了真实和磁性按相应的属性标题。此外,我们可以很容易地看到,这些值与设备顶部(设备坐标系的正Y轴)相关,这对我的目的不利。
我真正需要的是计算与设备屏幕相关的面(Z轴),因为我不需要指南针,而是AG应用程序之王。问题是当您将设备旋转到横向时,您将从面向方向向左或向右获取标题值,这是我最终需要的。
据我所知,我可以得到磁力计原始数据(以microtesla单位给出,每个设备轴的值为128到-128)以及陀螺仪原始数据(有三种类型) :欧拉天使,旋转矩阵或四元数)。我需要知道的是,我需要应用哪些计算来获得面向方向而不是标题

I need to calculate "facing" (it doesn't matter if it will be based on true north or magnetic one). As it can be seen on the iOS devices the CLHeading objects returned by the CLLocationManager gives us both the true and the magnetic heading by corresponding properties. Also, we can very easily see, that those values are related to the top of the device (the positive Y axis of the devices coordinate system) wich is not good for my purposes. What I actually need is to calculate the facing related to the screen of the device (Z axis) as I don't need the compass, but a king of AG application. The issue is when you rotate the device to landscape you get heading values to the left or to the right from your facing direction, wich is what I need in the end. As I know, I can get the magnetometer "raw" data (given to me in microtesla units with values from 128 to -128 for each device axis) along with the gyroscope "raw" data ( wich comes in three types: Euler angels, Rotation matrix or Quaternion). What I need is to know, which calculations I need to apply to those to get the "facing" direction instead of "heading"

推荐答案

<我已经做了一段时间了,因为我没有看到答案,所以我决定把我的解决方案放在那里为那些为同一个问题搜索答案的人......

I've made it a while ago and because I see no answers, I've decided to put my solution here for those who'll search answer for the same question...

_motionManager = [[CMMotionManager alloc]init];

    if (_motionManager.gyroAvailable) {
        _motionManager.deviceMotionUpdateInterval = 1.0/20.0;
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] 
                                            withHandler:^(CMDeviceMotion *motion, NSError *error) 
         {
             CMAcceleration gravity = motion.gravity;
             CGPoint tiltVector = CGPointMake(-gravity.x, -gravity.y);
             _tiltAngle = [self angleYAxisToVector:tiltVector];

             CLLocationDirection heaqding = [[SVSession sharedSession] heading].trueHeading;
             double newHeading = fmod(heaqding + _tiltAngle, 360.0);
             self.azimuth = degreesToRadian(newHeading);

             [self updateLocations]; //this function updates my ui for the new heading
         }];
    } else {
        NSLog(@"No gyroscope on device.");
        [_motionManager release],_motionManager = nil;
    }

这里有一些额外的片段可能有助于理解这个例子:

And here are some additional snippets that may help to understand this example:

-(double)angleYAxisToVector:(CGPoint)vector{
    double dX = vector.x;
    double dY = vector.y;

    if(dY == 0){
        if(dX > 0){
            return 0.0;
        }else{
            if(dX < 0){
                return 180.0;
            }else{
                return -1;
            }
        }
    }

    double beta = radiansToDegrees(atan(dX/dY));
    double angle;
    if(dX > 0){
        if (dY < 0){
            angle = 180 + beta;
        }else{
            angle = beta;
        }
    }else{
        if (dY < 0){
            angle = 180 + beta;
        }else{
            angle = 360 + beta;
        }
    }
    //    NSLog(@"angle = %f, normalized = %f",beta,angle);
    return angle; 
}

#define degreesToRadian(x)              (M_PI * (x) / 180.0)
#define radiansToDegrees(x)             ((x) * 180.0 / M_PI)
#define degreesToRadians(x)             degreesToRadian(x)
#define radiansToDegree(x)              radiansToDegrees(x)

快乐编码......

Happy coding...

这篇关于基于磁力计和陀螺仪的北方计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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