diff --git a/src/octoprint/comm/protocol/__init__.py b/src/octoprint/comm/protocol/__init__.py index 4c46e2481..f7dc4f789 100644 --- a/src/octoprint/comm/protocol/__init__.py +++ b/src/octoprint/comm/protocol/__init__.py @@ -1,5 +1,7 @@ # coding=utf-8 +import os import time +from octoprint.events import eventManager, Events __author__ = "Gina Häußge " __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" @@ -19,19 +21,16 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): self._protocolListener = protocolListener self._state = State.OFFLINE + self._heatupStartTime = None + self._heatupTimeLost = 0.0 + self._currentFile = None + self._sdAvailable = False + self._sdFiles = [] + self._error = None - # temperatures - self._temp = {} - self._targetTemp = {} - self._bedTemp = 0 - self._bedTargetTemp = 0 - - self._tempOffset = {} - self._bedTempOffset = 0 - ##~~ public def connect(self, opt): @@ -52,6 +51,10 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): def startPrint(self): if self._currentFile is None: raise ValueError("No file selected for printing") + + self._heatupStartTime = None + self._heatupTimeLost = 0.0 + self._changeState(State.PRINTING) self._currentFile.start() @@ -64,7 +67,27 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): def cancelPrint(self): pass - ##~~ private + def refreshSdFiles(self): + pass + + ##~~ granular state detection for implementations + + def _isHeatingUp(self): + return self._heatupStartTime is not None + + def _isPrinting(self): + return self._state == State.PRINTING or self._state == State.PAUSED + + def _isSdPrinting(self): + return isinstance(self._currentFile, PrintingSdFileInformation) and self._isBusy() + + def _isStreaming(self): + return self._state == State.STREAMING + + def _isBusy(self): + return self._isPrinting() or self._isStreaming() + + ##~~ state tracking and reporting to protocol listener def _changeState(self, newState): oldState = self._state @@ -84,9 +107,133 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): def _temperatureUpdated(self, temperatureData): pass - def onError(self, error): - self._changeState(State.ERROR) - self._error = error + def _reportProgress(self): + progress = { + "completion": self.__getPrintCompletion(), + "filepos": self.__getPrintFilepos(), + "printTime": self.__getPrintTime(), + "printTimeLeft": self.__getPrintTimeRemainingEstimate() + } + + self._progressReported(progress) + if self._protocolListener is not None: + self._protocolListener.onProgress(self, progress) + + def _progressReported(self, progress): + pass + + def _changeSdState(self, sdAvailable): + self._sdAvailable = sdAvailable + self._sdStateChanged(sdAvailable) + if self._sdAvailable: + self.refreshSdFiles() + else: + self._sdFiles = [] + if self._protocolListener is not None: + self._protocolListener.onSdStateChange(self, sdAvailable) + + def _sdStateChanged(self, sdAvailable): + pass + + def _selectFile(self, currentFile): + self._currentFile = currentFile + self._fileSelected(currentFile) + if self._protocolListener is not None: + self._protocolListener.onFileSelected(self, currentFile.getFilename(), currentFile.getFilesize(), currentFile.getFileLocation()) + eventManager().fire(Events.FILE_SELECTED, { + "file": self._currentFile.getFilename(), + "origin": self._currentFIle.getFileLocation() + }) + + def _fileSelected(self, currentFile): + pass + + def _sendSdFiles(self): + self._sdFilesSent() + if self._protocolListener is not None: + self._protocolListener.onSdFiles(self, self._sdFiles) + + def _sdFilesSent(self): + pass + + def _finishPrintjob(self): + self._changeState(State.OPERATIONAL) + if self._protocolListener is not None: + self._protocolListener.onPrintjobDone(self) + eventManager().fire(Events.PRINT_DONE, { + "file": self._currentFile.getFilename(), + "filename": os.path.basename(self._currentFile.getFilename()), + "origin": self._currentFile.getFileLocation(), + "time": time.time() - self._currentFile.getStartTime() + }) + + def _printJobFinished(self): + pass + + def _addSdFile(self, filename): + """ + Adds a file to the SD file list + + :param filename: the filename to add to the list + """ + self._sdFiles.append(filename) + self._sdFileAdded(filename) + + def _sdFileAdded(self, filename): + """ + Called when a file has been added to the SD file list, can be used by the underyling protocol implementation + to react to that. + + :param filename: the filename to add to the list + """ + pass + + def _addSdFiles(self, filenames): + """ + Adds a list of files to the SD file list + + :param filenames: the list of filenames to add to the list + """ + for filename in filenames: + self._sdFiles.append(filename) + self._sdFilesAdded(filenames) + + def _sdFilesAdded(self, filenames): + """ + Called when a list of files has been added to the SD file list, can be used by the underlying protocol + implementation to react to that. + + :param filenames: the filenames added to the list + """ + pass + + def _resetSdFiles(self): + """ + Resets the SD file list + """ + self._sdFiles = [] + self._sdFilesReset() + + def _sdFilesReset(self): + """ + Called when the list of SD files has been reset, can be used in the underlying protocol implementation to + react to that + """ + pass + + def _heatupDetected(self): + """ + Called when the underlying protocol detects the beginning of a heat-up interval. + """ + self._heatupStartTime = time.time() + + def _heatupDone(self): + """ + Called when the underlying protocol detects the end of a heat-up interval. + """ + if self._heatupStartTime is not None: + self._heatupTimeLost += time.time() - self._heatupStartTime + self._heatupStartTime = None ##~~ StateReceiver @@ -103,6 +250,13 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): elif newState == TransportState.DISCONNECTED_WITH_ERROR or newState == TransportState.ERROR: self._changeState(State.ERROR) + ##~~ MessageReceiver + + def onError(self, error): + self._changeState(State.ERROR) + self._error = error + eventManager().fire(Events.ERROR, {"error": error}) + ##~~ LogReceiver def onLogRx(self, source, rx): @@ -126,6 +280,37 @@ class Protocol(MessageReceiver, StateReceiver, LogReceiver): if self._protocolListener is not None: self._protocolListener.onLogError(self, error) + ##~~ helpers + + def __getPrintCompletion(self): + if self._currentFile is None: + return None + return self._currentFile.getProgress() * 100 + + def __getPrintFilepos(self): + if self._currentFile is None: + return None + return self._currentFile.getFilepos() + + def __getPrintTime(self): + if self._currentFile is None or self._currentFile.getStartTime() is None: + return None + else: + return max(int(time.time() - self._currentFile.getStartTime() - self._heatupTimeLost), 0) + + def __getPrintTimeRemainingEstimate(self): + printTime = self.__getPrintTime() + if printTime is None: + return None + + printTime /= 60 + progress = self._currentFile.getProgress() + if progress: + printTimeTotal = printTime / progress + return int(printTimeTotal - printTime) + else: + return None + class ProtocolListener(object): def onStateChange(self, source, oldState, newState): @@ -134,6 +319,21 @@ class ProtocolListener(object): def onTemperatureUpdate(self, source, temperatureData): pass + def onProgress(self, source, progress): + pass + + def onFileSelected(self, source, filename, filesize, origin): + pass + + def onPrintjobDone(self, source): + pass + + def onSdStateChange(self, source, sdAvailable): + pass + + def onSdFiles(self, source, files): + pass + def onLogTx(self, source, tx): pass @@ -153,7 +353,6 @@ class State(object): PRINTING = "Printing" STREAMING = "Streaming" PAUSED = "Paused" - HEATING = "Heating" ERROR = "Error" diff --git a/src/octoprint/comm/protocol/repetier.py b/src/octoprint/comm/protocol/repetier.py new file mode 100644 index 000000000..3469eda01 --- /dev/null +++ b/src/octoprint/comm/protocol/repetier.py @@ -0,0 +1,12 @@ +# coding=utf-8 +__author__ = "Gina Häußge " +__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" +__copyright__ = "Copyright (C) 2014 Gina Häußge - Released under terms of the AGPLv3 License" + +from octoprint.comm.protocol.reprap import RepRapProtocol + + +class RepetierTextualProtocol(RepRapProtocol): + + def _useChecksum(self): + return True \ No newline at end of file diff --git a/src/octoprint/comm/protocol/reprap/__init__.py b/src/octoprint/comm/protocol/reprap/__init__.py index 8c07848e3..2a7daf859 100644 --- a/src/octoprint/comm/protocol/reprap/__init__.py +++ b/src/octoprint/comm/protocol/reprap/__init__.py @@ -1,12 +1,430 @@ # coding=utf-8 +import Queue +from collections import deque +from octoprint.events import eventManager, Events + __author__ = "Gina Häußge " __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2014 Gina Häußge - Released under terms of the AGPLv3 License" -from octoprint.comm.protocol import ProtocolListener, State -from octoprint.comm.protocol.reprap.protocol import RepRapProtocol -from octoprint.comm.transport.serialTransport import VirtualSerialTransport +import re +import threading +import time +from octoprint.comm.protocol import ProtocolListener, State, Protocol, PrintingSdFileInformation +from octoprint.comm.protocol.reprap.util import GcodeCommand, CommandQueue, PrintingGcodeFileInformation +from octoprint.comm.transport import TransportProperties from octoprint.filemanager.destinations import FileDestinations +from octoprint.gcodefiles import isGcodeFileName +from octoprint.comm.transport.serialTransport import SerialTransport + + +class RepRapProtocol(Protocol): + + ## Firmware messages + MESSAGE_OK = staticmethod(lambda line: line.startswith("ok")) + MESSAGE_START = staticmethod(lambda line: line.startswith("start")) + MESSAGE_WAIT = staticmethod(lambda line: line.startswith("wait")) + MESSAGE_TEMPERATURE = staticmethod(lambda line: " T:" in line or line.startswith("T:") or " T0:" in line or line.startswith("T0:")) + + MESSAGE_SD_INIT_OK = staticmethod(lambda line: line.lower() == "sd card ok") + MESSAGE_SD_INIT_FAIL = staticmethod(lambda line: "sd init fail" in line.lower() or "volume.init failed" in line.lower() or "openroot failed" in line.lower()) + MESSAGE_SD_FILE_OPENED = staticmethod(lambda line: line.lower().startswith("file opened")) + MESSAGE_SD_FILE_SELECTED = staticmethod(lambda line: line.lower().startswith("file selected")) + MESSAGE_SD_BEGIN_FILE_LIST = staticmethod(lambda line: line.lower().startswith("begin file list")) + MESSAGE_SD_END_FILE_LIST = staticmethod(lambda line: line.lower().startswith("end file list")) + MESSAGE_SD_PRINTING_BYTE = staticmethod(lambda line: "sd printing byte" in line.lower()) + MESSAGE_SD_NOT_PRINTING = staticmethod(lambda line: "not sd printing" in line.lower()) + MESSAGE_SD_DONE_PRINTING = staticmethod(lambda line: "done printing file" in line.lower()) + MESSAGE_SD_BEGIN_WRITING = staticmethod(lambda line: "writing to file" in line.lower()) + MESSAGE_SD_END_WRITING = staticmethod(lambda line: "done saving file" in line.lower()) + + MESSAGE_ERROR = staticmethod(lambda line: line.startswith("Error:") or line.startswith("!!")) + MESSAGE_ERROR_COMMUNICATION = staticmethod(lambda line: 'checksum mismatch' in line.lower() + or 'wrong checksum' in line.lower() + or 'line number is not last line number' in line.lower() + or 'expected line' in line.lower() + or 'no line number with checksum' in line.lower() + or 'no checksum with line number' in line.lower() + or 'missing checksum' in line.lower()) + MESSAGE_RESEND = staticmethod(lambda line: line.lower().startswith("resend") or line.lower().startswith("rs")) + + ## Commands + COMMAND_GET_TEMP = staticmethod(lambda: GcodeCommand("M105")) + COMMAND_SET_EXTRUDER_TEMP = staticmethod(lambda x, w: GcodeCommand("M109", s=x) if w else GcodeCommand("M104", s=x)) + COMMAND_SET_BED_TEMP = staticmethod(lambda x, w: GcodeCommand("M190", s=x) if w else GcodeCommand("M140", s=x)) + COMMAND_SET_TOOL = staticmethod(lambda t: GcodeCommand("T%d" % t)) + COMMAND_SD_REFRESH = staticmethod(lambda: GcodeCommand("M20")) + COMMAND_SD_INIT = staticmethod(lambda: GcodeCommand("M21")) + COMMAND_SD_RELEASE = staticmethod(lambda: GcodeCommand("M22")) + COMMAND_SD_SELECT_FILE = staticmethod(lambda name: GcodeCommand("M23", param=name)) + COMMAND_SD_START = staticmethod(lambda: GcodeCommand("M24")) + COMMAND_SD_PAUSE = staticmethod(lambda: GcodeCommand("M25")) + COMMAND_SD_SET_POS = staticmethod(lambda pos: GcodeCommand("M26", s=pos)) + COMMAND_SD_STATUS = staticmethod(lambda: GcodeCommand("M27")) + COMMAND_SD_BEGIN_WRITE = staticmethod(lambda name: GcodeCommand("M28", param=name)) + COMMAND_SD_END_WRITE = staticmethod(lambda name: GcodeCommand("M29", param=name)) + COMMAND_SD_DELETE = staticmethod(lambda name: GcodeCommand("M30", param=name)) + + ## Command types + COMMAND_TYPE_TEMPERATURE = "temperature" + COMMAND_TYPE_SD_PROGRESS = "sd_progress" + + # Regex matching temperature entries in line. Groups will be as follows: + # - 1: whole tool designator incl. optional toolNumber ("T", "Tn", "B") + # - 2: toolNumber, if given ("", "n", "") + # - 3: actual temperature + # - 4: whole target substring, if given (e.g. " / 22.0") + # - 5: target temperature + REGEX_TEMPERATURE = re.compile("(B|T(\d*)):\s*([-+]?\d*\.?\d+)(\s*\/?\s*([-+]?\d*\.?\d+))?") + + # Regex matching "File opened" message. Groups will be as follows: + # - 1: name of the file that got opened (e.g. "file.gco") + # - 2: size of the file that got opened, in bytes, parseable to integer (e.g. "2392010") + REGEX_FILE_OPENED = re.compile("File opened:\s*(.*?)\s+Size:\s*([0-9]*)") + + # Regex matching printing byte message. Groups will be as follows: + # - 1: current file position + # - 2: file size + REGEX_SD_PRINTING_BYTE = re.compile("([0-9]*)/([0-9]*)") + + def __init__(self, transportFactory, protocolListener=None): + Protocol.__init__(self, transportFactory, protocolListener) + + self._lastTemperatureUpdate = time.time() + self._lastSdProgressUpdate = time.time() + + self._startSeen = False + self._receivingSdFileList = False + + self._sendQueue = CommandQueue() + + self._clearForSend = threading.Event() + self._clearForSend.clear() + + self._lastLines = deque([], 50) + self._resendDelta = None + + self._sendQueueProcessing = True + self._thread = threading.Thread(target=self._handleSendQueue, name="SendQueueHandler") + self._thread.daemon = True + self._thread.start() + + self._currentLine = 1 + self._state = State.OFFLINE + + # enqueue our first temperature query so it get's sent right on establishment of the connection + self._sendTemperatureQuery(withType=True) + + + def selectFile(self, filename, origin): + if origin == FileDestinations.SDCARD: + if not self._sdAvailable: + return + self._send(RepRapProtocol.COMMAND_SD_SELECT_FILE(filename), highPriority=True) + else: + self._currentFile = PrintingGcodeFileInformation(filename, None) + + def startPrint(self): + wasPaused = self._state == State.PAUSED + + Protocol.startPrint(self) + if isinstance(self._currentFile, PrintingSdFileInformation): + if wasPaused: + self._send(RepRapProtocol.COMMAND_SD_SET_POS(0)) + self._currentFile.setFilepos(0) + self._send(RepRapProtocol.COMMAND_SD_START()) + else: + self._sendNext() + + def refreshSdFiles(self): + if not self._sdAvailable: + return + + Protocol.refreshSdFiles(self) + self._send(RepRapProtocol.COMMAND_SD_REFRESH()) + + def sendManually(self, command, highPriority=False): + if self._isStreaming(): + return + self._send(command, highPriority=highPriority, withChecksum=self._useChecksum()) + + ##~~ callback methods + + def onMessageReceived(self, source, message): + if self._transport != source: + return + + message = self._handleErrors(message.strip()) + + # SD file list + if self._receivingSdFileList and isGcodeFileName(message.strip().lower()) and not RepRapProtocol.MESSAGE_SD_END_FILE_LIST(message): + self._addSdFile(message.strip().lower()) + return + + ##~~ regular message processing + + # temperature updates + if RepRapProtocol.MESSAGE_TEMPERATURE(message): + self._processTemperatures(message) + if not RepRapProtocol.MESSAGE_OK(message): + self._heatupDetected() + + # sd state + elif RepRapProtocol.MESSAGE_SD_INIT_OK(message): + self._changeSdState(True) + elif RepRapProtocol.MESSAGE_SD_INIT_FAIL(message): + self._changeSdState(False) + + # sd progress + elif RepRapProtocol.MESSAGE_SD_PRINTING_BYTE(message): + match = RepRapProtocol.REGEX_SD_PRINTING_BYTE.search(message) + self._currentFile.setFilepos(int(match.group(1))) + self._reportProgress() + elif RepRapProtocol.MESSAGE_SD_DONE_PRINTING(message): + self._currentFile.setFilepos(0) + self._changeState(State.OPERATIONAL) + self._finishPrintjob() + + # sd file list + elif RepRapProtocol.MESSAGE_SD_BEGIN_FILE_LIST(message): + self._resetSdFiles() + self._receivingSdFileList = True + elif RepRapProtocol.MESSAGE_SD_END_FILE_LIST(message): + self._receivingSdFileList = False + self._sendSdFiles() + + # sd file selection + elif RepRapProtocol.MESSAGE_SD_FILE_OPENED(message): + match = RepRapProtocol.REGEX_FILE_OPENED.search(message) + self._selectFile(PrintingSdFileInformation(match.group(1), int(match.group(2)))) + + # sd file streaming + elif RepRapProtocol.MESSAGE_SD_BEGIN_WRITING(message): + self._changeState(State.PRINTING) + self._clearForSend.set() + elif RepRapProtocol.MESSAGE_SD_END_WRITING(message): + self.refreshSdFiles() + + # initial handshake with the firmware + if self._state == State.CONNECTED: + if RepRapProtocol.MESSAGE_START(message): + self._startSeen = True + elif RepRapProtocol.MESSAGE_WAIT(message) and self._startSeen: + self._clearForSend.set() + elif RepRapProtocol.MESSAGE_OK(message) and self._startSeen: + self._changeState(State.OPERATIONAL) + + # resend == roll back time a bit + if RepRapProtocol.MESSAGE_RESEND(message): + self._handleResendRequest(message) + + # ok == go ahead with sending + if RepRapProtocol.MESSAGE_OK(message): + if self._isHeatingUp(): + self._heatupDone() + + if self._isPrinting(): + self._sendNext() + if self._resendDelta is None: + if time.time() > self._lastTemperatureUpdate + 5: + self._sendTemperatureQuery(withType=True) + elif self._isSdPrinting() and time.time() > self._lastSdProgressUpdate + 5: + self._sendSdProgressQuery(withType=True) + self._clearForSend.set() + + def onTimeoutReceived(self, source): + if self._transport != source: + return + self._sendTemperatureQuery(withType=True) + + ##~~ private + + def _send(self, command, highPriority=False, commandType=None, withChecksum=None, withLinenumber=None): + if withChecksum is None: + withChecksum = self._useChecksum() + commandTuple = (str(command), withChecksum, withLinenumber) + + priority = 100 + if highPriority: + priority = 1 + if commandType is not None: + self._sendQueue.put((priority, commandTuple, commandType)) + else: + self._sendQueue.put((priority, commandTuple)) + + def _sendNext(self): + if self._resendDelta is not None: + command = self._lastLines[-self._resendDelta] + lineNumber = self._currentLine - self._resendDelta + self._send(command, withLinenumber=lineNumber) + + self._resendDelta -= 1 + if self._resendDelta <= 0: + self._resendDelta = None + else: + command = self._currentFile.getNext() + if command is None: + self._finishPrintjob() + return + + self._send(command) + self._reportProgress() + + def _sendTemperatureQuery(self, withType=False): + if withType: + self._send(RepRapProtocol.COMMAND_GET_TEMP(), commandType=RepRapProtocol.COMMAND_TYPE_TEMPERATURE) + else: + self._send(RepRapProtocol.COMMAND_GET_TEMP()) + self._lastTemperatureUpdate = time.time() + + def _sendSdProgressQuery(self, withType=False): + if withType: + self._send(RepRapProtocol.COMMAND_SD_STATUS(), commandType=RepRapProtocol.COMMAND_TYPE_SD_PROGRESS) + else: + self._send(RepRapProtocol.COMMAND_SD_STATUS()) + self._lastSdProgressUpdate = time.time() + + def _handleErrors(self, line): + # TODO Somehow figure out how to handle multiline errors here... + + # No matter the state, if we see an error, goto the error state and store the error for reference. + if line.startswith('Error:'): + #Oh YEAH, consistency. + # Marlin reports an MIN/MAX temp error as "Error:x\n: Extruder switched off. MAXTEMP triggered !\n" + # But a bed temp error is reported as "Error: Temperature heated bed switched off. MAXTEMP triggered !!" + # So we can have an extra newline in the most common case. Awesome work people. + if re.match('Error:[0-9]\n', line): + line = line.rstrip() + self._readline() + #Skip the communication errors, as those get corrected. + if RepRapProtocol.MESSAGE_ERROR_COMMUNICATION(line): + pass + elif not self._state == State.ERROR: + self.onError(line[6:]) + return line + + def _parseTemperatures(self, line): + result = {} + maxToolNum = 0 + for match in re.finditer(RepRapProtocol.REGEX_TEMPERATURE, line): + tool = match.group(1) + toolNumber = int(match.group(2)) if match.group(2) and len(match.group(2)) > 0 else None + if toolNumber > maxToolNum: + maxToolNum = toolNumber + + try: + actual = float(match.group(3)) + target = None + if match.group(4) and match.group(5): + target = float(match.group(5)) + + result[tool] = (toolNumber, actual, target) + except ValueError: + # catch conversion issues, we'll rather just not get the temperature update instead of killing the connection + pass + + if "T0" in result.keys() and "T" in result.keys(): + del result["T"] + + return maxToolNum, result + + def _processTemperatures(self, line): + maxToolNum, parsedTemps = self._parseTemperatures(line) + + result = {} + + # extruder temperatures + if not "T0" in parsedTemps.keys() and "T" in parsedTemps.keys(): + # only single reporting, "T" is our one and only extruder temperature + toolNum, actual, target = parsedTemps["T"] + result["tool0"] = { + "actual": actual, + "target": target + } + elif "T0" in parsedTemps.keys(): + for n in range(maxToolNum + 1): + tool = "T%d" % n + if not tool in parsedTemps.keys(): + continue + + toolNum, actual, target = parsedTemps[tool] + result["tool%d" % toolNum] = { + "actual": actual, + "target": target + } + + # bed temperature + if "B" in parsedTemps.keys(): + toolNum, actual, target = parsedTemps["B"] + result["bed"] = { + "actual": actual, + "target": target + } + + self._updateTemperature(result) + + def _handleResendRequest(self, message): + lineToResend = None + try: + lineToResend = int(message.replace("N:", " ").replace("N", " ").replace(":", " ").split()[-1]) + except: + if "rs" in message: + lineToResend = int(message.split()[1]) + + if lineToResend is not None: + self._resendDelta = self._currentLine - lineToResend + try: + while not self._sendQueue.empty(): + self._sendQueue.get(False) + except Queue.Empty: + pass + if self._resendDelta > len(self._lastLines) or len(self._lastLines) == 0 or self._resendDelta <= 0: + error = "Printer requested line %d but no sufficient history is available, can't resend" % lineToResend + # TODO self._logger.warn(error) + if self._isPrinting(): + # abort the print, there's nothing we can do to rescue it now + self.onError(error) + else: + # reset resend delta, we can't do anything about it + self._resendDelta = None + + def _useChecksum(self): + return not self._transportProperties[TransportProperties.FLOWCONTROL] and self._isBusy() + + ##~~ the actual send queue handling starts here + + def _handleSendQueue(self): + while self._sendQueueProcessing: + self._clearForSend.wait() + self._clearForSend.clear() + entry = self._sendQueue.get() + if len(entry) == 3: + priority, commandTuple, commandType = entry + else: + priority, commandTuple = entry + + command, withChecksum, lineNumber = commandTuple + preprocessedCommand = self._preprocessCommand(command, withChecksum=withChecksum, withLinenumber=lineNumber) + self._transport.send(preprocessedCommand) + + if withChecksum: + self._lastLines.append(command) + + def _preprocessCommand(self, command, withChecksum=False, withLinenumber=None): + if withChecksum: + if withLinenumber is not None: + commandToSend = "N%d %s" % (withLinenumber, command) + else: + commandToSend = "N%d %s" % (self._currentLine, command) + + checksum = reduce(lambda x, y: x ^ y, map(ord, commandToSend)) + + if withLinenumber is None: + self._currentLine += 1 + + return "%s*%d" % (commandToSend, checksum) + else: + return command + if __name__ == "__main__": from octoprint.settings import settings @@ -21,12 +439,21 @@ if __name__ == "__main__": if newState == State.OPERATIONAL and self.firstPrint: self.firstPrint = False print "Selecting file and starting print job" - protocol.selectFile("C:/Users/Gina/AppData/Roaming/OctoPrint/uploads/short.gcode", FileDestinations.LOCAL) + protocol.selectFile("C:/Users/Gina/AppData/Roaming/OctoPrint/uploads/ExtruderQuickConnectClip.gcode", FileDestinations.LOCAL) protocol.startPrint() def onTemperatureUpdate(self, source, temperatureData): print "### Temperature update: %r" % temperatureData + def onPrintjobDone(self, source): + print "### Printjob done!" + + def onSdFiles(self, source, files): + print "### SD Files: %r" % files + + def onProgress(self, source, progress): + print "### Progress: %r" % progress + def onLogTx(self, source, tx): print ">> %s" % tx @@ -36,8 +463,10 @@ if __name__ == "__main__": def onLogError(self, source, error): print "Error: %s" % error - protocol = RepRapProtocol(VirtualSerialTransport, protocolListener=DummyProtocolListener()) - protocol.connect({}) + protocol = RepRapProtocol(SerialTransport, protocolListener=DummyProtocolListener()) + #from octoprint.comm.protocol.repetier import RepetierTextualProtocol + #protocol = RepetierTextualProtocol(SerialTransport, protocolListener=DummyProtocolListener()) + protocol.connect({"port": "VIRTUAL"}) import time while True: diff --git a/src/octoprint/comm/protocol/reprap/protocol.py b/src/octoprint/comm/protocol/reprap/protocol.py deleted file mode 100644 index 4f5488ca9..000000000 --- a/src/octoprint/comm/protocol/reprap/protocol.py +++ /dev/null @@ -1,293 +0,0 @@ -# coding=utf-8 -__author__ = "Gina Häußge " -__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" -__copyright__ = "Copyright (C) 2014 Gina Häußge - Released under terms of the AGPLv3 License" - -import re -import threading -import time -from octoprint.comm.protocol import State, Protocol, PrintingSdFileInformation -from octoprint.comm.protocol.reprap.util import GcodeCommand, CommandQueue, PrintingGcodeFileInformation -from octoprint.comm.transport import TransportProperties -from octoprint.filemanager.destinations import FileDestinations - - -class RepRapProtocol(Protocol): - - MESSAGE_OK = staticmethod(lambda line: line.startswith("ok")) - MESSAGE_START = staticmethod(lambda line: line.startswith("start")) - MESSAGE_WAIT = staticmethod(lambda line: line.startswith("wait")) - - MESSAGE_SD_INIT_OK = staticmethod(lambda line: line == "SD card ok") - MESSAGE_SD_INIT_FAIL = staticmethod(lambda line: "SD init fail" in line or "volume.init failed" in line or "" in line) - MESSAGE_SD_FILE_OPENED = staticmethod(lambda line: line.startswith("File opened")) - MESSAGE_SD_FILE_SELECTED = staticmethod(lambda line: line.startswith("File selected")) - - MESSAGE_ERROR = staticmethod(lambda line: line.startswith("Error:") or line.startswith("!!")) - MESSAGE_ERROR_COMMUNICATION = staticmethod(lambda line: 'checksum mismatch' in line.lower() - or 'wrong checksum' in line.lower() - or 'line number is not last line number' in line.lower() - or 'expected line' in line.lower() - or 'no line number with checksum' in line.lower() - or 'no checksum with line number' in line.lower() - or 'missing checksum' in line.lower()) - MESSAGE_RESEND = staticmethod(lambda line: line.lower().startswith("resend") or line.lower().startswith("rs")) - - MESSAGE_TEMPERATURE = staticmethod(lambda x: ' T:' in x or x.startswith('T:') or ' T0:' in x or x.startswith('T0:')) - - COMMAND_GET_TEMP = staticmethod(lambda: GcodeCommand("M105")) - COMMAND_SET_EXTRUDER_TEMP = staticmethod(lambda x, w: GcodeCommand("M109", s=x) if w else GcodeCommand("M104", s=x)) - COMMAND_SET_BED_TEMP = staticmethod(lambda x, w: GcodeCommand("M190", s=x) if w else GcodeCommand("M140", s=x)) - COMMAND_SET_TOOL = staticmethod(lambda t: GcodeCommand("T%d" % t)) - COMMAND_SD_REFRESH = staticmethod(lambda: GcodeCommand("M20")) - COMMAND_SD_INIT = staticmethod(lambda: GcodeCommand("M21")) - COMMAND_SD_RELEASE = staticmethod(lambda: GcodeCommand("M22")) - COMMAND_SD_SELECT_FILE = staticmethod(lambda name: GcodeCommand("M23", param=name)) - COMMAND_SD_START = staticmethod(lambda: GcodeCommand("M24")) - COMMAND_SD_PAUSE = staticmethod(lambda: GcodeCommand("M25")) - COMMAND_SD_SET_POS = staticmethod(lambda pos: GcodeCommand("M26", s=pos)) - COMMAND_SD_STATUS = staticmethod(lambda: GcodeCommand("M27")) - COMMAND_SD_BEGIN_WRITE = staticmethod(lambda name: GcodeCommand("M28", param=name)) - COMMAND_SD_END_WRITE = staticmethod(lambda name: GcodeCommand("M29", param=name)) - COMMAND_SD_DELETE = staticmethod(lambda name: GcodeCommand("M30", param=name)) - - COMMAND_TYPE_TEMPERATURE = "temperature" - COMMAND_TYPE_SD_PROGRESS = "sd_progress" - - # Regex matching temperature entries in line. Groups will be as follows: - # - 1: whole tool designator incl. optional toolNumber ("T", "Tn", "B") - # - 2: toolNumber, if given ("", "n", "") - # - 3: actual temperature - # - 4: whole target substring, if given (e.g. " / 22.0") - # - 5: target temperature - REGEX_TEMPERATURE = re.compile("(B|T(\d*)):\s*([-+]?\d*\.?\d+)(\s*\/?\s*([-+]?\d*\.?\d+))?") - - # Regex matching "File opened" message. Groups will be as follows: - # - 1: name of the file that got opened (e.g. "file.gco") - # - 2: size of the file that got opened, in bytes, parseable to integer (e.g. "2392010") - REGEX_FILE_OPENED = re.compile("File opened:\s*(.*?)\s+Size:\s*([0-9]*)") - - def __init__(self, transportFactory, protocolListener=None): - Protocol.__init__(self, transportFactory, protocolListener) - - self._lastTemperatureUpdate = time.time() - self._startSeen = False - self._sdAvailable = False - - self._sendQueue = CommandQueue() - - self._clearForSend = threading.Event() - self._clearForSend.clear() - - self._sendQueueProcessing = True - self._thread = threading.Thread(target=self._handleSendQueue, name="SendQueueHandler") - self._thread.daemon = True - self._thread.start() - - self._currentLine = 1 - self._state = State.OFFLINE - - def selectFile(self, filename, origin): - if origin == FileDestinations.SDCARD: - if not self._sdAvailable: - return - self._send(RepRapProtocol.COMMAND_SD_SELECT_FILE(filename), highPriority=True) - else: - self._currentFile = PrintingGcodeFileInformation(filename, None) - - def startPrint(self): - wasPaused = self._state == State.PAUSED - - Protocol.startPrint(self) - - if isinstance(self._currentFile, PrintingSdFileInformation): - if wasPaused: - self._send(RepRapProtocol.COMMAND_SD_SET_POS(0)) - self._currentFile.setFilepos(0) - self._send(RepRapProtocol.COMMAND_SD_START()) - else: - self._sendNext() - - def onMessageReceived(self, source, message): - if self._transport != source: - return - - message = self._handleErrors(message.strip()) - - # message processing - if RepRapProtocol.MESSAGE_TEMPERATURE(message): - self._processTemperatures(message) - - elif RepRapProtocol.MESSAGE_SD_INIT_OK(message): - self._sdAvailable = True - - elif RepRapProtocol.MESSAGE_SD_INIT_FAIL(message): - self._sdAvailable = False - - elif RepRapProtocol.MESSAGE_SD_FILE_OPENED(message): - match = RepRapProtocol.REGEX_FILE_OPENED.search(message) - self._currentFile = PrintingSdFileInformation(match.group(1), int(match.group(2))) - - elif RepRapProtocol.MESSAGE_SD_FILE_SELECTED(message): - # trigger callback and stuff - pass - - # initial handshake with the firmware - if self._state == State.CONNECTED: - - if RepRapProtocol.MESSAGE_START(message): - self._startSeen = True - elif RepRapProtocol.MESSAGE_WAIT(message) and self._startSeen: - self._clearForSend.set() - self._sendTemperatureQuery(withType=True) - elif RepRapProtocol.MESSAGE_OK(message): - self._changeState(State.OPERATIONAL) - - # ok == go ahead with sending - if RepRapProtocol.MESSAGE_OK(message): - if self._state == State.PRINTING: - if time.time() > self._lastTemperatureUpdate + 5: - self._sendTemperatureQuery(withType=True) - self._sendNext() - self._clearForSend.set() - - def onTimeoutReceived(self, source): - if self._transport != source: - return - self._sendTemperatureQuery(withType=True) - - def sendManually(self, command, highPriority=False): - self._send(command, highPriority=highPriority, withChecksum=self._useChecksum()) - - def _send(self, command, highPriority=False, commandType=None, withChecksum=False): - commandTuple = (str(command), withChecksum) - - priority = 100 - if highPriority: - priority = 1 - if commandType is not None: - self._sendQueue.put((priority, commandTuple, commandType)) - else: - self._sendQueue.put((priority, commandTuple)) - - def _sendNext(self): - command = self._currentFile.getNext() - if command is None: - self._changeState(State.OPERATIONAL) - return - - self._send(command, withChecksum=self._useChecksum()) - - def _sendTemperatureQuery(self, withType=False): - if withType: - self._send(RepRapProtocol.COMMAND_GET_TEMP(), commandType=RepRapProtocol.COMMAND_TYPE_TEMPERATURE) - else: - self._send(RepRapProtocol.COMMAND_GET_TEMP()) - self._lastTemperatureUpdate = time.time() - - def _handleErrors(self, line): - # TODO Somehow figure out how to handle multiline errors here... - - # No matter the state, if we see an error, goto the error state and store the error for reference. - if line.startswith('Error:'): - #Oh YEAH, consistency. - # Marlin reports an MIN/MAX temp error as "Error:x\n: Extruder switched off. MAXTEMP triggered !\n" - # But a bed temp error is reported as "Error: Temperature heated bed switched off. MAXTEMP triggered !!" - # So we can have an extra newline in the most common case. Awesome work people. - if re.match('Error:[0-9]\n', line): - line = line.rstrip() + self._readline() - #Skip the communication errors, as those get corrected. - if RepRapProtocol.MESSAGE_ERROR_COMMUNICATION(line): - pass - elif not self._state == State.ERROR: - self.onError(line[6:]) - return line - - def _parseTemperatures(self, line): - result = {} - maxToolNum = 0 - for match in re.finditer(RepRapProtocol.REGEX_TEMPERATURE, line): - tool = match.group(1) - toolNumber = int(match.group(2)) if match.group(2) and len(match.group(2)) > 0 else None - if toolNumber > maxToolNum: - maxToolNum = toolNumber - - try: - actual = float(match.group(3)) - target = None - if match.group(4) and match.group(5): - target = float(match.group(5)) - - result[tool] = (toolNumber, actual, target) - except ValueError: - # catch conversion issues, we'll rather just not get the temperature update instead of killing the connection - pass - - if "T0" in result.keys() and "T" in result.keys(): - del result["T"] - - return maxToolNum, result - - def _processTemperatures(self, line): - maxToolNum, parsedTemps = self._parseTemperatures(line) - - result = {} - - # extruder temperatures - if not "T0" in parsedTemps.keys() and "T" in parsedTemps.keys(): - # only single reporting, "T" is our one and only extruder temperature - toolNum, actual, target = parsedTemps["T"] - result["tool0"] = { - "actual": actual, - "target": target - } - elif "T0" in parsedTemps.keys(): - for n in range(maxToolNum + 1): - tool = "T%d" % n - if not tool in parsedTemps.keys(): - continue - - toolNum, actual, target = parsedTemps[tool] - result["tool%d" % toolNum] = { - "actual": actual, - "target": target - } - - # bed temperature - if "B" in parsedTemps.keys(): - toolNum, actual, target = parsedTemps["B"] - result["bed"] = { - "actual": actual, - "target": target - } - - self._updateTemperature(result) - - def _useChecksum(self): - return not self._transportProperties[TransportProperties.FLOWCONTROL] and \ - (self._state == State.PRINTING or self._state == State.HEATING) - - ##~~ the actual send queue handling starts here - - def _handleSendQueue(self): - while self._sendQueueProcessing: - self._clearForSend.wait() - self._clearForSend.clear() - entry = self._sendQueue.get() - if len(entry) == 3: - priority, commandTuple, commandType = entry - else: - priority, commandTuple = entry - - command, withChecksum = commandTuple - self._transport._send(self._preprocessCommand(command, withChecksum=withChecksum)) - - def _preprocessCommand(self, command, withChecksum=False): - if withChecksum: - commandToSend = "N%d %s" % (self._currentLine, command) - checksum = reduce(lambda x, y: x ^ y, map(ord, commandToSend)) - self._currentLine += 1 - return "%s*%d" % (commandToSend, checksum) - else: - return command - diff --git a/src/octoprint/comm/transport/__init__.py b/src/octoprint/comm/transport/__init__.py index a4efb9487..238a52430 100644 --- a/src/octoprint/comm/transport/__init__.py +++ b/src/octoprint/comm/transport/__init__.py @@ -16,6 +16,9 @@ class Transport(object): def getProperties(self): raise NotImplemented() + def getConnectionOptions(self): + return [] + def connect(self, opt): pass diff --git a/src/octoprint/comm/transport/serialTransport.py b/src/octoprint/comm/transport/serialTransport.py index 8e836ed92..789cdf34b 100644 --- a/src/octoprint/comm/transport/serialTransport.py +++ b/src/octoprint/comm/transport/serialTransport.py @@ -3,13 +3,20 @@ __author__ = "Gina Häußge " __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2014 Gina Häußge - Released under terms of the AGPLv3 License" - +import os +import glob import threading import serial +try: + import _winreg +except: + pass + from . import Transport, TransportProperties, State from octoprint.util import getExceptionString from octoprint.util.virtual import VirtualPrinter +from octoprint.settings import settings class SerialTransport(Transport): @@ -33,6 +40,12 @@ class SerialTransport(Transport): TransportProperties.FLOWCONTROL: False } + def getConnectionOptions(self): + return { + "port": self.__getSerialList(), + "baudrate": self.__getBaudrateList() + } + def connect(self, opt): self._port = opt["port"] if "port" in opt else None self._baudrate = opt["baudrate"] if "baudrate" in opt else None @@ -98,13 +111,18 @@ class SerialTransport(Transport): def _connect(self): self.changeState(State.OPENING_CONNECTION) - try: - self._serial = serial.Serial(self._port, self._baudrate, timeout=self._communicationTimeout, writeTimeout=self._writeTimeout) + if self._port == "VIRTUAL": + self._serial = VirtualPrinter() + self.changeState(State.CONNECTED) return True - except: - self.logError("Unexpected error while connecting to serial port: %s %s" % (self._port, getExceptionString())) - self.onError("Failed to open serial port, permissions correct?") - return False + else: + try: + self._serial = serial.Serial(self._port, self._baudrate, timeout=self._communicationTimeout, writeTimeout=self._writeTimeout) + return True + except: + self.logError("Unexpected error while connecting to serial port: %s %s" % (self._port, getExceptionString())) + self.onError("Failed to open serial port, permissions correct?") + return False def _readline(self): if self._serial is None: @@ -123,10 +141,41 @@ class SerialTransport(Transport): self.logRx(unicode(line, "ascii", "replace").encode("ascii", "replace").rstrip()) return line + def __getSerialList(self): + baselist=[] + if os.name == "nt": + try: + key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"HARDWARE\\DEVICEMAP\\SERIALCOMM") + i=0 + while(1): + baselist+=[_winreg.EnumValue(key,i)[1]] + i+=1 + except: + pass + baselist = baselist \ + + glob.glob("/dev/ttyUSB*") \ + + glob.glob("/dev/ttyACM*") \ + + glob.glob("/dev/ttyAMA*") \ + + glob.glob("/dev/tty.usb*") \ + + glob.glob("/dev/cu.*") \ + + glob.glob("/dev/rfcomm*") -class VirtualSerialTransport(SerialTransport): - def _connect(self): - self.changeState(State.OPENING_CONNECTION) - self._serial = VirtualPrinter() - self.changeState(State.CONNECTED) - return True + additionalPorts = settings().get(["serial", "additionalPorts"]) + for additional in additionalPorts: + baselist += glob.glob(additional) + + prev = settings().get(["serial", "port"]) + if prev in baselist: + baselist.remove(prev) + baselist.insert(0, prev) + if settings().getBoolean(["devel", "virtualPrinter", "enabled"]): + baselist.append("VIRTUAL") + return baselist + + def __getBaudrateList(self): + ret = [250000, 230400, 115200, 57600, 38400, 19200, 9600] + prev = settings().getInt(["serial", "baudrate"]) + if prev in ret: + ret.remove(prev) + ret.insert(0, prev) + return ret diff --git a/src/octoprint/util/virtual.py b/src/octoprint/util/virtual.py index 565e12cff..4d96e01f4 100644 --- a/src/octoprint/util/virtual.py +++ b/src/octoprint/util/virtual.py @@ -32,7 +32,7 @@ class VirtualPrinter(): self._unitModifier = 1 self._virtualSd = settings().getBaseFolder("virtualSd") - self._sdCardReady = False + self._sdCardReady = True self._sdPrinter = None self._sdPrintingSemaphore = threading.Event() self._selectedSdFile = None @@ -75,17 +75,15 @@ class VirtualPrinter(): if linenumber != expected: self.readList.append("Error: expected line %d got %d" % (expected, linenumber)) self.readList.append("Resend:%d" % expected) - if settings().getBoolean(["devel", "virtualPrinter", "okAfterResend"]): - self.readList.append("ok") + self.readList.append("ok") + return + elif self.currentLine == 100: + # simulate a resend at line 100 of the last 5 lines + self.lastN = 94 + self.readList.append("Error: Line Number is not Last Line Number\n") + self.readList.append("rs %d\n" % (self.currentLine - 5)) + self.readList.append("ok") return - # elif self.currentLine == 100: - # # simulate a resend at line 100 of the last 5 lines - # self.lastN = 94 - # self.readList.append("Error: Line Number is not Last Line Number\n") - # self.readList.append("rs %d\n" % (self.currentLine - 5)) - # if settings().getBoolean(["devel", "virtualPrinter", "okAfterResend"]): - # self.readList.append("ok") - # return else: self.lastN = linenumber data = data.split(None, 1)[1].strip()