Trying to get Positional Data With M114

Greetings,
I'm currently trying to get my project i made in a book with ESP controls to run in a plugin. The idea is, I get the height from a homed printer, move the print-head down to the last well done layer of a failed print using a piece of paper as a tool, then tell the plugin to start the print again from this exact height, it splits the GCode in python and starts the print.
It is working using my book, but i want it to run more elegantly in an Octoprint Plugin with UI.

The problem is, I don't know how to get the information from the M114 result back, and if I would get it, I have no idea how to pipe that information back to python. I have been following the custom controls wiki page that mentions using M114. I also saw some other implementations to get a button to work from the .js file, so my status is now like this:

$(function() {
    function GCode_Restarter_ViewModel(parameters) {
        var self = this;

        self.controlViewModel = parameters[0];
        self.getAdditionalControls = function() {
            return [{
                'name': 'Reporting', 'children':[
                    {'command': 'M114',
                    'default': '""',
                    'name': 'Get Position',
                    'regex': 'X:([-+]?[0-9.]+) Y:([-+]?[0-9.]+) Z:([-+]?[0-9.]+) E:([-+]?[0-9.]+)',
                    'template': '"Position: X={0}, Y={1}, Z={2}, E={3}"',
                    'type': 'feedback_command'}
                ]
            }];
        };
    }

    OCTOPRINT_VIEWMODELS.push({
        construct: GCode_Restarter_ViewModel,
        dependencies: [ "controlViewModel" ]
    });
});

This gives me a Button, I can press the Button, the button executes the M114 command, it gets something useful back:
Recv: X:-8.00 Y:-28.00 Z:0.00 E:0.00 Count X:-320 Y:-1120 Z:0

What it doesn't do is write the Template text next to it, does anyone have any idea? And While I'm asking, How do I execute my python script with that result? I could probably call my Blueprint API with some hacked together id addressing to do that if there is no more elegant way. JS is not a Language I'm super fluent in.

Does the regex require you to match the whole line?

is missing an exact match to the whole line. does it work if you add a .+ after the last parenthesis like this?

X:([-+]?[0-9.]+) Y:([-+]?[0-9.]+) Z:([-+]?[0-9.]+) E:([-+]?[0-9.]+).+

Minor nitpick: I would append .* not .+ because .* matches the empty string whereas .+ is one character minimum.

that's a good point, but assuming he's matching the whole line anyway the character would be there based on his example.

I concur with this example the difference is irrelevant, because of this I wrote "Minor nitpick" :wink:

I know many people, even senior programmers, who have problems with regexp. Therefore I think your solution will be copy&pasted by some without real comprehension. For these people .* will be better :slight_smile:

Thank you for the quick answer, this was however not the solution for me. I have tried it, it still does not work. I had a similar idea once, i had been adding the rest of the line to the regex, that also didn't work. Bildschirmfoto von 2020-10-05 13-05-36
as you can see, there is still nothing showing up next to my pressed Get Position button.
I have also, thanks to jandar been trying it out with a * instead of a +, this too did not work.

I wonder if this is an issue with getAdditionalControls versus being in the config.yaml file directly. I would think all the same things are available but have recently come across an issue with my BLTouch plugin that the buttons aren't disabled by default the same way they are if in config.yaml.

Oh about the config.yaml, another thing on that, when I try to add the reporting button to my controls in the config.yaml directly, it doesn't show up in my octoprint at all... So I can't really test if that is different

So this does seem to work in config.yaml.

image

controls:
-   children:
    -   command: M114
        name: Get Position
        regex: X:([-+]?[0-9.]+) Y:([-+]?[0-9.]+) Z:([-+]?[0-9.]+) E:([-+]?[0-9.]+)
        template: 'Position: X={0}, Y={1}, Z={2}, E={3}'
    name: Reporting

At least with the virtual printer.

I wonder if you changed your template to be this if it would work?

'\'Position: X={0}, Y={1}, Z={2}, E={3}\''

it doesn't make a difference to the functionality not working but this should be

type: `command`

After a little javascript debugging I found that the objects getting passed to the observable when building the buttons is completely different between the two.

in config.yaml I have this on load.
image

and from getAdditionalControls I only get this.
image

So looking at the code for the control tab it seems that because there is no key or template_key the data does not get linked to the control the same way. I attempted adding the missing key and template_key to my return value and it still doesn't work, so honestly think this is a bug.

Opened issue here.

1 Like

oh wow, you're right, i totaly forgot that I can test without using my printer

I'll try that later today :slight_smile:

good to know, then maybe I'll try to find a way around using the getAdditionalControls method, maybe even piping it through my Blueprint plugin part and back xD

I wonder if you could utilize a settings overlay instead. I use it to control tab order here. Maybe you could overly controls.

that sounds clever, maybe I'll have to look into it more later, however, I just had a breakthrough for me, I noticed that I completely overlooked the Eventhandler Mixin. I was a bit unsure how to use it, maybe I'll add a PR to the docs later but i figured the Eventhandlers out

So any M114 result triggers the PositionUpdate Event, and from the PositionUpdate event the payload is just the Positiondata the way I wanted it. So I can just trigger that, take the result and then I have a python script running with all the data that I can use, I can store the height and... well, no idea from there... Will look into UI Mixins next or try a Controls overlay like you suggested to make sure my users don't press the wrong Movement buttons xD

Oh and for no apparent reason, the button from the config.yaml started working today and the regex even reacts to M114 Commands it doesn't trigger like ones triggered by the home buttons

Oh yeah, that was going to be original recommendation using event callbacks, but thought the button thing was more important to you.

The results are in relative to getAdditionalControls. It doesn't seem to be able to be possible and the recommendation is a settings overlay.

i just figured out what those setting overlays do, i tried them by adding:

__plugin_settings_overlay__ = dict(controls=[dict(children=[dict(command="M114",name="Get Position",
        regex="X:([-+]?[0-9.]+) Y:([-+]?[0-9.]+) Z:([-+]?[0-9.]+) E:([-+]?[0-9.]+)",
        template="Position: X={0}, Y={1}, Z={2}, E={3}")],name="Reporting_Overlay")])

to the end of my plugin, thanks for the hint, i would have never guessed this could work
that returns this:

{"controls":[{"children":[{
"command":"M114",
"key":"27df2bcc9fac1d076764b2822900258e",
"name":"Get Position",
"regex":"X:([-+]?[0-9.]+) Y:([-+]?[0-9.]+) Z:([-+]?[0-9.]+) E:([-+]?[0-9.]+)",
"template":"Position: X={0}, Y={1}, Z={2}, E={3}",
"template_key":"74c24fa0c1ae2be17831cf79d8b0e75f"}],
"name":"Reporting_Overlay"}]}

And it actually works, and with the rest i know how to get The info into the Python script and into the Interface, thank you so much :smile:

1 Like

Great news, glad to help where I can.

yay, thanks to your help I found the things I needed, the rest was... not easy but possible, it is published now :smile: if anyone has a failed prusaprinter print and is willing to take the risk to restart a file from tested but arbitrary so dangerous GCode (or just wants to see how i made it): https://gitlab.com/Grosskopfgames/octoprint-gcode-restarter/

Great plugin idea. I hope to be able to review it soon when you submit it to the official plugin repo.

1 Like