Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # NoLash ported from java (https://github.com/Lenbok/NoLash)
- # Converted to Cura extension by yclee126
- from ..Script import Script
- from UM.Application import Application
- from UM.Logger import Logger
- __version__ = '0.0'
- class NoLashCompensator:
- 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_lash, y_lash):
- # forward and backward, thus divided by 2
- self.x_axis = NoLashCompensator.Axis('X', x_lash/2)
- self.y_axis = NoLashCompensator.Axis('Y', y_lash/2)
- def compensate(self, line):
- self.countermeasure = ''
- # get gcode command
- command = line.split(' ', 1)[0]
- # update commands if needed
- if command == 'G0' or command == 'G1':
- line = self.repair_command(self.x_axis, self.y_axis, line)
- elif command == 'G28':
- self.x_axis.reset()
- self.y_axis.reset()
- # write (modified) gcode
- return line, self.countermeasure
- def repair_command(self, x, y, line):
- # recalc each axis
- line = self.repair_axis(x, line)
- line = self.repair_axis(y, line)
- 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'
- # add countermeasure
- self.countermeasure += ' ' + axis.name + '%.3f' % (calc)
- axis.pos = new_pos
- # compensated value
- recalc = axis.pos + axis.lash * axis.dir
- # return updated command
- return line_front + axis.name + '%.3f' % (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: # no changes
- return True
- return new_dir == axis.dir
- class NoLash(Script):
- def __init__(self):
- super().__init__()
- def getSettingDataString(self):
- return """{
- "name": "Backlash Compensation (NoLash)",
- "key": "NoLash",
- "metadata": {},
- "version": 2,
- "settings":
- {
- "CompensationEnabled":
- {
- "label": "Compensation Enabled",
- "description": "",
- "type": "bool",
- "default_value": true
- },
- "xLash":
- {
- "label": "X Backlash",
- "description": "",
- "type": "float",
- "unit": "mm",
- "default_value": 0.4,
- "minimum_value": 0,
- "enabled": "CompensationEnabled"
- },
- "yLash":
- {
- "label": "Y Backlash",
- "description": "",
- "type": "float",
- "unit": "mm",
- "default_value": 0.4,
- "minimum_value": 0,
- "enabled": "CompensationEnabled"
- }
- }
- }"""
- def execute(self, data):
- x_lash = self.getSettingValueByKey("xLash") if self.getSettingValueByKey("CompensationEnabled") else 0
- y_lash = self.getSettingValueByKey("yLash") if self.getSettingValueByKey("CompensationEnabled") else 0
- no_lash = NoLashCompensator(x_lash, y_lash)
- # for each layer
- for layer_index in range(len(data)):
- lines = data[layer_index].split("\n")
- line_index = 0
- # for each line in a layer
- while line_index < len(lines):
- # skip comments
- if lines[line_index].startswith(';'):
- line_index += 1
- continue
- # compensate
- lines[line_index], countermeasure = no_lash.compensate(lines[line_index])
- if countermeasure != '':
- lines.insert(line_index, countermeasure)
- line_index += 1
- line_index += 1
- result = "\n".join(lines)
- data[layer_index] = result
- return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement