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