-
Notifications
You must be signed in to change notification settings - Fork 0
/
emgGraphs.js
executable file
·158 lines (119 loc) · 3.74 KB
/
emgGraphs.js
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
//This tells Myo.js to create the web sockets needed to communnicate with Myo Connect
Myo.on('connected', function(){
console.log('connected');
this.streamEMG(true);
setInterval(function(){
if(realTimeUpdate){
updateGraph(rawData);
updateCompositeGraph(rawData);
}
}, 5)
})
Myo.connect('com.myojs.emgGraphs');
var rawData = [0,0,0,0,0,0,0,0];
Myo.on('emg', function(data){
rawData = data;
})
var rangeEMG = 150;
var resolutionEMG = 200;
var emgGraphs = [];
var emgCompositeGraphs = [];
var graphData = [
// Array(200) - init array with 200; quickly populating an array with default values:
Array.apply("Channel 1", Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply("Channel 2", Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply("Channel 3", Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply("Channel 4", Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply("Channel 5", Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply("Channel 6", Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply("Channel 7", Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply("Channel 8", Array(resolutionEMG)).map(Number.prototype.valueOf,0)
]
var compositeGraphData = [
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0),
Array.apply(null, Array(resolutionEMG)).map(Number.prototype.valueOf,0)
]
$(document).ready(function(){
emgGraphs = graphData.map(function(podData, podIndex){
return $('#pod' + podIndex).plot(formatFlotData(podData), {
colors: ['#8aceb5'],
xaxis: {
show: false,
min : 0,
max : resolutionEMG
},
yaxis : {
min : -rangeEMG,
max : rangeEMG,
},
grid : {
borderColor : "#8aceb5",
borderWidth : 1
},
selection: {
mode: "x"
}
}).data("plot");
});
emgCompositeGraphs = $('#emgComposite').plot( formatCompositeFlotData(graphData), {
colors: [ '#04fbec', '#ebf1be', '#c14b2a', '#8aceb5'],
xaxis: {
show: true,
min : 0,
max : resolutionEMG
},
yaxis : {
min : -rangeEMG,
max : rangeEMG,
},
grid : {
borderColor : "#427F78",
borderWidth : 1,
},
selection: {
mode: "x"
},
interaction: {
redrawOverlayInterval: 1
}
}).data("plot");
});
var formatFlotData = function(data){
return [data.map(function(val, index){
return [index, val]
})]
}
var updateGraph = function(emgData){
graphData.map(function(data, index){
graphData[index] = graphData[index].slice(1);
graphData[index].push(emgData[index]);
emgGraphs[index].setData(formatFlotData(graphData[index]));
emgGraphs[index].draw();
})
}
var formatCompositeFlotData = function(data){
return Object.keys(graphData).map(function(axis){
return {
label : 'Channel ' + axis,
data : graphData[axis].map(function(val, index){
return [index, val]
})
}
});
}
var updateCompositeGraph = function(emgData){
// calls a provided callback function once for each element in an array, in order, and constructs a new array from the results.
// compositeGraphData.map(function(data, index){
// compositeGraphData[index] = compositeGraphData[index].slice(1);
// compositeGraphData[index].push(emgData[index]);
emgCompositeGraphs.setData(formatCompositeFlotData(graphData));
emgCompositeGraphs.draw();
}
/*
*/