-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·149 lines (121 loc) · 3.22 KB
/
app.js
File metadata and controls
executable file
·149 lines (121 loc) · 3.22 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var motion_dir = '/tmp/motion';
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
// Public files
app.use(express.static(__dirname + '/public'));
server.listen(3000);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// Watch the motion dir and return the latest image
/*var watch = require('watch');
var fs = require('fs');
var currentImage = null;
watch.createMonitor(motion_dir, function (monitor) {
//monitor.files['/home/mikeal/.zshrc'] // Stat object for my zshrc.
monitor.on("created", function (f, stat) {
// Handle file changes
console.log(f);
console.log(stat);
currentImage = f;
});
});
// routing
app.get('/camera', function (req, res) {
if (currentImage != null) {
var img = fs.readFileSync(currentImage);
res.writeHead(200, {'Content-Type': 'image/jpg' });
res.end(img, 'binary');
}
return res.end();
});*/
// Socket.io logic
io.sockets.on('connection', function (socket) {
// when the client emits 'update', this listens and executes
socket.on('update', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
//io.sockets.emit('updatechat', socket.username, data);
motor_move(data);
//console.log(data);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
});
});
var five = require('johnny-five'),
board, motor, led;
board = new five.Board();
var motor_pins = {'left': [3, 2], 'right': [8, 7]}; // {forward, backward}
var motors = {'left': new Array(), 'right': new Array()};
board.on("ready", function() {
// Add LED
led = new five.Led({
pin: 13
});
// Add motors
for (var side in motor_pins) {
for (var i = 0; i < motor_pins[side].length; i++) {
motors[side][i] = new five.Motor({
pin: motor_pins[side][i]
});
board.repl.inject({
motor: motors[side][i]
});
motors[side][i].on("start", function( err, timestamp ) {
console.log( "start", timestamp );
led.on();
});
// "stop" events fire when the motor is started.
motors[side][i].on("stop", function( err, timestamp ) {
console.log( "stop", timestamp );
led.off();
});
}
}
});
function motor_move(d){
console.log(d);
// Stop
if (d.dy == 0 && d.dx == 0) {
motors.left[0].stop();
motors.left[1].stop();
motors.right[0].stop();
motors.right[1].stop();
}
// Forward or backwards
else if (Math.abs(d.dy) > Math.abs(d.dx)) {
if (d.dy > 0) {
motors.left[0].start();
motors.right[0].start();
motors.left[1].stop();
motors.right[1].stop();
}
else {
motors.left[1].start();
motors.right[1].start();
motors.left[0].stop();
motors.right[0].stop();
}
}
// Left or right
else {
if (d.dx > 0) {
// Right
motors.left[0].start();
motors.right[1].start();
motors.left[1].stop();
motors.right[0].stop();
}
else {
// Left
motors.left[1].start();
motors.right[0].start();
motors.left[0].stop();
motors.right[1].stop();
}
}
}