forked from gokcekca/MyoLuminati
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
368 lines (297 loc) · 10.5 KB
/
Source.cpp
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
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <string>
#include <algorithm>
#include <WinSock2.h>
#include <windows.h>
#include <time.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#include <myo\myo.hpp>
// The only file that needs to be included to use the Myo C++ SDK is myo.hpp.
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 2390
#pragma comment(lib, "ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
void wait(unsigned timeout)
{
timeout += std::clock();
while(std::clock() < timeout) continue;
}
int communicate(char in[], char in2[] = ""){
char three[4];
memcpy(three, in, 1);
memcpy(&three[1], in2, 1);
#define APP_PORT 2390 // We define a port that we are going to use.
// Here is a structure contains the port we'll use,
// the protocol type and the IP address we'll communicate with.
SOCKADDR_IN sockaddr;
// This is our socket, it is the handle to the IO address to read/write packets
SOCKET sock;
WSADATA data;
// First we see if there is a winsock ver 2.2 installed on the computer,
// we initizalize the sockets DLL for out app.
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) return(0);
// Here we create our socket, which will be a UDP socket (SOCK_DGRAM).
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (!sock)
{
// Creation failed!
}
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = INADDR_ANY;
sockaddr.sin_addr.s_addr = inet_addr("192.168.0.102"); // IP to communicate with.
sockaddr.sin_port = htons(APP_PORT);
int ret = bind(sock, (SOCKADDR *)&sockaddr, sizeof(SOCKADDR));
if (ret)
{
// Bind failed!
}
char buffer[10];
strcpy(buffer, three);
int len = sizeof(SOCKADDR);
sendto(sock, buffer, strlen(buffer), 0, (SOCKADDR *)&sockaddr, sizeof(SOCKADDR));
// Easy huh?? Let's receive a packet..
WSACleanup();
return 0;
}
// Classes that inherit from myo::DeviceListener can be used to receive events from Myo devices. DeviceListener
// provides several virtual functions for handling different kinds of events. If you do not override an event, the
// default behavior is to do nothing.
class DataCollector : public myo::DeviceListener {
public:
DataCollector()
: onArm(false), isUnlocked(false), roll_w(0), pitch_w(0), yaw_w(0), currentPose()
{
}
// onUnpair() is called whenever the Myo is disconnected from Myo Connect by the user.
void onUnpair(myo::Myo* myo, uint64_t timestamp)
{
// We've lost a Myo.
// Let's clean up some leftover state.
roll_w = 0;
pitch_w = 0;
yaw_w = 0;
onArm = false;
isUnlocked = false;
}
// onOrientationData() is called whenever the Myo device provides its current orientation, which is represented
// as a unit quaternion.
void onOrientationData(myo::Myo* myo, uint64_t timestamp, const myo::Quaternion<float>& quat)
{
using std::atan2;
using std::asin;
using std::sqrt;
using std::max;
using std::min;
// 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(max(-1.0f, min(1.0f, 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 18.
roll_w = static_cast<int>((roll + (float)M_PI) / (M_PI * 2.0f) * 255);
pitch_w = static_cast<int>((pitch + (float)M_PI / 2.0f) / M_PI * 255);
yaw_w = static_cast<int>((yaw + (float)M_PI) / (M_PI * 2.0f) * 255);
}
// onPose() is called whenever the Myo detects that the person wearing it has changed their pose, for example,
// making a fist, or not making a fist anymore.
void onPose(myo::Myo* myo, uint64_t timestamp, myo::Pose pose)
{
currentPose = pose;
if (pose != myo::Pose::unknown && pose != myo::Pose::rest) {
// Tell the Myo to stay unlocked until told otherwise. We do that here so you can hold the poses without the
// Myo becoming locked.
myo->unlock(myo::Myo::unlockHold);
// Notify the Myo that the pose has resulted in an action, in this case changing
// the text on the screen. The Myo will vibrate.
myo->notifyUserAction();
}
else {
// Tell the Myo to stay unlocked only for a short period. This allows the Myo to stay unlocked while poses
// are being performed, but lock after inactivity.
myo->unlock(myo::Myo::unlockTimed);
}
}
//PoseControl() will be called to control the lights using speciffic poses
int poseControl(myo::Myo* myo, myo::Pose pose, int pos)
{
char* colors[7] = { "r", "s", "t", "u", "v", "w", "y" };
bool lock = false;
int tme = 0;
//Colors::Colors c;
currentPose = pose;
bool lightOff = false;
//lightOff = true;
//lights toggle
if (isUnlocked == false && pose == myo::Pose::fist && lightOff == true)
{
std::cout << "toggle on" << std::endl;
}
else if (isUnlocked == true && pose == myo::Pose::fist && lightOff == false)
{
communicate("q");
std::cout << "toggle off" << std::endl;
isUnlocked = false;
lightOff = true;
Sleep(5000);
}
//light intensity
if (lightOff == false && 0 <= pitch_w && pitch_w <= 170 )
{
char w = pitch_w / 2;
char z = (255-roll_w)/4;
char array2[] = { w };
char array3[] = { z };
//roll from 90 to 150
//roll from 170 to 110
if (roll_w >=70 && roll_w < 110){
communicate("A", array3);
}
else if (roll_w > 110 && roll_w <= 130){
communicate("C", array3);
}
else if (roll_w > 150){
communicate("B", array3);
}
if (roll_w < 65 && pitch_w < 110 ){
communicate("x", array2);
}
std::cout << "Light intensity" << std::endl;
}
//light hand switch color
if (isUnlocked == true && lightOff == false && pose == myo::Pose::waveOut)
{
pos++;
if (pos == 7){ pos = 0; }
lock = true;
std::cout << "Color Right" <<colors[pos]<<pos<< std::endl;
communicate(colors[pos]);
Sleep(1000);
}
else if (isUnlocked == true && lightOff == false && pose == myo::Pose::waveIn)
{
if (pos == 0){ pos = 6; }
pos--;
std::cout << "Color Left" <<colors[pos]<<pos<< std::endl;
communicate(colors[pos]);
lock = false;
Sleep(1000);
}
//light flash
if (lightOff == false && 200 <= pitch_w && pitch_w <= 255)
{
//myo->lightFlash();
pos++;
//pos %= 7;
if (pos == 7){ pos = 1; }
//myo->switchColor("right");
//communicate("a");
lock = true;
//std::cout << "Color Right" << colors[pos] << pos << std::endl;
communicate(colors[pos]);
//Sleep(1000);
//pos = ((pos + 1) % 7)+ 1;
//communicate("o");
std::cout << "Flash" << std::endl;
}
return pos;
}
// onArmSync() is called whenever Myo has recognized a Sync Gesture after someone has put it on their
// arm. This lets Myo know which arm it's on and which way it's facing.
void onArmSync(myo::Myo* myo, uint64_t timestamp, myo::Arm arm, myo::XDirection xDirection)
{
onArm = true;
whichArm = arm;
}
// onArmUnsync() is called whenever Myo has detected that it was moved from a stable position on a person's arm after
// it recognized the arm. Typically this happens when someone takes Myo off of their arm, but it can also happen
// when Myo is moved around on the arm.
void onArmUnsync(myo::Myo* myo, uint64_t timestamp)
{
onArm = false;
}
// onUnlock() is called whenever Myo has become unlocked, and will start delivering pose events.
void onUnlock(myo::Myo* myo, uint64_t timestamp)
{
isUnlocked = true;
}
// onLock() is called whenever Myo has become locked. No pose events will be sent until the Myo is unlocked again.
void onLock(myo::Myo* myo, uint64_t timestamp)
{
isUnlocked = false;
}
// There are other virtual functions in DeviceListener that we could override here, like onAccelerometerData().
// For this example, the functions overridden above are sufficient.
// We define this function to print the current values that were updated by the on...() functions above.
void print()
{
// Clear the current line
std::cout << '\r';
// Print out the orientation. Orientation data is always available, even if no arm is currently recognized.
//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, ' ') << ']';
std::cout << "Roll " << roll_w << " pitch " << pitch_w << " yaw " << yaw_w;
if (onArm) {
// Print out the lock state, the currently recognized pose, and which arm Myo is being worn on.
// Pose::toString() provides the human-readable name of a pose. We can also output a Pose directly to an
// output stream (e.g. std::cout << currentPose;). In this case we want to get the pose name's length so
// that we can fill the rest of the field with spaces below, so we obtain it as a string using toString().
std::string poseString = currentPose.toString();
std::cout << '[' << (isUnlocked ? "unlocked" : "locked ") << ']'
<< '[' << (whichArm == myo::armLeft ? "L" : "R") << ']'
<< '[' << poseString << std::string(14 - poseString.size(), ' ') << ']';
}
else {
// Print out a placeholder for the arm and pose when Myo doesn't currently know which arm it's on.
std::cout << '[' << std::string(8, ' ') << ']' << "[?]" << '[' << std::string(14, ' ') << ']';
}
std::cout << std::flush;
}
// These values are set by onArmSync() and onArmUnsync() above.
bool onArm;
myo::Arm whichArm;
// This is set by onUnlocked() and onLocked() above.
bool isUnlocked;
// These values are set by onOrientationData() and onPose() above.
int roll_w, pitch_w, yaw_w;
myo::Pose currentPose;
};
int main(int argc, char** argv)
{
unsigned int pos = 3;
try
{
std::cout << "Attempting to find a Myo..." << std::endl << std::endl;
myo::Hub hub("com.example.hello-myo");
myo::Myo* myo = hub.waitForMyo(10000);
if (!myo)
{
throw std::runtime_error("Unable to find a Myo!");
}
std::cout << "Connected to a Myo armband!" << std::endl << std::endl;
DataCollector collector;
//myo::Pose pose = collector.currentPose;
hub.addListener(&collector);
while (1)
{
hub.run(1000 / 20);
myo::Pose pose = collector.currentPose;
collector.print();
pos=collector.poseControl(myo, pose,pos);
}
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Press enter to continue.";
std::cin.ignore();
return 1;
}
}