-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (123 loc) · 3.3 KB
/
app.js
File metadata and controls
133 lines (123 loc) · 3.3 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
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server); // module for web socket.
const port = 3000;
const bodyParser = require('body-parser');
/*
// gpio related variables
const Gpio = require('pigpio').Gpio;
const motor1_pwm = new Gpio(4, {mode: Gpio.OUTPUT});
const motor1_1 = new Gpio(17, {mode: Gpio.OUTPUT});
const motor1_2 = new Gpio(27, {mode: Gpio.OUTPUT});
const motor2_pwm = new Gpio(9, {mode: Gpio.OUTPUT});
const motor2_1 = new Gpio(22, {mode: Gpio.OUTPUT});
const motor2_2 = new Gpio(10, {mode: Gpio.OUTPUT});
const MOTOR1 = 1;
const MOTOR2 = 2;
var lightValue = 0;
*/
var timer;
app.get('/', (req, res) => res.sendFile(__dirname + '/index.html'));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/test', function(req,res){
console.log('received object');
console.log(req.body);
res.end();
});
app.post('/move', function(req,res){
console.log('received object');
console.log(req.body);
if(req.body.move){
console.log('huzzah!');
console.log(req.body.move);
}
res.end();
});
io.on('connection', function(socket){
socket.on('disconnect',function() {
setTimeout(function() {
motorsOff();
console.log('lost connection');
}, 200);
});
// gamepad controller
socket.on('realtime', function(data) {
if(data.control && data.control.length === 6){
rawValue1 = parseFloat(data.control[0]);
rawValue2 = parseFloat(data.control[3]);
if(rawValue1 > .15 && rawValue1 <= 1){
lightValue = parseInt( (rawValue1 - .15) * 300 );
motor1_pwm.pwmWrite(lightValue);
motor1_1.pwmWrite( parseFloat(data.control[1])*255 );
motor1_2.pwmWrite( parseFloat(data.control[2])*255 );
}
else{
setLeft(0,0,0);
}
if(rawValue2 > .15 && rawValue2 <= 1){
lightValue = parseInt( (rawValue2 - .15) * 300 );
motor2_pwm.pwmWrite(lightValue);
motor2_1.pwmWrite( parseFloat(data.control[4])*255 );
motor2_2.pwmWrite( parseFloat(data.control[5])*255 );
}
else{
setRight(0,0,0);
}
}
});
socket.on('light', function(data) {
});
/*
// keyboard control
socket.on('keyboard', function(data) {
if(data.move === 0){
keyboardEnabled = true;
setLeft(255, 0, 255);
setRight(255, 0, 255);
}
else if(data.move === 2){
keyboardEnabled = true;
setLeft(255, 255, 0);
setRight(255, 255, 0);
}
else if(data.move === 1){
keyboardEnabled = true;
setLeft(255, 0, 255);
setRight(255, 255, 0);
}
else if(data.move === -1){
keyboardEnabled = true;
setLeft(255, 255, 0);
setRight(255, 0, 255);
}
else if(data.move == 3){
motorsOff();
}
else
motorsOff();
clearTimeout(timer);
timer = setTimeout( motorsOff, 500);
});*/
});
/*
function motorsOff() {
setLeft(0,0,0);
setRight(0,0,0);
}
function setLeft(x,y,z){
motor1_pwm.pwmWrite(x);
motor1_1.pwmWrite(y);
motor1_2.pwmWrite(z);
}
function setRight(x,y,z){
motor2_pwm.pwmWrite(x);
motor2_1.pwmWrite(y);
motor2_2.pwmWrite(z);
}
*/
app.use(express.static('public'));
server.listen(port, () => console.log('Drawbot listening on port ' + port));