166
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. System Frameworks #WWDC16 Session 713 Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer

Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

  • Upload
    others

  • View
    7

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

System Frameworks #WWDC16

Session 713Health and Fitness with Core Motion

Bharath Rao EngineerPaul Thompson Engineer

Page 2: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Agenda

Page 3: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Agenda

Historical Accelerometer

Page 4: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Agenda

Historical AccelerometerPedometer Events

Page 5: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Agenda

Historical AccelerometerPedometer EventsDevice Motion on Apple Watch

Page 6: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Historical Accelerometer

Page 7: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMSensorRecorderHistorical Accelerometer

Page 8: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMSensorRecorderHistorical Accelerometer

Access to raw sensor samples over long durations

Page 9: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMSensorRecorderHistorical Accelerometer

Access to raw sensor samples over long durationsSamples buffered while app is suspended

Page 10: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMSensorRecorderHistorical Accelerometer

Page 11: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMSensorRecorderHistorical Accelerometer

watchOS 2 watchOS 3

Query Duration 12 hours 36 hours

Page 12: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMSensorRecorderHistorical Accelerometer

watchOS 2 watchOS 3

Query Duration 12 hours 36 hours

Sample Delay ~180 seconds ~3 seconds

Page 13: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMSensorRecorderHistorical Accelerometer

watchOS 2 watchOS 3

Query Duration 12 hours 36 hours

Sample Delay ~180 seconds ~3 seconds

Use Cases Sleep Tracking Tremor Diagnosis

Page 14: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Pedometer

Page 15: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Data

Floor CountingDistanceSteps

Page 16: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Data

Floor CountingDistanceSteps Pace Cadence

Page 17: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events NEW

Page 18: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Urban run

NEW

Page 19: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Urban run• Manual Pause and Resume

NEW

Page 20: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Urban run• Manual Pause and Resume• Inaccurate distance and pace

NEW

Page 21: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Urban run• Manual Pause and Resume• Inaccurate distance and pace

Why not use GPS or steps to auto-pause and resume?

NEW

Page 22: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Urban run• Manual Pause and Resume• Inaccurate distance and pace

Why not use GPS or steps to auto-pause and resume?• Step counter has built-in delay to avoid

false positives

NEW

Page 23: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Solution• Predictive algorithm to recover latency

NEW

Page 24: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Solution• Predictive algorithm to recover latency• Supports both running and walking pace

NEW

Run Stop Run

WalkRun Stop RunWalk

Running Segment #1 Running Segment #2

Page 25: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerPedometer Events

Solution• Predictive algorithm to recover latency• Supports both running and walking pace• Available in iOS 10 and watchOS 3

NEW

Run Stop Run

WalkRun Stop RunWalk

Running Segment #1 Running Segment #2

Page 26: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Pedometer Events

public enum CMPedometerEventType : Int {

case pause

case resume

}

public class CMPedometerEvent : NSObject, NSSecureCoding, NSCopying {

public var date: Date { get }

public var type: CMPedometerEventType { get }

}

public class CMPedometer : NSObject {

public func startEventUpdates(handler: CoreMotion.CMPedometerEventHandler)

public func stopEventUpdates()

}

Page 27: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Pedometer Events

public enum CMPedometerEventType : Int {

case pause

case resume

}

public class CMPedometerEvent : NSObject, NSSecureCoding, NSCopying {

public var date: Date { get }

public var type: CMPedometerEventType { get }

}

public class CMPedometer : NSObject {

public func startEventUpdates(handler: CoreMotion.CMPedometerEventHandler)

public func stopEventUpdates()

}

Page 28: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Pedometer Events

public enum CMPedometerEventType : Int {

case pause

case resume

}

public class CMPedometerEvent : NSObject, NSSecureCoding, NSCopying {

public var date: Date { get }

public var type: CMPedometerEventType { get }

}

public class CMPedometer : NSObject {

public func startEventUpdates(handler: CoreMotion.CMPedometerEventHandler)

public func stopEventUpdates()

}

Page 29: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Pedometer Events

public enum CMPedometerEventType : Int {

case pause

case resume

}

public class CMPedometerEvent : NSObject, NSSecureCoding, NSCopying {

public var date: Date { get }

public var type: CMPedometerEventType { get }

}

public class CMPedometer : NSObject {

public func startEventUpdates(handler: CoreMotion.CMPedometerEventHandler)

public func stopEventUpdates()

}

Page 30: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerTrail Running with iPhone

Page 31: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerTrail Running with iPhone

Contextual interactions

Page 32: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMPedometerTrail Running with iPhone

Contextual interactionsElevation changes

Page 33: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

class MyTrailRunningApp {

let appQueue = OperationQueue()

let pedometerEventManager = CMPedometer() // For Pedometer Events

let elevationManager = CMAltimeter() // For Relative Altitude Updates

var relativeAltitudeNow = 0.0

var relativeAltitudeAtResume = 0.0

// ...

Page 34: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

class MyTrailRunningApp {

let appQueue = OperationQueue()

let pedometerEventManager = CMPedometer() // For Pedometer Events

let elevationManager = CMAltimeter() // For Relative Altitude Updates

var relativeAltitudeNow = 0.0

var relativeAltitudeAtResume = 0.0

// ...

Page 35: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

func startMyTrailRun {

// Developer Tip #1

// Core Location Best Practices

if CMAltimeter.isRelativeAltitudeAvailable() {

elevationManager.startRelativeAltitudeUpdates(to: appQueue, withHandler: {

(altitudeData: CMAltitudeData?, error: NSError?) in

// Insert error handling here

self.relativeAltitudeNow = try altitudeData?.relativeAltitude.doubleValue

})

}

Page 36: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

func startMyTrailRun {

// Developer Tip #1

// Core Location Best Practices

if CMAltimeter.isRelativeAltitudeAvailable() {

elevationManager.startRelativeAltitudeUpdates(to: appQueue, withHandler: {

(altitudeData: CMAltitudeData?, error: NSError?) in

// Insert error handling here

self.relativeAltitudeNow = try altitudeData?.relativeAltitude.doubleValue

})

}

Page 37: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

func startMyTrailRun {

// Developer Tip #1

// Core Location Best Practices

if CMAltimeter.isRelativeAltitudeAvailable() {

elevationManager.startRelativeAltitudeUpdates(to: appQueue, withHandler: {

(altitudeData: CMAltitudeData?, error: NSError?) in

// Insert error handling here

self.relativeAltitudeNow = try altitudeData?.relativeAltitude.doubleValue

})

}

Page 38: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

func startMyTrailRun {

// Developer Tip #1

// Core Location Best Practices

if CMAltimeter.isRelativeAltitudeAvailable() {

elevationManager.startRelativeAltitudeUpdates(to: appQueue, withHandler: {

(altitudeData: CMAltitudeData?, error: NSError?) in

// Insert error handling here

self.relativeAltitudeNow = try altitudeData?.relativeAltitude.doubleValue

})

}

Page 39: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

func startMyTrailRun {

// Developer Tip #1

// Core Location Best Practices

if CMAltimeter.isRelativeAltitudeAvailable() {

elevationManager.startRelativeAltitudeUpdates(to: appQueue, withHandler: {

(altitudeData: CMAltitudeData?, error: NSError?) in

// Insert error handling here

self.relativeAltitudeNow = try altitudeData?.relativeAltitude.doubleValue

})

}

Page 40: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

if CMPedometer.isPedometerEventTrackingAvailable() {

pedometerEventManager.startEventUpdates { (event: CMPedometerEvent?, error: NSError?) in

// Insert error handling here

// Developer Tip #2

self.appQueue.addOperation({

if event?.type == CMPedometerEventType.resume {

self.relativeAltitudeAtResume = self.relativeAltitudeNow

} else if event?.type == CMPedometerEventType.pause {

if self.relativeAltitudeNow - self.relativeAltitudeAtResume > 0.0 {

// Take that, hill

} else if self.relativeAltitudeNow - self.relativeAltitudeAtResume < 0.0 {

// Whoa, watch those knees

}

}

})

}

}

Page 41: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

if CMPedometer.isPedometerEventTrackingAvailable() {

pedometerEventManager.startEventUpdates { (event: CMPedometerEvent?, error: NSError?) in

// Insert error handling here

// Developer Tip #2

self.appQueue.addOperation({

if event?.type == CMPedometerEventType.resume {

self.relativeAltitudeAtResume = self.relativeAltitudeNow

} else if event?.type == CMPedometerEventType.pause {

if self.relativeAltitudeNow - self.relativeAltitudeAtResume > 0.0 {

// Take that, hill

} else if self.relativeAltitudeNow - self.relativeAltitudeAtResume < 0.0 {

// Whoa, watch those knees

}

}

})

}

}

Page 42: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

if CMPedometer.isPedometerEventTrackingAvailable() {

pedometerEventManager.startEventUpdates { (event: CMPedometerEvent?, error: NSError?) in

// Insert error handling here

// Developer Tip #2

self.appQueue.addOperation({

if event?.type == CMPedometerEventType.resume {

self.relativeAltitudeAtResume = self.relativeAltitudeNow

} else if event?.type == CMPedometerEventType.pause {

if self.relativeAltitudeNow - self.relativeAltitudeAtResume > 0.0 {

// Take that, hill

} else if self.relativeAltitudeNow - self.relativeAltitudeAtResume < 0.0 {

// Whoa, watch those knees

}

}

})

}

}

Page 43: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

if CMPedometer.isPedometerEventTrackingAvailable() {

pedometerEventManager.startEventUpdates { (event: CMPedometerEvent?, error: NSError?) in

// Insert error handling here

// Developer Tip #2

self.appQueue.addOperation({

if event?.type == CMPedometerEventType.resume {

self.relativeAltitudeAtResume = self.relativeAltitudeNow

} else if event?.type == CMPedometerEventType.pause {

if self.relativeAltitudeNow - self.relativeAltitudeAtResume > 0.0 {

// Take that, hill

} else if self.relativeAltitudeNow - self.relativeAltitudeAtResume < 0.0 {

// Whoa, watch those knees

}

}

})

}

}

Page 44: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

if CMPedometer.isPedometerEventTrackingAvailable() {

pedometerEventManager.startEventUpdates { (event: CMPedometerEvent?, error: NSError?) in

// Insert error handling here

// Developer Tip #2

self.appQueue.addOperation({

if event?.type == CMPedometerEventType.resume {

self.relativeAltitudeAtResume = self.relativeAltitudeNow

} else if event?.type == CMPedometerEventType.pause {

if self.relativeAltitudeNow - self.relativeAltitudeAtResume > 0.0 {

// Take that, hill

} else if self.relativeAltitudeNow - self.relativeAltitudeAtResume < 0.0 {

// Whoa, watch those knees

}

}

})

}

}

Page 45: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

if CMPedometer.isPedometerEventTrackingAvailable() {

pedometerEventManager.startEventUpdates { (event: CMPedometerEvent?, error: NSError?) in

// Insert error handling here

// Developer Tip #2

self.appQueue.addOperation({

if event?.type == CMPedometerEventType.resume {

self.relativeAltitudeAtResume = self.relativeAltitudeNow

} else if event?.type == CMPedometerEventType.pause {

if self.relativeAltitudeNow - self.relativeAltitudeAtResume > 0.0 {

// Take that, hill

} else if self.relativeAltitudeNow - self.relativeAltitudeAtResume < 0.0 {

// Whoa, watch those knees

}

}

})

}

}

Page 46: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

func stopMyTrailRun {

pedometerEventManager.stopEventUpdates()

elevationManager.stopRelativeAltitudeUpdates()

}

Page 47: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Trail Running with iPhone

func stopMyTrailRun {

pedometerEventManager.stopEventUpdates()

elevationManager.stopRelativeAltitudeUpdates()

}

Page 48: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

AvailabilityPedometer

iPhone 6/6+ iPhone 6s/6s+ iPhone SE Apple Watch

Steps

Distance

Floor Counting

Pace

Cadence

Pedometer Events

Page 49: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Device Motion on Apple Watch

Page 50: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion on Apple Watch NEW

Page 51: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion on Apple Watch

How to observe user interaction with environment?

NEW

Page 52: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion on Apple Watch

How to observe user interaction with environment?Device motion at wrist using sensor fusion

NEW

Page 53: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion on Apple Watch

How to observe user interaction with environment?Device motion at wrist using sensor fusion• Attitude• Gravity• Rotation rate• User acceleration

NEW

Page 54: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Related Sessions

Understanding Core Motion WWDC 2012

What's new in Core Motion WWDC 2011

CMDeviceMotion

Page 55: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionAttitude NEW

Page 56: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionAttitude

Device orientation represented as• Quaternion• Rotation matrix• Euler angle (roll, pitch, yaw)

NEW

Page 57: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionAttitude

Device orientation represented as• Quaternion• Rotation matrix• Euler angle (roll, pitch, yaw)

Relative to reference frame

NEW

Page 58: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionAttitude

Device orientation represented as• Quaternion• Rotation matrix• Euler angle (roll, pitch, yaw)

Relative to reference frameOrientation from a point not fixed to device

NEW

Page 59: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionAttitude

Device orientation represented as• Quaternion• Rotation matrix• Euler angle (roll, pitch, yaw)

Relative to reference frameOrientation from a point not fixed to deviceReference frame set at start of updates

NEW

Page 60: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionGravity NEW

g

Page 61: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionGravity

Unit vector in device frame

NEW

g

Page 62: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionGravity

Unit vector in device frameTip and Tilt of the device

NEW

g

Page 63: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionGravity

Unit vector in device frameTip and Tilt of the deviceSet using accelerometer while static

NEW

g

Page 64: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionGravity

Unit vector in device frameTip and Tilt of the deviceSet using accelerometer while staticTracked using gyroscope while moving

NEW

g

Page 65: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionRotation Rate NEW

g

r

Page 66: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionRotation Rate

Change in angular motion in device frame

NEW

g

r

Page 67: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionRotation Rate

Change in angular motion in device frameWrist rotation

NEW

g

r

Page 68: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionRotation Rate

Change in angular motion in device frameWrist rotationRotation around body

NEW

g

r

Page 69: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionUser Acceleration NEW

g

a

r

Page 70: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionUser Acceleration

Change in motion in device frame

NEW

g

a

r

Page 71: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionUser Acceleration

Change in motion in device frameCompensated for gravity

NEW

g

a

r

Page 72: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion NEW

Not to scale

Page 73: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion NEW

Not to scale

Property Example Measurement Example Use Case

Attitude Rep counting Weight training

Page 74: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion NEW

Not to scale

Property Example Measurement Example Use Case

Attitude Rep counting Weight training

Gravity Tracking poses Yoga

Page 75: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion NEW

Not to scale

Property Example Measurement Example Use Case

Attitude Rep counting Weight training

Gravity Tracking poses Yoga

Rotation Rate Speed of circular motion Bat speed

Page 76: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

CMDeviceMotionDevice Motion NEW

Not to scale

Property Example Measurement Example Use Case

Attitude Rep counting Weight training

Gravity Tracking poses Yoga

Rotation Rate Speed of circular motion Bat speed

User Acceleration Change in linear motion Punch / Recoil

Page 77: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Device Motion on Apple Watch

public class CMDeviceMotion : CMLogItem {

public var attitude: CMAttitude { get }

public var gravity: CMAcceleration { get }

public var rotationRate: CMRotationRate { get } // Units are in rad/sec

public var userAcceleration: CMAcceleration { get } // Units are in G’s

}

public class CMMotionManager : NSObject {

public var deviceMotionUpdateInterval: TimeInterval

public var deviceMotion: CMDeviceMotion? { get }

public func startDeviceMotionUpdates()

public func startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler:

CoreMotion.CMDeviceMotionHandler)

public func stopDeviceMotionUpdates()

}

Page 78: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Device Motion on Apple Watch

public class CMDeviceMotion : CMLogItem {

public var attitude: CMAttitude { get }

public var gravity: CMAcceleration { get }

public var rotationRate: CMRotationRate { get } // Units are in rad/sec

public var userAcceleration: CMAcceleration { get } // Units are in G’s

}

public class CMMotionManager : NSObject {

public var deviceMotionUpdateInterval: TimeInterval

public var deviceMotion: CMDeviceMotion? { get }

public func startDeviceMotionUpdates()

public func startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler:

CoreMotion.CMDeviceMotionHandler)

public func stopDeviceMotionUpdates()

}

Page 79: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Device Motion on Apple Watch

public class CMDeviceMotion : CMLogItem {

public var attitude: CMAttitude { get }

public var gravity: CMAcceleration { get }

public var rotationRate: CMRotationRate { get } // Units are in rad/sec

public var userAcceleration: CMAcceleration { get } // Units are in G’s

}

public class CMMotionManager : NSObject {

public var deviceMotionUpdateInterval: TimeInterval

public var deviceMotion: CMDeviceMotion? { get }

public func startDeviceMotionUpdates()

public func startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler:

CoreMotion.CMDeviceMotionHandler)

public func stopDeviceMotionUpdates()

}

Page 80: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Device Motion on Apple Watch

public class CMDeviceMotion : CMLogItem {

public var attitude: CMAttitude { get }

public var gravity: CMAcceleration { get }

public var rotationRate: CMRotationRate { get } // Units are in rad/sec

public var userAcceleration: CMAcceleration { get } // Units are in G’s

}

public class CMMotionManager : NSObject {

public var deviceMotionUpdateInterval: TimeInterval

public var deviceMotion: CMDeviceMotion? { get }

public func startDeviceMotionUpdates()

public func startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler:

CoreMotion.CMDeviceMotionHandler)

public func stopDeviceMotionUpdates()

}

Page 81: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Device Motion on Apple Watch

public class CMDeviceMotion : CMLogItem {

public var attitude: CMAttitude { get }

public var gravity: CMAcceleration { get }

public var rotationRate: CMRotationRate { get } // Units are in rad/sec

public var userAcceleration: CMAcceleration { get } // Units are in G’s

}

public class CMMotionManager : NSObject {

public var deviceMotionUpdateInterval: TimeInterval

public var deviceMotion: CMDeviceMotion? { get }

public func startDeviceMotionUpdates()

public func startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler:

CoreMotion.CMDeviceMotionHandler)

public func stopDeviceMotionUpdates()

}

Page 82: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// Device Motion on Apple Watch

public class CMDeviceMotion : CMLogItem {

public var attitude: CMAttitude { get }

public var gravity: CMAcceleration { get }

public var rotationRate: CMRotationRate { get } // Units are in rad/sec

public var userAcceleration: CMAcceleration { get } // Units are in G’s

}

public class CMMotionManager : NSObject {

public var deviceMotionUpdateInterval: TimeInterval

public var deviceMotion: CMDeviceMotion? { get }

public func startDeviceMotionUpdates()

public func startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler:

CoreMotion.CMDeviceMotionHandler)

public func stopDeviceMotionUpdates()

}

Page 83: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Code walkthroughSwing Watch

Page 84: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Page 85: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Start a Workout Session

Page 86: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Start a Workout SessionRegister for Device Motion

Page 87: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Start a Workout SessionRegister for Device MotionConsume data in detection algorithm

Page 88: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Start a Workout SessionRegister for Device MotionConsume data in detection algorithm

CMMotionManager

Page 89: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Start a Workout SessionRegister for Device MotionConsume data in detection algorithm

CMMotionManager HKHealthStore

Page 90: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Start a Workout SessionRegister for Device MotionConsume data in detection algorithm

CMMotionManager HKHealthStore

Page 91: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Configuration

Page 92: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Configuration

Enable Background Modes: Workout processing

// WatchKit Extension/Info.plist

<key>WKBackgroundModes</key>

<array>

<string>workout-processing</string>

</array>

Page 93: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Configuration

Enable Background Modes: Workout processingOnly during an active workout session

// WatchKit Extension/Info.plist

<key>WKBackgroundModes</key>

<array>

<string>workout-processing</string>

</array>

Page 94: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Resource Limits

Average CPU Usage must be limitedViolating this will suspend your app

Page 95: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Related Session

Getting the Most Out of HealthKit Nob Hill Wednesday 9:00AM

Page 96: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

0

0

Page 97: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

3

4

Page 98: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Page 99: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

User interface

Page 100: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

User interfaceWorkout Manager

Page 101: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

User interfaceWorkout ManagerMotion Manager

Page 102: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Swing Algorithm

Page 103: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Swing Algorithm

Model swing as a rotation about the user ω⃗

Page 104: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Swing Algorithm

Model swing as a rotation about the userExtract component parallel with ground

ω⃗

Page 105: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Swing Algorithm

Model swing as a rotation about the userExtract component parallel with groundGate detections on sufficient angle and speed

ω⃗

Page 106: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Code!

Page 107: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 108: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 109: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 110: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 111: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 112: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 113: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 114: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

User Interface

Page 115: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Workout Manager

Page 116: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Workout Manager

Create Workout Sessions

Page 117: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Workout Manager

Create Workout SessionsDirect Motion Manager

Page 118: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class WorkoutManager {

// ...

func startWorkout() {

if (session != nil) {

return

}

let workoutConfiguration = HKWorkoutConfiguration()

workoutConfiguration.activityType = .tennis

workoutConfiguration.locationType = .outdoor

do {

self.session = try HKWorkoutSession(configuration: workoutConfiguration)

} catch {

fatalError("Unable to create the workout session!")

}

healthStore.start(session!)

motionManager.startUpdates()

}

// ...

Page 119: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class WorkoutManager {

// ...

func startWorkout() {

if (session != nil) {

return

}

let workoutConfiguration = HKWorkoutConfiguration()

workoutConfiguration.activityType = .tennis

workoutConfiguration.locationType = .outdoor

do {

self.session = try HKWorkoutSession(configuration: workoutConfiguration)

} catch {

fatalError("Unable to create the workout session!")

}

healthStore.start(session!)

motionManager.startUpdates()

}

// ...

Page 120: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class WorkoutManager {

// ...

func startWorkout() {

if (session != nil) {

return

}

let workoutConfiguration = HKWorkoutConfiguration()

workoutConfiguration.activityType = .tennis

workoutConfiguration.locationType = .outdoor

do {

self.session = try HKWorkoutSession(configuration: workoutConfiguration)

} catch {

fatalError("Unable to create the workout session!")

}

healthStore.start(session!)

motionManager.startUpdates()

}

// ...

Page 121: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class WorkoutManager {

// ...

func stopWorkout() {

if (session == nil) {

return

}

motionManager.stopUpdates()

healthStore.end(session!)

// Store session data with HealthKit

session = nil

}

}

Page 122: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class WorkoutManager {

// ...

func stopWorkout() {

if (session == nil) {

return

}

motionManager.stopUpdates()

healthStore.end(session!)

// Store session data with HealthKit

session = nil

}

}

Page 123: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Motion Manager

Page 124: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Motion Manager

Interact with Core Motion

Page 125: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Motion Manager

Interact with Core MotionImplement detection algorithm

Page 126: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

import CoreMotion

class MotionManager {

let motionManager = CMMotionManager()

let queue = OperationQueue()

let wristLocationIsLeft = WKInterfaceDevice.current().wristLocation == .left

var forehandCount = 0

var backhandCount = 0

var recentDetection = false

let sampleInterval = 1.0 / 50

let rateAlongGravityBuffer = RunningBuffer(size: 50)

let yawThreshold = 1.95 // Radians

let rateThreshold = 5.5 // Radians / sec

let resetThreshold = 5.5 * 0.05 // 5% of rate threshold

weak var delegate: MotionManagerDelegate?

// ...

Page 127: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

import CoreMotion

class MotionManager {

let motionManager = CMMotionManager()

let queue = OperationQueue()

let wristLocationIsLeft = WKInterfaceDevice.current().wristLocation == .left

var forehandCount = 0

var backhandCount = 0

var recentDetection = false

let sampleInterval = 1.0 / 50

let rateAlongGravityBuffer = RunningBuffer(size: 50)

let yawThreshold = 1.95 // Radians

let rateThreshold = 5.5 // Radians / sec

let resetThreshold = 5.5 * 0.05 // 5% of rate threshold

weak var delegate: MotionManagerDelegate?

// ...

Page 128: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

import CoreMotion

class MotionManager {

let motionManager = CMMotionManager()

let queue = OperationQueue()

let wristLocationIsLeft = WKInterfaceDevice.current().wristLocation == .left

var forehandCount = 0

var backhandCount = 0

var recentDetection = false

let sampleInterval = 1.0 / 50

let rateAlongGravityBuffer = RunningBuffer(size: 50)

let yawThreshold = 1.95 // Radians

let rateThreshold = 5.5 // Radians / sec

let resetThreshold = 5.5 * 0.05 // 5% of rate threshold

weak var delegate: MotionManagerDelegate?

// ...

Page 129: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

import CoreMotion

class MotionManager {

let motionManager = CMMotionManager()

let queue = OperationQueue()

let wristLocationIsLeft = WKInterfaceDevice.current().wristLocation == .left

var forehandCount = 0

var backhandCount = 0

var recentDetection = false

let sampleInterval = 1.0 / 50

let rateAlongGravityBuffer = RunningBuffer(size: 50)

let yawThreshold = 1.95 // Radians

let rateThreshold = 5.5 // Radians / sec

let resetThreshold = 5.5 * 0.05 // 5% of rate threshold

weak var delegate: MotionManagerDelegate?

// ...

Page 130: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

import CoreMotion

class MotionManager {

let motionManager = CMMotionManager()

let queue = OperationQueue()

let wristLocationIsLeft = WKInterfaceDevice.current().wristLocation == .left

var forehandCount = 0

var backhandCount = 0

var recentDetection = false

let sampleInterval = 1.0 / 50

let rateAlongGravityBuffer = RunningBuffer(size: 50)

let yawThreshold = 1.95 // Radians

let rateThreshold = 5.5 // Radians / sec

let resetThreshold = 5.5 * 0.05 // 5% of rate threshold

weak var delegate: MotionManagerDelegate?

// ...

Page 131: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

import CoreMotion

class MotionManager {

let motionManager = CMMotionManager()

let queue = OperationQueue()

let wristLocationIsLeft = WKInterfaceDevice.current().wristLocation == .left

var forehandCount = 0

var backhandCount = 0

var recentDetection = false

let sampleInterval = 1.0 / 50

let rateAlongGravityBuffer = RunningBuffer(size: 50)

let yawThreshold = 1.95 // Radians

let rateThreshold = 5.5 // Radians / sec

let resetThreshold = 5.5 * 0.05 // 5% of rate threshold

weak var delegate: MotionManagerDelegate?

// ...

Page 132: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

import CoreMotion

class MotionManager {

let motionManager = CMMotionManager()

let queue = OperationQueue()

let wristLocationIsLeft = WKInterfaceDevice.current().wristLocation == .left

var forehandCount = 0

var backhandCount = 0

var recentDetection = false

let sampleInterval = 1.0 / 50

let rateAlongGravityBuffer = RunningBuffer(size: 50)

let yawThreshold = 1.95 // Radians

let rateThreshold = 5.5 // Radians / sec

let resetThreshold = 5.5 * 0.05 // 5% of rate threshold

weak var delegate: MotionManagerDelegate?

// ...

Page 133: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// ...

init() {

queue.maxConcurrentOperationCount = 1

queue.name = "MotionManagerQueue"

}

func resetAllState() {

forehandCount = 0

backhandCount = 0

recentDetection = false

rateAlongGravityBuffer.reset()

delegate?.didUpdateForehandSwingCount(self, forehandCount: 0)

delegate?.didUpdateBackhandSwingCount(self, backhandCount: 0)

}

// ...

Page 134: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// ...

init() {

queue.maxConcurrentOperationCount = 1

queue.name = "MotionManagerQueue"

}

func resetAllState() {

forehandCount = 0

backhandCount = 0

recentDetection = false

rateAlongGravityBuffer.reset()

delegate?.didUpdateForehandSwingCount(self, forehandCount: 0)

delegate?.didUpdateBackhandSwingCount(self, backhandCount: 0)

}

// ...

Page 135: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// ...

init() {

queue.maxConcurrentOperationCount = 1

queue.name = "MotionManagerQueue"

}

func resetAllState() {

forehandCount = 0

backhandCount = 0

recentDetection = false

rateAlongGravityBuffer.reset()

delegate?.didUpdateForehandSwingCount(self, forehandCount: 0)

delegate?.didUpdateBackhandSwingCount(self, backhandCount: 0)

}

// ...

Page 136: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// ...

func incrementForehandCountAndUpdateDelegate() {

if (!recentDetection) {

forehandCount += 1

recentDetection = true

delegate?.didUpdateForehandSwingCount(self, forehandCount: forehandCount)

}

}

func incrementBackhandCountAndUpdateDelegate() {

if (!recentDetection) {

backhandCount += 1

recentDetection = true

delegate?.didUpdateBackhandSwingCount(self, backhandCount: backhandCount)

}

}

// ...

Page 137: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// ...

func incrementForehandCountAndUpdateDelegate() {

if (!recentDetection) {

forehandCount += 1

recentDetection = true

delegate?.didUpdateForehandSwingCount(self, forehandCount: forehandCount)

}

}

func incrementBackhandCountAndUpdateDelegate() {

if (!recentDetection) {

backhandCount += 1

recentDetection = true

delegate?.didUpdateBackhandSwingCount(self, backhandCount: backhandCount)

}

}

// ...

Page 138: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func startUpdates() {

if !motionManager.isDeviceMotionAvailable {

print("Device Motion is not available.")

return

}

resetAllState()

motionManager.deviceMotionUpdateInterval = sampleInterval // 0.02 seconds

motionManager.startDeviceMotionUpdates(to: queue) {

(deviceMotion: CMDeviceMotion?, error:NSError?) in

if error != nil {

// Handle Error

}

if deviceMotion != nil {

self.processDeviceMotion(deviceMotion!)

}

}

}

// ...

Page 139: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func startUpdates() {

if !motionManager.isDeviceMotionAvailable {

print("Device Motion is not available.")

return

}

resetAllState()

motionManager.deviceMotionUpdateInterval = sampleInterval // 0.02 seconds

motionManager.startDeviceMotionUpdates(to: queue) {

(deviceMotion: CMDeviceMotion?, error:NSError?) in

if error != nil {

// Handle Error

}

if deviceMotion != nil {

self.processDeviceMotion(deviceMotion!)

}

}

}

// ...

Page 140: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func startUpdates() {

if !motionManager.isDeviceMotionAvailable {

print("Device Motion is not available.")

return

}

resetAllState()

motionManager.deviceMotionUpdateInterval = sampleInterval // 0.02 seconds

motionManager.startDeviceMotionUpdates(to: queue) {

(deviceMotion: CMDeviceMotion?, error:NSError?) in

if error != nil {

// Handle Error

}

if deviceMotion != nil {

self.processDeviceMotion(deviceMotion!)

}

}

}

// ...

Page 141: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func startUpdates() {

if !motionManager.isDeviceMotionAvailable {

print("Device Motion is not available.")

return

}

resetAllState()

motionManager.deviceMotionUpdateInterval = sampleInterval // 0.02 seconds

motionManager.startDeviceMotionUpdates(to: queue) {

(deviceMotion: CMDeviceMotion?, error:NSError?) in

if error != nil {

// Handle Error

}

if deviceMotion != nil {

self.processDeviceMotion(deviceMotion!)

}

}

}

// ...

Page 142: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

let gravity = deviceMotion.gravity

let rotationRate = deviceMotion.rotationRate

let rateAlongGravity = rotationRate.x * gravity.x // ω⃗ · ĝ

+ rotationRate.y * gravity.y

+ rotationRate.z * gravity.z

rateAlongGravityBuffer.addSample(rateAlongGravity)

if !rateAlongGravityBuffer.isFull() {

return

}

let accumulatedYawRot = rateAlongGravityBuffer.sum() * sampleInterval

let peakRate = accumulatedYawRot > 0 ?

rateAlongGravityBuffer.max() : rateAlongGravityBuffer.min()

// ...

Page 143: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

let gravity = deviceMotion.gravity

let rotationRate = deviceMotion.rotationRate

let rateAlongGravity = rotationRate.x * gravity.x // ω⃗ · ĝ

+ rotationRate.y * gravity.y

+ rotationRate.z * gravity.z

rateAlongGravityBuffer.addSample(rateAlongGravity)

if !rateAlongGravityBuffer.isFull() {

return

}

let accumulatedYawRot = rateAlongGravityBuffer.sum() * sampleInterval

let peakRate = accumulatedYawRot > 0 ?

rateAlongGravityBuffer.max() : rateAlongGravityBuffer.min()

// ...

Page 144: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

let gravity = deviceMotion.gravity

let rotationRate = deviceMotion.rotationRate

let rateAlongGravity = rotationRate.x * gravity.x // ω⃗ · ĝ

+ rotationRate.y * gravity.y

+ rotationRate.z * gravity.z

rateAlongGravityBuffer.addSample(rateAlongGravity)

if !rateAlongGravityBuffer.isFull() {

return

}

let accumulatedYawRot = rateAlongGravityBuffer.sum() * sampleInterval

let peakRate = accumulatedYawRot > 0 ?

rateAlongGravityBuffer.max() : rateAlongGravityBuffer.min()

// ...

Page 145: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

let gravity = deviceMotion.gravity

let rotationRate = deviceMotion.rotationRate

let rateAlongGravity = rotationRate.x * gravity.x // ω⃗ · ĝ

+ rotationRate.y * gravity.y

+ rotationRate.z * gravity.z

rateAlongGravityBuffer.addSample(rateAlongGravity)

if !rateAlongGravityBuffer.isFull() {

return

}

let accumulatedYawRot = rateAlongGravityBuffer.sum() * sampleInterval

let peakRate = accumulatedYawRot > 0 ?

rateAlongGravityBuffer.max() : rateAlongGravityBuffer.min()

// ...

Page 146: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

let gravity = deviceMotion.gravity

let rotationRate = deviceMotion.rotationRate

let rateAlongGravity = rotationRate.x * gravity.x // ω⃗ · ĝ

+ rotationRate.y * gravity.y

+ rotationRate.z * gravity.z

rateAlongGravityBuffer.addSample(rateAlongGravity)

if !rateAlongGravityBuffer.isFull() {

return

}

let accumulatedYawRot = rateAlongGravityBuffer.sum() * sampleInterval

let peakRate = accumulatedYawRot > 0 ?

rateAlongGravityBuffer.max() : rateAlongGravityBuffer.min()

// ...

Page 147: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

// ...

if (accumulatedYawRot < -yawThreshold && peakRate < -rateThreshold) {

// Counter clockwise swing.

if (wristLocationIsLeft) {

incrementBackhandCountAndUpdateDelegate()

} else {

incrementForehandCountAndUpdateDelegate()

}

} else if (accumulatedYawRot > yawThreshold && peakRate > rateThreshold) {

// Clockwise swing.

if (wristLocationIsLeft) {

incrementForehandCountAndUpdateDelegate()

} else {

incrementBackhandCountAndUpdateDelegate()

}

}

// ...

Page 148: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

// ...

if (accumulatedYawRot < -yawThreshold && peakRate < -rateThreshold) {

// Counter clockwise swing.

if (wristLocationIsLeft) {

incrementBackhandCountAndUpdateDelegate()

} else {

incrementForehandCountAndUpdateDelegate()

}

} else if (accumulatedYawRot > yawThreshold && peakRate > rateThreshold) {

// Clockwise swing.

if (wristLocationIsLeft) {

incrementForehandCountAndUpdateDelegate()

} else {

incrementBackhandCountAndUpdateDelegate()

}

}

// ...

Page 149: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

// ...

if (accumulatedYawRot < -yawThreshold && peakRate < -rateThreshold) {

// Counter clockwise swing.

if (wristLocationIsLeft) {

incrementBackhandCountAndUpdateDelegate()

} else {

incrementForehandCountAndUpdateDelegate()

}

} else if (accumulatedYawRot > yawThreshold && peakRate > rateThreshold) {

// Clockwise swing.

if (wristLocationIsLeft) {

incrementForehandCountAndUpdateDelegate()

} else {

incrementBackhandCountAndUpdateDelegate()

}

}

// ...

Page 150: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

// ...

if (recentDetection && abs(rateAlongGravityBuffer.recentMean()) < resetThreshold) {

recentDetection = false

rateAlongGravityBuffer.reset()

}

}

func stopUpdates() {

if motionManager.isDeviceMotionAvailable {

motionManager.stopDeviceMotionUpdates()

}

}

}

Page 151: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

// ...

if (recentDetection && abs(rateAlongGravityBuffer.recentMean()) < resetThreshold) {

recentDetection = false

rateAlongGravityBuffer.reset()

}

}

func stopUpdates() {

if motionManager.isDeviceMotionAvailable {

motionManager.stopDeviceMotionUpdates()

}

}

}

Page 152: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

// class MotionManager {

// func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {

// ...

if (recentDetection && abs(rateAlongGravityBuffer.recentMean()) < resetThreshold) {

recentDetection = false

rateAlongGravityBuffer.reset()

}

}

func stopUpdates() {

if motionManager.isDeviceMotionAvailable {

motionManager.stopDeviceMotionUpdates()

}

}

}

Page 153: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

Page 154: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

User interface

Page 155: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

User interfaceWorkout Manager

Page 156: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Sample App Concept

User interfaceWorkout ManagerMotion Manager

Page 157: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

AvailabilityDevice Motion

iPhone 6/6+ iPhone 6s/6s+ iPhone SE Apple Watch

Gravity

User Acceleration

Rotation Rate

Attitude

Magnetic Field

NEW

Page 158: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Points to ponderDevice Motion NEW

Page 159: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Points to ponderDevice Motion

Wrist placement

NEW

Page 160: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Points to ponderDevice Motion

Wrist placementReference frame

NEW

Page 161: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Points to ponderDevice Motion

Wrist placementReference frameSample rate

NEW

Page 162: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Summary

Improvements to Historical AccelerometerPedometer Events Device Motion on Apple Watch

Page 163: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

More Information

https://developer.apple.com/wwdc16/713

Page 164: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Related Sessions

Getting the Most Out of HealthKit Nob Hill Wednesday 9:00AM

Core Location Best Practices Pacific Heights Thursday 4:00PM

What's New in watchOS 3 Presidio Tuesday 5:00 PM

Page 165: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer

Labs

Core Motion Lab Frameworks Lab D Friday 12:30 PM

Page 166: Health and Fitness with Core Motion - Apple Developer€¦ · Health and Fitness with Core Motion Bharath Rao Engineer Paul Thompson Engineer. Agenda. Agenda Historical Accelerometer