2011-05-12 10:54:10 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#Interactive RepRap e axis calibration program
|
|
|
|
#(C) Nathan Zadoks 2011
|
|
|
|
#Licensed under CC-BY-SA or GPLv2 and higher - Pick your poison.
|
2012-08-08 07:38:48 +00:00
|
|
|
s = 300 #Extrusion speed (mm/min)
|
|
|
|
n = 100 #Default length to extrude
|
2012-08-08 06:39:50 +00:00
|
|
|
m= 0 #User-entered measured extrusion length
|
2012-08-08 07:38:48 +00:00
|
|
|
k = 300 #Default amount of steps per mm
|
2012-08-08 06:39:50 +00:00
|
|
|
port='/dev/ttyUSB0' #Default serial port to connect to printer
|
2012-08-08 07:38:48 +00:00
|
|
|
temp = 210 #Default extrusion temperature
|
2011-06-23 10:22:10 +00:00
|
|
|
|
2012-08-08 07:38:48 +00:00
|
|
|
tempmax = 250 #Maximum extrusion temperature
|
2011-05-12 10:54:10 +00:00
|
|
|
|
2012-08-08 07:38:48 +00:00
|
|
|
t = int(n*60)/s #Time to wait for extrusion
|
2011-12-03 19:18:48 +00:00
|
|
|
|
2011-05-12 10:54:10 +00:00
|
|
|
try:
|
2012-08-08 06:39:50 +00:00
|
|
|
from printdummy import printcore
|
2011-05-12 10:54:10 +00:00
|
|
|
except ImportError:
|
2012-08-08 06:39:50 +00:00
|
|
|
from printcore import printcore
|
2012-08-08 07:38:48 +00:00
|
|
|
import time, getopt, sys, os
|
2011-05-12 10:54:10 +00:00
|
|
|
|
|
|
|
def float_input(prompt=''):
|
2012-08-08 06:39:50 +00:00
|
|
|
import sys
|
2012-08-08 07:38:48 +00:00
|
|
|
f = None
|
|
|
|
while f == None:
|
|
|
|
s = raw_input(prompt)
|
2012-08-08 06:39:50 +00:00
|
|
|
try:
|
2012-08-08 07:38:48 +00:00
|
|
|
f = float(s)
|
2012-08-08 06:39:50 +00:00
|
|
|
except ValueError:
|
|
|
|
sys.stderr.write("Not a valid floating-point number.\n")
|
|
|
|
sys.stderr.flush()
|
|
|
|
return f
|
2012-08-08 07:38:48 +00:00
|
|
|
def wait(t, m=''):
|
|
|
|
import time, sys
|
2012-08-08 06:39:50 +00:00
|
|
|
sys.stdout.write(m+'['+(' '*t)+']\r'+m+'[')
|
|
|
|
sys.stdout.flush()
|
|
|
|
for i in range(t):
|
|
|
|
for s in ['|\b','/\b','-\b','\\\b','|']:
|
|
|
|
sys.stdout.write(s)
|
|
|
|
sys.stdout.flush()
|
|
|
|
time.sleep(1.0/5)
|
|
|
|
print
|
2011-06-23 10:22:10 +00:00
|
|
|
def w(s):
|
2012-08-08 06:39:50 +00:00
|
|
|
sys.stdout.write(s)
|
|
|
|
sys.stdout.flush()
|
2011-05-12 10:54:10 +00:00
|
|
|
|
2011-12-03 19:18:48 +00:00
|
|
|
|
2012-08-08 07:38:48 +00:00
|
|
|
def heatup(p, temp, s = 0):
|
|
|
|
curtemp = gettemp(p)
|
2012-08-08 06:39:50 +00:00
|
|
|
p.send_now('M109 S%03d'%temp)
|
2012-08-08 07:38:48 +00:00
|
|
|
p.temp = 0
|
2012-08-08 06:39:50 +00:00
|
|
|
if not s: w("Heating extruder up..")
|
2012-08-08 07:38:48 +00:00
|
|
|
f = False
|
2012-08-08 06:39:50 +00:00
|
|
|
while curtemp<=(temp-1):
|
|
|
|
p.send_now('M105')
|
|
|
|
time.sleep(0.5)
|
|
|
|
if not f:
|
|
|
|
time.sleep(1.5)
|
2012-08-08 07:38:48 +00:00
|
|
|
f = True
|
|
|
|
curtemp = gettemp(p)
|
2012-08-08 06:39:50 +00:00
|
|
|
if curtemp: w(u"\rHeating extruder up.. %3d \xb0C"%curtemp)
|
|
|
|
if s: print
|
|
|
|
else: print "\nReady."
|
2011-12-03 19:18:48 +00:00
|
|
|
|
|
|
|
def gettemp(p):
|
2012-08-08 06:39:50 +00:00
|
|
|
try: p.logl
|
|
|
|
except: setattr(p,'logl',0)
|
|
|
|
try: p.temp
|
|
|
|
except: setattr(p,'temp',0)
|
2012-08-08 07:38:48 +00:00
|
|
|
for n in range(p.logl, len(p.log)):
|
|
|
|
line = p.log[n]
|
2012-08-08 06:39:50 +00:00
|
|
|
if 'T:' in line:
|
|
|
|
try:
|
|
|
|
setattr(p,'temp',int(line.split('T:')[1].split()[0]))
|
|
|
|
except: print line
|
2012-08-08 07:38:48 +00:00
|
|
|
p.logl = len(p.log)
|
2012-08-08 06:39:50 +00:00
|
|
|
return p.temp
|
2011-06-23 09:49:04 +00:00
|
|
|
if not os.path.exists(port):
|
2012-08-08 07:38:48 +00:00
|
|
|
port = 0
|
2011-06-23 09:49:04 +00:00
|
|
|
|
2011-05-12 10:54:10 +00:00
|
|
|
#Parse options
|
2012-08-08 07:38:48 +00:00
|
|
|
help = u"""
|
2011-06-23 10:22:10 +00:00
|
|
|
%s [ -l DISTANCE ] [ -s STEPS ] [ -t TEMP ] [ -p PORT ]
|
2012-08-08 06:39:50 +00:00
|
|
|
-l --length Length of filament to extrude for each calibration step (default: %d mm)
|
|
|
|
-s --steps Initial amount of steps to use (default: %d steps)
|
|
|
|
-t --temp Extrusion temperature in degrees Celsius (default: %d \xb0C, max %d \xb0C)
|
|
|
|
-p --port Serial port the printer is connected to (default: %s)
|
|
|
|
-h --help This cruft.
|
2012-08-08 07:58:09 +00:00
|
|
|
"""[1:-1].encode('utf-8')%(sys.argv[0], n, k, temp, tempmax, port if port else 'auto')
|
2011-05-12 10:54:10 +00:00
|
|
|
try:
|
2012-08-08 07:58:09 +00:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:],"hl:s:t:p:",["help", "length=", "steps=", "temp=", "port="])
|
2012-08-08 07:38:48 +00:00
|
|
|
except getopt.GetoptError, err:
|
2012-08-08 06:39:50 +00:00
|
|
|
print str(err)
|
|
|
|
print help
|
|
|
|
sys.exit(2)
|
2012-08-08 07:38:48 +00:00
|
|
|
for o, a in opts:
|
2012-08-08 06:39:50 +00:00
|
|
|
if o in ('-h','--help'):
|
|
|
|
print help
|
|
|
|
sys.exit()
|
|
|
|
elif o in ('-l','--length'):
|
2012-08-08 07:38:48 +00:00
|
|
|
n = float(a)
|
2012-08-08 06:39:50 +00:00
|
|
|
elif o in ('-s','--steps'):
|
2012-08-08 07:38:48 +00:00
|
|
|
k = int(a)
|
2012-08-08 06:39:50 +00:00
|
|
|
elif o in ('-t','--temp'):
|
2012-08-08 07:38:48 +00:00
|
|
|
temp = int(a)
|
2012-08-08 06:39:50 +00:00
|
|
|
if temp>=tempmax:
|
|
|
|
print (u'%d \xb0C? Are you insane?'.encode('utf-8')%temp)+(" That's over nine thousand!" if temp>9000 else '')
|
|
|
|
sys.exit(255)
|
|
|
|
elif o in ('-p','--port'):
|
2012-08-08 07:38:48 +00:00
|
|
|
port = a
|
2011-06-23 11:55:46 +00:00
|
|
|
|
2011-05-12 10:54:10 +00:00
|
|
|
#Show initial parameters
|
|
|
|
print "Initial parameters"
|
|
|
|
print "Steps per mm: %3d steps"%k
|
|
|
|
print "Length extruded: %3d mm"%n
|
2012-08-08 06:39:50 +00:00
|
|
|
print
|
2011-06-23 09:49:04 +00:00
|
|
|
print "Serial port: %s"%(port if port else 'auto')
|
2011-05-12 10:54:10 +00:00
|
|
|
|
2012-08-08 07:38:48 +00:00
|
|
|
p = None
|
2011-06-23 11:20:11 +00:00
|
|
|
try:
|
2012-08-08 06:39:50 +00:00
|
|
|
#Connect to printer
|
|
|
|
w("Connecting to printer..")
|
|
|
|
try:
|
2012-08-08 07:38:48 +00:00
|
|
|
p = printcore(port, 115200)
|
2012-08-08 06:39:50 +00:00
|
|
|
except:
|
|
|
|
print 'Error.'
|
|
|
|
raise
|
|
|
|
while not p.online:
|
|
|
|
time.sleep(1)
|
|
|
|
w('.')
|
|
|
|
print " connected."
|
|
|
|
|
2012-08-08 07:38:48 +00:00
|
|
|
heatup(p, temp)
|
2012-08-08 06:39:50 +00:00
|
|
|
|
|
|
|
#Calibration loop
|
|
|
|
while n!=m:
|
2012-08-08 07:38:48 +00:00
|
|
|
heatup(p, temp, True)
|
2012-08-08 06:39:50 +00:00
|
|
|
p.send_now("G92 E0") #Reset e axis
|
2012-08-08 07:38:48 +00:00
|
|
|
p.send_now("G1 E%d F%d"%(n, s)) #Extrude length of filament
|
2012-08-08 06:39:50 +00:00
|
|
|
wait(t,'Extruding.. ')
|
2012-08-08 07:38:48 +00:00
|
|
|
m = float_input("How many millimeters of filament were extruded? ")
|
|
|
|
if m == 0: continue
|
2012-08-08 06:39:50 +00:00
|
|
|
if n!=m:
|
2012-08-08 07:38:48 +00:00
|
|
|
k = (n/m)*k
|
2012-08-08 06:39:50 +00:00
|
|
|
p.send_now("M92 E%d"%int(round(k))) #Set new step count
|
|
|
|
print "Steps per mm: %3d steps"%k #Tell user
|
|
|
|
print 'Calibration completed.' #Yay!
|
2011-12-03 19:18:48 +00:00
|
|
|
except KeyboardInterrupt:
|
2012-08-08 06:39:50 +00:00
|
|
|
pass
|
2011-12-03 19:18:48 +00:00
|
|
|
finally:
|
2012-08-08 06:39:50 +00:00
|
|
|
if p: p.disconnect()
|