-
Notifications
You must be signed in to change notification settings - Fork 0
/
gesturelistener.h
291 lines (234 loc) · 8.34 KB
/
gesturelistener.h
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
//
// gesturelistener.h
// myoSign
//
// Created by Andy Acosta on 3/7/17.
// Copyright © 2017 Andy Acosta. All rights reserved.
//
#include <sys/types.h>
#include <sys/stat.h>
#include <array>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <fstream>
#include <map>
#include <myo/myo.hpp>
#include "grt-master/GRT/GRT.h"
using namespace GRT;
template <typename T> string NumberToString( T Number )
{
ostringstream ss;
ss << Number;
return ss.str();
}
class gesturelistener : public myo::DeviceListener {
DTW dtw1;
public:
gesturelistener(string classnames, string datamodel) : onArm(false), isUnlocked(false), roll_w(0), pitch_w(0), yaw_w(0), currentPose(){
//emgSamples = vector<double>(0,0);
emgSamples = array<int, 8>();
roll = 0; pitch = 0; yaw = 0;
ax = ay = az = 0;
//gesturenames.push_back("null");
gestureIdName[0] = "null";
//gesturenames.push_back("1");
//gesturenames.push_back("2");
/*std::ofstream ofsClear;
ofsClear.open(classnames, std::ofstream::out | std::ofstream::trunc);
ofsClear.close();*/
ifstream infile( classnames );
std::cout << classnames << std::endl;
//infile.clear();
int gestureID;
while (infile)
{
std::string s;
if (!getline( infile, s )) break;
std::istringstream ss( s );
int col = 0;
while (ss)
{
std::string s;
if (!getline( ss, s, ',' ))
break;
int z;
if(col == 0){
z = std::stoi(s);
}
if(col == 1){
gestureIdName[z] = s;
std::cout << gestureIdName[z] << std::endl;
}
col++;
}
}
if (!infile.eof())
{
std::cerr << "Error with file!\n";
}
dtw1.enableTrimTrainingData(true, 0.1, 90);
dtw1.enableNullRejection(true);
//Load DTW model from file
if(!dtw1.loadModelFromFile(datamodel)){
cerr << "Could not load the classifier model.\n";
exit(EXIT_FAILURE);
}
else{
std::cout << "Loaded the classifier model.\n";
}
std::cout << "Device listener created!!" << std::endl;
}
void onUnpair(myo::Myo* myo, uint64_t timestamp){
roll_w = 0;
pitch_w = 0;
yaw_w = 0;
onArm = false;
isUnlocked = false;
emgSamples.fill(0);
}
void onAccelerometerData(myo::Myo* myo, uint64_t timestamp, const myo::Vector3<float>& acceleration){
ax = acceleration[0];
ay = acceleration[1];
az = acceleration[2];
}
void onOrientationData(myo::Myo* myo, uint64_t timestamp, const myo::Quaternion<float>& quat){
//Euler angles (r, p, y) from unit quaternion.
roll = atan2(2.0f * (quat.w() * quat.x() + quat.y() * quat.z()),1.0f - 2.0f * (quat.x() * quat.x() + quat.y() * quat.y()));
pitch = asin(max(-1.0f, min(1.0f, 2.0f * (quat.w() * quat.y() - quat.z() * quat.x()))));
yaw = atan2(2.0f * (quat.w() * quat.z() + quat.x() * quat.y()),1.0f - 2.0f * (quat.y() * quat.y() + quat.z() * quat.z()));
//cout << "[" << roll << " " << pitch << " " << yaw << "]" << endl;
// 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) * 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);
}
void onEmgData(myo::Myo* myo, uint64_t timestamp, const int8_t* emg){
//emgSamples.clear();
for(int i = 0; i < 8; i++){
int x = static_cast<double>(emg[i]);
emgSamples[i] = x;
ostringstream oss;
oss << static_cast<int>(emg[i]);
string emgString = oss.str();
//cout << '[' << emgString << string(4 - emgString.size(), ' ') << ']';
}
//cout << endl;
}
void onPair(myo::Myo* myo, uint64_t timestamp, myo::FirmwareVersion firmwareVersion)
{
}
void onPose(myo::Myo* myo, uint64_t timestamp, myo::Pose pose)
{
currentPose = pose;
if(pose != myo::Pose::unknown && pose != myo::Pose::rest)
{
myo->unlock(myo::Myo::unlockHold);
myo->notifyUserAction();
} else {
myo->unlock(myo::Myo::unlockTimed);
}
}
void onArmSync(myo::Myo* myo, uint64_t timestamp, myo::Arm arm, myo::XDirection xDirection, float rotation, myo::WarmupState warmupState)
{
onArm = true;
whichArm = arm;
}
void onArmUnsync(myo::Myo* myo, uint64_t timestamp)
{
onArm = false;
}
void onUnlock(myo::Myo* myo, uint64_t timestamp)
{
isUnlocked = true;
}
void onLock(myo::Myo* myo, uint64_t timestamp)
{
isUnlocked = false;
}
void print()
{
//Clear current line
cout << '\r';
cout << '[' << string(roll_w, '*') << string(18-roll_w, ' ') << ']'
<< '[' << string(pitch_w, '*') << string(18-pitch_w, ' ') << ']'
<< '[' << string(yaw_w, '*') << string(18-yaw_w, ' ') << ']';
if(onArm)
{
string poseString = currentPose.toString();
cout << '[' << (isUnlocked ? "unlocked" : "locked ") << ']'
<< '[' << (whichArm == myo::armLeft ? "L" : "R") << ']'
<< '[' << poseString << string(14-poseString.size(), ' ') << ']';
} else {
//if unknown arm and pose
cout << '[' << string(8, ' ') << ']' << "[?]" << '[' << string(14, ' ') << ']';
}
cout << flush;
}
void onGesture(double confidence, string gesturename) {
cout << gesturename << endl;
}
void clearData() {
for(int i = 0; i < 6; i++) {
data[i].clear();
}
}
void recData(){
vector<double> temp;
temp.push_back(ax);
temp.push_back(ay);
temp.push_back(az);
temp.push_back(roll);
temp.push_back(pitch);
temp.push_back(yaw);
temp.push_back(emgSamples[0]);
temp.push_back(emgSamples[1]);
temp.push_back(emgSamples[2]);
temp.push_back(emgSamples[3]);
temp.push_back(emgSamples[4]);
temp.push_back(emgSamples[5]);
temp.push_back(emgSamples[6]);
temp.push_back(emgSamples[7]);
data.push_back(temp);
int bufferSize = 200;
if(data.size() > bufferSize+1){
MatrixDouble window;
for(int i = 0; i < bufferSize; i++){
VectorDouble c;
for(int j = 0; j < 14; j++){
//c.push_back(data[data.size()-bufferSize+1][j]);
c.push_back(data[i][j]);
}
window.push_back(c);
}
if(!dtw1.predict(window)){
cerr << "Could not form a prediction." << endl;
}
UINT predictedClassLabel = dtw1.getPredictedClassLabel();
double maxLikelihood = dtw1.getMaximumLikelihood();
if(predictedClassLabel){
cout << "PREDICTED CLASS LABEL:" + gestureIdName[predictedClassLabel] << endl;
currentWord = gestureIdName[predictedClassLabel];
//onGesture(maxLikelihood, gesturenames[predictedClassLabel]);
temp.clear();
data.clear();
}
}
}
string getCurrentWord(){
return currentWord;
}
string currentWord;
vector<string> gesturenames;
std::map<int, string> gestureIdName;
vector<vector<double > > data;
bool onArm;
myo::Arm whichArm;
bool isUnlocked;
int roll_w, pitch_w, yaw_w;
float roll, pitch, yaw, ax, ay, az;
myo::Pose currentPose;
//vector<double> emgSamples;
array<int, 8> emgSamples;
};