This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Motion.mm
209 lines (176 loc) · 7.31 KB
/
Motion.mm
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
#import "Motion.hh"
@implementation Motion
-(id)init {
if (self = [super init]) {
}
return self;
}
//Continously retrieve the activity
- (void) getActivity: (double)interval withCallback:(void(^)(NSString*)) callback{
if(self.activityManager == nil)
self.activityManager = [[CMMotionActivityManager alloc] init];
//The handler of activity updates doesn't provide a error, therefore omitted
[self.activityManager startActivityUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMMotionActivity *activity)
{
callback([NSString stringWithFormat:@"{\"unknown\": %s, \"stationary\": %s, \"walking\": %s, \"running\": %s, \"automotive\": %s, \"cycling\": %s, \"confidence\": \"%s\"}",
activity.unknown ? "true" : "false", activity.stationary ? "true" : "false", activity.walking ? "true" : "false", activity.running ? "true" : "false", activity.automotive ? "true" : "false", activity.cycling ? "true" : "false", activity.confidence == 0 ? "low" : (activity.confidence == 1 ? "medium" : "high")
]);
}];
}
- (void) stopActivity{
if(self.activityManager != nil)
[self.activityManager stopActivityUpdates];
}
//Continously retrieve 3D accelerometer values measured in G:s
- (void) getAccelerometerValues: (double)interval withCallback:(void(^)(NSString*)) callback{
if(self.manager == nil)
self.manager = [[CMMotionManager alloc] init];
if(self.manager.accelerometerAvailable && !self.manager.accelerometerActive){
self.manager.accelerometerUpdateInterval = interval;
[self.manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMAccelerometerData *accelerometer, NSError *error)
{
if(error != nil){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %@}}",
error.localizedDescription
]);
return;
}
callback([NSString stringWithFormat:@"{\"acceleration\":{\"x\": %f, \"y\": %f, \"z\": %f}}",
accelerometer.acceleration.x, accelerometer.acceleration.y, accelerometer.acceleration.z
]);
}];
} else if(!self.manager.accelerometerAvailable){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %s}}",
"accelerometer is not available"
]);
}
}
- (void) stopAccelerometer{
if(self.manager != nil)
[self.manager stopAccelerometerUpdates];
}
//Continously retrieve 3D rotation rates measured in angles
- (void) getGyroValues: (double)interval withCallback:(void(^)(NSString*)) callback{
if(self.manager == nil)
self.manager = [[CMMotionManager alloc] init];
if(self.manager.gyroAvailable && !self.manager.gyroActive){
self.manager.gyroUpdateInterval = interval;
[self.manager startGyroUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMGyroData *gyro, NSError *error)
{
if(error != nil){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %@}}",
error.localizedDescription
]);
return;
}
//Return in degrees instead of radians
double degreesFromRadian = 180 / M_PI;
callback([NSString stringWithFormat:@"{\"rotationRate\":{\"x\": %f, \"y\": %f, \"z\": %f}}",
degreesFromRadian * gyro.rotationRate.x, degreesFromRadian * gyro.rotationRate.y, degreesFromRadian * gyro.rotationRate.z
]);
}];
} else if(!self.manager.gyroAvailable){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %s}}",
"gyroscope is not available"
]);
}
}
- (void) stopGyroscope{
if(self.manager != nil)
[self.manager stopGyroUpdates];
}
- (void) getMagnetometerValues: (double)interval withCallback:(void(^)(NSString*)) callback{
if(self.manager == nil)
self.manager = [[CMMotionManager alloc] init];
if(self.manager.magnetometerAvailable && !self.manager.magnetometerActive){
self.manager.magnetometerUpdateInterval = interval;
[self.manager startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMMagnetometerData *magnetometer, NSError *error)
{
if(error != nil){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %@}}",
error.localizedDescription
]);
return;
}
callback([NSString stringWithFormat:@"{\"magneticField\": {\"x\": %f, \"y\": %f, \"z\": %f}}",
magnetometer.magneticField.x, magnetometer.magneticField.y, magnetometer.magneticField.z
]);
}];
} else if(!self.manager.magnetometerAvailable){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %s}}",
"magnetometer is not available"
]);
}
}
- (void) stopMagnetometer{
if(self.manager != nil)
[self.manager stopMagnetometerUpdates];
}
- (void) getMotionValues: (double)interval withCallback:(void(^)(NSString*)) callback{
if(self.manager == nil)
self.manager = [[CMMotionManager alloc] init];
if(self.manager.deviceMotionAvailable && !self.manager.deviceMotionActive){
self.manager.deviceMotionUpdateInterval = interval;
[self.manager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
if(error != nil){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %@}}",
error.localizedDescription
]);
return;
}
//Return in degrees instead of radians
double degreesFromRadian = 180 / M_PI;
CMQuaternion quat = motion.attitude.quaternion;
double yaw = asin(2*(quat.x*quat.z - quat.w*quat.y));
if (self.previousAttitudeYaw == 0) {
self.previousAttitudeYaw = yaw;
}
//Kalman filtering
static float q = 0.1; // process noise
static float r = 0.1; // sensor noise
static float p = 0.1; // estimated error
static float k = 0.5; // kalman filter gain
float x = self.previousAttitudeYaw;
p = p + q;
k = p / (p + r);
x = x + k*(yaw - x);
p = (1 - k)*p;
self.previousAttitudeYaw = x;
callback([NSString stringWithFormat:@"{\
\"attitude\": {\"yaw\": %f, \"pitch\": %f, \"roll\": %f}, \
\"rotationRate\": {\"x\": %f, \"y\": %f, \"z\": %f}, \
\"gravity\": {\"x\": %f, \"y\": %f, \"z\": %f}, \
\"magneticField\": {\"accuracy\": %d, \"x\": %f, \"y\": %f, \"z\": %f}, \
\"userAcceleration\": {\"x\": %f, \"y\": %f, \"z\": %f}}",
x * degreesFromRadian, motion.attitude.pitch * degreesFromRadian, motion.attitude.roll * degreesFromRadian,
motion.rotationRate.x, motion.rotationRate.y, motion.rotationRate.z,
motion.gravity.x, motion.gravity.y, motion.gravity.z,
motion.magneticField.accuracy, motion.magneticField.field.x, motion.magneticField.field.y, motion.magneticField.field.z,
motion.userAcceleration.x, motion.userAcceleration.y, motion.userAcceleration.z
]);
}];
} else if(!self.manager.deviceMotionActive){
callback([NSString stringWithFormat:@"{\
\"error\": {\"description\": %s}}",
"device motion is not available"
]);
}
}
- (void) stopMotion{
if(self.manager != nil)
[self.manager stopDeviceMotionUpdates];
}
@end