-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp12.js
248 lines (204 loc) · 6.87 KB
/
app12.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http');
var app = express(); //- ONLY FOR NEWER 3.x express
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var dualShock = require('dualshock-controller');
var async = require('async'),
five = require("johnny-five"),
raspi = require('raspi-io'),
tank = {},
_leftMotorFront = 7,
_leftMotorBack = 11,
_rightMotorFront = 12,
_rightMotorBack = 13,
_turnTime = 100,
_speed = 1; //only possible when using arduino when we can use analogWrite instead of digitalWrite
//this is to use the Pi's GPIO pins:
var board = new raspi();
//var board = new five.Board({
// io: new raspi()
//});
board.on("ready", function() {
this.pinMode(7, board.MODES.OUTPUT);
this.pinMode(11, board.MODES.OUTPUT);
this.pinMode(12, board.MODES.OUTPUT);
this.pinMode(13, board.MODES.OUTPUT);
async.parallel([function(callback) { board.digitalWrite(_leftMotorFront, 0) },
function(callback) { board.digitalWrite(_leftMotorBack, 0) },
function(callback) { board.digitalWrite(_rightMotorFront, 0) },
function(callback) { board.digitalWrite(_rightMotorBack, 0) }],
function(err, results) {
res.end();
}
);
});
// Access server through port 80
server.listen(3000);
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded()); // to support URL-encoded bodies
// Set '/public' as the static folder. Any files there will be directly sent to the viewer
app.use(express.static(__dirname + '/public'));
// Set index.html as the base file
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index4.html');
});
tank.initPins = function(){
console.log("Tank ready");
};
tank.moveForward = function(){
console.log("Forward function");
async.parallel([function(callback) { board.digitalWrite(_leftMotorFront, _speed) },
function(callback) { board.digitalWrite(_leftMotorBack, 0) },
function(callback) { board.digitalWrite(_rightMotorFront, _speed) },
function(callback) { board.digitalWrite(_rightMotorBack, 0) }],
function(err, results) {
res.end();
}
);
};
tank.moveBackward = function(){
console.log("Backward function");
async.parallel([function(callback) { board.digitalWrite(_leftMotorFront, 0) },
function(callback) { board.digitalWrite(_leftMotorBack, _speed) },
function(callback) { board.digitalWrite(_rightMotorFront, 0) },
function(callback) { board.digitalWrite(_rightMotorBack, _speed) }],
function(err, results) {
res.end();
}
);
};
tank.turnLeft = function(){
console.log("Turn left function");
async.parallel([function(callback) { board.digitalWrite(_leftMotorFront, 0) },
function(callback) { board.digitalWrite(_leftMotorBack, _speed) },
function(callback) { board.digitalWrite(_rightMotorFront, _speed) },
function(callback) { board.digitalWrite(_rightMotorBack, 0) }],
function(err, results) {
res.end();
}
);
};
tank.turnLeftShort = function(){
tank.turnLeft();
setTimeout(tank.stopAllMotors, _turnTime);
};
tank.turnRight = function(){
console.log("Turn right function");
async.parallel([function(callback) { board.digitalWrite(_leftMotorFront, _speed) },
function(callback) { board.digitalWrite(_leftMotorBack, 0) },
function(callback) { board.digitalWrite(_rightMotorFront, 0) },
function(callback) { board.digitalWrite(_rightMotorBack, _speed) }],
function(err, results) {
res.end();
}
);
};
tank.turnRightShort = function(){
tank.turnRight();
setTimeout(tank.stopAllMotors, _turnTime);
};
tank.stopAllMotors = function(){
console.log("Stop function");
async.parallel([function(callback) { board.digitalWrite(_leftMotorFront, 0) },
function(callback) { board.digitalWrite(_leftMotorBack, 0) },
function(callback) { board.digitalWrite(_rightMotorFront, 0) },
function(callback) { board.digitalWrite(_rightMotorBack, 0) }],
function(err, results) {
res.end();
}
);
};
io.sockets.on('connection', function(socket) {
console.log("Socket.IO connected");
socket.emit('robot connected', { data: 'Connected' });
socket.on('robot command', function (data) {
//processRobotCommand (data.data);
console.log("Received command: " + data.command);
var command = data.command;
if (command == 'forward') {
tank.moveForward();
}
if (command == 'reverse') {
tank.moveBackward();
}
if (command == 'turn-left') {
tank.turnLeftShort();
}
if (command == 'turn-right') {
tank.turnRightShort();
}
if (command == 'stop') {
tank.stopAllMotors();
}
});
});
//pass options to init the controller.
var controller = dualShock(
{
//you can use a ds4 by uncommenting this line.
//config: "dualshock4-generic-driver",
//if using ds4 comment this line.
config : "dualShock3",
//smooths the output from the acelerometers (moving averages) defaults to true
accelerometerSmoothing : true,
//smooths the output from the analog sticks (moving averages) defaults to false
analogStickSmoothing : false
});
//make sure you add an error event handler
controller.on('error', function(data) {
console.log("Controller has done a whoopsie")
//...someStuffDidNotWork();
});
//add event handlers:
controller.on('left:move', function(data) {
//...doStuff();
});
controller.on('right:move', function(data) {
//...doStuff();
});
controller.on('connected', function(data) {
console.log("dualShock3 connected")
});
controller.on('dpadUp:press', function (data) {
tank.moveForward();
});
controller.on('dpadUp:release', function (data) {
tank.stopAllMotors();
});
controller.on('dpadLeft:press', function (data) {
tank.turnLeft();
});
controller.on('dpadLeft:release', function (data) {
tank.stopAllMotors();
});
controller.on('dpadDown:press', function (data) {
tank.moveBackward();
});
controller.on('dpadDown:release', function (data) {
tank.stopAllMotors();
});
controller.on('dpadRight:press', function (data) {
tank.turnRight();
});
controller.on('dpadRight:release', function (data) {
tank.stopAllMotors();
});
//controller status
//as of version 0.6.2 you can get the battery %, if the controller is connected and if the controller is charging
controller.on('battery:change', function (value) {
//...doStuff();
});
controller.on('connection:change', function (value) {
console.log("Connection state changed")
});
controller.on('charging:change', function (value) {
//...doStuff();
});
//connect the controller
controller.connect();
tank.initPins();