Skip to content

Commit

Permalink
Merge pull request jminardi#46 from jminardi/jm/feature-ouput-digits
Browse files Browse the repository at this point in the history
Add ability to specify # of output digits
  • Loading branch information
jminardi authored Nov 10, 2016
2 parents 66f6e0b + e3bd159 commit d07983b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
25 changes: 15 additions & 10 deletions mecode/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
g.view()
* *Author:* Jack Minardi
* *Email:* [email protected]
* *Email:* [email protected]
This software was developed by the Lewis Lab at Harvard University.
This software was developed by the Lewis Lab at Harvard University and Voxel8 Inc.
"""

Expand Down Expand Up @@ -81,7 +81,7 @@ def decode2To3(s):
class G(object):

def __init__(self, outfile=None, print_lines=True, header=None, footer=None,
aerotech_include=True, direct_write=False,
aerotech_include=True, output_digits=6, direct_write=False,
direct_write_mode='socket', printer_host='localhost',
printer_port=8000, baudrate=250000, two_way_comm=True,
x_axis='X', y_axis='Y', z_axis='Z', extrude=False,
Expand All @@ -103,6 +103,8 @@ def __init__(self, outfile=None, print_lines=True, header=None, footer=None,
of the output file.
aerotech_include : bool (default: True)
If true, add aerotech specific functions and var defs to outfile.
output_digits : int (default: 6)
How many digits to include after the decimal in the output gcode.
direct_write : bool (default: False)
If True a socket or serial port is opened to the printer and the
GCode is sent directly over.
Expand Down Expand Up @@ -155,6 +157,7 @@ def __init__(self, outfile=None, print_lines=True, header=None, footer=None,
self.header = header
self.footer = footer
self.aerotech_include = aerotech_include
self.output_digits = output_digits
self.direct_write = direct_write
self.direct_write_mode = direct_write_mode
self.printer_host = printer_host
Expand Down Expand Up @@ -482,10 +485,11 @@ def arc(self, x=None, y=None, z=None, direction='CW', radius='auto',
self.write(plane_selector)
args = self._format_args(**dims)
if helix_dim is None:
self.write('{0} {1} R{2:f}'.format(command, args, radius))
self.write('{0} {1} R{2:.{digits}f}'.format(command, args, radius,
digits=self.output_digits))
else:
self.write('{0} {1} R{2:f} G1 {3}{4}'.format(command, args, radius,
helix_dim.upper(), helix_len))
self.write('{0} {1} R{2:.{digits}f} G1 {3}{4}'.format(
command, args, radius, helix_dim.upper(), helix_len, digits=self.output_digits))
dims[helix_dim] = helix_len

self._update_current_position(**dims)
Expand Down Expand Up @@ -888,14 +892,15 @@ def _write_header(self):
self.out_fd.write(encode2To3('\n'))

def _format_args(self, x=None, y=None, z=None, **kwargs):
d = self.output_digits
args = []
if x is not None:
args.append('{0}{1:f}'.format(self.x_axis, x))
args.append('{0}{1:.{digits}f}'.format(self.x_axis, x, digits=d))
if y is not None:
args.append('{0}{1:f}'.format(self.y_axis, y))
args.append('{0}{1:.{digits}f}'.format(self.y_axis, y, digits=d))
if z is not None:
args.append('{0}{1:f}'.format(self.z_axis, z))
args += ['{0}{1:f}'.format(k, v) for k, v in kwargs.items()]
args.append('{0}{1:.{digits}f}'.format(self.z_axis, z, digits=d))
args += ['{0}{1:.{digits}f}'.format(k, v, digits=d) for k, v in kwargs.items()]
args = ' '.join(args)
return args

Expand Down
14 changes: 14 additions & 0 deletions mecode/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,20 @@ def test_triangular_wave(self):
self.assert_output()
self.assert_position({'x': 3, 'y': 4, 'z': 0})

def test_output_digits(self):
self.g.output_digits = 1
self.g.move(10)
self.expect_cmd("""
G1 X10.0
""")
self.assert_output()
self.g.output_digits = 6
self.g.move(10)
self.expect_cmd("""
G1 X10.000000
""")
self.assert_output()


if __name__ == '__main__':
unittest.main()

0 comments on commit d07983b

Please sign in to comment.