Sunteți pe pagina 1din 25

100

iOS Sensors
Where Mobile Begins

Roadmap

100

Roadmap
Where Were Going

Teaser: What to Expect!

96

Teaser

What to Expect!

Compass Ball Game Air Level Navigation System Audio Sampler Digital Camera

Mobile vs. Desktop: Whats the Dierence

92

Mobile vs. Desktop


Whats the Dierence

Speakers Microphone Camera GPS Accelerometer Gyroscope Magnetometer

Speakers Microphone Camera

Mobile Sensors

Sensors in 12 Device Generations

88

Sensors in Device Generations


Great Common Denominators All 12 Generations - 2007+
100.00%

With iPod Without iPod

75.00%

50.00%

25.00%

0%
Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

Sensors in 7 Device Generations

84

Sensors in Device Generations


Great Common Denominators Latest 7 Generations - 2010+
100.00%

With iPod Without iPod

75.00%

50.00%

Majority

25.00%

0%
Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

Delegate Pattern

80

Delegate Pattern
Quick Look

Delegate

didArriveAtBar: didDrinkBeerNumber: didUpdateAlcoholLevel: wantsMeToComeHome: didCallCab: didEnterCab: didExitCab:

Roadmap

76

Roadmap
Where Were Going

Light: Implementing a Camera

72

Light

Implementing a Camera

// setup image picker controller imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.allowsEditing = NO; imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; imagePickerController.delegate = self;

// display [viewController presentModalViewController:imagePickerController animated:YES];

Camera Demo

// grab photo as soon as it was taken -(void) imagePickerController:(UIImagePickerController *)picker / didFinishPickingMediaWithInfo:(NSDictionary *)info{ // captured image UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; // dismiss image picker [viewController dismissModalViewControllerAnimated:YES]; }

Sound: Implementing a Sound Recorder

68

Sound

Implementing a Sound Recorder

// get audio session AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; [audioSession setActive:YES error:nil]; // some settings NSMutableDictionary *settings = [[NSMutableDictionary alloc] init]; [settings setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey]; [settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; [settings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; tmpRecording = [NSURL fileURLWithPath:[NSTemporaryDirectory() / stringByAppendingPathComponent:@"recording.caf"]]; // start recording audioRecorder = [[AVAudioRecorder alloc] initWithURL:tmpRecording settings:settings error:nil]; [audioRecorder setDelegate:self]; [audioRecorder prepareToRecord]; [audioRecorder recordForDuration:2.0]; // do when recording is finished - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ isRecording = NO; }

Sound: Implementing a Sound Player

64

Sound

Implementing a Sound Player

// get audio session AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; [audioSession setActive:YES error:nil]; tmpRecording = [NSURL fileURLWithPath:[NSTemporaryDirectory() / stringByAppendingPathComponent:@"recording.caf"]];

Sound Demo

// start playing AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:tmpRecording error:nil]; [player setDelegate:self]; [player prepareToPlay]; [player play]; // do when recording is finished - (void)audioRecorderDidFinishPlaying:(AVAudioRecorder *)recorder successfully: (BOOL)flag{ isPlaying = NO; }

Location: Implementing a Positioning System

60

Location

Implementing a Positioning System

// create location manager locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // start location update [locationManager startUpdatingLocation];

Positioning Demo

// process position -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ // access position float latitude = newLocation.coordinate.latitude; float longitude = newLocation.coordinate.longitude; // one position is enough [locationManager stopUpdatingLocation]; }

Magnetic Field: Implementing a Compass

56

Magnetic Field
Implementing a Compass

// setup location manager locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; [locationManager startUpdatingHeading];

Compass Demo

// receive update of heading -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{ // device is pointing heading away from north float heading = manager.heading.magneticHeading; }

Roadmap

52

Roadmap
Where Were Going

Accelerometer: May the Force be with you

48

Accelerometer
May the Force be with you

Force
in g-force

Rotation
in degrees

Accelerometer Demo
-1.0 0.0 0.0 -0.5

0.0 0.5

Accelerometer: May the Force be with you

44

Accelerometer
May the Force be with you // enable accelerometer [[UIAccelerometer sharedAccelerometer] setDelegate:self]; // receive the acceleration values - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { // move ball ball.x += acceleration.x * kBallSpeed; ball.y += acceleration.y * kBallSpeed; // rotate air level level.rotation = acceleration.y * 90; }

Gyroscope: Im spinnin around

40

Gyroscope
Im spinnin around

Rotation Rate
in radians per second

Absolute Rotation
in radians by adding all rates to reference frame

Gyroscope Demo
0.0 0.0 0.78 0.0

0.0 0.0

Gyroscope: Im spinnin around

36

Gyroscope
Im spinnin around
// create core motion manager motionManager = [[CMMotionManager alloc] init]; motionManager.gyroUpdateInterval = 1.0/60.0; [motionManager startGyroUpdates]; // frequently call the update method [self schedule:@selector(update:)]; // frequently read the gyro data -(void)update:(ccTime)dt { // absolute rotation rotationX += motionManager.gyroData.rotationRate.x; rotationY += motionManager.gyroData.rotationRate.y; // move ball ball.x += rotationX + kBallSpeed; ball.y += rotationY + kBallSpeed; // rotate air level level.rotation = rotationY * 180/PI; }

CoreMotion: Use this!

32

CoreMotion
Use this!

+
Raw data
Accelerometer + Gyroscope

CoreMotion Framework 6 Degrees of Freedom Inertial System


Dead Reckoning

CoreMotion: Use this!

28

CoreMotion
Use this!

// create core motion manager motionManager = [[CMMotionManager alloc] init]; motionManager.motionUpdateInterval = 1.0/60.0; [motionManager startMotionUpdates]; // frequently call the update method [self schedule:@selector(update:)];

CoreMotion: Use this!

20

CoreMotion
Use this!
// frequently read the gyro data -(void)update:(ccTime)dt { // absolute rotationX = rotationY = rotationZ = rotation motionManager.deviceMotion.attitude.pitch; motionManager.deviceMotion.attitude.yaw; motionManager.deviceMotion.attitude.roll;

CoreMotion Demo

// absolute gravity gravityX = motionManager.deviceMotion.gravity.x; gravityY = motionManager.deviceMotion.gravity.y; gravityZ = motionManager.deviceMotion.gravity.z; // user acceleration accelerationX = motionManager.deviceMotion.userAcceleration.x; accelerationY = motionManager.deviceMotion.userAcceleration.y; accelerationZ = motionManager.deviceMotion.userAcceleration.z; }

Roadmap

16

Roadmap
Where Were Going

Summary: What we did not cover

10

Summary

What we did not cover

Sensor Availability Recording Movies Shaking-Motion Events GPS Accuracy


http://developer.apple.com/library/ios

Summary: What we learned!

Summary
What we learned!

Finding

Magnetic North

Reading Device Position Geolocating Recording & Playing Capturing

6 Degrees of Freedom Device Sound Images

Thank you: You learned a lot!

Download!
https://github.com/southdesign/SuperBall

Read!

Me!
Thomas Fankhauser
tommylefunk@googlemail.com

Thank you
You learned a lot!

Hire!
southdesign.de

Buy!
Beatfreak PianoTabs QuestionPad

S-ar putea să vă placă și