-
Notifications
You must be signed in to change notification settings - Fork 130
/
MotionKit.swift
456 lines (378 loc) · 16.1 KB
/
MotionKit.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
// MotionKit.swift
// MotionKit
//
// Created by Haroon on 14/02/2015.
// Launched under the Creative Commons License. You're free to use MotionKit.
//
// The original Github repository is https://github.com/MHaroonBaig/MotionKit
// The official blog post and documentation is https://medium.com/@PyBaig/motionkit-the-missing-ios-coremotion-wrapper-written-in-swift-99fcb83355d0
//
import Foundation
import CoreMotion
//_______________________________________________________________________________________________________________
// this helps retrieve values from the sensors.
@objc protocol MotionKitDelegate {
optional func retrieveAccelerometerValues (x: Double, y:Double, z:Double, absoluteValue: Double)
optional func retrieveGyroscopeValues (x: Double, y:Double, z:Double, absoluteValue: Double)
optional func retrieveDeviceMotionObject (deviceMotion: CMDeviceMotion)
optional func retrieveMagnetometerValues (x: Double, y:Double, z:Double, absoluteValue: Double)
optional func getAccelerationValFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getGravityAccelerationValFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getRotationRateFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getMagneticFieldFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getAttitudeFromDeviceMotion (attitude: CMAttitude)
}
class MotionKit {
let manager = CMMotionManager()
var delegate: MotionKitDelegate?
/*
* init:void:
*
* Discussion:
* Initialises the MotionKit class and throw a Log with a timestamp.
*/
init(){
NSLog("MotionKit has been initialised successfully")
}
/*
* getAccelerometerValues:interval:values:
*
* Discussion:
* Starts accelerometer updates, providing data to the given handler through the given queue.
* Note that when the updates are stopped, all operations in the
* given NSOperationQueue will be cancelled. You can access the retrieved values either by a
* Trailing Closure or through a Delgate.
*/
func getAccelerometerValues (interval: NSTimeInterval = 0.1, values: ((x: Double, y: Double, z: Double) -> ())? ){
var valX: Double!
var valY: Double!
var valZ: Double!
if manager.accelerometerAvailable {
manager.accelerometerUpdateInterval = interval
manager.startAccelerometerUpdatesToQueue(NSOperationQueue()) {
(data: CMAccelerometerData!, error: NSError!) in
if let isError = error {
NSLog("Error: %@", isError)
}
valX = data.acceleration.x
valY = data.acceleration.y
valZ = data.acceleration.z
if values != nil{
values!(x: valX,y: valY,z: valZ)
}
var absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ)
self.delegate?.retrieveAccelerometerValues!(valX, y: valY, z: valZ, absoluteValue: absoluteVal)
}
} else {
NSLog("The Accelerometer is not available")
}
}
/*
* getGyroValues:interval:values:
*
* Discussion:
* Starts gyro updates, providing data to the given handler through the given queue.
* Note that when the updates are stopped, all operations in the
* given NSOperationQueue will be cancelled. You can access the retrieved values either by a
* Trailing Closure or through a Delegate.
*/
func getGyroValues (interval: NSTimeInterval = 0.1, values: ((x: Double, y: Double, z:Double) -> ())? ) {
var valX: Double!
var valY: Double!
var valZ: Double!
if manager.gyroAvailable{
manager.gyroUpdateInterval = interval
manager.startGyroUpdatesToQueue(NSOperationQueue()) {
(data: CMGyroData!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
valX = data.rotationRate.x
valY = data.rotationRate.y
valZ = data.rotationRate.z
if values != nil{
values!(x: valX, y: valY, z: valZ)
}
var absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ)
self.delegate?.retrieveGyroscopeValues!(valX, y: valY, z: valZ, absoluteValue: absoluteVal)
}
} else {
NSLog("The Gyroscope is not available")
}
}
/*
* getMagnetometerValues:interval:values:
*
* Discussion:
* Starts magnetometer updates, providing data to the given handler through the given queue.
* You can access the retrieved values either by a Trailing Closure or through a Delegate.
*/
@availability(iOS, introduced=5.0)
func getMagnetometerValues (interval: NSTimeInterval = 0.1, values: ((x: Double, y:Double, z:Double) -> ())? ){
var valX: Double!
var valY: Double!
var valZ: Double!
if manager.magnetometerAvailable {
manager.magnetometerUpdateInterval = interval
manager.startMagnetometerUpdatesToQueue(NSOperationQueue()){
(data: CMMagnetometerData!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
valX = data.magneticField.x
valY = data.magneticField.y
valZ = data.magneticField.z
if values != nil{
values!(x: valX, y: valY, z: valZ)
}
var absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ)
self.delegate?.retrieveMagnetometerValues!(valX, y: valY, z: valZ, absoluteValue: absoluteVal)
}
} else {
NSLog("Magnetometer is not available")
}
}
/* MARK :- DEVICE MOTION APPROACH STARTS HERE */
/*
* getDeviceMotionValues:interval:values:
*
* Discussion:
* Starts device motion updates, providing data to the given handler through the given queue.
* Uses the default reference frame for the device. Examine CMMotionManager's
* attitudeReferenceFrame to determine this. You can access the retrieved values either by a
* Trailing Closure or through a Delegate.
*/
func getDeviceMotionObject (interval: NSTimeInterval = 0.1, values: ((deviceMotion: CMDeviceMotion) -> ())? ) {
if manager.deviceMotionAvailable{
manager.deviceMotionUpdateInterval = interval
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){
(data: CMDeviceMotion!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
if values != nil{
values!(deviceMotion: data)
}
self.delegate?.retrieveDeviceMotionObject!(data)
}
} else {
NSLog("Device Motion is not available")
}
}
/*
* getAccelerationFromDeviceMotion:interval:values:
* You can retrieve the processed user accelaration data from the device motion from this method.
*/
func getAccelerationFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double) -> ())? ) {
var valX: Double!
var valY: Double!
var valZ: Double!
if manager.deviceMotionAvailable{
manager.deviceMotionUpdateInterval = interval
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){
(data: CMDeviceMotion!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
valX = data.userAcceleration.x
valY = data.userAcceleration.y
valZ = data.userAcceleration.z
if values != nil{
values!(x: valX, y: valY, z: valZ)
}
self.delegate?.getAccelerationValFromDeviceMotion!(valX, y: valY, z: valZ)
}
} else {
NSLog("Device Motion is unavailable")
}
}
/*
* getGravityAccelerationFromDeviceMotion:interval:values:
* You can retrieve the processed gravitational accelaration data from the device motion from this
* method.
*/
func getGravityAccelerationFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double) -> ())? ) {
var valX: Double!
var valY: Double!
var valZ: Double!
if manager.deviceMotionAvailable{
manager.deviceMotionUpdateInterval = interval
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){
(data: CMDeviceMotion!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
valX = data.gravity.x
valY = data.gravity.y
valZ = data.gravity.z
if values != nil{
values!(x: valX, y: valY, z: valZ)
}
var absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ)
self.delegate?.getGravityAccelerationValFromDeviceMotion!(valX, y: valY, z: valZ)
}
} else {
NSLog("Device Motion is not available")
}
}
/*
* getAttitudeFromDeviceMotion:interval:values:
* You can retrieve the processed attitude data from the device motion from this
* method.
*/
func getAttitudeFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((attitude: CMAttitude) -> ())? ) {
if manager.deviceMotionAvailable{
manager.deviceMotionUpdateInterval = interval
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){
(data: CMDeviceMotion!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
if values != nil{
values!(attitude: data.attitude)
}
self.delegate?.getAttitudeFromDeviceMotion!(data.attitude)
}
} else {
NSLog("Device Motion is not available")
}
}
/*
* getRotationRateFromDeviceMotion:interval:values:
* You can retrieve the processed rotation data from the device motion from this
* method.
*/
func getRotationRateFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double) -> ())? ) {
var valX: Double!
var valY: Double!
var valZ: Double!
if manager.deviceMotionAvailable{
manager.deviceMotionUpdateInterval = interval
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){
(data: CMDeviceMotion!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
valX = data.rotationRate.x
valY = data.rotationRate.y
valZ = data.rotationRate.z
if values != nil{
values!(x: valX, y: valY, z: valZ)
}
var absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ)
self.delegate?.getRotationRateFromDeviceMotion!(valX, y: valY, z: valZ)
}
} else {
NSLog("Device Motion is not available")
}
}
/*
* getMagneticFieldFromDeviceMotion:interval:values:
* You can retrieve the processed magnetic field data from the device motion from this
* method.
*/
func getMagneticFieldFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double, accuracy: Int32) -> ())? ) {
var valX: Double!
var valY: Double!
var valZ: Double!
var valAccuracy: Int32!
if manager.deviceMotionAvailable{
manager.deviceMotionUpdateInterval = interval
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){
(data: CMDeviceMotion!, error: NSError!) in
if let isError = error{
NSLog("Error: %@", isError)
}
valX = data.magneticField.field.x
valY = data.magneticField.field.y
valZ = data.magneticField.field.z
valAccuracy = data.magneticField.accuracy.value
if values != nil{
values!(x: valX, y: valY, z: valZ, accuracy: valAccuracy)
}
self.delegate?.getMagneticFieldFromDeviceMotion!(valX, y: valY, z: valZ)
}
} else {
NSLog("Device Motion is not available")
}
}
/* MARK :- DEVICE MOTION APPROACH ENDS HERE */
/*
* From the methods hereafter, the sensor values could be retrieved at
* a particular instant, whenever needed, through a trailing closure.
*/
/* MARK :- INSTANTANIOUS METHODS START HERE */
func getAccelerationAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){
self.getAccelerationFromDeviceMotion(interval: 0.5) { (x, y, z) -> () in
values(x: x,y: y,z: z)
self.stopDeviceMotionUpdates()
}
}
func getGravitationalAccelerationAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){
self.getGravityAccelerationFromDeviceMotion(interval: 0.5) { (x, y, z) -> () in
values(x: x,y: y,z: z)
self.stopDeviceMotionUpdates()
}
}
func getAttitudeAtCurrentInstant (values: (attitude: CMAttitude) -> ()){
self.getAttitudeFromDeviceMotion(interval: 0.5) { (attitude) -> () in
values(attitude: attitude)
self.stopDeviceMotionUpdates()
}
}
func getMageticFieldAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){
self.getMagneticFieldFromDeviceMotion(interval: 0.5) { (x, y, z, accuracy) -> () in
values(x: x,y: y,z: z)
self.stopDeviceMotionUpdates()
}
}
func getGyroValuesAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){
self.getRotationRateFromDeviceMotion(interval: 0.5) { (x, y, z) -> () in
values(x: x,y: y,z: z)
self.stopDeviceMotionUpdates()
}
}
/* MARK :- INSTANTANIOUS METHODS END HERE */
/*
* stopAccelerometerUpdates
*
* Discussion:
* Stop accelerometer updates.
*/
func stopAccelerometerUpdates(){
self.manager.stopAccelerometerUpdates()
NSLog("Accelaration Updates Status - Stopped")
}
/*
* stopGyroUpdates
*
* Discussion:
* Stops gyro updates.
*/
func stopGyroUpdates(){
self.manager.stopGyroUpdates()
NSLog("Gyroscope Updates Status - Stopped")
}
/*
* stopDeviceMotionUpdates
*
* Discussion:
* Stops device motion updates.
*/
func stopDeviceMotionUpdates() {
self.manager.stopDeviceMotionUpdates()
NSLog("Device Motion Updates Status - Stopped")
}
/*
* stopMagnetometerUpdates
*
* Discussion:
* Stops magnetometer updates.
*/
@availability(iOS, introduced=5.0)
func stopmagnetometerUpdates() {
self.manager.stopMagnetometerUpdates()
NSLog("Magnetometer Updates Status - Stopped")
}
}