First working version - can now connect to and talk with firmware, handle flow control and resends.

master
kliment 2011-05-22 20:23:29 +02:00
parent 97128780f3
commit 140458281e
1 changed files with 51 additions and 35 deletions

View File

@ -12,10 +12,6 @@ class printcore():
self.clear=0 self.clear=0
self.mainqueue=[] self.mainqueue=[]
self.priqueue=[] self.priqueue=[]
if port is not None and baud is not None:
#print port, baud
self.connect(port, baud)
#print "connected\n"
self.readthread=None self.readthread=None
self.queueindex=0 self.queueindex=0
self.lineno=0 self.lineno=0
@ -23,6 +19,13 @@ class printcore():
self.online=False self.online=False
self.printing=False self.printing=False
self.sentlines={} self.sentlines={}
self.log=[]
self.sent=[]
if port is not None and baud is not None:
#print port, baud
self.connect(port, baud)
#print "connected\n"
def disconnect(self): def disconnect(self):
"""Disconnects from printer and pauses the print """Disconnects from printer and pauses the print
@ -49,27 +52,35 @@ class printcore():
def _listen(self): def _listen(self):
"""This function acts on messages from the firmware """This function acts on messages from the firmware
""" """
self.clear=True
time.sleep(1) time.sleep(1)
self.send_now("M105")
while(True): while(True):
if(not self.printer or not self.printer.isOpen): if(not self.printer or not self.printer.isOpen):
break break
line=self.printer.readline() line=self.printer.readline()
if(len(line)>1): if(len(line)>1):
print "RECV:",line self.log+=[line]
if(line.startswith('start')): if(line.startswith('start')):
self.clear=True self.clear=True
self.online=True self.online=True
elif(line.startswith('ok')): elif(line.startswith('ok')):
self.clear=True self.clear=True
self.online=True
self.resendfrom=-1 self.resendfrom=-1
#put temp handling here #put temp handling here
#callback for temp, status, whatever
elif(line.startswith('Error')): elif(line.startswith('Error')):
#callback for errors
pass pass
if "Resend" in line or "rs" in line: if "Resend" in line or "rs" in line:
toresend=int(line.replace(":"," ").split()[-1]) toresend=int(line.replace(":"," ").split()[-1])
self.resendfrom=toresend self.resendfrom=toresend
print "TORESEND:",self.resendfrom
self.clear=True self.clear=True
self.clear=True
#callback for disconnect
def _checksum(self,command): def _checksum(self,command):
return reduce(lambda x,y:x^y, map(ord,command)) return reduce(lambda x,y:x^y, map(ord,command))
@ -79,7 +90,7 @@ class printcore():
The print queue will be replaced with the contents of the data array, the next line will be set to 0 and the firmware notified. The print queue will be replaced with the contents of the data array, the next line will be set to 0 and the firmware notified.
Printing will then start in a parallel thread. Printing will then start in a parallel thread.
""" """
if(self.printing): if(self.printing or not self.online or not self.printer):
return False return False
self.printing=True self.printing=True
self.mainqueue=[]+data self.mainqueue=[]+data
@ -87,6 +98,7 @@ class printcore():
self.queueindex=0 self.queueindex=0
self.resendfrom=-1 self.resendfrom=-1
self._send("M110",-1, True) self._send("M110",-1, True)
self.clear=False
Thread(target=self._print).start() Thread(target=self._print).start()
return True return True
@ -94,12 +106,13 @@ class printcore():
"""Pauses the print, saving the current position. """Pauses the print, saving the current position.
""" """
self.printing=False self.printing=False
time.sleep(1)
def resume(self): def resume(self):
"""Resumes a paused print. """Resumes a paused print.
""" """
self.printing=True self.printing=True
threading.Thread(target=self._print).start() Thread(target=self._print).start()
def send(self,command): def send(self,command):
"""Adds a command to the checksummed main command queue if printing, or sends the command immediately if not printing """Adds a command to the checksummed main command queue if printing, or sends the command immediately if not printing
@ -123,36 +136,38 @@ class printcore():
while not self.clear: while not self.clear:
time.sleep(0.001) time.sleep(0.001)
self._send(command) self._send(command)
#callback for command sent
def _print(self): def _print(self):
while(self.printing and self.printer): #callback for printing started
while(self.printing and self.printer and self.online):
self._sendnext() self._sendnext()
#callback for printing done
def _sendnext(self): def _sendnext(self):
if(not self.printer): if(not self.printer):
return return
if(self.resendfrom>-1): while not self.clear:
while(self.resendfrom<self.lineno): time.sleep(0.001)
while not self.clear: self.clear=False
time.sleep(0.001) if not (self.printing and self.printer and self.online):
self.clear=False return
self._send(self.sentlines[self.resendfrom],self.resendfrom,False) if(self.resendfrom<self.lineno and self.resendfrom>-1):
self.resendfrom+=1 self._send(self.sentlines[self.resendfrom],self.resendfrom,False)
self.resendfrom=-1 self.resendfrom+=1
return
self.resendfrom=-1
for i in self.priqueue[:]: for i in self.priqueue[:]:
while not self.clear:
time.sleep(0.001)
self.clear=False
self._send(i) self._send(i)
del(self.preque[0]) del(self.priqueue[0])
return
if(self.printing and self.queueindex<len(self.mainqueue)): if(self.printing and self.queueindex<len(self.mainqueue)):
tline=self.mainqueue[self.queueindex] tline=self.mainqueue[self.queueindex]
if(not tline.startswith(';') and len(tline)>0): if(not tline.startswith(';') and len(tline)>0):
while not self.clear:
time.sleep(0.001)
self.clear=False
self._send(tline,self.lineno,True) self._send(tline,self.lineno,True)
self.lineno+=1 self.lineno+=1
else:
self.clear=True
self.queueindex+=1 self.queueindex+=1
else: else:
self.printing=False self.printing=False
@ -164,20 +179,21 @@ class printcore():
if(calcchecksum): if(calcchecksum):
prefix="N"+str(lineno)+" "+command prefix="N"+str(lineno)+" "+command
command=prefix+"*"+str(self._checksum(prefix)) command=prefix+"*"+str(self._checksum(prefix))
self.sentlines[lineno]=command if("M110" not in command):
self.sentlines[lineno]=command
if(self.printer): if(self.printer):
print "sending: "+command+"\n" self.sent+=[command]
self.printer.write(command+"\n") self.printer.write(command+"\n")
if __name__ == '__main__': if __name__ == '__main__':
p=printcore('/dev/ttyUSB0',115200) p=printcore('/dev/ttyUSB0',115200)
time.sleep(2)
testdata=[i.replace("\n","") for i in open("../prusamendel/sellsx_export.gcode")]
p.startprint(testdata)
time.sleep(1)
p.pause()
print "pause"
time.sleep(5) time.sleep(5)
testdata="""G28 p.resume()
G1 X0 Y0 time.sleep(1)
G1 X10 Y10
G1 X0 Y0
;
"""
p.startprint(testdata.split('\n'))
time.sleep(10)
p.disconnect() p.disconnect()