-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMyoObjectiveC.mm
303 lines (269 loc) · 9 KB
/
MyoObjectiveC.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
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
//
// MyoObjectiveC.m
// OSXGestureControl
//
// Created by Remi Santos on 05/08/2014.
// Copyright (c) 2014 Chris Willingham. All rights reserved.
//
#import "MyoObjectiveC.h"
#import <myo/myo.hpp>
@class Myo;
class DataCollector : public myo::DeviceListener {
public:
DataCollector()
: onArm(false), roll_w(0), pitch_w(0), yaw_w(0), currentPose()
{
}
void onOrientationData(myo::Myo* myo, uint64_t timestamp, const myo::Quaternion<float>& quat)
{
using std::atan2;
using std::asin;
using std::sqrt;
// Calculate Euler angles (roll, pitch, and yaw) from the unit quaternion.
float roll = atan2(2.0f * (quat.w() * quat.x() + quat.y() * quat.z()),
1.0f - 2.0f * (quat.x() * quat.x() + quat.y() * quat.y()));
float pitch = asin(2.0f * (quat.w() * quat.y() - quat.z() * quat.x()));
float yaw = atan2(2.0f * (quat.w() * quat.z() + quat.x() * quat.y()),
1.0f - 2.0f * (quat.y() * quat.y() + quat.z() * quat.z()));
// Convert the floating point angles in radians to a scale from 0 to 20.
roll_w = static_cast<int>((roll + (float)M_PI)/(M_PI * 2.0f) * 18);
pitch_w = static_cast<int>((pitch + (float)M_PI/2.0f)/M_PI * 18);
yaw_w = static_cast<int>((yaw + (float)M_PI)/(M_PI * 2.0f) * 18);
if ([_myo.delegate respondsToSelector:@selector(myo:onOrientationDataWithRoll:pitch:yaw:)]) {
[_myo.delegate myo:_myo onOrientationDataWithRoll:roll_w pitch:pitch_w yaw:yaw_w];
}
}
void onAccelerometerData(Myo* myo, uint64_t timestamp, const myo::Vector3<float>& accel)
{
MyoVector *vector = [[MyoVector alloc] initWithX:accel.x() y:accel.y() z:accel.z()];
if ([_myo.delegate respondsToSelector:@selector(myo:onAccelerometerDataWithVector:)]) {
[_myo.delegate myo:_myo onAccelerometerDataWithVector:vector];
}
}
/// Called when a paired Myo has provided new gyroscope data in units of deg/s.
void onGyroscopeData(Myo* myo, uint64_t timestamp, const myo::Vector3<float>& gyro)
{
MyoVector *vector = [[MyoVector alloc] initWithX:gyro.x() y:gyro.y() z:gyro.z()];
if ([_myo.delegate respondsToSelector:@selector(myo:onGyroscopeDataWithVector:)]) {
[_myo.delegate myo:_myo onGyroscopeDataWithVector:vector];
}
}
/// Called when a paired Myo has provided a new RSSI value.
/// @see Myo::requestRssi() to request an RSSI value from the Myo.
void onRssi(Myo* myo, uint64_t timestamp, int8_t rssi)
{
if ([_myo.delegate respondsToSelector:@selector(myo:onRssi:)]) {
[_myo.delegate myo:_myo onRssi:rssi];
}
}
void onPose(myo::Myo* myo, uint64_t timestamp, myo::Pose pose)
{
currentPose = pose;
// print();
MyoPose *myopose = [MyoPose new];
if (pose.type() == myo::Pose::fist)
myopose.poseType = MyoPoseTypeFist;
if (pose.type() == myo::Pose::fingersSpread)
myopose.poseType = MyoPoseTypeFingersSpread;
if (pose.type() == myo::Pose::waveIn)
myopose.poseType = MyoPoseTypeWaveIn;
if (pose.type() == myo::Pose::waveOut)
myopose.poseType = MyoPoseTypeWaveOut;
if (pose.type() == myo::Pose::doubleTap)
myopose.poseType = MyoPoseTypeDoubleTap;
if ([_myo.delegate respondsToSelector:@selector(myo:onPose:)]) {
[_myo.delegate myo:_myo onPose:myopose];
}
}
void onArmRecognized(myo::Myo* myo, uint64_t timestamp, myo::Arm arm, myo::XDirection xDirection)
{
onArm = true;
whichArm = arm;
MyoArm *thisArm = [MyoArm new];
switch (arm) {
case myo::armLeft:
thisArm.armType = MyoArmTypeLeft;
break;
case myo::armRight:
thisArm.armType = MyoArmTypeRight;
break;
default:
thisArm.armType = MyoArmTypeUnknown;
break;
}
if ([_myo.delegate respondsToSelector:@selector(myoOnArmRecognized:arm:)]) {
[_myo.delegate myoOnArmRecognized:_myo arm:thisArm];
}
else if ([_myo.delegate respondsToSelector:@selector(myoOnArmRecognized:)]) {
[_myo.delegate myoOnArmRecognized:_myo];
}
}
void onArmLost(myo::Myo* myo, uint64_t timestamp)
{
onArm = false;
if ([_myo.delegate respondsToSelector:@selector(myoOnArmLost:)]) {
[_myo.delegate myoOnArmLost:_myo];
}
}
void onPair(Myo* myo, uint64_t timestamp, myo::FirmwareVersion firmwareVersion)
{
if ([_myo.delegate respondsToSelector:@selector(myoOnPair:)]) {
[_myo.delegate myoOnPair:_myo];
}
}
/// Called when a paired Myo has been connected.
void onConnect(Myo* myo, uint64_t timestamp, myo::FirmwareVersion firmwareVersion)
{
if ([_myo.delegate respondsToSelector:@selector(myoOnConnect:)]) {
[_myo.delegate myoOnConnect:_myo];
}
}
/// Called when a paired Myo has been disconnected.
void onDisconnect(Myo* myo, uint64_t timestamp)
{
if ([_myo.delegate respondsToSelector:@selector(myoOnDisconnect:)]) {
[_myo.delegate myoOnDisconnect:_myo];
}
}
void print()
{
// Clear the current line
std::cout << '\r';
std::cout << '[' << std::string(roll_w, '*') << std::string(18 - roll_w, ' ') << ']'
<< '[' << std::string(pitch_w, '*') << std::string(18 - pitch_w, ' ') << ']'
<< '[' << std::string(yaw_w, '*') << std::string(18 - yaw_w, ' ') << ']';
if (onArm) {
std::string poseString = currentPose.toString();
std::cout << '[' << (whichArm == myo::armLeft ? "L" : "R") << ']'
<< '[' << poseString << std::string(14 - poseString.size(), ' ') << ']';
} else {
std::cout << "[?]" << '[' << std::string(14, ' ') << ']';
}
std::cout << std::flush;
}
// These values are set by onArmRecognized() and onArmLost() above.
bool onArm;
myo::Arm whichArm;
// These values are set by onOrientationData() and onPose() above.
int roll_w, pitch_w, yaw_w;
myo::Pose currentPose;
Myo *_myo;
};
#define DEFAULT_UPDATE_TIME 100
#pragma mark - MYOPOSE
@implementation MyoPose
@end
#pragma mark - MYOARM
@implementation MyoArm
@end
#pragma mark - MYOVECTOR
@implementation MyoVector
-(id)init
{
return [self initWithX:0 y:0 z:0];
}
- (instancetype)initWithX:(float)x y:(float)y z:(float)z
{
self = [super init];
if (self) {
_data[0] = x;
_data[1] = y;
_data[2] = z;
}
return self;
}
-(float)x
{
return _data[0];
}
-(float)y
{
return _data[1];
}
-(float)z
{
return _data[2];
}
-(float)magnitude
{
return std::sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
}
-(float)productWithVector:(MyoVector*)rhs
{
return self.x * self.x + self.y * self.y + self.z * self.z;
}
-(MyoVector*)normalized
{
float norm = self.magnitude;
return [[MyoVector alloc] initWithX:(self.x / norm) y:(self.y / norm) z:(self.z / norm)];
}
-(MyoVector*)crossProductWithVector:(MyoVector*)rhs
{
float x = self.x * rhs.y - self.y * rhs.x;
float y = self.y * rhs.z - self.z * rhs.y;
float z = self.z * rhs.x - self.x * rhs.z;
return [[MyoVector alloc] initWithX:x y:y z:z];
}
-(float)angleWithVector:(MyoVector *)rhs
{
return std::acos([self productWithVector:rhs] / (self.magnitude * rhs.magnitude));
}
@end
#pragma mark - MYO
@implementation Myo{
myo::Hub hub;
myo::Myo* myo;
DataCollector collector;
BOOL update;
}
- (instancetype)initWithApplicationIdentifier:(NSString*)identifier
{
self = [super init];
if (self) {
myo::Hub hub([identifier UTF8String]);
self.updateTime = DEFAULT_UPDATE_TIME;
}
return self;
}
-(BOOL)connectMyoWaiting:(int)milliseconds
{
myo = hub.waitForMyo(milliseconds);
if (!myo) {
return false;
}
std::cout << "Connected to a Myo armband!" << std::endl << std::endl;
collector._myo = self;
hub.addListener(&collector);
return true;
}
-(void)startUpdate
{
update = true;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
while (update) {
hub.run(_updateTime);
// collector.print();
}
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
});
});
}
-(void)stopUpdate {
update = false;
}
-(void)vibrateWithType:(MyoVibrationType)type {
switch (type) {
case MyoVibrationTypeShort:
myo->vibrate(myo::Myo::vibrationShort);
break;
case MyoVibrationTypeLong:
myo->vibrate(myo::Myo::vibrationLong);
break;
default:
myo->vibrate(myo::Myo::vibrationMedium);
break;
}
}
@end