-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialController.js
81 lines (63 loc) · 1.61 KB
/
serialController.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
const socket = require('socket.io-client')('http://forwardboard.herokuapp.com/');
var SerialPort = require("serialport");
var serialport = new SerialPort("/dev/ttyUSB0", {
parser: SerialPort.parsers.readline('\n')
});
setInterval(function () {
sendCommand('left');
}, 1000);
TOUCH_THRESHOLD = 1000;
CUR_TOUCHING = false;
xThresholds = {
"left": 0.2,
"right": 0.8
};
HIST_NUM_VALS = 250;
var touchHistory = new Array(HIST_NUM_VALS);
serialport.on('open', function(){
console.log('Serial Port Opened');
serialport.on('data', function(data){
console.log(data);
data = data.replace(/[^\x00-\x7F]/g, "");
data = JSON.parse(data);
var xVal = mapXFromRaw(data.left, data.right);
console.log(xVal);
});
});
function detectSwipe(xVal) {
}
function interpretValues(data) {
if((data.left + data.right) / 2 > TOUCH_THRESHOLD) {
CUR_TOUCHING = true;
var cur_x = mapXFromRaw(data.left, data.right);
if(cur_x < xThresholds.left){
sendCommand('left');
} else if (cur_x > xThresholds.right) {
sendCommand('right');
}
appendNewX(mapXFromRaw(data.left, data.right));
} else {
CUR_TOUCHING = false;
appendNewX(-1);
}
console.log(touchHistory);
}
function mapXFromRaw(left, right) {
// Make more robust
x = (left - right) / (left + right);
if (x < 0) {
console.log("Negative x!", x);
x = 0;
}
return x;
}
function appendNewX(x) {
if(touchHistory.length >= HIST_NUM_VALS){
touchHistory.shift();
}
touchHistory.append(x);
}
function sendCommand(command) {
socket.emit('manual_control', command);
console.log("sent", command);
}