Did a ajax calls and the gcode sending overhaul/rewrite. All failed calls (temp, process check, preheat, print and stop) are retried automatically. All printer communication is now handled in Printer.js.

This commit is contained in:
peteruithoven 2013-08-14 20:54:48 +02:00
parent 8ec49ed475
commit 7da38d2e98
5 changed files with 118 additions and 107 deletions

View File

@ -88,7 +88,6 @@
<script src="js/buttonbehaviors.js"></script>
<script src="js/canvasDrawing_v01.js"></script>
<script src="js/previewRendering_v01.js"></script>
<script src="js/doodlePrintCode.js"></script>
<script src="js/gcodeGenerating_v01.js"></script>
<script src="js/init_layout.js"></script>
<script src="js/Printer.js"></script>

View File

@ -5,15 +5,25 @@ function Printer() {
this.wifiboxURL;
this.maxTempLastMod = 5; // max time (seconds) since the last temp info modification before the printer connection is considered lost
this.checkTemperatureInterval = 3000;
this.checkTemperatureDelay;
this.checkProgressInterval = 3000;
this.checkProgressDelay;
this.timeoutTime = 3000;
this.gcode; // gcode to be printed
this.sendLength = 6000; // max amount of gcode lines per post (limited because WiFi box can't handle to much)
this.retryDelay = 2000; // retry setTimout delay
this.retrySendPrintPartDelay; // retry setTimout instance
this.retryCheckTemperatureDelay; // retry setTimout instance
this.retryCheckProgressDelay; // retry setTimout instance
this.retryStopDelay; // retry setTimout instance
this.retryPreheatDelay; // retry setTimout instance
this.maxGCodeSize = 10; // max size of gcode in MB's (estimation)
// Events
Printer.UPDATE = "update";
@ -41,29 +51,103 @@ function Printer() {
timeout: this.timeoutTime,
success: function(data){
console.log("Printer:preheat response: ",data);
},
error: function(jqXHR, status, errorThrown){ //the status returned will be "timeout"
//console.log("Printer:temperature error. Status: ",status,' errorThrown: ',errorThrown);
switch(status) {
case 'timeout':
console.log("retrieving printer/heatup timeout");
self.preheat();
break;
}
if(data.status == "error") {
clearTimeout(self.retryPreheatDelay);
self.retryPreheatDelay = setTimeout(function() { self.preheat() },self.retryDelay); // retry after delay
}
}
}).fail(function() {
console.log("Printer:preheat: failed");
clearTimeout(self.retryPreheatDelay);
self.retryPreheatDelay = setTimeout(function() { self.preheat() },self.retryDelay); // retry after delay
});
}
this.print = function(gcode) {
console.log("Printer:print");
console.log(" gcode total # of lines: " + gcode.length);
/*for (i = 0; i < gcode.length; i++) {
gcode[i] += " (" + i + ")";
}*/
this.sendIndex = 0;
this.gcode = gcode;
//console.log(" gcode[20]: ",gcode[20]);
var gcodeLineSize = this.byteSize(gcode[20]);
//console.log(" gcodeLineSize: ",gcodeLineSize);
var gcodeSize = gcodeLineSize*gcode.length/1024/1024; // estimate gcode size in MB's
console.log(" gcodeSize: ",gcodeSize);
if(gcodeSize > this.maxGCodeSize) {
console.log("Error: Printer:print: gcode file is probably to big ("+gcodeSize+"MB) (max: "+this.maxGCodeSize+"MB)");
return;
}
this.sendPrintPart(this.sendIndex, this.sendLength);
}
this.byteSize = function(s){
return~-encodeURI(s).split(/%..|./).length;
}
this.sendPrintPart = function(sendIndex,sendLength) {
console.log("Printer:sendPrintPart sendIndex: " + sendIndex + "/" + this.gcode.length + ", sendLength: " + sendLength);
var firstOne = (sendIndex == 0)? true : false;
var lastOne = false;
if (this.gcode.length < (sendIndex + sendLength)) {
console.log(" sending less than max sendLength (and last)");
sendLength = this.gcode.length - sendIndex;
lastOne = true;
}
var gcodePart = this.gcode.slice(sendIndex, sendIndex+sendLength);
var postData = { id: 0, gcode: gcodePart.join("\n"), first: firstOne, last: lastOne};
var self = this;
$.ajax({
url: this.wifiboxURL + "/printer/print",
type: "POST",
data: postData,
dataType: 'json',
timeout: this.timeoutTime,
success: function(data){
console.log("Printer:sendPrintPart response: ",data);
if(data.status == "success") {
if (lastOne) {
console.log("Printer:sendPrintPart:gcode sending completed");
this.gcode = [];
} else {
self.sendPrintPart(sendIndex + sendLength, sendLength);
}
}
}
}).fail(function() {
console.log("Printer:sendPrintPart: failed");
clearTimeout(self.retrySendPrintPartDelay);
self.retrySendPrintPartDelay = setTimeout(function() { self.sendPrintPart(sendIndex, sendLength) },self.retryDelay); // retry after delay
});
}
this.stop = function() {
console.log("Printer:stop");
var postData = { id: 0 };
$.post( this.wifiboxURL + "/printer/stop", postData , function(e) {
console.log("Printer:stop response: " + e);
if (e.success = true) {
console.log(" success");
}
});
var postData = { id: 0 };
$.ajax({
url: this.wifiboxURL + "/printer/stop",
type: "POST",
data: postData,
dataType: 'json',
timeout: this.timeoutTime,
success: function(data){
console.log("Printer:stop response: ", data);
}
}).fail(function() {
console.log("Printer:stop: failed");
clearTimeout(self.retryStopDelay);
self.retryStopDelay = setTimeout(function() { self.stop() },self.retryDelay); // retry after delay
});
}
this.checkTemperature = function() {
@ -91,16 +175,11 @@ function Printer() {
$(document).trigger(Printer.UPDATE);
self.checkTemperatureDelay = setTimeout(function() { self.checkTemperature() },self.checkTemperatureInterval);
},
error: function(jqXHR, status, errorThrown){ //the status returned will be "timeout"
//console.log("Printer:temperature error. Status: ",status,' errorThrown: ',errorThrown);
switch(status) {
case 'timeout':
console.log("retrieving printer/temperature timeout");
self.checkTemperature();
break;
}
}
}).fail(function() {
console.log("Printer:checkTemperature: failed");
clearTimeout(self.retryCheckTemperatureDelay);
self.retryCheckTemperatureDelay = setTimeout(function() { self.checkTemperature() },self.retryDelay); // retry after delay
});
}
this.checkProgress = function() {
@ -127,16 +206,11 @@ function Printer() {
$(document).trigger(Printer.UPDATE);
self.checkProgressDelay = setTimeout(function() { self.checkProgress() },self.checkProgressInterval);
},
error: function(jqXHR, status, errorThrown){ //the status returned will be "timeout"
//console.log("Printer:progress error. Status: ",status,' errorThrown: ',errorThrown);
switch(status) {
case 'timeout':
self.checkProgress();
console.log("retrieving printer/progress timeout");
break;
}
}
}).fail(function() {
console.log("Printer:checkProgress: failed");
clearTimeout(self.retryCheckProgressDelay);
self.retryCheckProgressDelay = setTimeout(function() { self.checkProgress() },self.retryDelay); // retry after delay
});
}

View File

@ -241,8 +241,9 @@ function print(e) {
if (_points.length > 2) {
setState(PRINTING_STATE);
var gencode = generate_gcode();
startPrint(gencode);
var gcode = generate_gcode();
//startPrint(gencode);
printer.print(gcode);
// console.log("");
// console.log("");
@ -254,7 +255,7 @@ function print(e) {
// console.log("");
// console.log("");
$("#textdump").text(gencode.join("\n"));
$("#textdump").text(gcode.join("\n"));
// copyToClipboard(gencode);
//*/
} else {

View File

@ -1,63 +0,0 @@
var sendIndex;
var sendLength;
var data = "";
function startPrint(gcode) {
console.log("f:startPrint()");
console.log("total # of lines: " + gcode.length);
data = gcode;
for (i = 0; i < data.length; i++) {
data[i] += " (" + i + ")";
}
sendIndex = 0;
sendLength = 2000; // 2000 regels
sendGCodeSlice(sendIndex, sendLength);
}
function sendGCodeSlice(startIndex, sendAmt) {
console.log("f:sendGCodeSlice >> startIndex:" + startIndex + ", sendAmt:" + sendAmt);
if (typeof startIndex == "number" && typeof sendAmt == "number") {
var lastOne = false;
if (data.length < (startIndex + sendAmt)) {
console.log("f:senGCodeSlice >> not enough data left for full slice, sending smaller (and last) one");
sendAmt = data.length - startIndex;
lastOne = true;
}
var _tmp = data.slice(startIndex, startIndex+sendAmt);
// console.log("f:senGCodeSlice >> _tmp.length:" + _tmp.length);
// $.post("/doodle3d.of", { data:data }, function(data) {
// btnPrint.disabled = false;
// });
var firstOne = false;
if (startIndex == 0) { firstOne = true; }
var postData = { id: 0, gcode: _tmp.join("\n"), first: firstOne, last: lastOne};
$.post( wifiboxURL + "/printer/print", postData , function(e) {
console.log("sendBoy callback: " + e);
// console.log(e);
// console.log("e.success: " + e.success);
if (e.success = true) {
if (lastOne) {
console.log("f:sendGCodeSlice >> DONE!");
} else {
sendGCodeSlice(startIndex + sendAmt, sendAmt);
}
}
})
} else {
console.log(" something wrong");
}
}
function sendBoy(sendObj, callback) {
console.log("f:sendBoy() (dummy kastje) >> data length: " + sendObj.data.length + ", lastOne: " + sendObj.last);
console.log("");
if (callback != undefined) callback({success:true});
}

View File

@ -107,13 +107,13 @@ function generate_gcode(callback) {
if (firstLayerSlow) {
//gcode.push("M220 S20"); //slow speed
speed = bottomSpeed;
console.log("> speed: ",speed);
//console.log("> speed: ",speed);
}
} else if (layer == 2) { ////////LET OP, pas bij layer 2 weer op normale snelheid ipv layer 1
gcode.push("M106"); //fan on
//gcode.push("M220 S100"); //normal speed
speed = normalSpeed;
console.log("> speed: ",speed);
//console.log("> speed: ",speed);
}
var curLayerCommand = 0;