This repository has been archived by the owner on Feb 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotter-server.js
executable file
·124 lines (100 loc) · 3.01 KB
/
plotter-server.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
#!/usr/bin/env node
var express = require('express')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var httpPort = process.env.PORT || 3000;
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const dimensions = {
x : 1300,
y : 1220
}
let serialPort
try {
SerialPort.list().then((ports) => {
ports.forEach((port) => {
console.log(port.path);
});
});
console.log(" ")
serialPort = new SerialPort('/dev/ttyACM0', {
baudRate: 115200
})
const parser = serialPort.pipe(new Readline({ delimiter: '\r\n' }))
parser.on('data', console.log)
} catch (error) {
console.error(error);
}
// WEB SERVER
// app.get('/', function(req, res){
// res.sendFile(__dirname + '/index.html');
// });
app.use(express.static('sketches'))
io.on('connection', function(socket){
socket.on('position', function(msg){
// console.log(msg)
if (msg.x < 0) msg.x = 0;
if (msg.x > dimensions.x) msg.x = dimensions.x;
if (msg.y < 0) msg.y = 0;
if (msg.y > dimensions.y) msg.y = dimensions.y;
const scale = 1
const command = "G90 X" + (msg.x * scale) + " Y" + (msg.y * scale) + "\n"
console.log(command)
serialPort.write(command, function(err, results) {
if (err) console.log('err ' + err);
if (results) console.log('results ' + results);
});
});
socket.on('positionNormalised', function(msg){
// console.log(msg)
if (msg.x < 0) msg.x = 0;
if (msg.x > 1) msg.x = 1;
if (msg.y < 0) msg.y = 0;
if (msg.y > 1) msg.y = 1;
const command = "G90 X" + (msg.x * dimensions.x) + " Y" + (msg.y * dimensions.y) + "\n"
console.log(command)
serialPort.write(command, function(err, results) {
if (err) console.log('err ' + err);
if (results) console.log('results ' + results);
});
});
socket.on('penDown', function(penDown){
// console.log(msg)
const up = 5
const down = 0
const command = "G90 Z" + (penDown ? down : up) + "\n"
console.log(command)
serialPort.write(command, function(err, results) {
if (err) console.log('err ' + err);
if (results) console.log('results ' + results);
});
});
});
http.listen(httpPort, function(){
console.log('listening on *:' + httpPort);
});
// CONNECTION TO GBRL PEN PLOTTER
if (serialPort) serialPort.on("open", async function () {
serialPort.write("\r\n\r\n", function(err, results) {
if (err) console.log('err ' + err);
if (results) console.log('results ' + results);
});
await new Promise(r => setTimeout(r, 4000));
serialPort.flush();
console.log("start sending commands")
serialPort.write("$X\n$H\nZ10\n", function(err, results) {
if (err) console.log('err ' + err);
if (results) console.log('results ' + results);
});
// port.on('data', function (data) {
// console.log(data);
// // dequeue();
// });
serialPort.on('error', function (err) {
console.log(err);
});
serialPort.on('close', function () {
process.exit(0);
});
});