Slide 1

Slide 1 text

15෼Ͱ෼͔Δ CoreMotionͷશͯ @iOSDC 2017 Reject Conference days1 ాத ୡ໵ (@tattn)

Slide 2

Slide 2 text

·ͣ͸͡Ίʹ ↑λΠτϧͷdiff͸ͪ͜ΒʹͳΓ·͢ 15෼Ͱ શͯ ͸ݫ͔ͬͨ͠Ͱ͢ - શͯ + watchҎ֎ͷ΄΅શͯ

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

CoreMotion

Slide 5

Slide 5 text

CoreMotion ୺຤ͷ܏͖΍า਺ͳͲɺσόΠεͷಈ͖ʹؔ͢Δ
 ৘ใΛऔಘͰ͖ΔϑϨʔϜϫʔΫɻ iOS 4͔ΒఏڙɻwatchOSͰ΋ར༻Մೳɻ
 (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

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

Slide 8

Slide 8 text

࣮૷ͷྲྀΕ

Slide 9

Slide 9 text

Info.plistʹઃఆͷ௥Ճ ※ iOS 10͔ΒϓϥΠόγʔʹؔ͢ΔσʔλʹΞΫηε͢Δࡍ͸ઃఆ͕ඞਢʹ Core Motionͷ࢖༻໨తΛ௥Ճ͠·͢

Slide 10

Slide 10 text

σʔλऔಘΫϥεͷੜ੒ ཉ͍͠σʔλΛऔಘ͢ΔΫϥεΛੜ੒ // Ճ଎౓ɾδϟΠϩɾ࣓ྗ let motionManager = CMMotionManager() // า਺ let pedometer = CMPedometer() // ߴ౓ let altimeter = CMAltimeter() // աڈͷՃ଎౓σʔλ let sensorRecoder = CMSensorRecorder() // Ϣʔβʔͷঢ়ଶ let motionActivityManager = CMMotionActivityManager() ↑͜ΕͰ؅ཧΫϥε͸શͯͰ͢ (ൃද࣌)

Slide 11

Slide 11 text

ػೳ͕ར༻Մೳ͔ΛνΣοΫ Ϋϥε໊.isʙAvailable()ϝιουͰ
 ୺຤͕ػೳΛར༻Ͱ͖Δ͔ΛऔಘͰ͖Δ if CMPedometer.isFloorCountingAvailable() { // ར༻Մೳ } CMAltimeter.isRelativeAltitudeAvailable() CMSensorRecorder.isAccelerometerRecordingAvailable() CMMotionActivityManager.isActivityAvailable() ↑ଞʹ΋ଟ਺͋Γ·͢

Slide 12

Slide 12 text

ػೳͷରԠද T TT %FWJDF.PUJPO ˓ ˓ ˓ ˓ "DDFMFSPNFUFS ˚ SFDPEJOH ˚ SFDPEJOH ˚ SFDPEJOH ˓ (ZSP ˓ ˓ ˓ ˓ .BHOFUPNFUFS ˓ ˓ ˓ ˓ 1FEPNFUFS ˚ qPPSDPVOUJOH QBDF DBEFODF FWFOUUSBDLJOH ˚ FWFOUUSBDLJOH ˓ ˓ "MUJNFUFS ☓ ˓ ˓ ˓ .PUJPO"DUJWJUZ ˓ ˓ ˓ ˓ ※()ͷத͸ඇରԠͳػೳ

Slide 13

Slide 13 text

ηϯαʔσʔλͷऔಘ ར༻Մೳͳͷ͔ΛνΣοΫޙ
 σʔλऔಘϝιουΛݺͼग़͢ // σʔλΛऔಘͰ͖Δ৔߹ͷΈऔಘϝιουΛݺͼग़͢Α͏ʹ͢Δ if CMPedometer.isPedometerEventTrackingAvailable() { pedometer.startEventUpdates { (event, error) in // σʔλऔಘͷͨͼʹΫϩʔδϟ͕ݺ͹ΕΔ } }

Slide 14

Slide 14 text

औಘํ๏ͷछྨ 1. ηϯαʔͷ஋ͷ؂ࢹ։࢝ 2. ஋Λఆظతʹऔಘ(ΫϩʔδϟorϓϩύςΟ) ఆظऔಘ ظؒऔಘ 1. ظؒΛࢦఆ 2. ظؒதʹଌఆͨ͠σʔλΛऔಘ

Slide 15

Slide 15 text

ύλʔϯ1 - ఆظऔಘ CMMotionManager().startDeviceMotionUpdates() CMMotionManager().startAccelerometerUpdates() CMMotionManager().startGyroUpdates() CMMotionManager().startMagnetometerUpdates() CMPedometer().startUpdates(from: date) { (data, error) in } CMPedometer().startEventUpdates { (event, error) in } CMAltimeter() .startRelativeAltitudeUpdates(to: .init()) { data, _ in } CMMotionActivityManager() .startActivityUpdates(to: .init()) { activity in } औಘ։࢝ϝιουΛݺͼग़͢

Slide 16

Slide 16 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 17

Slide 17 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) } (from:to)ͰظؒΛࢦఆ͢Δ

Slide 18

Slide 18 text

CoreMotionͷڐՄঢ়ଶͷνΣοΫ pedometer.queryPedometerData(from: now, to: now) { (data, error) in if let code = error?._code, code == Int(CMErrorMotionActivityNotAuthorized.rawValue) { // ະڐՄ } iOS10·Ͱ͸
 errorͰ൑ఆ͢Δ͔͋͠Γ·ͤΜͰͨ͠ʘ(^o^)ʗ

Slide 19

Slide 19 text

ڐՄঢ়ଶΛࣄલʹऔಘՄೳʹ ࠓ·Ͱ iOS 11 / watchOS 4͔Β 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 20

Slide 20 text

CoreMotionͷڐՄঢ়ଶͷνΣοΫ ͢ͰʹiOS11ະຬΛ੾ΕΔ
 ϫΠϧυͳ
 αʔϏεͷ৔߹͸ࣄલʹνΣοΫ͠·͠ΐ͏

Slide 21

Slide 21 text

࣮૷্ͷ஫ҙ఺

Slide 22

Slide 22 text

Operation Queueʹ஫ҙ ͲͷOperation QueueͰσʔλΛड͚औΔ͔ΛࢦఆͰ͖Δ motionManager.startMagnetometerUpdates(to: .main) { (data, error) in // ϝΠϯεϨου } ↑OperationQueue.main͸σʔλऔಘͷͨͼʹ
 UIεϨουΛϒϩοΩϯά͢Δ͜ͱʹͳΔɻɻ Because the processed events might arrive at a high rate, using the main operation queue is not recommended. https://developer.apple.com/documentation/coremotion/cmmotionmanager/1615968-startmagnetometerupdates

Slide 23

Slide 23 text

Operation Queueʹ஫ҙ όοΫάϥ΢ϯυεϨουͰड͚औΓɺ
 ඞཁʹԠͯ͡ϝΠϯεϨουʹ੾Γସ͑ΔΑ͏ʹ͢Δͱྑ͍ motionManager.startMagnetometerUpdates(to: .init()) { (data, error) in // όοΫάϥ΢ϯυεϨου if ඞཁͳΒ { DispatchQueue.main.async { // UIͷߋ৽ } } }

Slide 24

Slide 24 text

CMMotionManager CMMotionManager͸ಉ࣌ʹෳ਺ͷΠϯελϯεΛ࡞੒ͯ͠͸͍͚ͳ͍ https://developer.apple.com/documentation/coremotion/cmmotionmanager Ճ଎౓ɾδϟΠϩɾ࣓ྗͳΜͰ΋औಘͰ͖ΔਆΫϥε CMMotionManager().startMagnetometerUpdates(to: .init()) { (data, error) in } CMMotionManager().startAccelerometerUpdates(to: .init()) { (data, error) in } ෼͔Γ΍͍͢μϝͳྫ

Slide 25

Slide 25 text

CMMotionManager ෳ਺ͷը໘Ͱར༻͢Δ৔߹͸
 γϯάϧτϯΛ࡞Δͷ͕Φεεϝ https://developer.apple.com/documentation/coremotion/cmmotionmanager final class SharedMotionManager: CMMotionManager { static let shared = SharedMotionManager() // Ϋϥε֎ͰͷΠϯελϯεੜ੒ͷ๷ࢭ private override init() { super.init() } }

Slide 26

Slide 26 text

༷ʑͳଌఆํ๏͕͋Δ

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

࠷ޙʹ

Slide 30

Slide 30 text

ػೳͷରԠද ※()ͷத͸ඇରԠͳػೳ T TT %FWJDF.PUJPO ˓ ˓ ˓ ˓ "DDFMFSPNFUFS ˚ SFDPEJOH ˚ SFDPEJOH ˚ SFDPEJOH ˓ (ZSP ˓ ˓ ˓ ˓ .BHOFUPNFUFS ˓ ˓ ˓ ˓ 1FEPNFUFS ˚ qPPSDPVOUJOH QBDF DBEFODF FWFOUUSBDLJOH ˚ FWFOUUSBDLJOH ˓ ˓ "MUJNFUFS ☓ ˓ ˓ ˓ .PUJPO"DUJWJUZ ˓ ˓ ˓ ˓

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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