Get current probe state in a macro

Hello, I use a Klicky probe, which is essentially a switch. I’m trying to implement a macro to check the presence of the probe to avoid collisions.
Here’s what I’ve managed to do so far:

[gcode_macro PROVA]
gcode:
G28
# Controlla lo stato del probe
QUERY_PROBE
G4 P1000
QUERY_PROBE
G4 P1000
{% if printer.probe.last_query|int == 0 %}
RESPOND MSG=“OPEN”
{% elif printer.probe.last_query|int == 1 %}
RESPOND MSG=“CLOSE”
{% else %}
RESPOND MSG=“NON RICONOSCIUTO”
{% endif %}

When I run the macro, QUERY_PROBE gives me the correct state of the probe, but with printer.probe.last_query|int I think I get the previous state. Even though I’ve added 2 QUERY_PROBE lines, the read state isn’t updated. To get the correct state, I have to run the macro twice.

So this is a information for other users?

It would be a request for help; I phrased it poorly.

You initially posted to the wrong sub-forum (there have been notes)

Please attach the klippy.log to your next post and information on your system.

It’s a request for help with a macro, should I have posted it?
klippy.log (5.7 MB)

Because when you opened the thread, you got this:

grafik

1 Like

This is a limitation of standard macros (the status of printer objects not updating within a single run). DynamicMacros solves this by allowing printer objects to update mid-macro run.

"Thank you, with Dynamic Macros I was able to read the current state of the probe. Now I have the problem of passing it to other macros:

In dynamic.cfg:
[gcode_macro CHECK_PROBE]
description: Check current state of probe
variable_probe_state:“error”
gcode:
G28
QUERY_PROBE
{% if printer.probe.last_query|int == 0 %}
{% set PROBE_STATE = “open”|string %}
{% elif printer.probe.last_query|int == 1 %}
{% set PROBE_STATE = “triggered”|string %}
{% else %}
{% set PROBE_STATE = “error”|string %}
{% endif %}
#SET_GCODE_VARIABLE MACRO=CHECK_PROBE variable=probe_state value=“{PROBE_STAE}”

SET_GCODE_VARIABLE MACRO=CHECK_PROBE VARIABLE=probe_state VALUE=‘“{PROBE_STATE}”’

In macro.cfg
[gcode_macro PROVA]
gcode:
CHECK_PROBE
M118 {printer[“gcode_macro CHECK_PROBE”].probe_state %}

This is just one of the attempts I’ve made, but the macro doesn’t work, can you help me?"

Dynamic Macros uses a SET_DYNAMIC_VARIABLE command, not the standard SET_GCODE_VARIABLE command. See Variables - DynamicMacros Documentation

Also, in the future, it makes reading code a lot easier on these forums if you format your code in a code block (it’s the button by the bold and italic buttons that looks like </>)

1 Like

“The result doesn’t change, I’ve tried many times but I can’t pass the probe_state variable from the macro in dynamic.cfg to the macro in macros.cfg.”

dynamic.cfg

[gcode_macro CHECK_PROBE]
description: Check current state of probe
variable_probe_state: 2
gcode:
  G28
  QUERY_PROBE
  {% if printer.probe.last_query|int == 0 %}
    {% set PROBE_STATE =  0 %}
  {% elif printer.probe.last_query|int == 1 %}
    {% set PROBE_STATE = 1 %}
  {% else %}
    {% set PROBE_STATE = 2 %}
  {% endif %}
  SET_DYNAMIC_VARIABLE MACRO=CHECK_PROBE VARIABLE=probe_state VALUE=1

macro.cfg

[gcode_macro PROVA]
gcode:
    check_probe
    RESPOND MSG={printer.save_variables.probe_state}
    RESPOND MSG={probe_state}
    MESSAGE msg={printer['gcode_macro CHECK_PROBE'].PROBE_STATE}
    {% set settings = get_macro_variables("CHECK_PROBE") %}
    RESPOND MSG="Settings: {settings}"

I tried the update method, SAVE_VARIABLE, etc., I don’t know what else to do. If the code doesn’t give me an error, it just passes an empty variable. Can you help me with the code?

The get_macro_settings feature is only available for Dynamic Macros. Can you try moving the PROVA macro into your dynamic.cfg? Also, try changing this line (in the PROVA macro):

MESSAGE msg={printer['gcode_macro CHECK_PROBE'].PROBE_STATE}

to this (probe_state shouldn’t be capitalized):

MESSAGE msg={printer['gcode_macro CHECK_PROBE'].probe_state}

I need to pass the current state of the probe to start_print; the macro ‘prova’ is just an example. Somehow, I need to transfer information between dynamic macros and classic macros. Alternatively, I can convert the start_print macro into a dynamic one, but I expect other problems. I’ll try, thanks for the help.

You can convert your PRINT_START macro to be Dynamic. Almost all my macros are dynamic, including my PRINT_START and PRINT_END.

If you don’t want to convert it to dynamic, you can get the probe state in the PRINT_START macro with this:

{printer['gcode_macro CHECK_PROBE'].probe_state}
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.