printrun-src/calibrateextruder.py

changeset 45
c82943fb205f
parent 15
0bbb006204fc
equal deleted inserted replaced
44:310be640a303 45:c82943fb205f
1 #!/usr/bin/env python 1 #!/usr/bin/env python3
2 # This file is part of the Printrun suite. 2 # This file is part of the Printrun suite.
3 # 3 #
4 # Printrun is free software: you can redistribute it and/or modify 4 # Printrun is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by 5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or 6 # the Free Software Foundation, either version 3 of the License, or
39 import os 39 import os
40 40
41 def float_input(prompt=''): 41 def float_input(prompt=''):
42 f = None 42 f = None
43 while f is None: 43 while f is None:
44 s = raw_input(prompt) 44 s = input(prompt)
45 try: 45 try:
46 f = float(s) 46 f = float(s)
47 except ValueError: 47 except ValueError:
48 sys.stderr.write("Not a valid floating-point number.\n") 48 sys.stderr.write("Not a valid floating-point number.\n")
49 sys.stderr.flush() 49 sys.stderr.flush()
54 for i in range(t): 54 for i in range(t):
55 for s in ['|\b', '/\b', '-\b', '\\\b', '|']: 55 for s in ['|\b', '/\b', '-\b', '\\\b', '|']:
56 sys.stdout.write(s) 56 sys.stdout.write(s)
57 sys.stdout.flush() 57 sys.stdout.flush()
58 time.sleep(1.0 / 5) 58 time.sleep(1.0 / 5)
59 print 59 print()
60 def w(s): 60 def w(s):
61 sys.stdout.write(s) 61 sys.stdout.write(s)
62 sys.stdout.flush() 62 sys.stdout.flush()
63 63
64 64
73 time.sleep(0.5) 73 time.sleep(0.5)
74 if not f: 74 if not f:
75 time.sleep(1.5) 75 time.sleep(1.5)
76 f = True 76 f = True
77 curtemp = gettemp(p) 77 curtemp = gettemp(p)
78 if curtemp: w(u"\rHeating extruder up.. %3d \xb0C" % curtemp) 78 if curtemp: w("\rHeating extruder up.. %3d \xb0C" % curtemp)
79 if s: print 79 if s: print()
80 else: print "\nReady." 80 else: print("\nReady.")
81 81
82 def gettemp(p): 82 def gettemp(p):
83 try: p.logl 83 try: p.logl
84 except: setattr(p, 'logl', 0) 84 except: setattr(p, 'logl', 0)
85 try: p.temp 85 try: p.temp
87 for n in range(p.logl, len(p.log)): 87 for n in range(p.logl, len(p.log)):
88 line = p.log[n] 88 line = p.log[n]
89 if 'T:' in line: 89 if 'T:' in line:
90 try: 90 try:
91 setattr(p, 'temp', int(line.split('T:')[1].split()[0])) 91 setattr(p, 'temp', int(line.split('T:')[1].split()[0]))
92 except: print line 92 except: print(line)
93 p.logl = len(p.log) 93 p.logl = len(p.log)
94 return p.temp 94 return p.temp
95 if not os.path.exists(port): 95 if not os.path.exists(port):
96 port = 0 96 port = 0
97 97
98 # Parse options 98 # Parse options
99 help = u""" 99 help = """
100 %s [ -l DISTANCE ] [ -s STEPS ] [ -t TEMP ] [ -p PORT ] 100 %s [ -l DISTANCE ] [ -s STEPS ] [ -t TEMP ] [ -p PORT ]
101 -l --length Length of filament to extrude for each calibration step (default: %d mm) 101 -l --length Length of filament to extrude for each calibration step (default: %d mm)
102 -s --steps Initial amount of steps to use (default: %d steps) 102 -s --steps Initial amount of steps to use (default: %d steps)
103 -t --temp Extrusion temperature in degrees Celsius (default: %d \xb0C, max %d \xb0C) 103 -t --temp Extrusion temperature in degrees Celsius (default: %d \xb0C, max %d \xb0C)
104 -p --port Serial port the printer is connected to (default: %s) 104 -p --port Serial port the printer is connected to (default: %s)
105 -h --help This cruft. 105 -h --help This cruft.
106 """[1:-1].encode('utf-8') % (sys.argv[0], n, k, temp, tempmax, port if port else 'auto') 106 """[1:-1].encode('utf-8') % (sys.argv[0], n, k, temp, tempmax, port if port else 'auto')
107 try: 107 try:
108 opts, args = getopt.getopt(sys.argv[1:], "hl:s:t:p:", ["help", "length=", "steps=", "temp=", "port="]) 108 opts, args = getopt.getopt(sys.argv[1:], "hl:s:t:p:", ["help", "length=", "steps=", "temp=", "port="])
109 except getopt.GetoptError, err: 109 except getopt.GetoptError as err:
110 print str(err) 110 print(str(err))
111 print help 111 print(help)
112 sys.exit(2) 112 sys.exit(2)
113 for o, a in opts: 113 for o, a in opts:
114 if o in ('-h', '--help'): 114 if o in ('-h', '--help'):
115 print help 115 print(help)
116 sys.exit() 116 sys.exit()
117 elif o in ('-l', '--length'): 117 elif o in ('-l', '--length'):
118 n = float(a) 118 n = float(a)
119 elif o in ('-s', '--steps'): 119 elif o in ('-s', '--steps'):
120 k = int(a) 120 k = int(a)
121 elif o in ('-t', '--temp'): 121 elif o in ('-t', '--temp'):
122 temp = int(a) 122 temp = int(a)
123 if temp >= tempmax: 123 if temp >= tempmax:
124 print (u'%d \xb0C? Are you insane?'.encode('utf-8') % temp) + (" That's over nine thousand!" if temp > 9000 else '') 124 print(('%d \xb0C? Are you insane?'.encode('utf-8') % temp) + (" That's over nine thousand!" if temp > 9000 else ''))
125 sys.exit(255) 125 sys.exit(255)
126 elif o in ('-p', '--port'): 126 elif o in ('-p', '--port'):
127 port = a 127 port = a
128 128
129 # Show initial parameters 129 # Show initial parameters
130 print "Initial parameters" 130 print("Initial parameters")
131 print "Steps per mm: %3d steps" % k 131 print("Steps per mm: %3d steps" % k)
132 print "Length extruded: %3d mm" % n 132 print("Length extruded: %3d mm" % n)
133 print 133 print()
134 print "Serial port: %s" % (port if port else 'auto') 134 print("Serial port: %s" % (port if port else 'auto'))
135 135
136 p = None 136 p = None
137 try: 137 try:
138 # Connect to printer 138 # Connect to printer
139 w("Connecting to printer..") 139 w("Connecting to printer..")
140 try: 140 try:
141 p = printcore(port, 115200) 141 p = printcore(port, 115200)
142 except: 142 except:
143 print 'Error.' 143 print('Error.')
144 raise 144 raise
145 while not p.online: 145 while not p.online:
146 time.sleep(1) 146 time.sleep(1)
147 w('.') 147 w('.')
148 print " connected." 148 print(" connected.")
149 149
150 heatup(p, temp) 150 heatup(p, temp)
151 151
152 # Calibration loop 152 # Calibration loop
153 while n != m: 153 while n != m:
158 m = float_input("How many millimeters of filament were extruded? ") 158 m = float_input("How many millimeters of filament were extruded? ")
159 if m == 0: continue 159 if m == 0: continue
160 if n != m: 160 if n != m:
161 k = (n / m) * k 161 k = (n / m) * k
162 p.send_now("M92 E%d" % int(round(k))) # Set new step count 162 p.send_now("M92 E%d" % int(round(k))) # Set new step count
163 print "Steps per mm: %3d steps" % k # Tell user 163 print("Steps per mm: %3d steps" % k) # Tell user
164 print 'Calibration completed.' # Yay! 164 print('Calibration completed.') # Yay!
165 except KeyboardInterrupt: 165 except KeyboardInterrupt:
166 pass 166 pass
167 finally: 167 finally:
168 if p: p.disconnect() 168 if p: p.disconnect()

mercurial