-
Notifications
You must be signed in to change notification settings - Fork 33
/
gcode_controller.js
382 lines (313 loc) · 13.2 KB
/
gcode_controller.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
require('dotenv').config();
const SerialPort = require('serialport');
var fs = require('fs');
const { exit, send } = require('process');
const Process_Theta_Rho = require('./process_theta_rho');
const { start } = require('repl');
const PORT = process.env.MARLIN_ARDUINO_SERIAL_PORT;
const DEBUG = false;
var paused = false;
var doneCallback = () => { };
var file = [];
var index = 0;
var backwards = false;
var endPos = [Process_Theta_Rho.X_SIZE / 2, Process_Theta_Rho.Y_SIZE / 2] // Ball is in center
var cmdQueue = [];
const port = new SerialPort(PORT, {
baudRate: 250000
}).on("error", (e) => console.log(e));
var portParser = new SerialPort.parsers.Readline();
port.pipe(portParser);
function sendNextCmd(justExecuteWithoutReadingPort = false, dat) {
if (paused) return; // Don't do anything with the sand table if paused
var data = justExecuteWithoutReadingPort ? "ok" : dat;
if (DEBUG) console.log(justExecuteWithoutReadingPort ? "override" : data.toString('utf8'));
if (data.toString('utf8').includes("ok")) {
if (cmdQueue.length > 0) {
var cmd = cmdQueue.shift();
port.write(cmd + "\n");
if (DEBUG) console.log("Send CMD: " + cmd);
} else if (index >= 0 && index < file.length) {
// Send next gcode command
if (file[index] == "") {
index += backwards ? -1 : 1; // Recursively go to next cmd until valid one is found
sendNextCmd(true); // When valid cmd found or reached end of file,
return; // the recursive tree will collapse gracefully
}
port.write(file[index] + "\n");
if (DEBUG) console.log("Send CMD: " + file[index]);
index += backwards ? -1 : 1;
} else {
if (file.length > 0)
doneCallback();
file = [];
index = 0;
}
}
}
portParser.on('data', (dat) => sendNextCmd(false, dat));
function sendFile(filename) {
pause();
// Clear file buffer if another file is running
if (file.length > 0) stop();
setTimeout(() => {
file = fs.readFileSync(__dirname + "/../../files/" + filename + ".gcode")
.toString()
.split("\n")
.filter(function (el) {
return el != null && el != "";
});
var amountFromFirst = 0;
var firstCmd = file[amountFromFirst];
while (firstCmd == "") {
amountFromFirst++;
firstCmd = file[amountFromFirst];
}
firstCmd = firstCmd.split(' ');
var startPos = [parseFloat(firstCmd[1].replace("X", "")), parseFloat(firstCmd[2].replace("Y", ""))];
const nextTrackStartsInCenter = Math.abs(startPos[0] - Process_Theta_Rho.X_SIZE / 2) < 175
&& Math.abs(startPos[1] - Process_Theta_Rho.Y_SIZE / 2) < 175;
const currentTrackEndedInCenter = Math.abs(endPos[0] - Process_Theta_Rho.X_SIZE / 2) < 175
&& Math.abs(endPos[1] - Process_Theta_Rho.Y_SIZE / 2) < 175;
if (nextTrackStartsInCenter
&& currentTrackEndedInCenter)
backwards = false; // End of prev, start of next at center...do nothing special
else if (currentTrackEndedInCenter) {
// ended at center, but start of next is along the outside
var amountFromLast = 1;
var lastCmd = file[file.length - amountFromLast];
while (lastCmd == "") {
amountFromLast++;
lastCmd = file[file.length - amountFromLast];
}
lastCmd = lastCmd.split(' ');
var lastPos = [parseFloat(lastCmd[1].replace("X", "")), parseFloat(lastCmd[2].replace("Y", ""))];
if (Math.abs(lastPos[0] - Process_Theta_Rho.X_SIZE / 2) < 175
&& Math.abs(lastPos[1] - Process_Theta_Rho.Y_SIZE / 2) < 175) {
backwards = true;
} else {
backwards = false; // We don't want to reverse, since we end on the outside too
}
} else if (nextTrackStartsInCenter) {
// ended on outside, but start of next is at center
backwards = true;
var amountFromLast = 1;
var lastCmd = file[file.length - amountFromLast];
while (lastCmd == "") {
amountFromLast++;
lastCmd = file[file.length - amountFromLast];
}
lastCmd = lastCmd.split(' ');
var lastPos = [parseFloat(lastCmd[1].replace("X", "")), parseFloat(lastCmd[2].replace("Y", ""))];
if (Math.abs(lastPos[0] - Process_Theta_Rho.X_SIZE / 2) < 175
&& Math.abs(lastPos[1] - Process_Theta_Rho.Y_SIZE / 2) < 175) {
backwards = false; // Next track ends in center, so don't reverse
} else {
startPos = lastPos;
}
if (Math.abs(endPos[0] - startPos[0]) > 50 && Math.abs(endPos[1] - startPos[1]) > 50) {
var moveCmd = [];
var minDist = Math.min(
endPos[0], Process_Theta_Rho.X_SIZE - endPos[0],
endPos[1], Process_Theta_Rho.Y_SIZE - endPos[1]);
switch (minDist) {
case endPos[0]:
moveCmd.push("G0 X0");
if (startPos[1] < Process_Theta_Rho.Y_SIZE - startPos[1])
moveCmd.push("G0 Y0");
else
moveCmd.push(`G0 Y${Process_Theta_Rho.Y_SIZE}`);
moveCmd.push(`G0 X${startPos[0]}`);
break;
case Process_Theta_Rho.X_SIZE - endPos[0]:
moveCmd.push(`G0 X${Process_Theta_Rho.X_SIZE}`);
if (startPos[1] < Process_Theta_Rho.Y_SIZE - startPos[1])
moveCmd.push("G0 Y0");
else
moveCmd.push(`G0 Y${Process_Theta_Rho.Y_SIZE}`);
moveCmd.push(`G0 X${startPos[0]}`);
break;
case endPos[1]:
moveCmd.push("G0 Y0");
if (startPos[0] < Process_Theta_Rho.X_SIZE - startPos[0])
moveCmd.push("G0 X0");
else
moveCmd.push(`G0 X${Process_Theta_Rho.X_SIZE}`);
moveCmd.push(`G0 Y${startPos[1]}`);
break;
case Process_Theta_Rho.Y_SIZE - endPos[1]:
moveCmd.push(`G0 Y${Process_Theta_Rho.Y_SIZE}`);
if (startPos[0] < Process_Theta_Rho.X_SIZE - startPos[0])
moveCmd.push("G0 X0");
else
moveCmd.push(`G0 X${Process_Theta_Rho.X_SIZE}`);
moveCmd.push(`G0 Y${startPos[1]}`);
break;
}
if (DEBUG) console.log(moveCmd);
file = [].concat(file, moveCmd.reverse());
}
} else {
// ended on outside, start of next is on outside
backwards = false;
if (Math.abs(endPos[0] - startPos[0]) > 50 && Math.abs(endPos[1] - startPos[1]) > 50) {
// Only do move sequence if startPos and endPos are a significant distance apart
// ...in this case, more than 50mm apart (otherwise, just execute pattern)
var moveCmd = [];
var minDist = Math.min(
endPos[0], Process_Theta_Rho.X_SIZE - endPos[0],
endPos[1], Process_Theta_Rho.Y_SIZE - endPos[1]);
switch (minDist) {
case endPos[0]:
moveCmd.push("G0 X0");
if (startPos[1] < Process_Theta_Rho.Y_SIZE - startPos[1])
moveCmd.push("G0 Y0");
else
moveCmd.push(`G0 Y${Process_Theta_Rho.Y_SIZE}`);
moveCmd.push(`G0 X${startPos[0]}`);
break;
case Process_Theta_Rho.X_SIZE - endPos[0]:
moveCmd.push(`G0 X${Process_Theta_Rho.X_SIZE}`);
if (startPos[1] < Process_Theta_Rho.Y_SIZE - startPos[1])
moveCmd.push("G0 Y0");
else
moveCmd.push(`G0 Y${Process_Theta_Rho.Y_SIZE}`);
moveCmd.push(`G0 X${startPos[0]}`);
break;
case endPos[1]:
moveCmd.push("G0 Y0");
if (startPos[0] < Process_Theta_Rho.X_SIZE - startPos[0])
moveCmd.push("G0 X0");
else
moveCmd.push(`G0 X${Process_Theta_Rho.X_SIZE}`);
moveCmd.push(`G0 Y${startPos[1]}`);
break;
case Process_Theta_Rho.Y_SIZE - endPos[1]:
moveCmd.push(`G0 Y${Process_Theta_Rho.Y_SIZE}`);
if (startPos[0] < Process_Theta_Rho.X_SIZE - startPos[0])
moveCmd.push("G0 X0");
else
moveCmd.push(`G0 X${Process_Theta_Rho.X_SIZE}`);
moveCmd.push(`G0 Y${startPos[1]}`);
break;
}
if (DEBUG) console.log(moveCmd);
file = [].concat(moveCmd, file);
}
}
index = backwards ? file.length - 1 : 0;
// Process file by switching G3 to G2 and vice versa if running backwards
if (backwards) {
for (var cmd of file) {
cmd.toString().replace(/G2|G3/g, function (m) {
// `m` is a matched string.
return m === 'G2' ? 'G3' : 'G2';
})
}
}
if (backwards) {
var amountFromFirst = 0;
var firstCmd = file[amountFromFirst];
while (firstCmd == "") {
amountFromFirst++;
firstCmd = file[amountFromFirst];
}
firstCmd = firstCmd.toString().split(' ');
endPos = [parseFloat(firstCmd[1].replace("X", "")), parseFloat(firstCmd[2].replace("Y", ""))];
} else {
var amountFromLast = 1;
var lastCmd = file[file.length - amountFromLast];
while (lastCmd == "" || lastCmd.split(' ').length < 3) {
amountFromLast++;
lastCmd = file[file.length - amountFromLast];
}
lastCmd = lastCmd.split(' ');
endPos = [parseFloat(lastCmd[1].replace("X", "")), parseFloat(lastCmd[2].replace("Y", ""))];
}
// Start the file
resume();
sendNextCmd(true);
console.log("Playing track: " + filename);
}, 1000);
}
function pause() {
paused = true;
}
function resume() {
paused = false;
sendNextCmd(true);
}
function stop() {
if (backwards) {
var amountFromFirst = index;
var firstCmd = file[amountFromFirst];
while (firstCmd == "") {
amountFromFirst++;
firstCmd = file[amountFromFirst];
}
// console.log(firstCmd);
firstCmd = firstCmd.toString().split(' ');
// console.log(file[index]);
// console.log(firstCmd);
if (firstCmd.length != 3) endPos = [0, 0];
else endPos = [parseFloat(firstCmd[1].replace("X", "")), parseFloat(firstCmd[2].replace("Y", ""))];
} else {
var amountFromLast = index;
var lastCmd = file[amountFromLast];
while (lastCmd == "") {
amountFromLast--;
lastCmd = file[amountFromLast];
}
// console.log(lastCmd);
lastCmd = lastCmd.split(' ');
// console.log(file[index]);
// console.log(lastCmd);
if (lastCmd.length != 3) endPos = [0, 0];
else endPos = [parseFloat(lastCmd[1].replace("X", "")), parseFloat(lastCmd[2].replace("Y", ""))];
}
file = [];
index = 0;
}
function setDoneCallback(cb) {
doneCallback = cb;
}
function setSpeed(newSpeed) {
cmdQueue.push(`M220 S${parseFloat(newSpeed).toFixed(0)}`);
console.log("Set speed to " + newSpeed + "%");
}
// Send first line
setTimeout(() => {
// Start gcode
backwards = false;
index = 0;
file = [
"G90",
"G28",
"M220 S100",
"M204 P4000 T4000",
"M205 J0.0001",
`G0 X${Process_Theta_Rho.X_SIZE / 2} Y${Process_Theta_Rho.Y_SIZE / 2}`, // center of table
];
sendNextCmd(true); // Run start gcode
console.log("GCode Controller Initialized");
}, 5000);
setDoneCallback(() => process.send("Done"));
process.on("message", async ({ msg: msg, args: args }) => {
switch (msg) {
case "sendFile":
sendFile(...args);
break;
case "pause":
pause(...args);
break;
case "resume":
resume(...args);
break;
case "stop":
stop(...args);
break;
case "setSpeed":
setSpeed(...args);
break;
}
});