Conditional g-code



  • Is there a way to make a if statement in g-code?

    I want to setup a conditional ending script, where I can set a variable (via macro) at the start of the Print
    and depending on that set variable execute different commands.

    So I would need a way to set and store variables and a way to do conditional branching in g-code

    I know there are g-code coommands for that (like so) , but I think they are not implemented in the duet.



  • That would be very interesting.

    Probably a quick implementation could be done using Lua scripting.
    There is an implementation of Lua for Free RTOS and now that Duet is built on top of Free RTOS it could be possible to add a thread with lua scripting interface.

    Once lua is in place, it could be possible to send G code or read the serial interface and parse results, then use the results on any script. There are several projects on Git with code to send Gcode from Lua or to read a serial communications where command outputs would be posted.



  • Do you have a link to a project like that, maybe with good explanation?

    I haven't worked with lua before, but im happy to try it out...



  • @niocio01 said in Conditional g-code:

    I haven't worked with lua before, but im happy to try it out...

    Sorry but I can't sit on my hands....

    I love Lua. I learned it a few years ago to use the Torch7 neural nets package; I now consider it to be the optimal programming language, certainly for scientific programming - based on my experience of 15(+?) languages since 1980.

    The cons are:

    • Really simplistic syntax, with annoyances like arrays start at [1] instead of [0] and and no variable++ shortcuts (must write variable = variable+1)
    • Not as much library support as more popular languages like perl or python

    The pros are:

    • Really simplistic syntax, so quick to learn, small to implement on embedded systems, minus all sorts of bug sources, and not hard to go back and figure out old or external source code.
    • Essentially one data structure, the hash table. This is tremendous for neatly modifying old code to do new tricks (if my_new_tag exists then do my_new_thing else work_like_before)
    • Faster than perl in my tests
    • Extensible in C

    sorry... back to 3d printing now 🙂 . Had not even heard of any Lua - G code link ups.



  • @niocio01 You can find information about the Lua interpreter at the official web page: https://www.lua.org/download.html

    Lua RTOS for ESP8266: adds a LUA interpreter on top or Free RTOS.

    Probably the cleaner interface would be to use the serial communication port, and use LUA to put content (G coded) and read content, that could be parsed and scripted.

    I do not know how (un)easily this would integrate with the current code, since some M code should probably be created to call the lua interpreter that would then execute the scripts.



  • Conditional g code can be useful especially for config.g. For example you can configure it depends on 3d printer, laser or cnc mode.


Log in to reply