Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions mecode/devices/efd_pico_pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ def disconnect(self):
self.s.close()

def send(self, command):
"""Send message over serial to PicoTouch controller."""
"""Send message over serial to PicoTouch controller."""
msg = command + EOT
self.s.write(msg)
# return response and remove ACK
return self.s.read_until(ACK)[:-2]

def set_valve_mode(self, mode):
"""Set valve mode to Timed, Purge, Continous, or read current mode.
"""Set valve mode to Timed, Purge, Continous, or read current mode.

Keyword argument:
mode -- 1 = Timed; 2 = Purge; 3 = Continuous; 5 = read current mode """
Keyword argument:
mode -- 1 = Timed; 2 = Purge; 3 = Continuous; 5 = read current mode """
return self.send(str(mode) + 'drv1')

def set_dispense_count(self, count):
"""Set how many times valve dispenses with each cycle."""
"""Set how many times valve dispenses with each cycle."""
return self.send('{:05}'.format(count) + 'dcn1')

def get_valve_status(self):
"""Return valve's current parameters and dispense statistics."""
"""Return valve's current parameters and dispense statistics."""
return self.send('rdr1')

def cycle_valve(self):
"""Cycle the valve (eqiuvalent to pressing cycle button)."""
"""Cycle the valve (eqiuvalent to pressing cycle button)."""
return self.send('1cycl') + self.send('0cycl')

def set_heater_mode(self, mode):
Expand All @@ -66,18 +66,18 @@ def set_heater_temp(self, temp):
return self.send('{:05.1f}'.format(temp) + 'stmp')

def get_heater_status(self):
"""Return mode, heater setpoint temp, and heater actual temp."""
"""Return mode, heater setpoint temp, and heater actual temp."""
return self.send('rhtr')

def get_valve_info(self):
"""Return controller and valve SN and type, fw version, pcb rev."""
"""Return controller and valve SN and type, fw version, pcb rev."""
return self.send('info')

def get_alarm_hist(self):
"""Return last 40 alarm conditions with time and alarm name."""
"""Return last 40 alarm conditions with time and alarm name."""
return self.send('ralr')

def reset_alarm(self):
"""Reset a currently active alarm."""
"""Reset a currently active alarm."""
return self.send('arst')

99 changes: 80 additions & 19 deletions mecode/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def __init__(self, outfile=None, print_lines='auto', header=None, footer=None,
extrusion_multiplier=1,
setup=True,
lineend='os',
comment_char=';'):
comment_char=';',
absolute=False):
"""
Parameters
----------
Expand Down Expand Up @@ -179,6 +180,11 @@ def __init__(self, outfile=None, print_lines='auto', header=None, footer=None,
lineending insertion.
comment_char : str (default: ';')
Character to use when outputting comments.
Special case handling for parenthesis comments. If "(" is specified
as the comment symbol, the comments will be wrapped in both opening
and closing parenthesis: `G1 X5 ( this is a comment )`
absolute : bool (default: False)
Should the system default to relative or absolute mode

"""
self.outfile = outfile
Expand All @@ -199,7 +205,7 @@ def __init__(self, outfile=None, print_lines='auto', header=None, footer=None,
self.i_axis = i_axis
self.j_axis = j_axis
self.k_axis = k_axis
self.comment_char = comment_char
self._comment_char = comment_char

self._current_position = defaultdict(float)
self.is_relative = True
Expand Down Expand Up @@ -237,6 +243,10 @@ def __init__(self, outfile=None, print_lines='auto', header=None, footer=None,
if setup:
self.setup()

if absolute:
self.absolute()


@property
def current_position(self):
return self._current_position
Expand All @@ -259,6 +269,54 @@ def __exit__(self, exc_type, exc_value, traceback):
"""
self.teardown()

def _commentify(self, txt):
'''
Format text `txt` in whichever manner is needed to indicate it's a comment.
The mode is set by the `comment_char` parameter on class construction
'''

# We need to special case using parenthesis for comments, since we have to
# wrap them around the beginning and the end of the comment
if self._comment_char == "(":
return "( {} )".format(txt)

# Otherwise, just prepend the comment character (and a space)
return "{} {}".format(self._comment_char, txt)


def write_comment(self, comment):
""" Insert a comment into the gcode output file
"""
self.write(self._commentify(comment))

def move_other_axis(self, a, rapid=False):
""" Move an auxilliary axis (currently "A")

Primarily, this is useful for a rotational indexer (if present)
"""

cmd = 'G0 ' if rapid else 'G1 '
self.write(cmd + "A{:.{digits}f}".format(a, digits=self.output_digits))

def insert_machine_stop(self, comment=None):
'''Insert a machine stop (M00) command with an optional comment
'''
if comment:
self.write("M00 {}".format(self._commentify(comment)))
else:
self.write("M00")

def insert_optional_stop(self, comment=None):
'''Insert a optional stop (M01) command with an optional comment
'''
if comment:
self.write("M01 {}".format(self._commentify(comment)))
else:
self.write("M01")




# GCode Aliases ########################################################

def set_home(self, x=None, y=None, z=None, **kwargs):
Expand All @@ -272,7 +330,7 @@ def set_home(self, x=None, y=None, z=None, **kwargs):
"""
args = self._format_args(x, y, z, **kwargs)
space = ' ' if len(args) > 0 else ''
self.write('G92' + space + args + ' {}set home'.format(self.comment_char))
self.write('G92' + space + args + " " +self._commentify('set home'))

self._update_current_position(mode='absolute', x=x, y=y, z=z, **kwargs)

Expand All @@ -282,15 +340,15 @@ def reset_home(self):
# FIXME This does not work with internal current_position
# FIXME You must call an abs_move after this to re-sync
# current_position
self.write('G92.1 {}reset position to machine coordinates without moving'.format(self.comment_char))
self.write('G92.1 {}'.format(self._commentify("reset position to machine coordinates without moving")))

def relative(self):
""" Enter relative movement mode, in general this method should not be
used, most methods handle it automatically.

"""
if not self.is_relative:
self.write('G91 {}relative'.format(self.comment_char))
self.write('G91 {}'.format(self._commentify("relative")))
self.is_relative = True

def absolute(self):
Expand All @@ -299,7 +357,7 @@ def absolute(self):

"""
if self.is_relative:
self.write('G90 {}absolute'.format(self.comment_char))
self.write('G90 {}'.format(self._commentify("absolute")))
self.is_relative = False

def feed(self, rate):
Expand All @@ -314,7 +372,7 @@ def feed(self, rate):
self.write('G1 F{}'.format(rate))
self.speed = rate

def dwell(self, time):
def dwell(self, time, comment=None):
""" Pause code executions for the given amount of time.

Parameters
Expand All @@ -323,7 +381,11 @@ def dwell(self, time):
Time in milliseconds to pause code execution.

"""
self.write('G4 P{}'.format(time))

if comment:
self.write('G4 P{} {}'.format(time, self._commentify(comment)))
else:
self.write('G4 P{}'.format(time))

# Composed Functions #####################################################

Expand All @@ -334,9 +396,9 @@ def setup(self):
"""
self._write_header()
if self.is_relative:
self.write('G91 {}relative'.format(self.comment_char))
self.write('G91 {}'.format(self._commentify("relative")))
else:
self.write('G90 {}absolute'.format(self.comment_char))
self.write('G90 {}'.format(self._commentify("absolute")))

def teardown(self, wait=True):
""" Close the outfile file after writing the footer if opened. This
Expand Down Expand Up @@ -482,14 +544,14 @@ def arc(self, x=None, y=None, z=None, direction='CW', radius='auto',
raise RuntimeError(msg)
dimensions = [k.lower() for k in dims.keys()]
if 'x' in dimensions and 'y' in dimensions:
plane_selector = 'G17 {}XY plane'.format(self.comment_char) # XY plane
plane_selector = 'G17 {}'.format(self._commentify('XY plane')) # XY plane
axis = helix_dim
elif 'x' in dimensions:
plane_selector = 'G18 {}XZ plane'.format(self.comment_char) # XZ plane
plane_selector = 'G18 {}'.format(self._commentify('XZ plane')) # XZ plane
dimensions.remove('x')
axis = dimensions[0].upper()
elif 'y' in dimensions:
plane_selector = 'G19 {}YZ plane'.format(self.comment_char) # YZ plane
plane_selector = 'G19 {}'.format(self._commentify('YZ plane')) # YZ plane
dimensions.remove('y')
axis = dimensions[0].upper()
else:
Expand Down Expand Up @@ -539,7 +601,7 @@ def arc(self, x=None, y=None, z=None, direction='CW', radius='auto',
dims['E'] = filament_length + current_extruder_position

if axis is not None:
self.write('G16 X Y {} {}coordinate axis assignment'.format(axis, self.comment_char)) # coordinate axis assignment
self.write('G16 X Y {} {}'.format(axis, self._commentify("coordinate axis assignment"))) # coordinate axis assignment
self.write(plane_selector)
args = self._format_args(**dims)
if helix_dim is None:
Expand Down Expand Up @@ -582,7 +644,7 @@ def arc_ijk(self, target, center, plane, direction='CW', helix_len=None):
raise RuntimeError("'center' must be a 2-tuple of numbers (passed %s)" % center)

if plane == 'xy':
self.write('G17 {}XY plane'.format(self.comment_char)) # XY plane
self.write('G17 {}'.format(self._commentify('XY plane'))) # XY plane
dims = {
'x' : target[0],
'y' : target[1],
Expand All @@ -592,7 +654,7 @@ def arc_ijk(self, target, center, plane, direction='CW', helix_len=None):
if helix_len:
dims['z'] = helix_len
elif plane == 'yz':
self.write('G19 {}YZ plane'.format(self.comment_char)) # YZ plane
self.write('G19 {}'.format(self._commentify('YZ plane'))) # YZ plane
dims = {
'y' : target[0],
'z' : target[1],
Expand All @@ -602,7 +664,7 @@ def arc_ijk(self, target, center, plane, direction='CW', helix_len=None):
if helix_len:
dims['x'] = helix_len
elif plane == 'xz':
self.write('G18 {}XZ plane'.format(self.comment_char)) # XZ plane
self.write('G18 {}'.format(self._commentify('XZ plane'))) # XZ plane
dims = {
'x' : target[0],
'z' : target[1],
Expand Down Expand Up @@ -756,8 +818,7 @@ def meander(self, x, y, spacing, start='LL', orientation='x', tail=False,

actual_spacing = self._meander_spacing(minor, spacing)
if abs(actual_spacing) != spacing:
msg = '{}WARNING! meander spacing updated from {} to {}'
self.write(msg.format(self.comment_char, spacing, actual_spacing))
self.write(self._commentify("WARNING! meander spacing updated from {} to {}".format(spacing, actual_spacing)))
spacing = actual_spacing
sign = 1

Expand Down
Loading