Sending commands to printer from plugin

I'm writing a pretty useful OctoPrint plugin. The way it works is by intercepting an unused Gcode - G910 - and then performing a calibration step based on a computer vision analysis.

So far I've been able to intercept the G910 calls using the octoprint.comm.protocol.gcode hooks. I can perform my analysis, and prepare the ~10 Gcode commands that I'd like to send.

In my code, after receiving the G910 command, I'd like to:

  1. Pause further gcodes from sending until my script finishes
  2. Send my own ~ 10 gcodes commands to the printer
  3. Resume the remainder of the .gcode script

Is it possible to do this in OctoPrint? I can't find info on plugins sending commands to the printer and I'm not sure the .gcode file will wait for G910 to finish before sending more commands right now.

In the hook documentation you linked there's an example of how to replace a gcode command by returning a different one in the hook callback.

https://docs.octoprint.org/en/master/plugins/hooks.html#id11

Thanks. G910 takes about 5 seconds to run and process. I will comment back here if it doesnt work but thank you

Unless it's already there, you might want to then add G910 to the list of long-running gcode commands in the Settings.

Wouldn't an atcommand be better than a non-standard gcode in this case?

An example from the docs:

“queuing” phase only: A list of any of the above to allow for expanding one command into many. The following example shows how any queued command could be turned into a sequence of a temperature query, line number reset, display of the gcode on the printer’s display and finally the actual command (this example does not make a lot of sense to be quite honest):

def rewrite_foo(self, comm_instance, phase, cmd, cmd_type, gcode, subcode=None, tags=None *args, **kwargs):
    if gcode or not cmd.startswith("@foo"):
        return

    return [("M105", "temperature_poll"),    # 2-tuple, command & command type
            ("M110",),                       # 1-tuple, just the command
            "M117 echo foo: {}".format(cmd)] # string, just the command

__plugin_hooks__ = {
    "octoprint.comm.protocol.gcode.queuing": rewrite_foo
}
1 Like