CMDeviceMotion 与multiplyByInverseOfAttitude 相关的问题
我第一次使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在开始更新之前访问 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.