Question

I am unable to figure out why the roll, pitch and yaw values are giving 0.0000 when logged.. I am sure it is a something that i miss but i cant figure it out..

This is the code:

//ViewController.m

#import "ViewController.h"
@interface ViewController (){

}
@property (nonatomic) CMMotionManager *motionManager;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.motionManager = [[CMMotionManager alloc] init];
    CMDeviceMotion *devMotion = [[CMDeviceMotion alloc]init];

    if ([self.motionManager isDeviceMotionAvailable]) {
        NSLog(@"Device Motion Available");
        [self.motionManager setDeviceMotionUpdateInterval:1.0/30.0];
        // Pull mechanism is used
        [self.motionManager startDeviceMotionUpdates];
    }
    devMotion = self.motionManager.deviceMotion;

    NSLog(@"Roll Pitch and Yaw are %f, %f, %f",devMotion.attitude.roll, devMotion.attitude.pitch, devMotion.attitude.yaw);

}

I have gone thru this similar question: SO Question

Please help me understand this..

Thanks..


Updated Code:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.motionManager = [[CMMotionManager alloc] init];

    if ([self.motionManager isDeviceMotionAvailable]) {
        NSLog(@"Device Motion Available");
        [self.motionManager setDeviceMotionUpdateInterval:1.0/30.0];
        // Pull mechanism is used
        [self.motionManager startDeviceMotionUpdates];
    }
    CMDeviceMotion *devMotion = self.motionManager.deviceMotion;
    NSLog(@"*Roll,Pitch and Yaw are %f, %f, %f",devMotion.attitude.roll, devMotion.attitude.pitch, devMotion.attitude.yaw);

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(updateValues:) userInfo:nil repeats:YES];
}

-(void) updateValues:(NSTimer *)timer{
    CMDeviceMotion *currDeviceMotion = self.motionManager.deviceMotion;

    NSLog(@"Roll Pitch and Yaw are %f, %f, %f",currDeviceMotion.attitude.roll, currDeviceMotion.attitude.pitch, currDeviceMotion.attitude.yaw);


}

This code also has a large part of its initial values as 0.000000 . After that it starts to get values... So i guess there is some delay for startDeviceMotionUpdates for providing values to deviceMotion. So looks like i need to figure out how to save the first non zero values.

Était-ce utile?

La solution

  • Notice that your devMotion variable won’t hold usable values (or any at all) right away as CMMotionManager takes a while to update the deviceMotion property after startDeviceMotionUpdates. So you should have some sort of timer that fires periodically and reads that property. Your linked article does something similar.
    • As long as the gyroscope is starting up, the deviceMotion property will be null (you’re only seeing 0.0 because messages to nil return zero).
  • As a sidenote: your call to [[CMDeviceMotion alloc] init] is useless as you then override the variable the result was assigned to with self.motionManager.deviceMotion.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top