forked from andySigler/nodela
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodela.js
executable file
·468 lines (351 loc) · 9.72 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
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
var serialport = require('serialport');
var portname = undefined;
var myPort = undefined;
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
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);
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
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;
// alert the socket of our connection status
var portConnected = false;
if(myPort) portConnected = true;
var msg = {
'type' : 'connection',
'data' : portConnected
};
theSocket.send(JSON.stringify(msg));
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 = {
///////////
///////////
///////////
'scan' : function () {
// get a list of all available serial ports
// and then update the browser with that list
scanPorts(function (portArray) {
if(theSocket){
var msg = {
'type' : 'scan',
'data' : portArray
};
theSocket.send(JSON.stringify(msg));
}
});
},
///////////
///////////
///////////
'connect' : function (data) {
portname = data;
openSerialPort();
},
///////////
///////////
///////////
'erase' : function (){
// just disconnect then connect the serial port
openSerialPort();
},
///////////
///////////
///////////
'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));
}
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 scanPorts(callback) {
serialport.list(function (error, ports) {
callback(ports);
});
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
function sendToRoland(fileText,onSuccess,onError){
if(myPort) {
myPort.write(fileText);
}
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
function openSerialPort (callback) {
function createIt () {
if(portname) {
var options = {
'flowControl' : true
};
myPort = new serialport.SerialPort(portname,options);
myPort.on('open',function(){
// tell the browser we've connected
var msg = {
'type' : 'connection',
'data' : true
};
theSocket.send(JSON.stringify(msg));
if(callback) callback();
});
myPort.on('close',function(){
myPort = undefined;
// tell the browser we've disconnected
var msg = {
'type' : 'connection',
'data' : false
};
theSocket.send(JSON.stringify(msg));
});
}
else {
// no portname has been set!
}
}
if(!myPort) createIt();
else closeSerialPort(function(){
setTimeout(function(){
createIt();
},1000);
});
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
function closeSerialPort (callback) {
if(myPort) {
myPort.close(function(){
myPort = undefined;
// tell the browser we've disconnected
var msg = {
'type' : 'connection',
'data' : false
};
theSocket.send(JSON.stringify(msg));
if(callback) callback();
});
}
else if(callback) callback();
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////