-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpu.js
132 lines (118 loc) · 3.86 KB
/
mpu.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
var socket;
var firstconnect = true,
fs = 8000,
Ts = 1/fs*1000,
samples = 80,
plotTop,
samplesCollected = 0,
globalDataX = [],globalDataY = [],globalDataZ = [];
globalDataX[samples] = 0,globalDataY[samples] = 0,globalDataZ[samples] = 0;
globalDataXg = [],globalDataYg = [],globalDataZg = [];
globalDataXg[samples] = 0,globalDataYg[samples] = 0,globalDataZg[samples] = 0;
function connect() {
if(firstconnect) {
socket = io.connect(null);
socket.emit('mpustart',1);
socket.on('mpu',refreshmpu);
firstconnect = false;
}
else {
socket.socket.reconnect();
}
function disconnect() {
socket.disconnect();
}
}
connect();
function refreshmpu(data){
$("#0").html(data[0]);
$("#1").html(data[1]);
$("#2").html(data[2]);
$("#3").html(data[3]);
$("#4").html(data[4]);
$("#5").html(data[5]);
$("#0g").html(Math.round(1000*data[0]/16384.0)/1000 + "g");
$("#1g").html(Math.round(1000*data[1]/16384.0)/1000 + "g");
$("#2g").html(Math.round(1000*data[2]/16384.0)/1000 + "g");
$("#3r").html(Math.round(1000*data[3]/131.0)/1000 + "deg/s");
$("#4r").html(Math.round(1000*data[4]/131.0)/1000 + "deg/s");
$("#5r").html(Math.round(1000*data[5]/131.0)/1000 + "deg/s");
if (samplesCollected < samples) {
globalDataX[samplesCollected] = [samplesCollected*Ts, Math.round(1000*data[0]/16384.0)/1000];
globalDataY[samplesCollected] = [samplesCollected*Ts, Math.round(1000*data[1]/16384.0)/1000];
globalDataZ[samplesCollected] = [samplesCollected*Ts, Math.round(1000*data[2]/16384.0)/1000];
globalDataXg[samplesCollected] = [samplesCollected*Ts, Math.round(1000*data[3]/131.0)/1000];
globalDataYg[samplesCollected] = [samplesCollected*Ts, Math.round(1000*data[4]/131.0)/1000];
globalDataZg[samplesCollected] = [samplesCollected*Ts, Math.round(1000*data[5]/131.0)/1000];
samplesCollected++;
plotTop.setData([globalDataX,globalDataY,globalDataZ]);
plotTop.draw();
plotBottom.setData([globalDataXg,globalDataYg,globalDataZg]);
plotBottom.draw();
} else {
samplesCollected = 0;
plotTop.setData([globalDataX,globalDataY,globalDataZ]);
plotTop.draw();
plotBottom.setData([globalDataXg,globalDataYg,globalDataZg]);
plotBottom.draw();
}
}
$(function () {
function initPlotData() {
// zip the generated y values with the x values
var result = [];
for (var i = 0; i <= samples; i++)
result[i] = [i*Ts, 0];
return result;
}
// setup control widget
var updateInterval = 100;
// setup plot
var optionsTop = {
series: {
shadowSize: 0, // drawing is faster without shadows
points: { show: false},
lines: { show: true, lineWidth: 3}
},
yaxis: { min: -2, max: 2 },
xaxis: { show: true },
legend: { position: "ne" }
};
var optionsBottom = {
series: {
shadowSize: 0, // drawing is faster without shadows
points: { show: false},
lines: { show: true, lineWidth: 3}
},
yaxis: { min: -100, max: 100 },
xaxis: { show: true },
legend: { position: "ne" }
};
console.log("setting up plot");
plotTop = $.plot($("#plotTop"),
[
{ data: initPlotData(),
label: "X" },
{ data: initPlotData(),
label: "Y" },
{ data: initPlotData(),
label: "Z" }
],
optionsTop );
plotBottom = $.plot($("#plotBottom"),
[
{ data: initPlotData(),
label: "X" },
{ data: initPlotData(),
label: "Y" },
{ data: initPlotData(),
label: "Z" }
],
optionsBottom );
// Request data every updateInterval ms
function update() {
socket.emit('mpustart', 1);
setTimeout(update, updateInterval);
}
update();
});