forked from andySigler/nodela
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodela.js
executable file
·521 lines (404 loc) · 12 KB
/
nodela.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
console.log('');
console.log('');
console.log('');
console.log('\t+-+-+-+-+-+-+');
console.log('\t N O D E L A');
console.log('\t+-+-+-+-+-+-+');
console.log('');
console.log('');
console.log('');
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
var rolandPortName = 'com3';
var sys = require('sys')
var childProcess = require('child_process');
var _test = false;
var _windows = true;
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
var http_port = 8080;
var filePath = __dirname + '/www';
var http = require('http');
var fs = require('fs');
// create the HTTP server to serve the file
var my_HTTPServer = http.createServer(function (request, response) {
if(request.url==='/') request.url = '/index.html';
fs.readFile(filePath + request.url, function (error, my_HTML) {
if(error) {
my_HTML = JSON.stringify(error);
}
response.writeHead(200);
response.write(my_HTML);
response.end();
});
});
my_HTTPServer.listen(http_port);
console.log('HTTP server started at "http://localhost:' + http_port+'"');
console.log('');
console.log('To quit, close this terminal window');
var ws = require('ws');
var execTemp = childProcess.exec;
var tempCommand = _windows ? 'start' : 'open';
execTemp(tempCommand+' http://localhost:'+http_port,function(err,stdin,stdout){
if(err) {
console.log('');
console.log('Error opening a browser window...');
console.log('Please open a browser and go to "http://localhost:'+http_port+'"');
}
});
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
var WebSocketServer = require('ws').Server;
var socketServer = new WebSocketServer({'server':my_HTTPServer});
var theSocket = undefined;
socketServer.on('connection',function(socket){
if(theSocket===undefined){
theSocket = socket;
theSocket.on('message',function(data){
data = JSON.parse(data);
if(data.type && handlers[data.type] && typeof handlers[data.type]==='function'){
var response = handlers[data.type](data.data);
if(response){
try{
socket.send(JSON.stringify(response));
}
catch(error){
console.log('error sending message!!');
}
}
}
else{
console.log('bad type: '+data.type);
}
});
theSocket.on('close',function(){
theSocket = undefined;
});
}
else socket.close();
});
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
var theRoland = {
///////////
///////////
///////////
// static values
'coord' : {
'x' : 0,
'y' : 0
},
///////////
///////////
///////////
'updateCoord' : function(axis,amount){
if(this.coord[axis]!==undefined && !isNaN(amount)){
this.coord[axis] = Math.floor(amount+this.coord[axis]);
if(this.coord[axis]<0){
this.coord[axis] = 0;
}
}
}
///////////
///////////
///////////
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
// controls the current position
var handlers = {
///////////
///////////
///////////
'erase' : function(){
if(_test){
var delayTime = 1000;
setTimeout(function(){
if(theSocket){
var msg = {'type':'erase','data':'1/4'};
theSocket.send(JSON.stringify(msg));
}
setTimeout(function(){
if(theSocket){
var msg = {'type':'erase','data':'2/4'};
theSocket.send(JSON.stringify(msg));
}
setTimeout(function(){
if(theSocket){
var msg = {'type':'erase','data':'3/4'};
theSocket.send(JSON.stringify(msg));
}
setTimeout(function(){
if(theSocket){
var msg = {'type':'erase','data':'4/4'};
theSocket.send(JSON.stringify(msg));
}
setTimeout(function(){
if(theSocket){
var msg = {'type':'erase','data':'success'};
theSocket.send(JSON.stringify(msg));
}
},delayTime);
},delayTime);
},delayTime);
},delayTime);
},delayTime);
}
else{
eraseRoland();
}
},
///////////
///////////
///////////
'jog' : function(data){
var axis = data.axis;
var amount = Number(data.amount);
if(axis==='x' || axis==='y'){
theRoland.updateCoord(axis,amount);
}
this.sync();
},
///////////
///////////
///////////
'sync' : function(){
var instruction = moveHead(theRoland.coord);
if(theSocket){
var msg = {
'type' : 'sync',
'data' : theRoland.coord
};
theSocket.send(JSON.stringify(msg));
}
if(!_test) sendToRoland(instruction);
},
///////////
///////////
///////////
'mill' : function(data){
var minDepth = 1; // minimum depth we will mill
var minDiameter = 10; // 1/100 inch bit
// get the lines
var lines = data.lines;
var depth = Math.abs(Math.floor(Number(data.depth)));
if(isNaN(depth) || depth<minDepth) depth = minDepth;
var diameter = Math.floor(Number(data.diameter));
if(isNaN(diameter) || diameter<minDiameter) diameter = minDiameter;
var plungeDepth = Math.round(diameter/5);
// decide how many times we have go around cutting
var iterations = Math.floor(depth/plungeDepth);
// riase the head between line segments if
// the bit is 1/64 inch or 1/100 inch
var VS_value = 6;
var VZ_value = 1.2;
var RC_value = 12000;
if (diameter<20) {
VS_value = 2.4;
VZ_value = 0.6;
}
// go through every full-plungeDepth iteration
var job_text = 'PA;PA;';
job_text += 'VS'+VS_value+';';
job_text += '!VZ'+VZ_value+';';
job_text += '!RC'+RC_value+';';
// loop through all lines
for(var l=0;l<lines.length;l++){
// we will go through this line for each iteration
// starting shallow, and going down until we've reached the full depth
var cuts = lines[l];
var cutBackwards = false;
var stepDepth = 0;
// then loop through the number of iteration we will take
// if we're only doing one pass, iterations will equal 0
for(var i=1;i<=iterations;i++){
// each iteration will go down a step in the plunge depth
stepDepth = Math.floor(plungeDepth * i);
// create and save the new line at this depth
job_text += makeIteration(stepDepth, cuts, cutBackwards);
cutBackwards = !cutBackwards;
}
// now do the one final depth cut, if we need to
if(stepDepth<depth) {
job_text += makeIteration(depth, cuts, cutBackwards);
}
}
// stop the spindle, and return to the origin
job_text += 'PU;!MC0;PU';
job_text += theRoland.coord.x;
job_text += ',';
job_text += theRoland.coord.y;
job_text += ';!PZ0,200;PD;';
sendToRoland(job_text);
///////////
///////////
///////////
function makeIteration(depth, cuts, backwards){
depth *= -1;
var text = '!PZ';
text += depth; // the current depth we will cut at
text += ',50;!MC1;\r\n';
var startIndex = backwards ? cuts.length-1 : 0;
var indexStep = backwards ? -1 : 1;
if(cuts.length){
// an array of cut points creates a line
// so start by flying above the first point in the line
text += flyTo(cuts[startIndex]);
// then cut to each point in the array
for(var c=startIndex;c<cuts.length && c>=0;c+=indexStep){
text += millTo(cuts[c]);
}
}
return text;
}
///////////
///////////
///////////
function millTo (cut) {
var string = 'PD;PD';
string += cut.x + theRoland.coord.x;
string += ',';
string += cut.y + theRoland.coord.y;
string += ';\r\n';
return string;
}
///////////
///////////
///////////
function flyTo (cut) {
var string = 'PU;PU';
string += cut.x + theRoland.coord.x;
string += ',';
string += cut.y + theRoland.coord.y;
string += ';\r\n';
return string;
}
///////////
///////////
///////////
}
///////////
///////////
///////////
};
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
function moveHead(point){
var jogText = 'PA;PA;VS10;!VZ10;!PZ0,300;!MC0;';
// move up
jogText += 'PU;';
// then move to the desired spot
jogText += ('PU'+point.x+','+point.y+';');
// then move down
jogText += 'PD;';
return jogText;
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
function sendToRoland(fileText,onSuccess,onError){
var fileName = 'move.mill'; // the temp file we create
// create the temporary file in this directory
fs.writeFile(fileName,fileText,function(error){
if(error){
console.log('\t\tError saving the file:\r\r');
console.log(error);
if(onError) onError();
}
else{
var commandLine = 'type '+fileName+' > '+rolandPortName;
if(_test){
if(_windows) commandLine = 'type '+fileName; // view it before erasing
else commandLine = 'cat '+fileName;
}
var exec = childProcess.exec;
exec(commandLine, function (error, stdout, stderr) {
if(error){
console.log('\t\tError sending the command "'+commandLine+'":\r\r');
console.log(error);
if(onError) onError();
}
if(stdout){
if(_test){
console.log('\r\n');
console.log(stdout+'\r\r');
}
// on completion, erase the temporary file
fs.unlink(fileName,function(){
// i dunno, yay?
if(onSuccess) onSuccess();
});
}
});
}
});
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
function eraseRoland(){
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
function fireCommand(cmd,onSuccess,onError){
var exec = childProcess.exec;
exec(cmd,function(error,stdin,stdout){
if(error){
if(onError){
onError(error);
}
else{
console.log('Error running command: '+cmd);
console.log(error);
}
}
else{
if(onSuccess) onSuccess(stdin,stdout);
}
});
}
function eraseResponse(response){
if(theSocket){
var msg = {
'type' : 'erase',
'data' : response
};
theSocket.send(JSON.stringify(msg));
}
else{
console.log(response);
}
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
var firstCommand = 'net stop spooler';
eraseResponse('(1/4) stopping the spooler');
fireCommand(firstCommand,function(){
var secondCommand = 'del C:\\windows\\System32\\spool\\PRINTERS\\*.SPL';
eraseResponse('(2/4) erasing .SPL files');
fireCommand(secondCommand,function(){
var thirdCommand = 'del C:\\windows\\System32\\spool\\PRINTERS\\*.SHD';
eraseResponse('(3/4) erasing .SHD files');
fireCommand(thirdCommand,function(){
var finalCommand = 'net start spooler';
eraseResponse('(4/4) starting the spooler');
fireCommand(finalCommand,function(){
eraseResponse('success');
},function(){eraseResponse('(4/4) failed starting spooler');});
},function(){eraseResponse('(3/4) failed erasing .SHD files');});
},function(){eraseResponse('(2/4) failed erasing .SPL files');});
},function(){eraseResponse('(1/4) failed stopping spooler');});
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////