2012-12-25 10:55:00 +00:00
|
|
|
# coding=utf-8
|
2012-12-28 19:37:40 +00:00
|
|
|
__author__ = "Gina Häußge <osd@foosel.net>"
|
2013-01-01 20:04:00 +00:00
|
|
|
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
import time
|
2012-12-28 19:37:40 +00:00
|
|
|
from threading import Thread
|
2012-12-29 19:57:02 +00:00
|
|
|
import datetime
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-31 12:18:54 +00:00
|
|
|
import printer_webui.util.comm as comm
|
|
|
|
from printer_webui.util import gcodeInterpreter
|
2013-01-01 20:04:00 +00:00
|
|
|
|
|
|
|
from printer_webui.settings import settings
|
2012-12-28 19:37:40 +00:00
|
|
|
|
|
|
|
def getConnectionOptions():
|
|
|
|
"""
|
|
|
|
Retrieves the available ports, baudrates, prefered port and baudrate for connecting to the printer.
|
|
|
|
"""
|
|
|
|
return {
|
2012-12-31 12:18:54 +00:00
|
|
|
"ports": comm.serialList(),
|
|
|
|
"baudrates": comm.baudrateList(),
|
2013-01-01 20:04:00 +00:00
|
|
|
"portPreference": settings().get("serial", "port"),
|
|
|
|
"baudratePreference": settings().getInt("serial", "baudrate")
|
2012-12-28 19:37:40 +00:00
|
|
|
}
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-29 19:57:02 +00:00
|
|
|
def _getFormattedTimeDelta(d):
|
|
|
|
hours = d.seconds // 3600
|
|
|
|
minutes = (d.seconds % 3600) // 60
|
|
|
|
seconds = d.seconds % 60
|
|
|
|
return "%02d:%02d:%02d" % (hours, minutes, seconds)
|
|
|
|
|
2012-12-25 10:55:00 +00:00
|
|
|
class Printer():
|
|
|
|
def __init__(self):
|
|
|
|
# state
|
|
|
|
self.temps = {
|
2012-12-28 19:37:40 +00:00
|
|
|
"actual": [],
|
|
|
|
"target": [],
|
|
|
|
"actualBed": [],
|
|
|
|
"targetBed": []
|
2012-12-25 10:55:00 +00:00
|
|
|
}
|
|
|
|
self.messages = []
|
|
|
|
self.log = []
|
|
|
|
self.state = None
|
|
|
|
self.currentZ = None
|
|
|
|
self.progress = None
|
|
|
|
self.printTime = None
|
|
|
|
self.printTimeLeft = None
|
|
|
|
self.currentTemp = None
|
|
|
|
self.currentBedTemp = None
|
2012-12-26 14:03:34 +00:00
|
|
|
self.currentTargetTemp = None
|
|
|
|
self.currentBedTargetTemp = None
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
self.gcode = None
|
|
|
|
self.gcodeList = None
|
|
|
|
self.filename = None
|
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
self.gcodeLoader = None
|
|
|
|
|
2012-12-29 14:41:23 +00:00
|
|
|
self.feedrateModifierMapping = {"outerWall": "WALL-OUTER", "innerWall": "WALL_INNER", "fill": "FILL", "support": "SUPPORT"}
|
|
|
|
|
2013-01-03 14:25:20 +00:00
|
|
|
self.timelapse = None
|
|
|
|
|
2012-12-25 10:55:00 +00:00
|
|
|
# comm
|
|
|
|
self.comm = None
|
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
def connect(self, port=None, baudrate=None):
|
|
|
|
"""
|
|
|
|
Connects to the printer. If port and/or baudrate is provided, uses these settings, otherwise autodetection
|
|
|
|
will be attempted.
|
|
|
|
"""
|
|
|
|
if self.comm is not None:
|
2012-12-25 10:55:00 +00:00
|
|
|
self.comm.close()
|
2012-12-31 12:18:54 +00:00
|
|
|
self.comm = comm.MachineCom(port, baudrate, callbackObject=self)
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
def disconnect(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Closes the connection to the printer.
|
|
|
|
"""
|
|
|
|
if self.comm is not None:
|
2012-12-25 10:55:00 +00:00
|
|
|
self.comm.close()
|
|
|
|
self.comm = None
|
|
|
|
|
|
|
|
def command(self, command):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Sends a single gcode command to the printer.
|
|
|
|
"""
|
2012-12-26 14:03:34 +00:00
|
|
|
self.commands([command])
|
|
|
|
|
|
|
|
def commands(self, commands):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Sends multiple gcode commands (provided as a list) to the printer.
|
|
|
|
"""
|
2012-12-26 14:03:34 +00:00
|
|
|
for command in commands:
|
|
|
|
self.comm.sendCommand(command)
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-29 14:41:23 +00:00
|
|
|
def setFeedrateModifier(self, structure, percentage):
|
|
|
|
if (not self.feedrateModifierMapping.has_key(structure)) or percentage < 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.comm.setFeedrateModifier(self.feedrateModifierMapping[structure], percentage / 100.0)
|
|
|
|
|
2013-01-03 14:25:20 +00:00
|
|
|
def setTimelapse(self, timelapse):
|
|
|
|
if self.timelapse is not None and self.isPrinting():
|
|
|
|
self.timelapse.onPrintjobStopped()
|
|
|
|
del self.timelapse
|
|
|
|
self.timelapse = timelapse
|
|
|
|
|
|
|
|
def getTimelapse(self):
|
|
|
|
return self.timelapse
|
|
|
|
|
2012-12-25 10:55:00 +00:00
|
|
|
def mcLog(self, message):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Callback method for the comm object, called upon log output.
|
|
|
|
Log line is stored in internal buffer, which is truncated to the last 300 lines.
|
|
|
|
"""
|
2012-12-25 10:55:00 +00:00
|
|
|
self.log.append(message)
|
|
|
|
self.log = self.log[-300:]
|
|
|
|
|
|
|
|
def mcTempUpdate(self, temp, bedTemp, targetTemp, bedTargetTemp):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Callback method for the comm object, called upon receiving new temperature information.
|
|
|
|
Temperature information (actual and target) for print head and print bed is stored in corresponding
|
|
|
|
temperature history (including timestamp), history is truncated to 300 entries.
|
|
|
|
"""
|
2012-12-26 14:03:34 +00:00
|
|
|
currentTime = int(time.time() * 1000)
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
self.temps["actual"].append((currentTime, temp))
|
|
|
|
self.temps["actual"] = self.temps["actual"][-300:]
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
self.temps["target"].append((currentTime, targetTemp))
|
|
|
|
self.temps["target"] = self.temps["target"][-300:]
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
self.temps["actualBed"].append((currentTime, bedTemp))
|
|
|
|
self.temps["actualBed"] = self.temps["actualBed"][-300:]
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
self.temps["targetBed"].append((currentTime, bedTargetTemp))
|
|
|
|
self.temps["targetBed"] = self.temps["targetBed"][-300:]
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
self.currentTemp = temp
|
2012-12-26 14:03:34 +00:00
|
|
|
self.currentTargetTemp = targetTemp
|
2012-12-25 10:55:00 +00:00
|
|
|
self.currentBedTemp = bedTemp
|
2012-12-26 14:03:34 +00:00
|
|
|
self.currentBedTargetTemp = bedTargetTemp
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
def mcStateChange(self, state):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Callback method for the comm object, called if the connection state changes.
|
|
|
|
New state is stored for retrieval by the frontend.
|
|
|
|
"""
|
2013-01-03 14:25:20 +00:00
|
|
|
oldState = self.state
|
2012-12-25 10:55:00 +00:00
|
|
|
self.state = state
|
|
|
|
|
2013-01-03 14:25:20 +00:00
|
|
|
if self.timelapse is not None:
|
|
|
|
if oldState == self.comm.STATE_PRINTING:
|
|
|
|
self.timelapse.onPrintjobStopped()
|
|
|
|
elif state == self.comm.STATE_PRINTING:
|
2013-01-03 20:38:46 +00:00
|
|
|
self.timelapse.onPrintjobStarted(self.filename)
|
2013-01-03 14:25:20 +00:00
|
|
|
|
2012-12-25 10:55:00 +00:00
|
|
|
def mcMessage(self, message):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Callback method for the comm object, called upon message exchanges via serial.
|
|
|
|
Stores the message in the message buffer, truncates buffer to the last 300 lines.
|
|
|
|
"""
|
2012-12-25 10:55:00 +00:00
|
|
|
self.messages.append(message)
|
2012-12-26 14:03:34 +00:00
|
|
|
self.messages = self.messages[-300:]
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
def mcProgress(self, lineNr):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Callback method for the comm object, called upon any change in progress of the printjob.
|
|
|
|
Triggers storage of new values for printTime, printTimeLeft and the current line.
|
|
|
|
"""
|
2012-12-25 10:55:00 +00:00
|
|
|
self.printTime = self.comm.getPrintTime()
|
|
|
|
self.printTimeLeft = self.comm.getPrintTimeRemainingEstimate()
|
2013-01-03 14:25:20 +00:00
|
|
|
oldProgress = self.progress;
|
2012-12-25 10:55:00 +00:00
|
|
|
self.progress = self.comm.getPrintPos()
|
2013-01-03 14:25:20 +00:00
|
|
|
if self.timelapse is not None:
|
|
|
|
self.timelapse.onPrintjobProgress(oldProgress, self.progress, int(round(self.progress * 100 / len(self.gcodeList))))
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
def mcZChange(self, newZ):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Callback method for the comm object, called upon change of the z-layer.
|
|
|
|
"""
|
2013-01-03 14:25:20 +00:00
|
|
|
oldZ = self.currentZ
|
2012-12-25 10:55:00 +00:00
|
|
|
self.currentZ = newZ
|
2013-01-03 14:25:20 +00:00
|
|
|
if self.timelapse is not None:
|
|
|
|
self.timelapse.onZChange(oldZ, self.currentZ)
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
def onGcodeLoaded(self, gcodeLoader):
|
|
|
|
"""
|
|
|
|
Callback method for the gcode loader, gets called when the gcode for the new printjob has finished loading.
|
|
|
|
Takes care to set filename, gcode and commandlist from the gcode loader and reset print job progress.
|
|
|
|
"""
|
|
|
|
self.filename = gcodeLoader.filename
|
|
|
|
self.gcode = gcodeLoader.gcode
|
|
|
|
self.gcodeList = gcodeLoader.gcodeList
|
|
|
|
self.currentZ = None
|
|
|
|
self.progress = None
|
|
|
|
self.printTime = None
|
|
|
|
self.printTimeLeft = None
|
|
|
|
|
|
|
|
self.gcodeLoader = None
|
|
|
|
|
2012-12-25 10:55:00 +00:00
|
|
|
def jobData(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Returns statistics regarding the currently loaded printjob, or None if no printjob is loaded.
|
|
|
|
"""
|
|
|
|
if self.gcode is not None:
|
2012-12-25 10:55:00 +00:00
|
|
|
formattedPrintTime = None
|
|
|
|
if (self.printTime):
|
2012-12-29 19:57:02 +00:00
|
|
|
formattedPrintTime = _getFormattedTimeDelta(datetime.timedelta(seconds=self.printTime))
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
formattedPrintTimeLeft = None
|
|
|
|
if (self.printTimeLeft):
|
2012-12-29 19:57:02 +00:00
|
|
|
formattedPrintTimeLeft = _getFormattedTimeDelta(datetime.timedelta(minutes=self.printTimeLeft))
|
|
|
|
|
|
|
|
formattedPrintTimeEstimation = None
|
|
|
|
formattedFilament = None
|
|
|
|
if self.gcode:
|
|
|
|
if self.gcode.totalMoveTimeMinute:
|
|
|
|
formattedPrintTimeEstimation = _getFormattedTimeDelta(datetime.timedelta(minutes=self.gcode.totalMoveTimeMinute))
|
|
|
|
if self.gcode.extrusionAmount:
|
2013-01-01 20:04:00 +00:00
|
|
|
formattedFilament = "%.2fm" % (self.gcode.extrusionAmount / 1000)
|
2012-12-29 19:57:02 +00:00
|
|
|
|
|
|
|
formattedCurrentZ = None
|
|
|
|
if self.currentZ:
|
|
|
|
formattedCurrentZ = "%.2f mm" % (self.currentZ)
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
data = {
|
2012-12-28 19:37:40 +00:00
|
|
|
"filename": self.filename,
|
2012-12-29 19:57:02 +00:00
|
|
|
"currentZ": formattedCurrentZ,
|
2012-12-28 19:37:40 +00:00
|
|
|
"line": self.progress,
|
|
|
|
"totalLines": len(self.gcodeList),
|
|
|
|
"printTime": formattedPrintTime,
|
|
|
|
"printTimeLeft": formattedPrintTimeLeft,
|
2012-12-29 19:57:02 +00:00
|
|
|
"filament": formattedFilament,
|
|
|
|
"estimatedPrintTime": formattedPrintTimeEstimation
|
2012-12-25 10:55:00 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
data = None
|
|
|
|
return data
|
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
def gcodeState(self):
|
|
|
|
if self.gcodeLoader is not None:
|
|
|
|
return {
|
|
|
|
"filename": self.gcodeLoader.filename,
|
|
|
|
"progress": self.gcodeLoader.progress
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2012-12-29 14:41:23 +00:00
|
|
|
def feedrateState(self):
|
|
|
|
if self.comm is not None:
|
|
|
|
feedrateModifiers = self.comm.getFeedrateModifiers()
|
|
|
|
result = {}
|
|
|
|
for structure in self.feedrateModifierMapping.keys():
|
|
|
|
if (feedrateModifiers.has_key(self.feedrateModifierMapping[structure])):
|
|
|
|
result[structure] = int(round(feedrateModifiers[self.feedrateModifierMapping[structure]] * 100))
|
|
|
|
else:
|
|
|
|
result[structure] = 100
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2012-12-25 10:55:00 +00:00
|
|
|
def getStateString(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Returns a human readable string corresponding to the current communication state.
|
|
|
|
"""
|
|
|
|
if self.comm is None:
|
|
|
|
return "Offline"
|
2012-12-25 10:55:00 +00:00
|
|
|
else:
|
|
|
|
return self.comm.getStateString()
|
|
|
|
|
|
|
|
def isClosedOrError(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
return self.comm is None or self.comm.isClosedOrError()
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
def isOperational(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
return self.comm is not None and self.comm.isOperational()
|
2012-12-25 10:55:00 +00:00
|
|
|
|
2012-12-26 14:03:34 +00:00
|
|
|
def isPrinting(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
return self.comm is not None and self.comm.isPrinting()
|
2012-12-26 14:03:34 +00:00
|
|
|
|
|
|
|
def isPaused(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
return self.comm is not None and self.comm.isPaused()
|
2012-12-26 14:03:34 +00:00
|
|
|
|
|
|
|
def isError(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
return self.comm is not None and self.comm.isError()
|
2012-12-26 14:03:34 +00:00
|
|
|
|
|
|
|
def isReady(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
return self.gcodeLoader is None and self.gcodeList and len(self.gcodeList) > 0
|
|
|
|
|
|
|
|
def isLoading(self):
|
|
|
|
return self.gcodeLoader is not None
|
2012-12-26 14:03:34 +00:00
|
|
|
|
2012-12-25 10:55:00 +00:00
|
|
|
def loadGcode(self, file):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Loads the gcode from the given file as the new print job.
|
|
|
|
Aborts if the printer is currently printing or another gcode file is currently being loaded.
|
|
|
|
"""
|
|
|
|
if (self.comm is not None and self.comm.isPrinting()) or (self.gcodeLoader is not None):
|
2012-12-25 10:55:00 +00:00
|
|
|
return
|
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
self.filename = None
|
|
|
|
self.gcode = None
|
|
|
|
self.gcodeList = None
|
|
|
|
|
|
|
|
self.gcodeLoader = GcodeLoader(file, self)
|
|
|
|
self.gcodeLoader.start()
|
2012-12-25 10:55:00 +00:00
|
|
|
|
|
|
|
def startPrint(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Starts the currently loaded print job.
|
|
|
|
Only starts if the printer is connected and operational, not currently printing and a printjob is loaded
|
|
|
|
"""
|
|
|
|
if self.comm is None or not self.comm.isOperational():
|
2012-12-25 10:55:00 +00:00
|
|
|
return
|
2012-12-28 19:37:40 +00:00
|
|
|
if self.gcodeList is None:
|
2012-12-25 10:55:00 +00:00
|
|
|
return
|
|
|
|
if self.comm.isPrinting():
|
|
|
|
return
|
|
|
|
self.currentZ = -1
|
|
|
|
self.comm.printGCode(self.gcodeList)
|
|
|
|
|
|
|
|
def togglePausePrint(self):
|
2012-12-28 19:37:40 +00:00
|
|
|
"""
|
|
|
|
Pause the current printjob.
|
|
|
|
"""
|
|
|
|
if self.comm is None:
|
2012-12-25 10:55:00 +00:00
|
|
|
return
|
|
|
|
self.comm.setPause(not self.comm.isPaused())
|
|
|
|
|
2012-12-28 19:37:40 +00:00
|
|
|
def cancelPrint(self, disableMotorsAndHeater=True):
|
|
|
|
"""
|
|
|
|
Cancel the current printjob.
|
|
|
|
"""
|
|
|
|
if self.comm is None:
|
2012-12-25 10:55:00 +00:00
|
|
|
return
|
|
|
|
self.comm.cancelPrint()
|
2012-12-28 19:37:40 +00:00
|
|
|
if disableMotorsAndHeater:
|
|
|
|
self.commands(["M84", "M104 S0", "M140 S0"]) # disable motors, switch off heaters
|
|
|
|
|
|
|
|
# reset line, height, print time
|
|
|
|
self.currentZ = None
|
|
|
|
self.progress = None
|
|
|
|
self.printTime = None
|
|
|
|
self.printTimeLeft = None
|
|
|
|
|
|
|
|
class GcodeLoader(Thread):
|
|
|
|
"""
|
|
|
|
The GcodeLoader takes care of loading a gcode-File from disk and parsing it into a gcode object in a separate
|
|
|
|
thread while constantly notifying interested listeners about the current progress.
|
|
|
|
The progress is returned as a float value between 0 and 1 which is to be interpreted as the percentage of completion.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, filename, printerCallback):
|
|
|
|
Thread.__init__(self);
|
|
|
|
|
|
|
|
self.printerCallback = printerCallback;
|
|
|
|
|
|
|
|
self.filename = filename
|
|
|
|
self.progress = None
|
|
|
|
|
|
|
|
self.gcode = None
|
|
|
|
self.gcodeList = None
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
#Send an initial M110 to reset the line counter to zero.
|
|
|
|
prevLineType = lineType = "CUSTOM"
|
|
|
|
gcodeList = ["M110"]
|
|
|
|
with open(self.filename, "r") as file:
|
|
|
|
for line in file:
|
|
|
|
if line.startswith(";TYPE:"):
|
|
|
|
lineType = line[6:].strip()
|
|
|
|
if ";" in line:
|
|
|
|
line = line[0:line.find(";")]
|
|
|
|
line = line.strip()
|
|
|
|
if len(line) > 0:
|
|
|
|
if prevLineType != lineType:
|
|
|
|
gcodeList.append((line, lineType, ))
|
|
|
|
else:
|
|
|
|
gcodeList.append(line)
|
|
|
|
prevLineType = lineType
|
|
|
|
|
|
|
|
self.gcodeList = gcodeList
|
|
|
|
self.gcode = gcodeInterpreter.gcode()
|
|
|
|
self.gcode.progressCallback = self.onProgress
|
|
|
|
self.gcode.loadList(self.gcodeList)
|
|
|
|
|
|
|
|
self.printerCallback.onGcodeLoaded(self)
|
|
|
|
|
|
|
|
def onProgress(self, progress):
|
|
|
|
self.progress = progress
|
2012-12-25 10:55:00 +00:00
|
|
|
|