ios sensors

Post on 15-Jan-2015

6.099 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Demonstrates the usage of all available iOS sensors with source code. Example use-cases are a compass, air level, navigation, acceleration and audio recording and playback.

TRANSCRIPT

100

iOS SensorsWhere Mobile Begins

100Roadmap

RoadmapWhere We’re Going

96Teaser: What to Expect!

TeaserWhat to Expect!

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Audio Sampler

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Audio Sampler

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Air Level

Audio Sampler

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Air Level

Audio Sampler

Ball Game

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Compass

Air Level

Audio Sampler

Ball Game

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

Speakers Speakers

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

SpeakersMicrophone

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPS

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

Gyroscope

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

GyroscopeMagnetometer

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

GyroscopeMagnetometer

Mobile Sensors

88Sensors in 12 Device Generations

Sensors in Device GenerationsGreat Common Denominators

With iPodWithout iPodAll 12 Generations - 2007+

88Sensors in 12 Device Generations

Sensors in Device GenerationsGreat Common Denominators

0%

25.00%

50.00%

75.00%

100.00%

Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

With iPodWithout iPodAll 12 Generations - 2007+

84Sensors in 7 Device Generations

Sensors in Device GenerationsGreat Common Denominators

0%

25.00%

50.00%

75.00%

100.00%

Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

With iPodWithout iPodLatest 7 Generations - 2010+

84Sensors in 7 Device Generations

Sensors in Device GenerationsGreat Common Denominators

0%

25.00%

50.00%

75.00%

100.00%

Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

With iPodWithout iPodLatest 7 Generations - 2010+

Majority

80Delegate Pattern

Delegate PatternQuick Look

80Delegate Pattern

Delegate PatternQuick Look

Delegate

80Delegate Pattern

Delegate PatternQuick Look

Delegate

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

76Roadmap

RoadmapWhere We’re Going

72Light: Implementing a Camera

LightImplementing a Camera

72Light: Implementing a Camera

LightImplementing a Camera

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

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

// 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];}

72Light: Implementing a Camera

LightImplementing a Camera

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

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

// 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];}

Camera Demo

68Sound: Implementing a Sound Recorder

SoundImplementing a Sound Recorder

68Sound: Implementing a Sound Recorder

SoundImplementing a Sound Recorder

// get audio sessionAVAudioSession *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;    }

64Sound: Implementing a Sound Player

SoundImplementing a Sound Player

64Sound: Implementing a Sound Player

SoundImplementing a Sound Player

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

// 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;  }

64Sound: Implementing a Sound Player

SoundImplementing a Sound Player

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

// 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;  }

Sound Demo

60Location: Implementing a Positioning System

LocationImplementing a Positioning System

60Location: Implementing a Positioning System

LocationImplementing a Positioning System

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

// process position-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

// access positionfloat latitude = newLocation.coordinate.latitude;float longitude = newLocation.coordinate.longitude;

    // one position is enough    [locationManager stopUpdatingLocation];}

60Location: Implementing a Positioning System

LocationImplementing a Positioning System

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

// process position-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

// access positionfloat latitude = newLocation.coordinate.latitude;float longitude = newLocation.coordinate.longitude;

    // one position is enough    [locationManager stopUpdatingLocation];}

Positioning Demo

56Magnetic Field: Implementing a Compass

Magnetic FieldImplementing a Compass

56Magnetic Field: Implementing a Compass

Magnetic FieldImplementing a Compass

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

// receive update of heading-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

// device is pointing ‘heading’ away from northfloat heading = manager.heading.magneticHeading;

    }

56Magnetic Field: Implementing a Compass

Magnetic FieldImplementing a Compass

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

// receive update of heading-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

// device is pointing ‘heading’ away from northfloat heading = manager.heading.magneticHeading;

    }

Compass Demo

52Roadmap

RoadmapWhere We’re Going

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Force

0.0

0.0

-1.0

in g-force

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Force

0.0

0.0

-1.0

in g-forceRotation

0.5

0.0

-0.5

in degrees

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Force

0.0

0.0

-1.0

in g-forceRotation

0.5

0.0

-0.5

in degrees

Accelerometer Demo

44Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

44

// enable accelerometer[[UIAccelerometer sharedAccelerometer] setDelegate:self];

// receive the acceleration values- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

// move ballball.x += acceleration.x * kBallSpeed;ball.y += acceleration.y * kBallSpeed;

// rotate air levellevel.rotation = acceleration.y * 90;

}

Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Rotation Rate

0.0

0.0

0.0

in radians per second

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Rotation Rate

0.0

0.0

0.0

in radians per secondAbsolute Rotation

0.0

0.78

0.0

in radiansby adding all rates to reference frame

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Rotation Rate

0.0

0.0

0.0

in radians per secondAbsolute Rotation

0.0

0.78

0.0

in radiansby adding all rates to reference frame

Gyroscope Demo

36Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

36

// create core motion managermotionManager = [[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 rotationrotationX += motionManager.gyroData.rotationRate.x;rotationY += motionManager.gyroData.rotationRate.y;

// move ballball.x += rotationX + kBallSpeed;ball.y += rotationY + kBallSpeed;

// rotate air levellevel.rotation = rotationY * 180/PI;

}

Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

32CoreMotion: Use this!

CoreMotionUse this!

32CoreMotion: Use this!

CoreMotionUse this!

Raw dataAccelerometer + Gyroscope

+

32CoreMotion: Use this!

CoreMotionUse this!

Raw dataAccelerometer + Gyroscope

+

6 Degrees of Freedom Inertial SystemDead Reckoning

CoreMotion Framework

28CoreMotion: Use this!

CoreMotionUse this!

+

28

// create core motion managermotionManager = [[CMMotionManager alloc] init];motionManager.motionUpdateInterval = 1.0/60.0;[motionManager startMotionUpdates];

// frequently call the update method[self schedule:@selector(update:)];

CoreMotion: Use this!

CoreMotionUse this!

+

20CoreMotion: Use this!

CoreMotionUse this!

+

20

// frequently read the gyro data-(void)update:(ccTime)dt {

// absolute rotationrotationX = motionManager.deviceMotion.attitude.pitch;rotationY = motionManager.deviceMotion.attitude.yaw;rotationZ = motionManager.deviceMotion.attitude.roll;

// absolute gravitygravityX = motionManager.deviceMotion.gravity.x;gravityY = motionManager.deviceMotion.gravity.y;gravityZ = motionManager.deviceMotion.gravity.z;

// user accelerationaccelerationX = motionManager.deviceMotion.userAcceleration.x;accelerationY = motionManager.deviceMotion.userAcceleration.y;accelerationZ = motionManager.deviceMotion.userAcceleration.z;

}

CoreMotion: Use this!

CoreMotionUse this!

+

20

// frequently read the gyro data-(void)update:(ccTime)dt {

// absolute rotationrotationX = motionManager.deviceMotion.attitude.pitch;rotationY = motionManager.deviceMotion.attitude.yaw;rotationZ = motionManager.deviceMotion.attitude.roll;

// absolute gravitygravityX = motionManager.deviceMotion.gravity.x;gravityY = motionManager.deviceMotion.gravity.y;gravityZ = motionManager.deviceMotion.gravity.z;

// user accelerationaccelerationX = motionManager.deviceMotion.userAcceleration.x;accelerationY = motionManager.deviceMotion.userAcceleration.y;accelerationZ = motionManager.deviceMotion.userAcceleration.z;

}

CoreMotion: Use this!

CoreMotionUse this!

+

CoreMotion Demo

16Roadmap

RoadmapWhere We’re Going

10Summary: What we did not cover

SummaryWhat we did not cover

10Summary: What we did not cover

SummaryWhat we did not cover

GPS Accuracy

10Summary: What we did not cover

SummaryWhat we did not cover

Shaking-Motion Events

GPS Accuracy

10Summary: What we did not cover

SummaryWhat we did not cover

Recording Movies

Shaking-Motion Events

GPS Accuracy

10Summary: What we did not cover

SummaryWhat we did not cover

Sensor Availability

Recording Movies

Shaking-Motion Events

GPS Accuracy

10Summary: What we did not cover

SummaryWhat we did not cover

Sensor Availability

Recording Movies

Shaking-Motion Events

GPS Accuracy

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

5Summary: What we learned!

SummaryWhat we learned!

5Summary: What we learned!

SummaryWhat we learned!

Capturing Images

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Capturing

Sound

Images

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Capturing

Geolocating

Sound

Images

Device

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Capturing

Geolocating

6 Degrees of Freedom

Sound

Reading Device Position

Images

Device

+

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Magnetic North

Capturing

Geolocating

6 Degrees of Freedom

Sound

Reading Device Position

Images

Device

Finding

+

0Thank you: You learned a lot!

Thank youYou learned a lot!

0Thank you: You learned a lot!

Thank youYou learned a lot!

Read!

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

Me!Thomas Fankhauser

tommylefunk@googlemail.com

Buy!BeatfreakPianoTabs

QuestionPad

Hire!southdesign.de

top related