Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # NoLash ported from java (https://github.com/Lenbok/NoLash) by yclee126
- # Standalone version
- class NoLash:
- class Axis:
- def __init__(self, name, lash):
- self.name = name
- self.lash = lash
- self.dir = 1
- self.pos = 0
- def reset(self):
- self.dir = 1
- self.pos = 0
- def __init__(self, x, y):
- # forward and backward, thus divided by 2
- self.x_lash = x/2
- self.y_lash = y/2
- def compensate(self, in_file, out_file):
- x = NoLash.Axis('X', self.x_lash)
- y = NoLash.Axis('Y', self.y_lash)
- for line in in_file:
- line = line.strip()
- # get gcode command
- command = line.split(' ', 1)[0]
- # update commands if needed
- if command == 'G0' or command == 'G1':
- line = self.repair_command(x, y, line, out_file)
- elif command == 'G28':
- x.reset()
- y.reset()
- # write (modified) gcode
- out_file.write(line + '\n')
- def repair_command(self, x, y, line, out_file):
- self.countermeasure = ''
- # recalc each axis
- line = self.repair_axis(x, line)
- line = self.repair_axis(y, line)
- # write counterpeasure to file before writing modded line
- if self.countermeasure != '':
- out_file.write(self.countermeasure + '\n')
- return line
- def repair_axis(self, axis, line):
- # check if backlash is present
- if axis.lash <= 0:
- return line
- # remove comments (possibly containing X or Y characters inside)
- line_comments = ''
- parts = line.split(';', 1)
- if len(parts) > 1:
- line, line_comments = parts
- line_comments = ';' + line_comments
- # check if this axis move
- parts = line.split(axis.name, 1)
- if len(parts) == 1:
- return line + line_comments
- # parse value
- line_front, axis_value = parts
- parts = axis_value.split(' ', 1)
- line_back = ''
- if len(parts) > 1:
- axis_value, line_back = parts
- line_back = ' ' + line_back
- new_pos = float(axis_value)
- # compare to curr pos, generate necessary countermeasure
- if not self.is_same_direction(axis, new_pos):
- axis.dir = -axis.dir
- calc = axis.pos + axis.lash * axis.dir # countermeasure pos
- # init countermeasure string
- if self.countermeasure == '':
- self.countermeasure = 'G0'
- self.countermeasure += ' ' + axis.name + str(calc)
- axis.pos = new_pos
- # compensated value
- recalc = new_pos + axis.lash * axis.dir
- # return updated command
- return line_front + axis.name + str(recalc) + line_back
- def is_same_direction(self, axis, new_pos):
- new_dir = 0
- if axis.pos < new_pos:
- new_dir = 1
- elif axis.pos > new_pos:
- new_dir = -1
- else:
- return True
- return new_dir == axis.dir
- if __name__ == '__main__':
- # basic CUI interface
- import sys
- # check arg count
- if len(sys.argv) != 5:
- print('Usage: NoLash.py X_LASH Y_LASH IN_FILE.gcode OUT_FILE.gcode')
- exit()
- # parse args
- __name = sys.argv[0]
- x_lash = float(sys.argv[1])
- y_lash = float(sys.argv[2])
- in_file = open(sys.argv[3], 'r', encoding='utf-8')
- out_file = open(sys.argv[4], 'w', encoding='utf-8')
- # process gcode
- no_lash = NoLash(x_lash, y_lash)
- no_lash.compensate(in_file, out_file)
- # close files
- in_file.close()
- out_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement