iphone によるセンサプログラミング 基応専般 沼田哲史大阪電気通信大学総合情報学部デジタルゲーム学科 iphone プログラミングの概要 Apple iphone/ipad ios 2011 10 12 iphone/ipad OS ios 5 ios 5 ios 5 iphone 3GS iphone 4 iphone 4S ipad ipad 2 3 4 2009 ipod touch 図 -1 3 3G iphone/ipad 情報 GPS iphone/ipad Xcode 4 Objective-C 図 -1 iphone 4, 第 4 世代 ipod touch,ipad 2( 図中の Web ページは Wikipedia より Gyroscope Accelerometer の項目 ) ios Cocoa Touch NeXTSTEP Mac OS X Cocoa Cocoa Mac OS X 10 Cocoa Touch Cocoa Objective-C MVC ios 8 情報処理 Vol.53 No.1 Jan. 2012
1 iphone によるセンサプログラミング 図 -2 新規プロジェクトの設定 Core Location Core Motion Core Location GPS 情報 情報 Wi-Fi 情報 情報 Core Motion Core Location Core Motion GPS 情報 Core Location を利用した位置情報取得 Core Location Core Location Xcode 4.2 Core Location Xcode File - New - New Project ios Application Single View Application Product Name CoreLocation Sample Class Prefix CS 理 Use Automatic Reference Counting 図 -2 Xcode CoreLocation Sample TARGETS Summary Linked Frameworks and Libraries + 情報処理 Vol.53 No.1 Jan. 2012 9
図 -3 Core Location フレームワークの追加 kcllocationaccuracybestfornavigation kcllocationaccuracybest kcllocationaccuracynearesttenmeters kcllocationaccuracyhundredmeters kcllocationaccuracykilometer kcllocationaccuracythreekilometers 表 -2 位置取得の精度の定数一覧 情報 10 100 3 CoreLocation.framework 図 -3 CSViewController.h Core Location CSViewController Delegate Core Location Core Location 理 #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface CSViewController : UIViewController<CLLocationManagerDelegate> CLLocationManager *locationmanager; @end CSViewController.m viewdidload CLLocationManager 情報 理 CLLocationManager Delegate 情報 desiredaccuracy 表 -2 情報 startupdatinglocation startupdatingheading 情報 情報 10 情報処理 Vol.53 No.1 Jan. 2012
1 iphone によるセンサプログラミング - (void)viewdidload [super viewdidload]; locationmanager = [[CLLocationManager alloc] init]; locationmanager.delegate = self; locationmanager.desiredaccuracy = kcllocationaccuracybestfornavigation; [locationmanager startupdatinglocation]; [locationmanager startupdatingheading]; 情報 Delegate 2 CSViewController.m @end - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation CLLocationCoordinate2D coord = newlocation.coordinate; NSLog(@"coord=(%.2f,%.2f)", coord.latitude, coord.longitude); // NSLog(@"c_acc=(%.2f,%.2f)", // newlocation.horizontalaccuracy, newlocation.verticalaccuracy); - (void)locationmanager:(cllocationmanager *)manager didupdateheading:(clheading *)newheading NSLog(@"dir=%.2f", newheading.magneticheading); NSLog(@"d_acc=%.2f", newheading.headingaccuracy); 情報 Delegate locationmanager:didupdatetolocation:from Location: 情報 newlocation 情報 oldlocation 情報 CLLocationDegrees double CLLocationAccuracy double 情報 Delegate locationmanager:didup dateheading: magneticheading CLLocationDirection double 0.0 90.0 180.0 headingaccuracy 情報 情報処理 Vol.53 No.1 Jan. 2012 11
図 -4 シミュレータ上での位置情報のカスタマイズ 2011-10-26 05:06:15.474 CoreLocation Sample[76725:f803] coord=(37.33,-122.03) 2011-10-26 05:06:15.475 CoreLocation Sample[76725:f803] c_acc=(30.00,-1.00) 2011-10-26 05:06:15.475 CoreLocation Sample[76725:f803] altitude=104.50 2011-10-26 05:06:16.476 CoreLocation Sample[76725:f803] dir=21.64 2011-10-26 05:06:16.476 CoreLocation Sample[76725:f803] d_acc=25.00... ios 情報 ios - 情報 情報 図 -4 Core Location Core Location 情報 GPS 情報 情報 Wi-Fi GPS ipod touch Core Location 情報 情報 図 -5 情報 図 -5 位置情報取得に関するアラートと環境設定 12 情報処理 Vol.53 No.1 Jan. 2012
1 iphone によるセンサプログラミング 図 -6 Core Motion フレームワークの追加 CLLocationManager locationservicesenabled 情報 if ([CLLocationManager locationservicesenabled]) // 情報 Core Location Core Location Apple ios Dev Center http:// developer.apple.com/devcenter/ios/ Sample Code 3 2011 10 12 Teslameter LocateMe GeocoderDemo 情報 LocateMe Core Motion を利用したモーション取得 Core Motion Core Location Delegate CMMotionManager Core Location Core Motion 図 -6 CSViewController.h Core Motion 情報処理 Vol.53 No.1 Jan. 2012 13
#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> #import <CoreMotion/CoreMotion.h> @interface CSViewController : UIViewController<CLLocationManagerDelegate> CLLocationManager *locationmanager; CMMotionManager *motionmanager; @end viewdidload ios gyroavailable gyroupdateinterval 0.5 startgyroupdates gyrodata 0.5 motionproc: - (void)viewdidload // ( Core Location ) CMMotionManager *manager = [[CMMotionManager alloc] init]; if (manager.gyroavailable) manager.gyroupdateinterval = 0.5; [manager startgyroupdates]; [NSTimer scheduledtimerwithtimeinterval:0.5 target:self selector:@selector(motionproc:) userinfo:nil repeats:yes]; CSViewController.m @end motionproc: double X Y Z 3 - (void)motionproc:(nstimer *)timer CMGyroData *data = motionmanager.gyrodata; CMRotationRate rate = data.rotationrate; NSLog(@"rotation=(%.2f, %.2f, %.2f)", rate.x, rate.y, rate.z); 14 情報処理 Vol.53 No.1 Jan. 2012
1 iphone によるセンサプログラミング 2011-10-26 05:40:05.048 CoreLocation Sample[728:707] rotation=(0.00, 0.00, 0.00) 2011-10-26 05:40:05.549 CoreLocation Sample[728:707] rotation=(0.00, 0.00, 0.00) 2011-10-26 05:40:06.049 CoreLocation Sample[728:707] rotation=(0.02, -0.00, -0.02) 2011-10-26 05:40:06.549 CoreLocation Sample[728:707] rotation=(-0.00, 0.04, 0.04) 2011-10-26 05:40:07.048 CoreLocation Sample[728:707] rotation=(-0.30, 2.19, -0.02) 2011-10-26 05:40:07.548 CoreLocation Sample[728:707] rotation=(-0.13, 1.92, 0.05) 2011-10-26 05:40:08.049 CoreLocation Sample[728:707] rotation=(0.02, 2.48, 0.01) 2011-10-26 05:40:08.548 CoreLocation Sample[728:707] rotation=(0.10, 1.17, 0.33) 2011-10-26 05:40:09.048 CoreLocation Sample[728:707] rotation=(-0.01, 0.36, 0.11) accelerometeravailable if (manager.accelerometeravailable) manager.accelerometerupdateinterval = 1.0 / 60; [manager startaccelerometerupdates]; double X Y Z 3 G CMAccelerometerData *data = manager.accelerometerdata; CMAcceleration accel = data.acceleration; double x = accel.x; double y = accel.y; double z = accel.z; 2 if (manager.devicemotionavailable) manager.devicemotionupdateinterval = 1.0 / 60; [manager startdevicemotionupdates]; CMDeviceMotion 情報 attitude attitude 情報 情報 AR Augmented Reality 情報 3 double 情報処理 Vol.53 No.1 Jan. 2012 15
CMDeviceMotion *motion = manager.devicemotion; CMAtttude attitude = motion.attitude; double roll = attitude.roll; double pitch = attitude.pitch; double yaw = attitude.yaw; CMRotationMatrix mat = attitude.rotationmatrix; // 3x3 CMQuaternion quat = attitude.quaternion; // Core Motion Core Motion ios Dev Center http://developer.apple.com/devcenter/ ios/ Sample Code 1 2011 10 12 park AR 図 -7 図 -7 本体の向きに合わせて公園の名前を重ね合わせて表示する park サンプルコード おわりに Core Location Core Motion ios 情報 Map Kit 3D CG OpenGL ES 3D OpenAL ios 5 Core Image 処理 2011 10 14 沼田哲史 numata@dg.osakac.ac.jp 1978 1 2005 情報 情報 情報 ipad/iphone ipad/iphone 70 15 iphone 16 情報処理 Vol.53 No.1 Jan. 2012