Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
plasticdb - Simple tool for 3D printer filament calibration For the best 3D printing results the "steps per e" should be calibrated for each different filament. Plasticdb makes that an easier task. Works best with matterQ (http://matterq.org).
#!/usr/bin/env python
import os
import sys
import subprocess
import tempfile
import shutil
# The file that stores all the data
DB = "plastics.txt"
# Set to False to write gcode to file instead of LPR
LPR = True
BODY = """M110
G90
G21
G92 X0 Y0 Z0 E0
M112
M110
G90
G21
G92 X0 Y0 Z0 E0
M105
M302
G1 E100 F100
"""
def db_read(name=None):
parts = None
for line in open(DB):
parts = line.strip().split("\t")
assert len(parts) == 3
if parts[0] == name:
return float(parts[1]), float(parts[2])
if name is not None:
raise KeyError
if parts is None:
return 0.0, 0.0
else:
return float(parts[1]), float(parts[2])
def db_write(name, steps_per_mm, diameter, delete=False):
tmp = tempfile.mktemp()
out = open(tmp, "w")
for line in open(DB):
parts = line.strip().split("\t")
assert len(parts) == 3
if parts[0] == name:
if not delete:
out.write("%s\t%f\t%f\n" % (name, steps_per_mm, diameter))
else:
out.write(line)
out.close()
shutil.move(tmp, DB)
def db_list():
titles = ("name", "steps per E", "diameter")
lens = [len(t) for t in titles]
for line in open(DB):
parts = line.strip().split("\t")
assert len(parts) == 3
for i in range(len(lens)):
lens[i] = max(lens[i], len(parts[i]))
for line in ["\t".join(titles)] + open(DB).readlines():
parts = line.strip().split("\t")
assert len(parts) == 3
for i in range(len(lens)):
sys.stdout.write(parts[i].rjust(lens[i] + 2))
sys.stdout.write("\n")
def db_add():
default_e, default_d = db_read()
name = raw_input("name: ")
steps_per_mm = raw_input("steps per mm [%f]: " % default_e)
if len(steps_per_mm) == 0:
steps_per_mm = default_e
else:
steps_per_mm = float(steps_per_mm)
diameter = raw_input("diameter [%f]: " % default_d)
if len(diameter) == 0:
diameter = default_d
else:
diameter = float(diameter)
open(DB, "a").write("%s\t%f\t%f\n" % (name, steps_per_mm, diameter))
def db_delete():
name = raw_input("name: ")
if len(name):
db_write(name, 0.0, 0.0, delete=True)
def calibrate():
name = raw_input("name: ")
assert len(name)
initial_e, initial_d = db_read(name)
raw_input("mark filament and press enter to start move")
if LPR:
p = subprocess.Popen(["/usr/bin/lpr"], stdin=subprocess.PIPE)
stream = p.stdin
else:
tmp = tempfile.mktemp()
stream = open(tmp, "w")
stream.write("M92 E%.3f\n" % initial_e)
stream.write(BODY)
stream.close()
if LPR:
p.wait()
else:
print "send this gcode to your printer: %s" % tmp
print
print "now mark again, remove and measure length"
length = raw_input("length: ")
new_e = 100.0 / float(length) * initial_e
db_write(name, new_e, initial_d)
print "old: E%f" % initial_e
print "new: E%d" % new_e
def help():
print "Command action"
print " l list plastics"
print " a add plastic"
print " d delete plastic"
print " c calibrate plastic"
print " q quit"
if __name__ == "__main__":
if not os.path.exists(DB):
open(DB, "w")
while True:
input = raw_input("Command (m for help): ")
if input == "m":
help()
elif input == "l":
db_list()
elif input == "a":
db_add()
elif input == "d":
db_delete()
elif input == "c":
calibrate()
elif input == "q":
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.