Skip to content

Instantly share code, notes, and snippets.

@nophead
Last active February 1, 2016 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nophead/7f28ac6d5e9f51475d25 to your computer and use it in GitHub Desktop.
Save nophead/7f28ac6d5e9f51475d25 to your computer and use it in GitHub Desktop.
Coolometer data gathering script
import serial
probe_z = 45
x_range = [-40, 40]
y_range = [-30, 60]
step = 2
def do_command(port,s): # send command and look for ok reply
while True:
port.write(s)
if s == "M114\n":
port.readline()
line = port.readline()
if line[:2] == 'ok':
break
print "Retry:", s, line.strip()
def read_meter(meter): # read power from coolometer
do_command(meter,'s')
return int(meter.readline().split()[5][:-3])
def wait_for_move(printer): # wait for printer move to finish
do_command(printer,"M400\n") # wait for motion buffer empty
do_command(printer,"M114\n") # will only reply when processed
printer = serial.Serial('/dev/ttyUSB0', 250000, timeout = None)
do_command(printer,"M107\n") # fan off
do_command(printer,"G28 Z0\n") # home z
meter = serial.Serial('/dev/ttyUSB1', 9600, timeout = 5)
print "init: ", meter.readline().strip() # coolometer sends a reading when reset
do_command(meter,'z') # set zero power level
do_command(printer,"M201 X1000 Y100\n") # very low Y acceleration to avoid coolometer slipping
do_command(printer,"G28 X0 Y0\n") # home x and y
do_command(printer,"G1 X0 Y0 F12000\n") # goto centre
do_command(printer,"G1 Z%d\n" % probe_z) # move nozzle to just above the probe
wait_for_move(printer) # wait for moves to finish
do_command(printer,"M106\n") # turn the fan on
f = open("fanmap.dat","w") #write file for GNUplot
for y in range(y_range[0], y_range[1] + 1, step):
for x in range(x_range[0], x_range[1] + 1, step):
do_command(printer,"G1 X%d Y%d F12000\n" % (x,y))
wait_for_move(printer)
if x == x_range[0]: # extra delay at start of line for temperature to stabilise after quick trip under the fan
read_meter(meter)
power = read_meter(meter)
print x, y, power
print >> f, x, y, power
print >> f
f.close()
do_command(printer,"M107\n") #fan off
do_command(printer,"M18\n") #motors off
printer.close()
meter.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment