-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrinterWorker.js
243 lines (199 loc) · 5.96 KB
/
PrinterWorker.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
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const moment = require('moment')
const Defered = require('./Defered')
const GcodeRunner = require('./GcodeRunner')
module.exports = class PrinterWorker {
constructor(printer, opts) {
this.partCount = 0
this.printer = printer
this.opts = opts
this.running = null
this.logFile = path.join(__dirname, 'logs', printer.name + '.txt')
this.gcodes = {
heat: new GcodeRunner(this.filePath('heat'), opts),
heatWait: new GcodeRunner(this.filePath('heatWait'), opts),
home: new GcodeRunner(this.filePath('home'), opts),
print: new GcodeRunner(this.filePath('LedHolder'), opts),
start: new GcodeRunner(this.filePath('start'), opts),
eject: new GcodeRunner(this.filePath('eject'), opts),
end: new GcodeRunner(this.filePath('end'), opts),
}
this.offset = {
x: 0,
y: 0,
}
this.maxOffsetX = 180
this.maxOffsetY = 140
}
get tag() {
return (' ' + this.printer.name + ' ')
}
// Counts one more part
printed(duration) {
this.partCount++
duration = Math.round(duration / 1000)
fs.appendFileSync(this.logFile,
`${new Date().toString()}: new part (${duration} s)\n`)
}
log(str) {
fs.appendFileSync(this.logFile, str + '\n')
}
filePath(name) {
return path.join(__dirname, './gcodes', name + '.gcode')
}
async stop() {
this.running = false
console.log('Stopping print')
}
run() {
return this.running ? this.running : null
}
async start() {
if (this.running) {
return this.running
}
fs.appendFileSync(this.logFile, '========= Start ========\n')
this.running = new Promise(async (resolve, reject) => {
try {
// Connect and wait printer to be ready
console.log(chalk.dim(this.tag), 'starting')
await this.printer.connect()
console.log(chalk.dim(this.tag), 'Connected')
await this.printer.ready()
console.log(chalk.dim(this.tag), 'Ready')
// Start heating
this.assertRunning()
await this.execGcode(this.gcodes.heat)
// Go home and level bed
this.assertRunning()
await this.execGcode(this.gcodes.home)
// Make sure heat is ok before starting
this.assertRunning()
await this.execGcode(this.gcodes.heatWait)
// Start main loop of program
while (this.running) {
// Measure total time
let begin = Date.now()
await this.resetOffset()
// Print object
this.assertRunning()
await this.execGcode(this.gcodes.start)
await this.nextPartPosition()
// Print object
this.assertRunning()
await this.execGcode(this.gcodes.print)
// Eject object
this.assertRunning()
await this.execGcode(this.gcodes.eject)
// Compute duration
let duration = Date.now() - begin
// Count object
this.printed(duration)
}
await this.execGcode(this.gcodes.end)
console.log(this.tag, 'Finished')
resolve('Finished printing')
} catch (e) {
reject(e)
}
})
}
async resetOffset(){
// this.opts.params.printOffset = (this.opts.params.printOffset) % 100
await this.printer.command(`G1 X0 Y0 F6000`)
await this.printer.command(`G92 X${this.offset.x} Y${this.offset.y}`)
await this.printer.command(`G1 X0 Y0 F6000`)
this.offset = {
x: 0,
y: 0,
}
}
async applyOffset(offset){
await this.resetOffset()
await this.printer.command(`G1 X0 Y0 F6000`)
await this.printer.command(`G92 X${-offset.x} Y${-offset.y}`)
await this.printer.command(`G1 X0 Y0 F6000`)
this.offset = offset
}
async nextPartPosition() {
let partNumber = this.partCount
let spaceX = 3
let spaceY = 10
let maxColum = Math.ceil(this.maxOffsetX/spaceX)
let maxLine = Math.ceil(this.maxOffsetY/spaceY)
let x = 0
let y = 0
let batch = (maxColum*maxLine)
if (partNumber >= batch) {
partNumber = partNumber - (Math.floor(partNumber/batch)*batch)
}
let partY = Math.floor(partNumber/maxColum)
let partX = partNumber - (partY * maxColum)
if(partX <= maxColum){
x = partX * spaceX
y = partY * spaceY
}else{
partY++
}
console.log("OffSet X:",x)
console.log("OffSet Y:",y)
await this.applyOffset({x, y})
}
assertRunning() {
if (!this.running)
throw new Error('Worker stopped')
}
async execGcode(gcode) {
let name = chalk.yellow.bold(gcode.name)
let begin = Date.now()
let draft = console.draft('...')
let lastPrint = 0
await gcode.load()
// Run until is not stopped/paused
do {
// Load next command
let command = gcode.next()
if (command == null) {
break
}
// Log command
draft(
chalk.bgYellow.black(this.tag),
name,
chalk.white(gcode.percentage() + '%'),
chalk.dim('command:'),
chalk.white(this.printer.gcode)
)
// Print status on printer every 5 seconds
if (Date.now() > lastPrint + 5000) {
lastPrint = Date.now()
await this.printer.display(
`#${this.partCount} > ${gcode.name} ${gcode.percentage()}%`)
}
// Wait command to finish
await this.printer.command(command)
// // Check if it ended
// if (!shouldContinue) {
// break
// }
} while (this.running)
let end = Date.now()
draft(
chalk.bgGreen.black(this.tag),
name,
chalk.white(toDuration(Date.now() - begin))
)
}
}
function toDuration(milis) {
let mm = moment.duration(milis)
let str = ''
str += (mm.hours() < 10 ? '0' : '') + mm.hours()
str += ':'
str += (mm.minutes() < 10 ? '0' : '') + mm.minutes()
str += ':'
str += (mm.seconds() < 10 ? '0' : '') + mm.seconds()
return str
}