grid-apps-cmms/src/mode/fdm/export.js

458 lines
17 KiB
JavaScript
Raw Normal View History

2020-11-05 18:29:04 -05:00
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
"use strict";
(function() {
let KIRI = self.kiri,
BASE = self.base,
UTIL = BASE.util,
FDM = KIRI.driver.FDM;
/**
* @returns {Array} gcode lines
*/
2020-11-08 17:35:40 -05:00
FDM.export = function(print, online) {
2020-11-05 18:29:04 -05:00
let layers = print.output,
settings = FDM.fixExtruders(print.settings),
device = settings.device,
extruders = device.extruders,
process = settings.process,
gcodeFan = device.gcodeFan,
gcodeLayer = device.gcodeLayer,
gcodeTrack = device.gcodeTrack,
tool = 0,
2021-01-09 19:10:39 -05:00
isBelt = device.bedBelt,
2021-01-07 18:41:53 -05:00
bedType = isBelt ? "belt" : "fixed",
2020-11-05 18:29:04 -05:00
extruder = extruders[tool],
offset_x = extruder.extOffsetX,
offset_y = extruder.extOffsetY,
extrudeAbs = device.extrudeAbs,
time = 0,
layer = 0,
pause = [],
pauseCmd = device.gcodePause,
output = [],
outputLength = 0,
lastProgress = 0,
decimals = BASE.config.gcode_decimals || 4,
progress = 0,
distance = 0,
emitted = 0,
retracted = 0,
pos = {x:0, y:0, z:0, f:0},
lout = null,
2020-11-05 18:29:04 -05:00
last = null,
zpos = 0,
zhop = process.zHopDistance || 0,
seekMMM = process.outputSeekrate * 60,
retDist = process.outputRetractDist,
retSpeed = process.outputRetractSpeed * 60,
retDwell = process.outputRetractDwell || 0,
timeDwell = retDwell / 1000,
2021-01-16 20:39:55 -05:00
originCenter = process.outputOriginCenter,
offset = originCenter ? null : {
2020-11-05 18:29:04 -05:00
x: device.bedWidth/2,
2021-01-08 14:42:01 -05:00
y: isBelt ? 0 : device.bedDepth/2
2020-11-05 18:29:04 -05:00
},
subst = {
travel_speed: seekMMM,
retract_speed: retSpeed,
retract_distance: retDist,
temp: process.firstLayerNozzleTemp || process.outputTemp,
temp_bed: process.firstLayerBedTemp || process.outputBedTemp,
bed_temp: process.firstLayerBedTemp || process.outputBedTemp,
fan_speed: process.outputFanMax,
speed: process.outputFanMax, // legacy
top: offset ? device.bedDepth : device.bedDepth/2,
left: offset ? 0 : -device.bedWidth/2,
right: offset ? device.bedWidth : device.bedWidth/2,
bottom: offset ? 0 : -device.bedDepth/2,
z_max: device.maxHeight,
layers: layers.length,
nozzle: 0,
tool: 0
},
pidx, path, out, speedMMM, emitMM, emitPerMM, lastp, laste, dist,
append,
lines = 0,
2021-01-07 18:41:53 -05:00
bytes = 0,
bcos = Math.cos(Math.PI/4),
icos = 1 / bcos,
loops = process.outputLoopLayers,
inLoop;
if (isBelt && loops) {
loops = loops.split(',').map(range => {
return range.split('-').map(v => parseInt(v));
}).filter(a => a.length > 1).map(a => {
return {
start: a[0],
end: a[1] ? a[1] + 1 : Infinity,
iter: a[2] >= 0 ? a[2] : 1
}
});
}
if (!isBelt || (loops && loops.length < 1)) {
loops = undefined;
}
2021-01-09 21:08:24 -05:00
2020-11-05 18:29:04 -05:00
(process.gcodePauseLayers || "").split(",").forEach(function(lv) {
let v = parseInt(lv);
if (v >= 0) pause.push(v);
});
// console.log(loops)
2020-11-08 19:38:02 -05:00
append = function(line) {
if (line) {
2020-11-05 18:29:04 -05:00
lines++;
bytes += line.length;
2020-11-08 19:38:02 -05:00
output.append(line);
2020-11-05 18:29:04 -05:00
}
2020-11-08 19:38:02 -05:00
if (!line || output.length > 1000) {
online(output.join("\n"));
output = [];
}
};
2020-11-05 18:29:04 -05:00
function appendSub(line) {
append(print.constReplace(line, subst));
}
function appendAll(arr) {
if (!arr) return;
if (!Array.isArray(arr)) arr = [ arr ];
arr.forEach(function(line) { append(line) });
}
function appendAllSub(arr) {
if (!arr) return;
if (!Array.isArray(arr)) arr = [ arr ];
arr.forEach(function(line) { appendSub(line) });
}
append(`; Generated by Kiri:Moto ${KIRI.version}`);
append(`; ${new Date().toString()}`);
appendSub("; Bed left:{left} right:{right} top:{top} bottom:{bottom}");
append(`; Bed type: ${bedType}`);
2020-11-05 18:29:04 -05:00
append(`; Target: ${settings.filter[settings.mode]}`);
append("; --- process ---");
for (let pk in process) {
append("; " + pk + " = " + process[pk]);
}
append("; --- startup ---");
let t0 = false;
let t1 = false;
for (let i=0; i<device.gcodePre.length; i++) {
let line = device.gcodePre[i];
if (line.indexOf('T0') >= 0) t0 = true; else
if (line.indexOf('T1') >= 0) t1 = true; else
if (line.indexOf('M82') >= 0) extrudeAbs = true; else
if (line.indexOf('M83') >= 0) extrudeAbs = false; else
if (line.indexOf('G90') >= 0) extrudeAbs = true; else
if (line.indexOf('G91') >= 0) extrudeAbs = false; else
if (line.indexOf('G92') === 0) {
line.split(";")[0].split(' ').forEach(function (tok) {
let val = parseFloat(tok.substring(1) || 0) || 0;
switch (tok[0]) {
case 'X': pos.x = val; break;
case 'Y': pos.y = val; break;
case 'Z': pos.z = val; break;
case 'E': outputLength = val; break;
}
});
}
if (extrudeAbs && line.indexOf('E') > 0) {
line.split(";")[0].split(' ').forEach(function (tok) {
// use max E position from gcode-preamble
if (tok[0] == 'E') {
outputLength = Math.max(outputLength, parseFloat(tok.substring(1)) || 0);
}
});
}
if (line.indexOf("{tool}") > 0 && extruders.length > 1) {
for (let i=0; i<extruders.length; i++) {
subst.tool = i;
appendSub(line);
}
subst.tool = 0;
} else {
appendSub(line);
}
2020-11-05 18:29:04 -05:00
}
function dwell(ms) {
append(`G4 P${ms}`);
time += timeDwell;
}
function retract(zhop) {
2021-01-24 18:35:19 -05:00
if (retracted) {
// console.log({double_retract: zhop});
return;
}
// console.log({retract:zhop});
2020-11-05 18:29:04 -05:00
retracted = retDist;
moveTo({e:-retracted}, retSpeed, `e-retract ${retDist}`);
if (zhop) moveTo({z:zpos + zhop}, seekMMM, "z-hop start");
time += (retDist / retSpeed) * 60 * 2; // retraction time
}
2021-01-07 18:41:53 -05:00
let taxis = new THREE.Vector3( 1, 0, 0 );
let tcent = new THREE.Vector2( 0, 0 );
let angle = -Math.PI / 4;
2020-11-05 18:29:04 -05:00
function moveTo(newpos, rate, comment) {
if (comment) {
append(";; " + comment);
}
let o = [!rate && !newpos.e ? 'G0' : 'G1'];
let emit = { x: false, y: false, z: false };
if (typeof newpos.x === 'number' && newpos.x !== pos.x) {
pos.x = newpos.x;
2021-01-07 18:41:53 -05:00
emit.x = true;
2020-11-05 18:29:04 -05:00
}
if (typeof newpos.y === 'number' && newpos.y !== pos.y) {
pos.y = newpos.y;
2021-01-07 18:41:53 -05:00
emit.y = true;
if (isBelt) emit.z = true;
2020-11-05 18:29:04 -05:00
}
if (typeof newpos.z === 'number' && newpos.z !== pos.z) {
pos.z = newpos.z;
2021-01-07 18:41:53 -05:00
emit.z = true;
if (isBelt) emit.y = true;
2021-01-07 18:41:53 -05:00
}
let epos = isBelt ? { x: pos.x, y: pos.y, z: pos.z } : pos;
if (isBelt) {
2021-01-16 20:39:55 -05:00
epos.x = originCenter ? -pos.x : device.bedWidth - pos.x;
2021-01-09 11:51:46 -05:00
epos.z = pos.z * icos;
2021-01-09 19:25:29 -05:00
epos.y = -pos.y + epos.z * bcos;
lout = epos;
2020-11-05 18:29:04 -05:00
}
2021-01-07 18:41:53 -05:00
if (emit.x) o.append(" X").append(epos.x.toFixed(decimals));
if (emit.y) o.append(" Y").append(epos.y.toFixed(decimals));
if (emit.z) o.append(" Z").append(epos.z.toFixed(decimals));
2020-11-05 18:29:04 -05:00
if (typeof newpos.e === 'number') {
outputLength += newpos.e;
if (extrudeAbs) {
// for cumulative (absolute) extruder positions
o.append(" E").append(outputLength.toFixed(decimals));
} else {
o.append(" E").append(newpos.e.toFixed(decimals));
}
}
if (rate && rate != pos.f) {
o.append(" F").append(Math.round(rate));
pos.f = rate
}
let line = o.join('');
if (last == line) {
// console.log({dup:line});
return;
}
last = line;
append(line);
}
// calc total distance traveled by head as proxy for progress
let allout = [], totaldistance = 0;
layers.forEach(function(outs) {
allout.appendAll(outs);
});
allout.forEachPair(function (o1, o2) {
totaldistance += o1.point.distTo2D(o2.point);
}, 1);
// retract before first move
retract();
while (layer < layers.length) {
path = layers[layer];
emitPerMM = print.extrudePerMM(
extruder.extNozzle,
extruder.extFilament,
path.layer === 0 ?
(process.firstSliceHeight || process.sliceHeight) : path.height);
2021-01-09 19:10:39 -05:00
subst.z = zpos = path.z;
2020-11-05 18:29:04 -05:00
subst.Z = subst.z;
subst.e = subst.E = outputLength;
2020-11-05 18:29:04 -05:00
subst.layer = layer;
subst.height = path.height.toFixed(3);
if (isBelt) {
pos.z = zpos;
}
2020-11-05 18:29:04 -05:00
if (pauseCmd && pause.indexOf(layer) >= 0) {
appendAllSub(pauseCmd)
}
if (loops) {
if (inLoop) {
if (layer === inLoop.end) {
append(`M808`);
inLoop = undefined;
}
} else {
for (let loop of loops) {
if (layer === loop.start) {
append(`M808 L${loop.iter}`);
if (extrudeAbs) {
append(`G92 Z${lout.z.round(decimals)} E${outputLength.round(decimals)}`);
} else {
append(`G92 Z${lout.z.round(decimals)}`);
}
inLoop = loop;
break;
}
}
}
}
2020-11-05 18:29:04 -05:00
if (gcodeLayer && gcodeLayer.length) {
appendAllSub(gcodeLayer);
} else {
append(`;; --- layer ${layer} (${subst.height} @ ${subst.z.round(3)}) ---`);
2020-11-05 18:29:04 -05:00
}
if (layer > 0 && process.outputLayerRetract) {
retract();
}
// enable fan at fan layer
if (gcodeFan && layer === process.outputFanLayer) {
appendAllSub(gcodeFan);
}
// second layer transitions
if (layer === 1) {
// update temps when first layer overrides are present
if (process.firstLayerNozzleTemp) {
subst.temp = process.outputTemp;
if (t0) appendSub("M104 S{temp} T0");
if (t1) appendSub("M104 S{temp} T1");
if (!(t0 || t1)) appendSub("M104 S{temp} T{tool}");
}
if (process.firstLayerBedTemp) {
subst.bed_temp = subst.temp_bed = process.outputBedTemp;
appendSub("M140 S{temp_bed} T0");
}
}
// move Z to layer height
2021-01-08 14:42:01 -05:00
if (layer > 0 || !isBelt) {
moveTo({z:zpos}, seekMMM);
}
2020-11-05 18:29:04 -05:00
// iterate through layer outputs
for (pidx=0; pidx<path.length; pidx++) {
out = path[pidx];
speedMMM = (out.speed || process.outputFeedrate) * 60;
// look for extruder change and recalc emit factor
if (out.tool !== undefined && out.tool !== tool) {
tool = out.tool;
subst.nozzle = subst.tool = tool;
extruder = extruders[tool];
offset_x = extruder.extOffsetX;
offset_y = extruder.extOffsetY;
emitPerMM = print.extrudePerMM(
extruder.extNozzle,
extruder.extFilament,
path.layer === 0 ?
(process.firstSliceHeight || process.sliceHeight) : path.height);
appendAllSub(extruder.extSelect);
}
// if no point in output, it's a dwell command
if (!out.point) {
dwell(out.speed);
continue;
}
let x = out.point.x + offset_x,
y = out.point.y + offset_y,
z = out.point.z;
2021-01-08 14:42:01 -05:00
// adjust for inversions and origin offsets
2020-11-05 18:29:04 -05:00
if (process.outputInvertX) x = -x;
if (process.outputInvertY) y = -y;
if (offset) {
x += offset.x;
y += offset.y;
}
dist = lastp ? lastp.distTo2D(out.point) : 0;
// re-engage post-retraction before new extrusion
if (out.emit && retracted) {
2021-01-24 18:35:19 -05:00
// console.log({engage:zhop});
2020-11-05 18:29:04 -05:00
// when enabled, resume previous Z
if (zhop && pos.z != zpos) moveTo({z:zpos}, seekMMM, "z-hop end");
// re-engage retracted filament
moveTo({e:retracted}, retSpeed, `e-engage ${retracted}`);
retracted = 0;
// optional dwell after re-engaging filament to allow pressure to build
if (retDwell) dwell(retDwell);
time += (retDist / retSpeed) * 60 * 2; // retraction time
}
if (lastp && out.emit) {
emitMM = emitPerMM * out.emit * dist;
moveTo({x:x, y:y, e:emitMM}, speedMMM);
emitted += emitMM;
} else {
moveTo({x:x, y:y}, seekMMM);
// TODO disabling out of plane z moves until a better mechanism
// can be built that doesn't rely on computed zpos from layer heights...
// when making z moves (like polishing) allow slowdown vs fast seek
// let moveSpeed = (lastp && lastp.z !== z) ? speedMMM : seekMMM;
// moveTo({x:x, y:y, z:z}, moveSpeed);
}
// retract filament if point retract flag set
2021-01-24 18:35:19 -05:00
if (out.retract) {
2020-11-05 18:29:04 -05:00
retract(zhop);
}
// update time and distance (should calc in moveTo() instead)
time += (dist / speedMMM) * 60 * 1.5;
distance += dist;
subst.progress = progress = Math.round((distance / totaldistance) * 100);
// emit tracked progress
if (gcodeTrack && progress != lastProgress) {
appendAllSub(gcodeTrack);
lastProgress = progress;
}
lastp = out.point;
laste = out.emit;
}
layer++;
}
if (inLoop) {
append(`M808`);
}
2020-11-05 18:29:04 -05:00
subst.time = UTIL.round(time,2);
subst.material = UTIL.round(emitted,2);
append("; --- shutdown ---");
appendAllSub(device.gcodePost);
append(`; --- filament used: ${subst.material} mm ---`);
append(`; --- print time: ${time.toFixed(0)}s ---`);
// force emit of buffer
append();
print.distance = emitted;
print.lines = lines;
print.bytes = bytes + lines - 1;
print.time = time;
};
})();