-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmgSystem.pde
More file actions
256 lines (208 loc) · 7.05 KB
/
EmgSystem.pde
File metadata and controls
256 lines (208 loc) · 7.05 KB
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
interface IEmgManager {
boolean registerAction(String action);
boolean registerAction(String action, int sensorID);
boolean loadCalibration(String calibrationFile);
void saveCalibration(String calibrationFile);
void setSensitivity(String action, float value);
void setMinimumActivationThreshold(String action, float value);
int getSensor(String action);
float getSensitivity(String action);
float getMinimumActivationThreshold(String action);
boolean isCalibrated();
HashMap<String, Float> poll();
HashMap<String, Float> pollIgnoringControlStrategy();
HashMap<String, Float> pollRaw();
void startEmgLogging();
void flushEmgLog();
void stopEmgLogging();
}
class EmgManager implements IEmgManager {
LibMyoProportional myoProportional;
EmgManager() throws MyoNotDetectectedError {
myoProportional = new LibMyoProportional(mainObject);
}
boolean registerAction(String action) {
try {
myoProportional.registerAction(string2Action(action));
} catch (CalibrationFailedException e) {
return false;
}
return true;
}
boolean registerAction(String label, int sensorID)
{
try {
myoProportional.registerAction(string2Action(label), sensorID);
} catch (CalibrationFailedException e) {
return false;
}
return true;
}
/*
* Returns sensor readings that are normalized to current sensitivity settings,
* restricted to the range [0.0, 1.0], and processed using the current control
* policy settings (i.e., DIFFERENCE, MAXIMUM, FIRST_OVER)
*/
HashMap<String, Float> poll() {
EmgSamplingPolicy p = options.getIOOptions().getEmgSamplingPolicy();
HashMap<Action, Float> results = myoProportional.pollAndTrim(emgSamplingPolicy2Policy(p));
HashMap<String, Float> toReturn = new HashMap<String, Float>();
for (Action a : results.keySet()) {
toReturn.put(action2String(a), results.get(a));
}
return toReturn;
}
/*
* Returns sensor readings that are normalized to current sensitivity
* settings, restricted to the range [0.0, 1.0], but are not affected by the
* current control policy settings (i.e., DIFFERENCE, MAXIMUM, FIRST_OVER)
*/
HashMap<String, Float> pollIgnoringControlStrategy() {
HashMap<Action, Float> results = myoProportional.pollAndTrim(Policy.RAW);
HashMap<String, Float> toReturn = new HashMap<String, Float>();
for (Action a : results.keySet()) {
toReturn.put(action2String(a), results.get(a));
}
return toReturn;
}
/*
* Returns sensor readings that are normalized to current sensitivity
* settings, but are not restricted to a particular range (i.e., readings above
* 1.0 are perfectly legal, and are not affected by the current control policy
* settings (i.e., DIFFERENCE, MAXIMUM, FIRST_OVER)
*/
HashMap<String, Float> pollRaw() {
HashMap<Action, Float> results = myoProportional.poll(Policy.RAW);
HashMap<String, Float> toReturn = new HashMap<String, Float>();
for (Action a : results.keySet()) {
toReturn.put(action2String(a), results.get(a));
}
return toReturn;
}
boolean isCalibrated() {
return myoProportional.isCalibrated();
}
boolean loadCalibration(String calibrationFile) {
if (!fileExists(calibrationFile))
return false;
try {
myoProportional.loadCalibrationSettings(calibrationFile);
} catch (CalibrationFailedException e) {
return false;
}
return true;
}
void saveCalibration(String calibrationFile) {
myoProportional.writeCalibrationSettings(calibrationFile);
}
void setSensitivity(String action, float value) {
// should this be the inverse?
myoProportional.setSensitivity(string2Action(action), value);
}
void setMinimumActivationThreshold(String action, float value) {
myoProportional.setMinimumActivationThreshold(string2Action(action), value);
}
int getSensor(String action) {
Map<Action, SensorConfig> sensorConfigs = myoProportional.getCalibrationSettings();
if (sensorConfigs.containsKey(string2Action(action)))
return sensorConfigs.get(string2Action(action)).sensorID;
else
return 0;
}
float getSensitivity(String action) {
// should this be the inverse?
Map<Action, SensorConfig> sensorConfigs = myoProportional.getCalibrationSettings();
if (sensorConfigs.containsKey(string2Action(action)))
return sensorConfigs.get(string2Action(action)).maxReading;
else
return 0.0;
}
float getMinimumActivationThreshold(String action) {
Map<Action, SensorConfig> sensorConfigs = myoProportional.getCalibrationSettings();
if (sensorConfigs.containsKey(string2Action(action)))
return sensorConfigs.get(string2Action(action)).minimumActivationThreshold;
else
return 0.0;
}
void startEmgLogging() {
myoProportional.enableEmgLogging(sketchPath() + "/data/emg.csv");
}
void flushEmgLog() {
myoProportional.flushEmgLog();
}
void stopEmgLogging() {
myoProportional.disableEmgLogging();
}
private Policy emgSamplingPolicy2Policy(EmgSamplingPolicy policy) {
switch (policy) {
case MAX: return Policy.MAXIMUM;
case DIFFERENCE: return Policy.DIFFERENCE;
case FIRST_OVER: return Policy.FIRST_OVER;
default: return Policy.RAW;
}
}
private Action string2Action(String s) {
switch (s) {
case LEFT_DIRECTION_LABEL: return Action.LEFT;
case RIGHT_DIRECTION_LABEL: return Action.RIGHT;
case JUMP_DIRECTION_LABEL: return Action.IMPULSE;
// TODO
default: return Action.LEFT;
}
}
private String action2String(Action a) {
switch (a) {
case LEFT: return LEFT_DIRECTION_LABEL;
case RIGHT: return RIGHT_DIRECTION_LABEL;
case IMPULSE: return JUMP_DIRECTION_LABEL;
// TODO
default: return LEFT_DIRECTION_LABEL;
}
}
private boolean fileExists(String filename) {
File file = new File("data/" + filename);
return file.exists();
}
}
class NullEmgManager implements IEmgManager {
boolean registerAction(String label) {
return false;
}
boolean registerAction(String label, int sensorID) {
return false;
}
HashMap<String, Float> poll() {
HashMap<String, Float> toReturn = new HashMap<String, Float>();
toReturn.put(LEFT_DIRECTION_LABEL, 0.0);
toReturn.put(RIGHT_DIRECTION_LABEL, 0.0);
toReturn.put(JUMP_DIRECTION_LABEL, 0.0);
return toReturn;
}
HashMap<String, Float> pollIgnoringControlStrategy() {
return poll();
}
HashMap<String, Float> pollRaw() {
return poll();
}
boolean isCalibrated() {
return false;
}
boolean loadCalibration(String calibrationFile) {
return false;
}
void saveCalibration(String calibrationFile) {} // no-op
void setSensitivity(String action, float value) {} // no-op
void setMinimumActivationThreshold(String action, float value) {} // no-op
int getSensor(String action) {
return 0;
}
float getSensitivity(String action) {
return 0.0;
}
float getMinimumActivationThreshold(String action) {
return 0.0;
}
void startEmgLogging() {} // no-op
void flushEmgLog() {} // no-op
void stopEmgLogging() {} // no-op
}