-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVaavudDynamicsController.m
executable file
·127 lines (97 loc) · 4.02 KB
/
VaavudDynamicsController.m
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
//
// VaavudDynamicsController.m
// VaavudCore
//
// Created by Andreas Okholm on 5/19/13.
// Copyright (c) 2013 Andreas Okholm. All rights reserved.
//
// Check if abs(acceleration) & device orientation & gyroscope is below theshold values
//
#import "VaavudDynamicsController.h"
#import <CoreMotion/CoreMotion.h>
@interface VaavudDynamicsController ()
@property (nonatomic, strong) CMMotionManager *motionManager;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic) BOOL accelerationIsValid;
@property (nonatomic) BOOL orientationIsValid;
@property (nonatomic) BOOL angularVeclocityIsValid;
@property (nonatomic, strong) CLLocationManager *locationManager;
- (void)updateValidity;
@end
@implementation VaavudDynamicsController
- (id)init {
self = [super init];
if (self) {
self.accelerationIsValid = YES;
self.angularVeclocityIsValid = YES;
self.orientationIsValid = YES;
}
return self;
}
- (void)updateValidity {
self.isValid = self.accelerationIsValid && self.orientationIsValid && self.angularVeclocityIsValid;
[self.vaavudCoreController dynamicsIsValid:self.isValid];
}
- (void)start {
self.motionManager = [[CMMotionManager alloc] init];
if (self.motionManager.deviceMotionAvailable) {
self.motionManager.deviceMotionUpdateInterval = 1.0/accAndGyroSampleFrequency;
if (!self.operationQueue) {
self.operationQueue = [NSOperationQueue currentQueue];
}
[self.motionManager startDeviceMotionUpdatesToQueue:self.operationQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
// Orientation
double deviceDeviationFromVertical = M_PI/2 - fabs(motion.attitude.pitch);
if (deviceDeviationFromVertical > orientationDeviationMaxForValid) {
self.orientationIsValid = NO;
// TODO: reinsert
//NSLog(@"Orientation deviation from vertical is too big with value %f", deviceDeviationFromVertical);
}
else {
self.orientationIsValid = YES;
}
// angular velocity
double angularVelocity = fabs(sqrt(pow(motion.rotationRate.x, 2) + pow(motion.rotationRate.x, 2) + pow(motion.rotationRate.x, 2)));
if (angularVelocity > angularVelocityMaxForValid) {
self.angularVeclocityIsValid = NO;
// TODO: reinsert
//NSLog(@"Angular velocity is too big with value %f ", angularVelocity);
}
else {
self.angularVeclocityIsValid = YES;
}
// acceleration
double acceleration = fabs(sqrt(pow(motion.userAcceleration.x, 2) + pow(motion.userAcceleration.y, 2) + pow(motion.userAcceleration.z, 2)));
if (acceleration > accelerationMaxForValid) {
self.accelerationIsValid = NO;
// TODO: reinsert
//NSLog(@"Acceleration is too big with value %f", acceleration);
}
else {
self.accelerationIsValid = YES;
}
[self updateValidity];
}];
}
if ([CLLocationManager headingAvailable]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.headingFilter = 1;
[self.locationManager startUpdatingHeading];
}
else {
NSLog(@"No heading avaliable!!!");
}
}
- (void)stop {
[self.motionManager stopDeviceMotionUpdates];
self.motionManager = nil;
self.operationQueue = nil;
[self.locationManager stopUpdatingHeading];
}
// Heading
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
// [self.vaavudCoreController newHeading: [NSNumber numberWithDouble: newHeading.trueHeading]];
// NSLog(@"heading accuracy: %f", newHeading.headingAccuracy);
}
@end