Doodle3D-Slicer/src/gcode.js

165 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-07-26 15:32:10 +02:00
import THREE from 'three.js';
2015-06-15 10:21:05 +02:00
2015-07-26 15:32:10 +02:00
export default class {
constructor () {
2015-08-26 18:27:56 +02:00
this.gcode = '';
2015-07-26 15:32:10 +02:00
this.current = {};
2015-06-09 11:08:06 +02:00
2015-07-26 15:32:10 +02:00
this.extruder = 0.0;
this.bottom = true;
this.isRetracted = false;
this.isFanOn = false;
this._nozzlePosition = new THREE.Vector2(0, 0);
}
_addGCode (command) {
2015-08-26 18:27:56 +02:00
var str = '';
2015-07-26 15:32:10 +02:00
var first = true;
2015-07-26 15:32:10 +02:00
for (var i in command) {
if (first) {
str = i + command[i];
2015-07-26 15:32:10 +02:00
first = false;
}
else if (this.current[i] !== command[i]) {
2015-08-26 18:27:56 +02:00
str += ' ' + i + command[i];
2015-06-09 11:08:06 +02:00
2015-07-26 15:32:10 +02:00
this.current[i] = command[i];
}
}
2015-06-09 11:08:06 +02:00
2015-08-26 18:27:56 +02:00
this.gcode += str + '\n';
2015-07-26 15:32:10 +02:00
}
setSettings (settings) {
this.settings = settings;
2015-06-09 11:08:06 +02:00
2015-07-26 15:32:10 +02:00
return this;
2015-06-09 11:08:06 +02:00
}
2015-07-26 15:32:10 +02:00
turnFanOn (fanSpeed) {
this.isFanOn = true;
2015-06-09 11:08:06 +02:00
2015-07-26 15:32:10 +02:00
var gcode = {
2015-08-26 18:27:56 +02:00
'M': 106
2015-07-26 15:32:10 +02:00
}
2015-06-09 11:08:06 +02:00
2015-07-26 15:32:10 +02:00
if (fanSpeed !== undefined) {
2015-08-26 18:27:56 +02:00
gcode['S'] = fanSpeed;
2015-07-26 15:32:10 +02:00
}
2015-07-26 15:32:10 +02:00
this._addGCode(gcode);
2015-06-09 11:08:06 +02:00
2015-07-26 15:32:10 +02:00
return this;
}
turnFanOff () {
this.isFanOn = false;
2015-06-09 11:08:06 +02:00
2015-07-26 15:32:10 +02:00
this._addGCode({
2015-08-26 18:27:56 +02:00
'M': 107
2015-07-26 15:32:10 +02:00
});
2015-07-26 15:32:10 +02:00
return this;
}
2015-06-11 15:42:38 +02:00
2015-07-26 15:32:10 +02:00
moveTo (x, y, layer) {
2015-08-26 18:27:56 +02:00
var layerHeight = this.settings.config['layerHeight'];
var travelSpeed = this.settings.config['travelSpeed'];
2015-07-26 15:32:10 +02:00
var z = (layer + 1) * layerHeight;
var speed = travelSpeed * 60;
this._addGCode({
2015-08-26 18:27:56 +02:00
'G': 0,
'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3),
'F': speed.toFixed(3)
2015-07-26 15:32:10 +02:00
});
this._nozzlePosition.set(x, y);
return this;
}
2015-06-11 15:42:38 +02:00
2015-07-26 15:32:10 +02:00
lineTo (x, y, layer, type) {
var newNozzlePosition = new THREE.Vector2(x, y);
2015-08-26 18:27:56 +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-08-26 18:27:56 +02:00
var profile = this.settings.config[(this.bottom ? 'bottom' : type)];
2015-08-26 18:27:56 +02:00
var speed = profile['speed'] * 60;
var flowRate = profile['flowRate'];
2015-07-26 15:32:10 +02:00
var z = (layer + 1) * layerHeight;
2015-06-11 15:42:38 +02:00
2015-07-26 15:32:10 +02:00
var lineLength = this._nozzlePosition.distanceTo(newNozzlePosition);
2015-07-26 15:32:10 +02:00
var filamentSurfaceArea = Math.pow((filamentThickness / 2), 2) * Math.PI;
this.extruder += lineLength * nozzleDiameter * layerHeight / filamentSurfaceArea * flowRate;
2015-06-11 15:42:38 +02:00
2015-07-26 15:32:10 +02:00
this._addGCode({
2015-08-26 18:27:56 +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-07-26 15:32:10 +02:00
});
2015-06-11 15:42:38 +02:00
2015-07-26 15:32:10 +02:00
this._nozzlePosition.copy(newNozzlePosition);
2015-06-11 15:42:38 +02:00
2015-07-26 15:32:10 +02:00
return this;
2015-06-09 21:58:22 +02:00
}
2015-06-11 15:42:38 +02:00
2015-07-26 15:32:10 +02:00
unRetract () {
2015-08-26 18:27:56 +02:00
var retractionEnabled = this.settings.config['retractionEnabled'];
var retractionMinDistance = this.settings.config['retractionMinDistance'];
var retractionSpeed = this.settings.config['retractionSpeed'];
2015-07-26 15:32:10 +02:00
if (this.isRetracted && retractionEnabled) {
this.isRetracted = false;
var speed = retractionSpeed * 60;
if (this.extruder > retractionMinDistance) {
this._addGCode({
2015-08-26 18:27:56 +02:00
'G': 0,
'E': this.extruder.toFixed(3),
'F': speed.toFixed(3)
2015-07-26 15:32:10 +02:00
});
}
2015-06-09 21:58:22 +02:00
}
2015-06-15 10:21:05 +02:00
2015-07-26 15:32:10 +02:00
return this;
}
retract () {
2015-08-26 18:27:56 +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-07-26 15:32:10 +02:00
if (!this.isRetracted && retractionEnabled) {
this.isRetracted = true;
var speed = retractionSpeed * 60;
if (this.extruder > retractionMinDistance && retractionEnabled) {
this._addGCode({
2015-08-26 18:27:56 +02:00
'G': 0,
'E': (this.extruder - retractionAmount).toFixed(3),
'F': speed.toFixed(3)
2015-07-26 15:32:10 +02:00
});
}
}
2015-07-26 15:32:10 +02:00
return this;
}
getGCode () {
return this.settings.startCode() + this.gcode + this.settings.endCode();
}
2015-10-14 17:11:29 +02:00
}