iPhoneのサンプルプログラム

ジャイロの参考アプリ

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

@interface testViewController : UIViewController {
	IBOutlet UITextView *gyroDataTextView_;
	CMMotionManager *motionManager;
//	CMDeviceMotion *deviceMotion;
	NSOperationQueue *opQ;
	CMGyroHandler gyroHandler;
	NSTimer *mainTimer;
	CMAttitude *referenceAttitude;
}
@property (nonatomic, retain) UITextView *gyroDataTextView;
- (IBAction)toggleGyroUpdates:(id)sender;

@end

ViewController.m
#import "ViewController.h"

@implementation ViewController


@synthesize gyroDataTextView=gyroDataTextView_;

- (void)startTimer {
	if (motionManager.isDeviceMotionAvailable) {
		[motionManager startDeviceMotionUpdates];

		mainTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES];
	}
}

- (void)stopTimer {
    if (motionManager.isDeviceMotionActive){
		[mainTimer invalidate];
		mainTimer = nil;

        [motionManager stopDeviceMotionUpdates];
	}
}

- (void)timerEvent {
    CMAttitude *currentAttitude = motionManager.deviceMotion.attitude;

	if (referenceAttitude != nil) {
		[currentAttitude multiplyByInverseOfAttitude: referenceAttitude];
	}

	NSMutableString* strGyroData = [NSMutableString string];
	
	[strGyroData appendFormat:@"[descriptionの全データ]\n"];
	[strGyroData appendFormat:@"%@\n", [currentAttitude description]];
	[strGyroData appendFormat:@"------------------------\n"];

	NSArray *array = [[currentAttitude description] componentsSeparatedByString:@" "];
	NSString* str;
	str = [array objectAtIndex:1];
	double pitch = [str doubleValue];
	str = [array objectAtIndex:3];
	double roll = [str doubleValue];
	str = [array objectAtIndex:5];
	double yaw = [str doubleValue];

	/*
	[strGyroData appendFormat:@"%7.3f %7.3f %7.3f\n", 
	 pitch/currentAttitude.pitch, roll/currentAttitude.roll, yaw/currentAttitude.yaw];
	 */
	[strGyroData appendFormat:@"[プロパティの値]\n"];
	[strGyroData appendFormat:@"%7.3f %7.3f %7.3f\n", 
	 currentAttitude.pitch, currentAttitude.roll, currentAttitude.yaw];
#define PI 3.14159265358979323846
#define RadianToDegree(Radian) ((Radian) * (180.0 / PI))
	[strGyroData appendFormat:@"[descriptionの値]\n"];
	[strGyroData appendFormat:@"%7.3f %7.3f %7.3f\n", 
	 pitch, roll, yaw];
	[strGyroData appendFormat:@"[プロパティの値をDegree度に変換したもの]\n"];
	[strGyroData appendFormat:@"%7.3f %7.3f %7.3f\n", 
	 RadianToDegree(currentAttitude.pitch),
	 RadianToDegree(currentAttitude.roll),
	 RadianToDegree(currentAttitude.yaw)];

	gyroDataTextView_.text = strGyroData;
}

- (IBAction)toggleGyroUpdates:(id)sender {
	referenceAttitude = [motionManager.deviceMotion.attitude retain];
//	[motionManager startGyroUpdatesToQueue:opQ withHandler:gyroHandler];
//	[motionManager stopGyroUpdates];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [self becomeFirstResponder];
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	motionManager = [[CMMotionManager alloc] init];
	motionManager.deviceMotionUpdateInterval = 0.02; // 50 Hz
	[self startTimer];

	/*
    motionManager = [[CMMotionManager alloc] init];
    if (motionManager.gyroAvailable) {
		motionManager.gyroUpdateInterval = 1.0/60.0;
        opQ = [[NSOperationQueue currentQueue] retain];
		
		[motionManager startGyroUpdatesToQueue:opQ withHandler:^( CMGyroData* data, NSError* error) {
				             NSLog(@"Gyroscope: %@", [data description]);
				         }];

		gyroHandler = ^ (CMGyroData *gyroData, NSError *error) {
            CMRotationRate rotate = gyroData.rotationRate;
			gyroDataLabel.text = [NSString stringWithFormat:
								  @"%f %f %f", 
								  rotate.x,
								  rotate.y,
								  rotate.z
								  ];	
        };
		[motionManager startGyroUpdatesToQueue:opQ withHandler:gyroHandler];
    } else {
        NSLog(@"No gyroscope on device.");
        [motionManager release];
    }
	 */
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[gyroDataTextView_ release];
	[self stopTimer];
	[opQ release];
	[motionManager release];
//	[deviceMotion release];
	[referenceAttitude release];
	
    [super dealloc];
}

@end