printrun-src/calibrateextruder.py

Sat, 08 Sep 2018 12:00:12 +0200

author
mdd
date
Sat, 08 Sep 2018 12:00:12 +0200
changeset 39
74801c0f2709
parent 15
0bbb006204fc
child 45
c82943fb205f
permissions
-rw-r--r--

bad idea :(

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 # Interactive RepRap e axis calibration program
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 # (C) Nathan Zadoks 2011
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 s = 300 # Extrusion speed (mm/min)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 n = 100 # Default length to extrude
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 m = 0 # User-entered measured extrusion length
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 k = 300 # Default amount of steps per mm
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 port = '/dev/ttyUSB0' # Default serial port to connect to printer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 temp = 210 # Default extrusion temperature
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 tempmax = 250 # Maximum extrusion temperature
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 t = int(n * 60) / s # Time to wait for extrusion
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 from printdummy import printcore
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 except ImportError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 from printcore import printcore
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 import getopt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 def float_input(prompt=''):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 f = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 while f is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 s = raw_input(prompt)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 f = float(s)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 except ValueError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 sys.stderr.write("Not a valid floating-point number.\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 sys.stderr.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 return f
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 def wait(t, m=''):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 sys.stdout.write(m + '[' + (' ' * t) + ']\r' + m + '[')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 for i in range(t):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 for s in ['|\b', '/\b', '-\b', '\\\b', '|']:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 sys.stdout.write(s)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 time.sleep(1.0 / 5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 print
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 def w(s):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 sys.stdout.write(s)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 def heatup(p, temp, s = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 curtemp = gettemp(p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 p.send_now('M109 S%03d' % temp)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 p.temp = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 if not s: w("Heating extruder up..")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 f = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 while curtemp <= (temp - 1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 p.send_now('M105')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 time.sleep(0.5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 if not f:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 time.sleep(1.5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 f = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 curtemp = gettemp(p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 if curtemp: w(u"\rHeating extruder up.. %3d \xb0C" % curtemp)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 if s: print
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 else: print "\nReady."
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 def gettemp(p):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 try: p.logl
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 except: setattr(p, 'logl', 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 try: p.temp
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 except: setattr(p, 'temp', 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 for n in range(p.logl, len(p.log)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 line = p.log[n]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 if 'T:' in line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 setattr(p, 'temp', int(line.split('T:')[1].split()[0]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 except: print line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 p.logl = len(p.log)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 return p.temp
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 if not os.path.exists(port):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 port = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 # Parse options
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 help = u"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 %s [ -l DISTANCE ] [ -s STEPS ] [ -t TEMP ] [ -p PORT ]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 -l --length Length of filament to extrude for each calibration step (default: %d mm)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 -s --steps Initial amount of steps to use (default: %d steps)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 -t --temp Extrusion temperature in degrees Celsius (default: %d \xb0C, max %d \xb0C)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 -p --port Serial port the printer is connected to (default: %s)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 -h --help This cruft.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 """[1:-1].encode('utf-8') % (sys.argv[0], n, k, temp, tempmax, port if port else 'auto')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 opts, args = getopt.getopt(sys.argv[1:], "hl:s:t:p:", ["help", "length=", "steps=", "temp=", "port="])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 except getopt.GetoptError, err:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 print str(err)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 print help
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 sys.exit(2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 for o, a in opts:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 if o in ('-h', '--help'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 print help
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 sys.exit()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 elif o in ('-l', '--length'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 n = float(a)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 elif o in ('-s', '--steps'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 k = int(a)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 elif o in ('-t', '--temp'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 temp = int(a)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 if temp >= tempmax:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 print (u'%d \xb0C? Are you insane?'.encode('utf-8') % temp) + (" That's over nine thousand!" if temp > 9000 else '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 sys.exit(255)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 elif o in ('-p', '--port'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 port = a
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 # Show initial parameters
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 print "Initial parameters"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 print "Steps per mm: %3d steps" % k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 print "Length extruded: %3d mm" % n
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 print
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 print "Serial port: %s" % (port if port else 'auto')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 p = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 # Connect to printer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 w("Connecting to printer..")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 p = printcore(port, 115200)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 print 'Error.'
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 raise
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 while not p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 time.sleep(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 w('.')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 print " connected."
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 heatup(p, temp)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 # Calibration loop
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 while n != m:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 heatup(p, temp, True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 p.send_now("G92 E0") # Reset e axis
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 p.send_now("G1 E%d F%d" % (n, s)) # Extrude length of filament
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 wait(t, 'Extruding.. ')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 m = float_input("How many millimeters of filament were extruded? ")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 if m == 0: continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 if n != m:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 k = (n / m) * k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 p.send_now("M92 E%d" % int(round(k))) # Set new step count
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 print "Steps per mm: %3d steps" % k # Tell user
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 print 'Calibration completed.' # Yay!
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 except KeyboardInterrupt:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 finally:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 if p: p.disconnect()

mercurial