-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrinter.js
203 lines (165 loc) · 4.41 KB
/
Printer.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
const _ = require('lodash')
const fs = require('fs')
const {promisify} = require('util');
const fsReadFile = promisify(fs.readFile)
const Bot = require('./Bot')
const MarlinServer = require('./MarlinServer')
const sleep = ms => new Promise(res => setTimeout(res, ms))
module.exports = class Printer {
constructor (options) {
this.options = options
this.channel = new MarlinServer(options)
this.channel.on('data', this.parse.bind(this))
this.switch = {
x: false,
y: false,
z: false,
b: false,
}
}
parse(data) {
if (data.startsWith('x_min:')) {
this.switch.x = data.includes('TRIGGERED')
} else if (data.startsWith('y_min:')) {
this.switch.y = data.includes('TRIGGERED')
} else if (data.startsWith('z_min:')) {
this.switch.z = data.includes('TRIGGERED')
} else if (data.startsWith('x_max:')){
this.switch.b = data.includes('TRIGGERED')
}
}
get name () {
return this.options.name || this.channel.name
}
// Resolves promise once connected
ready() {
return this.channel.ready()
}
connect() {
return this.channel.connect()
}
async command(gcode) {
let original = gcode
gcode = gcode.replace(/\s*;.*/g, '').trim()
if (gcode.length <= 1) {
if (this.options.debug)
console.log('!', 'skip command:', original)
return
}
this.gcode = gcode
// Special Soft cases
if (gcode.startsWith('SOFT:')){
let cmd = gcode.replace('SOFT:', '')
if (this.options.debug) {
console.log('> SOFT', cmd)
}
await this[cmd]()
return
}
await this.channel.execute(gcode)
}
async commands(gcodes) {
for (let gcode of gcodes) {
await this.command(gcode)
}
}
async executeFile(path) {
let file = await fsReadFile(path)
let commands = file.toString().split('\n')
await this.commands(commands)
}
async display(string) {
await this.command(`M117 ${string}`)
}
async homeAll() {
await this.homeW()
await this.meshBedLevel()
}
async home(axes){
axes = _.intersection(axes, ['X', 'Y', 'Z', 'W'])
let cmd = 'G28 ' + axes.join(' ')
await this.channel.execute(cmd)
}
async homeX(){ await this.home(['X']) }
async homeY(){ await this.home(['Y']) }
async homeZ(){ await this.home(['Z']) }
async homeW(){ await this.home(['W']) }
async waitForButtonPress(){
this.display("Press Extrud Button")
do {
await this.readSwitches()
await sleep(50)
} while(!this.switch.b)
this.display("Ok! Beginning")
}
async softwareHome(axis) {
let homed = false
let limit = 2000
let AXIS = axis.toUpperCase()
axis = axis.toLowerCase()
if (AXIS != 'X' && AXIS != 'Y' && AXIS != 'Z') {
throw new Error('Invalid softwareHome axis: ' + AXIS)
}
await this.command(`G4 P40`)
await this.readSwitches()
if (this.switch[axis]) {
await this.command(`G1 ${AXIS}5`)
await this.command(`G4 P40`)
await this.readSwitches()
}
while(limit--) {
await this.readSwitches()
if (this.switch[axis]) {
homed = true
break
}
this.command(`G92 ${AXIS}0.5 F3000`)
this.command(`G1 ${AXIS}0`)
}
let offset = {X: -0.1, Y: -3, Z: 0}
this.command(`G92 ${AXIS}${offset[AXIS]}`)
this.command(`G1 ${AXIS}0`)
this.command(`M300 S2000 P200`)
if (homed) {
console.log("Home Ok")
} else {
throw new Error("Homing failed on:", AXIS)
}
}
async checkPrint() {
await this.readSwitches()
if (this.switch.b){
await this.display("Part Found 200 OK")
}else {
await this.shutdown()
await this.command('M300 S2000 P500')
await this.command('M300 S0 P20')
await this.command('M300 S2000 P500')
await this.command('M300 S0 P20')
await this.command('M300 S2000 P500')
await this.command('M300 S0 P20')
throw new Error("Part Not Found 404")
}
}
async shutdown(){
await this.command('G1 Z30 F3000.0')
await this.command('M84')
await this.command('M104 S0')
await this.command('M140 S0')
}
async softwareHomeY() {
await this.softwareHome('Y')
}
async softwareHomeX() {
await this.softwareHome('X')
}
async softwareHomeZ() {
await this.softwareHome('Z')
}
async readSwitches() {
await this.command('M119')
}
async meshBedLevel(){
await this.command('G80')
}
}