-
Notifications
You must be signed in to change notification settings - Fork 10
/
myo_2c_sfunc.cpp
195 lines (140 loc) · 5.68 KB
/
myo_2c_sfunc.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
#include <math.h>
#include <matrix.h>
#include <mex.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include "myo_class2a.hpp"
DataCollector dataCollector;
void matAssign (mxArray *A, mxArray *B, int Counter, int mRow, int nCol)
{
for (int i=0;i<nCol;i++)
{
*(mxGetPr (A)+Counter + mRow*i) = *(mxGetPr (B) + i);
}
}
mxArray *create_fill_vector3 (const myo::Vector3<float>&v)
{
mxArray *p = mxCreateNumericMatrix (1, 3, mxSINGLE_CLASS, mxREAL);
*(mxGetPr (p) + 0) = (v.x());
*(mxGetPr (p) + 1) = (v.y());
*(mxGetPr (p) + 2) = (v.z());
return p;
}
mxArray *create_fill_EMG (const std::array<int8_t, 8> emgData)
{
mxArray *p = mxCreateNumericMatrix (1, 8, mxDOUBLE_CLASS, mxREAL);
for (int i=0;i<8;i++)
{
*(mxGetPr (p) + i) = emgData[i];
}
return p;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
myo::Hub hUb("com.example.hello-myo");
hUb.addListener(&dataCollector);
myo::Myo* mYo = hUb.waitForMyo(10);
if (!mYo) {throw std::runtime_error("Unable to find a Myo!");}
// Enable EMG streaming
// mYo->setStreamEmg(myo::Myo::streamEmgEnabled);
mYo->unlock(myo::Myo::unlockHold);
hUb.setLockingPolicy(hUb.lockingPolicyNone);
const char *frame_field_names[] =
{
"Roll",
"Pitch",
"Yaw",
"accData",
"gyroData",
"emgData",
"onArm",
"isUnlocked",
"onWhichArm",
"whichPose",
"eCounter",
"aCounter",
"gCounter"
};
int frame_fields = sizeof (frame_field_names) / sizeof (*frame_field_names);
plhs[0] = mxCreateStructMatrix (1, 1, frame_fields, frame_field_names);
int counter=0;
int mRow=12000;
const mwSize dims[]={mRow,1};
const mwSize dimsV[]={mRow,3};
const mwSize dimsE[]={mRow,8};
mxArray *mxRoll = mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
mxArray *mxPitch = mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
mxArray *mxYaw = mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
mxArray *mxaccDataVec = mxCreateNumericArray (2, dimsV, mxDOUBLE_CLASS, mxREAL);
mxArray *mxgyroDataVec = mxCreateNumericArray (2, dimsV, mxDOUBLE_CLASS, mxREAL);
mxArray *mxEMGDataVec = mxCreateNumericArray (2, dimsE, mxDOUBLE_CLASS, mxREAL);
mxArray *mxOnArm = mxCreateLogicalArray (2, dims);
mxArray *mxWhichArm=mxCreateLogicalArray (2, dims);
mxArray *mxisUnlocked=mxCreateLogicalArray (2, dims);
mxArray *mxwPose = mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
mxArray *mxeCounter = mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
mxArray *mxaCounter = mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
mxArray *mxgCounter = mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
/* Check for proper number of input and output arguments */
if (nrhs !=0) {
mexErrMsgTxt("No input argument required.");
}
if(nlhs > 1){
mexErrMsgTxt("Too many output arguments.");
}
// int counter=1;
// // Finally we enter our main loop.
while (1)
{
// In each iteration of our main loop, we run the Myo event loop for a set number of milliseconds.
// In this case, we wish to update our display 20 times a second, so we run for 1000/20 milliseconds.
hUb.run(1);
// mYo->unlock(myo::Myo::unlockHold);
// get the frame
const Frame &f = dataCollector.getFrame();
// getFrame (nlhs, plhs);
*(mxGetPr (mxRoll) + counter) = f.roll;
*(mxGetPr (mxPitch) + counter) = f.pitch;
*(mxGetPr (mxYaw) + counter) = f.yaw;
*(mxGetLogicals (mxOnArm) + counter) = f.onArm;
*(mxGetPr (mxwPose) + counter) = f.wPose;
*(mxGetPr (mxeCounter) + counter) = f.ecounter;
*(mxGetPr (mxaCounter) + counter) = f.acounter;
*(mxGetPr (mxgCounter) + counter) = f.gcounter;
if(f.onArm)
{
*(mxGetLogicals (mxWhichArm) + counter) = (f.whichArm);
}
*(mxGetLogicals (mxisUnlocked) + counter) = (f.isUnlocked);
mxArray *accData = create_fill_vector3 (f.accData);
mxArray *gyroData = create_fill_vector3 (f.gyroData);
// mxArray *emgData = create_fill_EMG(f.emgData);
matAssign(mxaccDataVec, accData, counter, mRow, 3);
matAssign(mxgyroDataVec, gyroData, counter, mRow, 3);
mxArray *emgData = create_fill_EMG(f.emgData);
matAssign(mxEMGDataVec, emgData, counter, mRow, 8);
counter++;
if( counter>= mRow)//||f.isUnlocked==false)
{
// terminate the loop
break;
}
// mexPrintf("Counter: %f\n", counter);
}
mxSetFieldByNumber (plhs[0], 0, 0, mxRoll);
mxSetFieldByNumber (plhs[0], 0, 1, mxPitch);
mxSetFieldByNumber (plhs[0], 0, 2, mxYaw);
mxSetFieldByNumber (plhs[0], 0, 3, mxaccDataVec);
mxSetFieldByNumber (plhs[0], 0, 4, mxgyroDataVec);
mxSetFieldByNumber (plhs[0], 0, 5, mxEMGDataVec);
mxSetFieldByNumber (plhs[0], 0, 6, mxOnArm);
mxSetFieldByNumber (plhs[0], 0, 7, mxisUnlocked);
mxSetFieldByNumber (plhs[0], 0, 8, mxWhichArm);
mxSetFieldByNumber (plhs[0], 0, 9, mxwPose);
mxSetFieldByNumber (plhs[0], 0, 10, mxeCounter);
mxSetFieldByNumber (plhs[0], 0, 11, mxaCounter);
mxSetFieldByNumber (plhs[0], 0, 12, mxgCounter);
hUb.removeListener(&dataCollector);
}