Doodle3D-Slicer/src/gcode.js

182 lines
4.0 KiB
JavaScript
Raw Normal View History

/******************************************************
*
* GCode
*
* Manages the gcode
2015-06-09 21:58:22 +02:00
* Also handles different flavours of gcode
* TODO
* calculate extrusion length and total time
*
******************************************************/
D3D.GCode = function () {
"use strict";
this.gcode = "";
2015-06-09 11:08:06 +02:00
this.current = {};
this.extruder = 0.0;
this.bottom = true;
this.isRetracted = false;
2015-06-09 11:08:06 +02:00
this.isFanOn = false;
2015-06-15 11:40:19 +02:00
this._nozzlePosition = new THREE.Vector2(0, 0);
};
2015-06-15 11:40:19 +02:00
D3D.GCode.prototype._addGCode = function (command) {
2015-06-09 11:08:06 +02:00
"use strict";
2015-07-10 12:59:50 +02:00
var str = "";
2015-06-15 10:21:05 +02:00
var first = true;
2015-06-09 11:08:06 +02:00
for (var i in command) {
2015-06-15 10:21:05 +02:00
if (first) {
str = i + command[i];
first = false;
2015-06-09 21:58:22 +02:00
}
else if (this.current[i] !== command[i]) {
2015-06-15 10:21:05 +02:00
str += " " + i + command[i];
2015-06-09 11:08:06 +02:00
this.current[i] = command[i];
}
}
2015-06-15 10:21:05 +02:00
this.gcode += str + "\n";
2015-06-09 11:08:06 +02:00
};
D3D.GCode.prototype.setSettings = function (printer) {
"use strict";
this.settings = printer;
return this;
};
2015-06-09 11:08:06 +02:00
D3D.GCode.prototype.turnFanOn = function (fanSpeed) {
"use strict";
this.isFanOn = true;
var gcode = {
"M": 106
};
if (fanSpeed !== undefined) {
gcode["S"] = fanSpeed;
}
2015-06-15 11:40:19 +02:00
this._addGCode(gcode);
2015-06-09 11:08:06 +02:00
return this;
};
D3D.GCode.prototype.turnFanOff = function () {
"use strict";
2015-06-09 11:08:06 +02:00
this.isFanOn = false;
2015-06-15 11:40:19 +02:00
this._addGCode({
2015-06-09 11:08:06 +02:00
"M": 107
});
return this;
};
2015-06-11 15:42:38 +02:00
D3D.GCode.prototype.moveTo = function (x, y, layer) {
"use strict";
2015-06-11 15:42:38 +02:00
var layerHeight = this.settings.config["layerHeight"];
var travelSpeed = this.settings.config["travelSpeed"];
var z = (layer + 1) * layerHeight;
2015-06-11 15:42:38 +02:00
var speed = travelSpeed * 60;
2015-06-15 11:40:19 +02:00
this._addGCode({
2015-06-11 15:42:38 +02:00
"G": 0,
"X": x.toFixed(3), "Y": y.toFixed(3), "Z": z.toFixed(3),
"F": speed.toFixed(3)
});
2015-06-15 11:40:19 +02:00
this._nozzlePosition.set(x, y);
2015-06-11 15:42:38 +02:00
return this;
};
D3D.GCode.prototype.lineTo = function (x, y, layer, type) {
"use strict";
2015-06-11 15:42:38 +02:00
var newNozzlePosition = new THREE.Vector2(x, y);
2015-06-11 15:42:38 +02:00
var layerHeight = this.settings.config["layerHeight"];
var nozzleDiameter = this.settings.config["nozzleDiameter"];
var filamentThickness = this.settings.config["filamentThickness"];
var travelSpeed = this.settings.config["travelSpeed"];
2015-06-11 15:42:38 +02:00
var profile = this.settings.config[(this.bottom ? "bottom" : type)];
var speed = profile["speed"] * 60;
var flowRate = profile["flowRate"];
var z = (layer + 1) * layerHeight;
2015-06-15 11:40:19 +02:00
var lineLength = this._nozzlePosition.distanceTo(newNozzlePosition);
2015-06-11 15:42:38 +02:00
2015-07-10 12:59:50 +02:00
var filamentSurfaceArea = Math.pow((filamentThickness / 2), 2) * Math.PI;
2015-06-11 15:42:38 +02:00
this.extruder += lineLength * nozzleDiameter * layerHeight / filamentSurfaceArea * flowRate;
2015-06-15 11:40:19 +02:00
this._addGCode({
2015-06-11 15:42:38 +02:00
"G": 1,
"X": x.toFixed(3), "Y": y.toFixed(3), "Z": z.toFixed(3),
"F": speed.toFixed(3),
"E": this.extruder.toFixed(3)
});
2015-06-15 11:40:19 +02:00
this._nozzlePosition.copy(newNozzlePosition);
2015-06-09 11:08:06 +02:00
return this;
};
D3D.GCode.prototype.unRetract = function () {
"use strict";
2015-06-11 15:42:38 +02:00
var retractionEnabled = this.settings.config["retractionEnabled"];
var retractionMinDistance = this.settings.config["retractionMinDistance"];
var retractionSpeed = this.settings.config["retractionSpeed"];
2015-06-11 15:42:38 +02:00
if (this.isRetracted && retractionEnabled) {
this.isRetracted = false;
2015-06-09 21:58:22 +02:00
var speed = retractionSpeed * 60;
2015-06-11 15:42:38 +02:00
if (this.extruder > retractionMinDistance) {
2015-06-15 11:40:19 +02:00
this._addGCode({
2015-06-09 21:58:22 +02:00
"G": 0,
"E": this.extruder.toFixed(3),
"F": speed.toFixed(3)
});
}
}
2015-06-15 10:21:05 +02:00
return this;
};
D3D.GCode.prototype.retract = function () {
"use strict";
2015-06-11 15:42:38 +02:00
var retractionAmount = this.settings.config["retractionAmount"];
var retractionEnabled = this.settings.config["retractionEnabled"];
var retractionMinDistance = this.settings.config["retractionMinDistance"];
var retractionSpeed = this.settings.config["retractionSpeed"];
2015-06-11 15:42:38 +02:00
if (!this.isRetracted && retractionEnabled) {
this.isRetracted = true;
2015-06-09 21:58:22 +02:00
var speed = retractionSpeed * 60;
2015-06-09 21:58:22 +02:00
if (this.extruder > retractionMinDistance && retractionEnabled) {
2015-06-15 11:40:19 +02:00
this._addGCode({
2015-06-09 21:58:22 +02:00
"G": 0,
"E": (this.extruder - retractionAmount).toFixed(3),
"F": speed.toFixed(3)
});
}
}
2015-06-15 10:21:05 +02:00
return this;
};
2015-06-09 11:08:06 +02:00
D3D.GCode.prototype.getGCode = function () {
"use strict";
return this.settings.getStartCode() + this.gcode + this.settings.getEndCode();
};