Slide 1

Slide 1 text

CoreMotionΛ ࢖ͬͯؾ͍ͮͨ͜ͱͱ iOS11Ͱͷ৽ػೳ ాத ୡ໵ (@tattn)

Slide 2

Slide 2 text

• Yahoo!৐׵Ҋ಺ • iOSΞϓϦΤϯδχΞ • 2016೥৽ଔ • GitHub: @tattn • Qiita: @tattn • Twitter: @tanakasan2525 ాத ୡ໵ (@tattn)

Slide 3

Slide 3 text

CoreMotionΛ ͕ͬͭΓ৮ͬͯΈ·ͨ͠

Slide 4

Slide 4 text

CoreMotion

Slide 5

Slide 5 text

CoreMotion ୺຤ͷ܏͖΍า਺ͳͲɺσόΠεͷಈ͖΍ͦΕ͔Β෼ ͔Δਓͷಈ͖ͳͲΛऔಘͰ͖ΔϑϨʔϜϫʔΫɻ iOS 4͔Βఏڙ͞Ε͍ͯΔɻ
 (iOS 4 Ҏલ͸ UIAccelerometer ͱ͍͏ΫϥεͰՃ଎౓Λऔಘ͍ͯͨ͠) Sensing Device Motion in iOS 4 https://developer.apple.com/videos/play/wwdc2010/423/

Slide 6

Slide 6 text

CoreMotion iPhone 5sҎ߱ʹ౥ࡌͷ
 ίϓϩηοα (M7Ҏ߱) Ͱେ෯ػೳڧԽ What's New in Core Motion https://developer.apple.com/videos/play/wwdc2015/705/ ←Ճ଎౓ηϯα ←δϟΠϩηϯα ←࣓ؾηϯα ←ߴ౓ηϯα

Slide 7

Slide 7 text

۩ମతʹͰ͖Δ͜ͱ

Slide 8

Slide 8 text

ଌఆͰ͖Δ͜ͱͷ͢΂ͯ Ճ଎౓ηϯα δϟΠϩηϯα ࣓ؾηϯα ߴ౓ηϯα • ୺຤ͷ޲͖ (ճస) • (୺຤ʹର͢Δ)ॏྗͷํ޲ • ୺຤ͷՃ଎౓(ॏྗແࢹ) • ֤ํ޲ͷ࣓ଋີ౓ • า਺ • า͍ͨڑ཭ (ਪఆ) • ฏۉ/ݱࡏͷา͘଎͞ [m/s] • Ұඵؒͷา਺ [า਺/s] • า͍ͯొͬͨ֊਺ (ਪఆ) • (ଌఆ͔ͯ͠Βͷ)૬ରߴ౓ • ؾѹ • ׆ಈঢ়ଶ 
 (ఀࢭ/ెา/૸Δ/ࣗಈं/
 ࣗసं)

Slide 9

Slide 9 text

࢖͍ํ

Slide 10

Slide 10 text

جຊతͳ࢖͍ํ 1. ηϯαʔͷ஋औಘ։࢝ 2. ஋Λఆظతʹऔಘ(ΫϩʔδϟorϓϩύςΟ) ύλʔϯ1 ύλʔϯ2 1. ظؒΛࢦఆ 2. ظؒதʹଌఆͨ͠σʔλΛऔಘ

Slide 11

Slide 11 text

ύλʔϯ1 - ఆظऔಘ CMMotionManager().startDeviceMotionUpdates() CMMotionManager().startAccelerometerUpdates() CMMotionManager().startGyroUpdates() CMMotionManager().startMagnetometerUpdates() CMPedometer().startUpdates() CMPedometer().startEventUpdates() CMAltimeter().startRelativeAltitudeUpdates() CMMotionActivityManager().startActivityUpdates() ෼͔Γ΍͍͢☺औಘ։࢝ϝιου

Slide 12

Slide 12 text

ύλʔϯ1 - ఆظऔಘ manager.deviceMotion?.rotationRate motionActivityManager.startActivityUpdates(to: .init()) { activity in guard let activity = activity else { return } if activity.stationary { print("ࢭ·ͬͯΔΑ") } if activity.cycling { print("νϟϦͰདྷͨ") } } ෼͔Γ΍͍͢☺औಘํ๏

Slide 13

Slide 13 text

ύλʔϯ2 - ظؒͰऔಘ let now = Date() let dayBeforeYesterDay = now.addingTimeInterval(-60 * 60 * 24 * 2) pedometer.queryPedometerData(from: dayBeforeYesterDay, to: now) { data, error in guard let data = data, error == nil else { return } print(data.distance ?? 0) print(data.numberOfSteps) print(data.floorsAscended ?? 0) } ෼͔Γ΍͍͢☺औಘํ๏

Slide 14

Slide 14 text

࢖ͬͯΈͯؾ͍ͮͨ͜ͱ

Slide 15

Slide 15 text

ѹ౗త৘ใྔͷগͳ͞ ϓϩύςΟ໊Ͱݕࡧͯ͠΋ 177݅ (2017/7/25࣌఺)

Slide 16

Slide 16 text

༷ʑͳଌఆํ๏͕͋Δ

Slide 17

Slide 17 text

༷ʑͳଌఆํ๏͕͋Δ xArbitraryZVertical Z͕࣠ਨ௚ɺX͕࣠ฏ໘ͷ೚ҙํ޲ xArbitraryCorrectedZVertical ࣓ؾηϯαͰyawͷਫ਼౓͕޲্ͨ͠xArbitraryZVertical
 CPU࢖༻཰্͕͕Δ xMagneticNorthZVertical Z͕࣠ਨ௚ɺX͕࣓࣠๺
 ୺຤Λಈ͔࣓ͯ͠ؾηϯαͷΩϟϦϒϨʔγϣϯ͕ඞཁ xTrueNorthZVertical Z͕࣠ਨ௚ɺX͕࣠ਅ๺
 ࣓๺ͱਅ๺ͷܭࢉʹ࣓ؾηϯαͱGPSΛ࢖༻

Slide 18

Slide 18 text

༷ʑͳଌఆํ๏͕͋Δ xArbitraryCorrectedZVertical ࣓ؾηϯαͰyawͷਫ਼౓͕޲্ͨ͠xArbitraryZVertical
 CPU࢖༻཰্͕͕Δ xTrueNorthZVertical Z͕࣠ਨ௚ɺX͕࣠ਅ๺
 ࣓๺ͱਅ๺ͷܭࢉʹ࣓ؾηϯαͱGPSΛ࢖༻ ࣮͸࣓ؾηϯαΛ࢖༻
 (໊લ͔Β͸Θ͔Γʹ͍͘) ࣮͸GPSΛ࢖༻
 (GPSͷڐՄΞϥʔτग़ͳ͍ & 
 Ґஔ৘ใαʔϏεͷON͕ඞਢ)

Slide 19

Slide 19 text

υΩϡϝϯτ͕ͨ·ʹࡶ վߦ https://developer.apple.com/documentation/coremotion/getting_processed_device_motion_data

Slide 20

Slide 20 text

I/F͕ͨ·ʹ΍Δؾͳ͍ @available(iOS 9.0, *) open class CMSensorDataList : NSObject, NSFastEnumeration { } Swift͔Β࢖͍ʹ͍͘ protocol NSFastEnumeration { func countByEnumerating( with state: UnsafeMutablePointer, objects buffer: AutoreleasingUnsafeMutablePointer!, count len: Int) -> Int }

Slide 21

Slide 21 text

I/F͕ͨ·ʹ΍Δؾͳ͍ @available(iOS 9.0, *) open class CMSensorDataList : NSObject, NSFastEnumeration { } extension CMSensorDataList: Sequence { public func makeIterator() -> NSFastEnumerationIterator { return NSFastEnumerationIterator(self) } } Tips: ΍ΔؾΛ࣮૷ ͍ͭ΋ͷ for-in, map ͳͲ

Slide 22

Slide 22 text

iOS 11Ͱͷ৽ػೳ https://developer.apple.com/videos/play/wwdc2017/704/

Slide 23

Slide 23 text

ڐՄঢ়ଶΛࣄલʹऔಘՄೳʹ ࠓ·Ͱ iOS 11͔Β pedometer.queryPedometerData(from: now, to: now) { (data, error) in if let code = error?._code, code == Int(CMErrorMotionActivityNotAuthorized.rawValue) { // ະڐՄ } if CMPedometer.isStepCountingAvailable() { switch CMPedometer.authorizationStatus() { case .notDetermined: break case .restricted: break case .denied: break case .authorized: break } }

Slide 24

Slide 24 text

ίϯύεͷ஋ΛऔಘՄೳʹ ๺ʹରͯ͠ͲΕ͚ͩճస͍ͯ͠Δ͔Λ 0.0° ʙ 360.0 ° ͰऔಘՄೳ Heading

Slide 25

Slide 25 text

Ճ଎౓ͷه࿥͕Մೳʹ (7/7+ on iOS11) CMSensorRecoder • ه࿥࣌ؒΛࢦఆͯ͠ه࿥։࢝ (࠷େ36h) • όοΫάϥ΢ϯυͰ50HzͰܭଌ (ΞϓϦΛऴྃͯ͠΋) • ࠷େ3೔ؒͷه࿥ΛऔಘՄೳ • ࠶౓ه࿥Λ։࢝ͨ͠Γ
 ଞͷΞϓϦ͕ه࿥Λ։࢝͢Δͱ࣌ؒΛԆ௕Մೳ

Slide 26

Slide 26 text

Ճ଎౓ͷه࿥͕Մೳʹ (7/7+ on iOS11) CMSensorRecoder • Apple Watch • Apple Watch Series 1 • Apple Watch Series 2 • iPhone 7/7 Plus (iOS 11) ॳΊͯiPhoneͰ࢖༻Մೳʹ

Slide 27

Slide 27 text

(´ɾωɾʆ) iOS11Ͱͷ৽ػೳΛௐ΂ऴΘͬͨ࣌ͷإ

Slide 28

Slide 28 text

࠷ޙʹ

Slide 29

Slide 29 text

࠷ޙʹ https://github.com/tattn/TryCoreMotion ΄΅શػೳ࢖ͬͨαϯϓϧΞϓϦ࡞Γ·ͨ͠

Slide 30

Slide 30 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠