Skip to content

Commit

Permalink
fix #44
Browse files Browse the repository at this point in the history
reworked framing gcode.  possibly fixes #35
  • Loading branch information
synman committed Dec 14, 2021
1 parent bdddf76 commit d5640f8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 82 deletions.
163 changes: 82 additions & 81 deletions octoprint_bettergrblsupport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self):
self.disablePrinterSafety = True
self.showZ = False
self.weakLaserValue = 1
self.framingPercentOfMaxSpeed = 25

self.overrideM8 = False
self.overrideM9 = False
Expand All @@ -70,7 +71,7 @@ def __init__(self):
self.ignoreErrors = False
self.doSmoothie = False

self.grblIsBusy = False
self.grblCmdQueue = []

self.customControlsJson = r'[{"layout": "horizontal", "children": [{"commands": ["$10=0", "G28.1", "G92 X0 Y0 Z0"], "name": "Set Origin", "confirm": null}, {"command": "M999", "name": "Reset", "confirm": null}, {"commands": ["G1 F4000 S0", "M5", "$SLP"], "name": "Sleep", "confirm": null}, {"command": "$X", "name": "Unlock", "confirm": null}, {"commands": ["$32=0", "M4 S1"], "name": "Weak Laser", "confirm": null}, {"commands": ["$32=1", "M5"], "name": "Laser Off", "confirm": null}], "name": "Laser Commands"}, {"layout": "vertical", "type": "section", "children": [{"regex": "<([^,]+)[,|][WM]Pos:([+\\-\\d.]+,[+\\-\\d.]+,[+\\-\\d.]+)", "name": "State", "default": "", "template": "State: {0} - Position: {1}", "type": "feedback"}, {"regex": "F([\\d.]+) S([\\d.]+)", "name": "GCode State", "default": "", "template": "Speed: {0} Power: {1}", "type": "feedback"}], "name": "Realtime State"}]'

Expand Down Expand Up @@ -109,6 +110,7 @@ def get_settings_defaults(self):
grblSettingsBackup = "",
showZ = False,
weakLaserValue = 1,
framingPercentOfMaxSpeed = 25,
overrideM8 = False,
overrideM9 = False,
m8Command = "/home/pi/bin/tplink_smartplug.py -t air-assist.shellware.com -c on",
Expand Down Expand Up @@ -155,6 +157,7 @@ def on_after_startup(self):

self.showZ = self._settings.get_boolean(["showZ"])
self.weakLaserValue = self._settings.get(["weakLaserValue"])
self.framingPercentOfMaxSpeed = self._settings.get(["framingPercentOfMaxSpeed"])

self.grblSettingsText = self._settings.get(["grblSettingsText"])

Expand Down Expand Up @@ -247,7 +250,7 @@ def on_after_startup(self):

self._settings.save()

self.deSerializeGrblSettings()
self.loadGrblSettings()


def loadGrblDescriptions(self):
Expand Down Expand Up @@ -282,26 +285,26 @@ def loadGrblDescriptions(self):
# self._logger.info("alarm id={} desc={}".format(k, v))


def deSerializeGrblSettings(self):
def loadGrblSettings(self):
self.grblSettingsText = self._settings.get(["grblSettingsText"])

for setting in self.grblSettingsText.split("||"):
if len(setting.strip()) > 0:

self._logger.debug("deSerializeGrblSettings=[{}]".format(setting))
self._logger.debug("loadGrblSettings=[{}]".format(setting))

set = setting.split("|")
if not set is None:
self.grblSettings.update({int(set[0]): [set[1], self.grblSettingsNames.get(int(set[0]))]})
return


def serializeGrblSettings(self):
def saveGrblSettings(self):
ret = ""
for id, data in sorted(self.grblSettings.items(), key=lambda x: int(x[0])):
ret = ret + "{}|{}|{}||".format(id, data[0], data[1])

self._logger.debug("serializeGrblSettings=[\n{}\n]".format(ret))
self._logger.debug("saveGrblSettings=[\n{}\n]".format(ret))

self.grblSettingsText = ret;
return ret
Expand Down Expand Up @@ -337,7 +340,7 @@ def get_template_configs(self):
]

# def get_template_vars(self):
# return dict(grblSettingsText=self.serializeGrblSettings())
# return dict(grblSettingsText=self.saveGrblSettings())


# #-- EventHandlerPlugin mix-in
Expand Down Expand Up @@ -476,7 +479,7 @@ def hook_gcode_sending(self, comm_instance, phase, cmd, cmd_type, gcode, *args,

# suppress temperature if printer is printing
if cmd.upper().startswith('M105'):
if self.disablePolling and (self._printer.is_printing() or self.grblIsBusy):
if self.disablePolling and self._printer.is_printing():
self._logger.debug('Ignoring %s', cmd)
return (None, )
else:
Expand Down Expand Up @@ -641,7 +644,7 @@ def hook_gcode_received(self, comm_instance, line, *args, **kwargs):
self._logger.debug("setting id={} value={} description={}".format(settingsId, settingsValue, self.grblSettingsNames.get(settingsId)))

if settingsId >= 132:
self._settings.set(["grblSettingsText"], self.serializeGrblSettings())
self._settings.set(["grblSettingsText"], self.saveGrblSettings())
self._settings.save()

return line
Expand All @@ -662,13 +665,14 @@ def hook_gcode_received(self, comm_instance, line, *args, **kwargs):
# It needs a different format. Put both on the same line so the GRBL info is not lost
# and is accessible for "controls" to read.
response = 'ok X:{1} Y:{2} Z:{3} E:0 {original}'.format(*match.groups(), original=line)
self._logger.debug('[%s] rewrote as [%s]', line.strip(), response.strip())

self.grblState = str(match.groups(1)[0])
self.grblX = float(match.groups(1)[1])
self.grblY = float(match.groups(1)[2])
self.grblZ = float(match.groups(1)[3])

self._logger.debug('[%s] rewrote as [%s] - state is [%s]', line.strip(), response.strip(), self.grblState)

match = re.search(r'.*\|FS:(-?[\d\.]+),(-?[\d\.]+)', line)
if not match is None:
self.grblSpeed = round(float(match.groups(1)[0]))
Expand All @@ -683,6 +687,12 @@ def hook_gcode_received(self, comm_instance, line, *args, **kwargs):
speed=self.grblSpeed,
power=self.grblPowerLevel))

# pop any queued commands if state is not RUN
if len(self.grblCmdQueue) > 0 and self.grblState.upper().strip() != "RUN":
self._logger.debug('sending queued command [%s] - depth [%d]', self.grblCmdQueue[0], len(self.grblCmdQueue))
self._printer.commands(self.grblCmdQueue[0])
self.grblCmdQueue.pop(0)

return response

# match = re.search(r"F(-?[\d.]+) S(-?[\d.]+)", line)
Expand Down Expand Up @@ -720,32 +730,29 @@ def hook_gcode_received(self, comm_instance, line, *args, **kwargs):

# i am thinking these commands are all wrong (all framing)
def send_frame_init_gcode(self):
self.grblIsBusy = True

xF = int(float(self.grblSettings.get(110)[0]) * (float(self.framingPercentOfMaxSpeed) * .01))
yF = int(float(self.grblSettings.get(111)[0]) * (float(self.framingPercentOfMaxSpeed) * .01))

self._logger.debug("Feed Rate X:{} Y:{}".format(xF,yF))

self.queue_cmds_send_and_wait(self, ["$32=0","$110=" + str(xF),"$111=" + str(yF)])

self._printer.commands("G4 P0")
self._printer.commands("$32=0")
self._printer.commands("$110=1000")
self._printer.commands("$111=1000")
self._printer.commands("G00 G17 G40 G21 G54")
self._printer.commands("G91")
self._printer.commands("M4 F1000 S{}".format(self.weakLaserValue))
self._printer.commands("M4 S{}".format(self.weakLaserValue))
self._printer.commands("G91")


def send_frame_end_gcode(self):
self._printer.commands("$32=1")
self._printer.commands("$110=5000")
self._printer.commands("$111=5000")

self._printer.commands("G1S0")
self._printer.commands("M4 F0 S0")
self._printer.commands("M5")
self._printer.commands("M2")
self._printer.commands("G4 P0")

time.sleep(1)

self.grblIsBusy = False
self.queue_cmds_send_and_wait(self, ["$32=1","$110=" + self.grblSettings.get(110)[0],"$111=" + self.grblSettings.get(111)[0]])


def send_bounding_box_upper_left(self, y, x):
Expand All @@ -756,65 +763,65 @@ def send_bounding_box_upper_left(self, y, x):


def send_bounding_box_upper_center(self, y, x):
self._printer.commands("G0 X{:f} F2000 S{}".format(x / 2, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x / 2, self.weakLaserValue))
self._printer.commands("G0 X{:f}".format(x / 2))
self._printer.commands("G0 Y{:f}".format(y * -1))
self._printer.commands("G0 X{:f}".format(x * -1))
self._printer.commands("G0 Y{:f}".format(y))
self._printer.commands("G0 X{:f}".format(x / 2))


def send_bounding_box_upper_right(self, y, x):
self._printer.commands("G0 Y{:f} F2000 S{}".format(y * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x, self.weakLaserValue))
self._printer.commands("G0 Y{:f}".format(y * -1))
self._printer.commands("G0 X{:f}".format(x * -1))
self._printer.commands("G0 Y{:f}".format(y))
self._printer.commands("G0 X{:f}".format(x))


def send_bounding_box_center_left(self, y, x):
self._printer.commands("G0 Y{:f} F2000 S{}".format(y / 2, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y / 2, self.weakLaserValue))
self._printer.commands("G0 Y{:f}".format(y / 2))
self._printer.commands("G0 X{:f}".format(x))
self._printer.commands("G0 Y{:f}".format(y * -1))
self._printer.commands("G0 X{:f}".format(x * -1))
self._printer.commands("G0 Y{:f}".format(y / 2))


def send_bounding_box_center(self, y, x):
self._printer.commands("G0 X{:f} Y{:f} F4000".format(x / 2 * -1, y / 2))
self._printer.commands("G0 X{} F2000 S{}".format(x, self.weakLaserValue))
self._printer.commands("G0 Y{:f} S{}".format(y * -1, self.weakLaserValue))
self._printer.commands("G0 X{} S{}".format(x * -1, self.weakLaserValue))
self._printer.commands("G0 Y{} S{}".format(y, self.weakLaserValue))
self._printer.commands("G0 X{:f} Y{:f} F4000".format(x / 2, y / 2 * -1))
self._printer.commands("G0 X{:f} Y{:f}".format(x / 2 * -1, y / 2))
self._printer.commands("G0 X{}".format(x))
self._printer.commands("G0 Y{:f}".format(y * -1))
self._printer.commands("G0 X{}".format(x * -1))
self._printer.commands("G0 Y{}".format(y))
self._printer.commands("G0 X{:f} Y{:f}".format(x / 2, y / 2 * -1))


def send_bounding_box_center_right(self, y, x):
self._printer.commands("G0 Y{:f} F2000 S{}".format(y / 2 * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y / 2 * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f}".format(y / 2 * -1))
self._printer.commands("G0 X{:f}".format(x * -1))
self._printer.commands("G0 Y{:f}".format(y))
self._printer.commands("G0 X{:f}".format(x))
self._printer.commands("G0 Y{:f}".format(y / 2 * -1))


def send_bounding_box_lower_left(self, y, x):
self._printer.commands("G0 Y{:f} F2000 S{}".format(y, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f}".format(y))
self._printer.commands("G0 X{:f}".format(x))
self._printer.commands("G0 Y{:f}".format(y * -1))
self._printer.commands("G0 X{:f}".format(x * -1))


def send_bounding_box_lower_center(self, y, x):
self._printer.commands("G0 X{:f} F2000 S{}".format(x / 2 * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x / 2 * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f}".format(x / 2 * -1))
self._printer.commands("G0 Y{:f}".format(y))
self._printer.commands("G0 X{:f}".format(x))
self._printer.commands("G0 Y{:f}".format(y * -1))
self._printer.commands("G0 X{:f}".format(x / 2 * -1))


def send_bounding_box_lower_right(self, y, x):
self._printer.commands("G0 X{:f} F2000 S{}".format(x * -1, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y, self.weakLaserValue))
self._printer.commands("G0 X{:f} F2000 S{}".format(x, self.weakLaserValue))
self._printer.commands("G0 Y{:f} F2000 S{}".format(y * -1, self.weakLaserValue))
self._printer.commands("G0 X{:f}".format(x * -1))
self._printer.commands("G0 Y{:f}".format(y))
self._printer.commands("G0 X{:f}".format(x))
self._printer.commands("G0 Y{:f}".format(y * -1))


def get_api_commands(self):
Expand Down Expand Up @@ -864,7 +871,7 @@ def on_api_command(self, command, data):
return

if command == "backupGrblSettings":
self._settings.set(["grblSettingsBackup"], self.serializeGrblSettings())
self._settings.set(["grblSettingsBackup"], self.saveGrblSettings())
self._settings.save()
return

Expand Down Expand Up @@ -981,46 +988,30 @@ def on_api_command(self, command, data):

if command == "originxy":
# do xy-origin stuff
self.queue_cmds_send_and_wait(self, ["$10=0"])

# saveIgnoreErrors = self.ignoreErrors
# self.ignoreErrors = True
self.grblIsBusy = True

self._printer.commands("$10=0")
self._printer.commands("G28.1")
self._printer.commands("G92 X0 Y0")

time.sleep(1)
self.queue_cmds_send_and_wait(self, ["$10=0"])

self._printer.commands("$10=0")
self._printer.commands("G28.1")
self._printer.commands("G92 X0 Y0")

# self.ignoreErrors = saveIgnoreErrors
self.grblIsBusy = False

return

if command == "originz":
# do z-origin stuff
self.queue_cmds_send_and_wait(self, ["$10=0"])

# saveIgnoreErrors = self.ignoreErrors
# self.ignoreErrors = True
self.grblIsBusy = True

self._printer.commands("$10=0")
self._printer.commands("G28.1")
self._printer.commands("G92 Z0")

time.sleep(1)
self.queue_cmds_send_and_wait(self, ["$10=0"])

self._printer.commands("$10=0")
self._printer.commands("G28.1")
self._printer.commands("G92 Z0")

# self.ignoreErrors = saveIgnoreErrors
self.grblIsBusy = False

return

if command == "toggleWeak":
Expand All @@ -1046,6 +1037,16 @@ def toggleWeak(self):

return res


def queue_cmds_send_and_wait(self, *args):
cmds = args[1];

for cmd in cmds:
self.grblCmdQueue.append(cmd)

while len(self.grblCmdQueue) > 0:
time.sleep(.1)

# #~~ Softwareupdate hook
def get_update_information(self):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@

<br>

<label class="control-label">Framing Feed Rate</label>
<div class="controls">
<input type="text" class="input-mini" data-bind="value: settings.settings.plugins.bettergrblsupport.framingPercentOfMaxSpeed">% of max
</div>

<br>

<label class="control-label">Weak Laser Intensity</label>
<div class="controls">
<input type="text" class="input-mini" data-bind="value: settings.settings.plugins.bettergrblsupport.weakLaserValue">
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "Better Grbl Support"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.7.6dev5"
plugin_version = "1.7.6dev6"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit d5640f8

Please sign in to comment.