CMDeviceMotion 与multiplyByInverseOfAttitude 相关的问题

发布于 2024-11-15 01:51:06 字数 1355 浏览 2 评论 0原文

我第一次使用 Core Motion,遇到了一个问题,我无法弄清楚发生了什么。我确信这将是一个简单的问题。以下是一些代码:

.h 文件

#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>

@interface TempVC : UIViewController {
CMMotionManager *motionManager;
CMAttitude *referenceAttitude;
CMAttitude *attitude;
}
- (void)updateData;
@end

.m 文件

#import "TempVC.h"
#define DEGREES(radians) (radians / M_PI * 180.0)
@implementation TempVC

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];

motionManager = [[CMMotionManager alloc] init]; 
referenceAttitude = nil;

CMDeviceMotion *dm = motionManager.deviceMotion;
attitude = dm.attitude;
referenceAttitude = [attitude retain];

motionManager.deviceMotionUpdateInterval = 0.1;
[motionManager startDeviceMotionUpdates];

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateData) userInfo:nil repeats:YES];

}

- (void)updateData
{
attitude = motionManager.deviceMotion.attitude;
[attitude multiplyByInverseOfAttitude:referenceAttitude];
NSLog(@"\n Roll: %.0f\n Pitch: %.0f\n Yaw: %.0f",DEGREES(attitude.roll), DEGREES(attitude.pitch), DEGREES(attitude.yaw));
}

此代码将崩溃,并且没有真正指示出了什么问题。如果我注释掉

[attitude multiplyByInverseOfAttitude:referenceAttitude];

一切正常???我是否错误地实施了参考?

I am having my first play around with Core Motion and have struck an issue and I can't work out what is going on. I am sure it is going to be a simple issue. Here is some code:

.h file

#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>

@interface TempVC : UIViewController {
CMMotionManager *motionManager;
CMAttitude *referenceAttitude;
CMAttitude *attitude;
}
- (void)updateData;
@end

.m file

#import "TempVC.h"
#define DEGREES(radians) (radians / M_PI * 180.0)
@implementation TempVC

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];

motionManager = [[CMMotionManager alloc] init]; 
referenceAttitude = nil;

CMDeviceMotion *dm = motionManager.deviceMotion;
attitude = dm.attitude;
referenceAttitude = [attitude retain];

motionManager.deviceMotionUpdateInterval = 0.1;
[motionManager startDeviceMotionUpdates];

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateData) userInfo:nil repeats:YES];

}

- (void)updateData
{
attitude = motionManager.deviceMotion.attitude;
[attitude multiplyByInverseOfAttitude:referenceAttitude];
NSLog(@"\n Roll: %.0f\n Pitch: %.0f\n Yaw: %.0f",DEGREES(attitude.roll), DEGREES(attitude.pitch), DEGREES(attitude.yaw));
}

This code will crash with no real indication of what goes wrong. If I comment out

[attitude multiplyByInverseOfAttitude:referenceAttitude];

Everything works fine??? Have I implemented the reference wrong?

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

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

发布评论

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

评论(1

硬不硬你别怂 2024-11-22 01:51:06

您可以在开始更新之前访问 CMMotionManager 的 deviceMotion 属性。你在那里存储你的referenceAttitude,那时它仍然为零。当几毫秒后调用 updateData 时,[attitudemultiplyByInverseOfAttitude:referenceAttitude]; 将失败。

尝试在第一次调用 updateData 时设置referenceAttitude,它应该可以工作。

You access CMMotionManager's deviceMotion property before starting updates. There you store your referenceAttitude which will still be nil at that time. When updateData is called a few millis later [attitude multiplyByInverseOfAttitude:referenceAttitude]; will fail.

Try instead to set the referenceAttitude on the first call of updateData and it should work.

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