基于磁力计和陀螺仪的寻北计算

发布于 2025-01-04 20:20:50 字数 397 浏览 3 评论 0原文

我需要计算“面向”(无论它是基于真北还是磁北)。正如在 iOS 设备上所见,CLLocationManager 返回的 CLHeading 对象通过相应的属性为我们提供了真实航向和磁航向。另外,我们可以很容易地看到,这些值与设备的顶部(设备坐标系的正 Y 轴)相关,这不利于我的目的。

我实际上需要的是计算与设备屏幕相关的朝向(Z 轴),因为我不需要指南针,而是 AG 应用程序之王。问题是,当您将设备旋转到横向时,您会得到从您面向的方向向左或向右的航向值,这正是我最终需要的。 据我所知,我可以获得磁力计“原始”数据(以微特斯拉为单位,每个设备轴的值从 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) which 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, which 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 ( which 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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丿*梦醉红颜 2025-01-11 20:20:51

我不久前就做到了,因为我没有看到答案,所以我决定将我的解决方案放在这里,供那些将搜索同一问题答案的人......

_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;
    }

这里有一些额外的片段,可能有助于理解这一点示例:

-(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)

快乐编码...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文