-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (101 loc) · 2.76 KB
/
server.js
File metadata and controls
115 lines (101 loc) · 2.76 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
var express = require('express')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var points = [];
var commands = [];
var doodlebot;
/*
* Calculates the angle ABC (in radians)
*
* A first point
* C second point
* B center point
*/
function findAngle(A,B,C) {
var AB = Math.sqrt(Math.pow(B[0]-A[0],2)+ Math.pow(B[1]-A[1],2));
var BC = Math.sqrt(Math.pow(B[0]-C[0],2)+ Math.pow(B[1]-C[1],2));
var AC = Math.sqrt(Math.pow(C[0]-A[0],2)+ Math.pow(C[1]-A[1],2));
return Math.acos((BC*BC+AB*AB-AC*AC)/(2*BC*AB));
}
/**
* Calculates Euclidian distance
*/
function findDistance(A,B) {
return Math.sqrt( Math.pow((A[0]-B[0]), 2) + Math.pow((A[1]-B[1]), 2) );
}
function generatePathCommands(id, points) {
if (points.length === 3) {
var angle = findAngle(points[0],points[1],points[2]);
angle = angle / 3.1415 * 360; //degrees!
angle = angle - 180; //bearing!
if (angle > 2) {
doodlebot.emit('command',{
id: id,
command: 'turn',
args: [angle]
});
}
var distance = findDistance(points[1],points[2]);
if (distance > 0.05) {
doodlebot.emit('command',{
id: id,
command: 'go',
args:['fwd',distance*50]
});
}
}
}
app.use('/static', express.static(__dirname + '/server/static'));
app.get('/', function(req, res){
res.sendFile( __dirname + '/server/webClient.html');
});
app.set('port', process.env.PORT || 3000);
http.listen(app.get('port'), function(){
console.log('listening on *:',app.get('port'));
});
io.on('connection', function (socket) {
console.log('connected');
for (var i = 0; i < commands.length; i++) {
socket.emit('command', commands[i]);
}
socket.on('IAMAROBOT', function () {
doodlebot = socket;
socket.emit('command',{
id: 'server',
command: 'marker',
args: ['down']
});
socket.emit('command',{
id: 'server',
command: 'go',
args: ['fwd',250]
});
socket.broadcast.emit('IAMAROBOT',{});
socket.on('disconnect', function () {
socket.broadcast.emit('ROBOTOFFLINE',{});
});
socket.on('finishedCommand', function (id) {
for (var i = 0; i < commands.length; i++) {
if (commands[i].id === id) {
commands = commands.splice(i, 1);
break;
}
}
socket.broadcast.emit('complete',id);
})
});
socket.on('command', function(cmd){
console.log('command: ', cmd);
commands.push(cmd);
io.sockets.emit('command', cmd);
});
socket.on('point', function(point) {
console.log('point: ', point);
points.push(point);
socket.broadcast.emit('point', point);
if (doodlebot) {
generatePathCommands(socket.id, points.slice(-3));
}
});
});