-
Notifications
You must be signed in to change notification settings - Fork 0
/
roverControlServer.js
379 lines (327 loc) · 9.79 KB
/
roverControlServer.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var sys = require("sys"),
var my_http = require("http");
var path = require("path"),
var url = require("url"),
var filesys = require("fs");
var eventsA = require("events");
var serialport = require("serialport");
var SerialPort = serialport.SerialPort; // localize object constructor
var webPort = 8084;
var robotData = new roverDataInit();
var comPort = "/dev/tty.usbserial-A900fwHn";
var sp = new SerialPort(
robotData.getCommPort(),
{ parser: serialport.parsers.readline(), baudrate: 9600,},
{},
messagesError);
sp.on("open", function (error) {
if ( error ) {
console.log('failed to open: '+error);
process.exit(1);
}
else {
console.log ("connection %s is open and ready", comPort)
}
sp.write(0x80, function (error) {
if (error) {
console.log("Serial Write Init write failed %s", error);
process.exit(1);
}
});
sp.write('123456\r', function (error) {
if (error) {
console.log ("Serial write test 123456 failed to write %s", error);
process.exit(1);
}
else {
console.log ("communitcates between base and robot are ready.");
}
});
});
// create the webservice listener on the webport
// for local testing localhost:webport/index.html
my_http.createServer(function(request,response) {
// parsing paths requested helps give html response
var my_path = url.parse(request.url).pathname;
var full_path = path.join(process.cwd(),my_path);
path.exists(full_path,function(exists) {
if(!exists) {
response.writeHeader(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
}
else {
filesys.readFile(full_path, function(err, file) {
console.log("reading file %s", full_path);
if(err) {
response.writeHeader(500, {"Content-Type": "text/plain"});
response.write("please select a project\n");
response.write("The is the base directory for all.\n");
response.end();
}
else {
var _get = url.parse(request.url, true).query;
response.writeHeader(200);
if (_get.roverData && !_get.direction) {
console.log("\n");
var sendToBrowser = JSON.stringify(robotData);
console.log(sendToBrowser)
response.write (sendToBrowser);
}
else if (_get.engineUpDate) {
if (_get.engineUpDate == "left") {
robotData.setEngineLeft(_get.engine);
}
else if (_get.engineUpDate == "power"){
robotData.setEngineImpulse(_get.engine);
}
else {
robotData.setEngineRight(_get.engine);
}
var sendToBrowser = JSON.stringify(robotData);
response.write (sendToBrowser);
}
else if (!_get.stopevent && _get.direction && !_get.roverData) {
//console.log("direction request");
robotData.setLastCommand(_get.direction);
var sendToBrowser = JSON.stringify(robotData);
response.write (sendToBrowser);
}
else if (_get.stopevent == 1) {
//console.log("stop event");
robotData.setLastCommand("stop")
var sendToBrowser = JSON.stringify(robotData );
response.write (sendToBrowser);
}
else {
response.write(file);
}
response.end();
}
});
}
});
sp.on('data', function (arduinoData) {
//console.log("\n\n\n----\ntest serial from Arduino \n------ \n\n\n")
if (arduinoData.length < 2) {
return;
}
var ArduinoString = arduinoData.toString();
var re = /(ALERT|Rover|data|radar|ready6cmd)/i;
var match = re.exec(ArduinoString);
//console.log ("data recieved ->" + ArduinoString + "<- aaa ");
//console.log ("data recieved ->" + arduinoData + "<- aaa ");
if (match) {
//console.log ("data recieved ->" + ArduinoString + "<- aaa ");
matchResults = match[0].toLowerCase();
if (matchResults == "alert") {
var splitResults = ArduinoString.split(",");
//console.log("Alert data " + splitResults.length + " " + data);
robotData.setAlert(splitResults[1]);
robotData.setRange(splitResults[2]);
}
else if (matchResults == "rover") {
var splitResults = ArduinoString.split(",");
console.log("rover data " + splitResults.length + " " + ArduinoString);
robotData.setRoverInfo(ArduinoString);
robotData.setLastCommand(splitResults[1]);
robotData.setRange(splitResults[5]);
robotData.setEngineLeft(splitResults[2]);
robotData.setEngineRight(splitResults[3]);
robotData.setEngineImpulse(splitResults[4]);
robotData.setAlert("");
}
else if (matchResults == "radar") {
robotData.setRadarData(ArduinoString);
}
else if (matchResults == "data") {
robotData.setRoverData(ArduinoString);
}
else if (matchResults == "ready4cmd") {
console.log ("data recieved ->" + match + "<- aaa n");
robotData.setReady4Command(true);
}
else {
//console.log("->unmatched data ", match[0], data);
}
}
match = "";
});
request.on('end', function () {
var _get = url.parse(request.url, true).query;
var sendToArduino = "";
if (_get.direction) {
sendToArduino = new String ( _get.direction).toLowerCase() ;
robotData.setLastCommand(sendToArduino);
robotData.setReady4Command(false);
}
else if (_get.engineUpDate) {
var engineSend = "engine" + _get.engineUpDate + ":" + _get.engine + "\r";
sendToArduino = new String (engineSend).toLowerCase() ;
}
else if (_get.roverData) {
//console.log("roverData xxxxxxxxxxx");
sendToArduino = new String ("roverData").toLowerCase() + "\r";
}
else if (_get.radarData) {
//console.log("radardata xxxxxxxxxxx");
sendToArduino = new String ("radardata").toLowerCase() + "\r";
console.log(sendToArduino);
}
else {
//console.log ("aaaa no data");
return;
}
//console.log ("sending ... we hope. ->> " + sendToArduino + "<--- ");
sendToArduino = sendToArduino + "\r";
sp.write(sendToArduino , function(err, results) {
if (err) {
console.log("we have an issue writing data to the rover %s", err);
//REWRITE set a message for feedback to user of error
}
sp.drain(function(err, result){
if (err) {
console.log("we have an issue draining the buffer on write %s", err);
// REWRITE again send message to user throw web
} else {
console.log (result);
}
});
});
});
}).listen(webPort);
sys.puts("Server Running on " + webPort);
//////////////////////
//
// Functions
//
function roverDataInit() {
this.lastCommand = "starting";
this.status = true;
this.heart = false;
this.range = 33;
this.comm = "/dev/tty.usbserial-A900fwHn";
this.alert = "";
this.roverMessage = "";
this.roverData = "";
this.leftEngine = 255;
this.rightEngine = 255;
this.engineImpulse = 100;
this.radarData = "";
this.Ready4Command = "ready";
this.setReady4Command=function(bool){
if (bool == true) {
this.Ready4Command = "ready";
}
else {
this.Ready4Command = "busy";
}
}
this.getReady4Command = function(){
return this.Ready4Command;
}
this.setLastCommand=function(strValue){
this.lastCommand=strValue;
}
this.getLastCommand = function(){
return this.lastCommand;
}
this.setRadarData=function(strValue){
//console.log("reading roverdata" + strValue);
this.radarData=strValue;
}
this.getRadarData = function(){
//console.log("getting radar data");
return this.radarData;
}
this.setHeartBeat = function (strValue) {
this.Status = signal;
}
this.getHeartBeat = function() {
return this.Status;
}
this.setRange=function(strValue) {
this.range = strValue;
}
this.getRange=function(strValue) {
return this.range;
}
this.setAlert=function(strValue) {
this.alert = strValue;
}
this.getAlert=function(strValue) {
return this.alert;
}
this.setRoverInfo=function(strValue) {
this.roverMessage = strValue;
}
this.getRoverInfo=function(strValue) {
return this.roverMessage;
}
this.setRoverData=function(strValue) {
this.roverData = strValue;
};
this.getRoverData=function(strValue) {
return this.roverData;
}
this.setEngineLeft=function(strValue) {
this.leftEngine= strValue;
}
this.getEngineLeft=function(strValue) {
return this.leftEngine;
}
this.setEngineRight=function(strValue) {
this.rightEngine = strValue;
}
this.getEngineRight=function(strValue) {
return this.rightEngine;
}
this.setEngineImpulse = function(strValue) {
this.engineImpulse = strValue;
}
this.getEngineImpulse = function(strValue) {
return this.engineImpulse;
}
this.setCommPort = function(strValue) {
console.log("setting comm port" + strValue);
this.comm = strValue;
}
this.getCommPort = function(strValue) {
return this.comm;
}
console.log ("done init");
}
// REWRITE
// I think at the time this was ment to be attached or extend serialport but its useless
function messagesError (error) {
console.log ("\nThere was an error with Serial Port %s", error);
console.log ("Exiting...");
process.exit(1);
}
////////////////////////////////
//
// Junk or notes not sure which
//
/* serialport.list(function (err, ports) {
var FTDIFlag = false;
ports.forEach(function(port) {
if (ports.manufacturer == "FTDI") {
FTDIFlag = true;
robotData.setCommPort(ports.comName);
console.log(ports.comName + "-----");
return;
}
if (FTDIFlag == false && ports.vendorId == '0x2341') {
robotData.setCommPort(ports.comName);
//console.log(port.comName);
}
//console.log(port);
//console.log(port.comName);
//console.log(port.vendorId);
});
var test = robotData.getCommPort();
console.log("test --->");
console.log(test);
});
*/
// this try is not catching the error and I am still working on the error event