Make use of variables



  • It would be very handy to define variables in e.g. config.g for bed center, default speed, acc, jerk, pin for probe etc. Right now, changing bed center has to be done in many different places which makes things error-prone. I can think of multiple use cases.

    Suggestion:

    • set value of a simple variable like that: Mxx0 Kkeyname Vvalue.
      To retrieve the value simply prepend an @: G1 X@keyname


  • You can call macros from config.g https://duet3d.dozuki.com/Wiki/GCode#Section_M98_Call_Macro_Subprogram
    In addition, it is possible to write commands to a .g file: https://duet3d.dozuki.com/Wiki/GCode#Section_M28_Begin_write_to_SD_card

    I think you can achieve something you want with these commands.



  • @OBELIKS Thanks,I know I can use macros, but my suggestion is a different animal. Consider this case:
    I setup my BLTouch on fan2, which is pin 22. If I now want to switch to another fan, I have to edit deploy probe.g, retractprobe.g and some macros for testing / resetting the probe. With global variables (or constants) that could be achieved in one place.

    Another case is the bed center coordinates: There are numerous references to them, so I have to keep track of them in every single macro and config. Way easier to keep things clean with variables. There is a ton of other cases I can think of.
    Gcode is several decades old and extending it is a small step towards a modern(?) programming language. Basic arithmetic calculations would be another step...



  • Well, you can have a macro with one line only (oneline.g). And then you change "deploy probe.g, retractprobe.g and some macros" to run that macro. With change in the oneline.g you will change value for all of them.
    This works the same as using a variable.



  • I'm afraid that is not possible. Or how would you suggest to just change the pin parameter in
    M280 P22 I1 S10 ;deployprobe.g
    without affecting the others? This does not just set the pin, but makes the servo move.



  • Hm, now I am confused.
    With M28 the commands are only written in to the specified file, you close the command with M29. There is no code execution. So this is the same as setting a variable. If you want to change it to default, you need to write the file again. There were some bugs with M28/M29, but they should be solved.
    Or if you want to change it only in deployprobe.g change it there. Why do you then want to have it in one place?
    Both use cases that you provided before are solvable like I suggested.



  • @obeliks please stop replying. No offense, but it cannot be done the way you think. M28 / M29 are NOT helping either.

    Would please someone else try to make a better point than I did. I know for a fact that it can't be achieved with "static" code. Unless you want to maintain a ton of macros - which is exactly what I want to avoid.



  • Good reading here:

    https://forum.duet3d.com/topic/5039/macro-variables/4

    tl;dr - g-code variables are not a simple matter to do, and unlikely to be implemented.

    What I'd recommend in your case is commenting everywhere there's a variable to change with some searchable word, then just doing a find operation and replacing all the relevant values.



  • Thanks for the link. While loops and arithmetics are probably difficult to implement, I doubt that variables are hard to process. Why not 'just' do a search / replace if the first letter after the parameter is an @ or #? Is the storage of key / value pairs the tricky part? Or limited CPU resources or performance issues?

    I'm not familiar with RepRap dev (yet), but if someone @dc42 could point me to a good entry point, I would be willing to look into it.


  • administrators

    @googliola said in Make use of variables:

    It would be very handy to define variables in e.g. config.g for bed center, default speed, acc, jerk, pin for probe etc. Right now, changing bed center has to be done in many different places which makes things error-prone. I can think of multiple use cases.

    Suggestion:

    • set value of a simple variable like that: Mxx0 Kkeyname Vvalue.
      To retrieve the value simply prepend an @: G1 X@keyname

    A facility to make this possible is already planned.

    Why do you need to change bed centre after initial configuration?



  • @dc42 I'm not sure if the goal is to change bed center after it's configured, or to use the location of bed center as a variable that could be referred to in other macros.

    Honestly this is readily serviced by macros as it currently stands. The difference is between managing a collection of individual macros that serve a single function, either movement, or configuration, and managing a collection of variables in a single file like how Marlin does it. Yes variables gain some flexibility, and augment macros, but the current macro system largely satisfies what you're looking for.

    There are some examples of config file sets that put a lot of work into parameterizing the whole thing, making system state switches by calling macros. This can be tedious to troubleshoot as you have to chase down the issues between macros to see how the system state is being altered through each step of the way. But it does allow for keeping sets of configuration variables in one place to edit, and then apply everywhere the macro is called.

    Having $variables defined in one place would let macros become a lot more readable and you could refer back to the single variable definition file to see what is what. Currently the state of a gcode is always explicitly defined when it's called.

    I'd be curious to see if people would find that more difficult or less difficult than it is now. And beyond the initial setup of the machine, you're not really editing those variables very often.

    An example of a highly parameterized config set:
    https://github.com/mzbotreprap/VORON/tree/master/Firmware/Duet/V2/daveidmx



  • This does seem like something that is more useful for a machine that is in development, where you might change some parameter several times before you get the one that is the way that you want it.

    I'm unlikely to be changing major things, and in some of these cases (Bed centre for one) it not only has to be changed for the printer, it also needs to be changed in the slicer, and there may be more than one slicer involved, so cases like this would require the slicer also be able to deal with the change.

    Marlin configuration uses a .h file wiht a lot of #define statements. This then forces direct substitutions in the source code. (Forget about #ifdef/#ifndef statements, which get terrible for something like this.) A mechanism where you could have a .h file with:

    #define BED_X_CENTRE 110.0
    #define BED_Y_CENTRE 137.5
    #define BL_TOUCH_PIN 22
    Then, as a part of your gcode

    G1 BED_X_CENTRE BED_Y_CENTRE 5.0
    M280 PBL_TOUCH_PIN I1 S10

    The pre-processor would translate this to

    G1 110.0 137.5 5.0
    M280 P22 I1 S10

    Of course your slicer would have to be able to put out what is basically a non-numeric and pretend that it is a numeric. Might be easier with macros, or the .g files that the Duet keeps on-board, but honestly, I think that it's probably more trouble than it's worth, but then I suppose it depends on how many macros you make/use regularly, and how many count on things that you might change.

    I have a few macros right now that change the Z probe definition, since mine isn't working correctly, so I have a few macros that set the probe to a type "0" (No probe installed) and then set it back to a type "8" (digital, unfiltered) when they've done the job that I ask for. I suppose that having some global variables (Or #define substitutions) to re-set the probe might be handy for this, but I'm definitely not planning on needing these macros for long-term, as I intend to just fix the actual problem so that the probe will just function as defined in the config.g file, which I feel would be a better solution.

    It is, however true that the defined variables might be easier to read in the .g files, later when you can't remember what does P8 mean on the M558 command, as opposed to P0. Having:
    #define Z_PROBE_NONE 0
    #define Z_PROBE_BLTOUCH 1
    #define Z_PROBE_DIGITAL 8
    #define Z_PROBE_SPEED 600
    #define Z_PROBE_DELAY 0.8
    then in config.g

    M558 PZ_PROBE_DIGITAL H8 FZ_PROBE_SPEED I1 T9000 RZ_PROBE_DELAY Z1

    Easier to read, probably doesn't even need a comment anymore... but probably easier to comment. I mean the point of config.g is to set up something that sets the configuration all in one place, abstracting that to another place where the configuration is... Who watches the watchers?


  • administrators

    IMO it's better to define the bed centre as X=0 Y=0 no matter what type of printer you have. That's what I do on my Cartesian, Delta and SCARA printers. It is one of the things that allows me to print the same GCode files on all of them.



  • @phaedrux said in Make use of variables:

    There are some examples of config file sets that put a lot of work into parameterizing the whole thing, making system state switches by calling macros. This can be tedious to troubleshoot as you have to chase down the issues between macros to see how the system state is being altered through each step of the way. But it does allow for keeping sets of configuration variables in one place to edit, and then apply everywhere the macro is called.
    Having $variables defined in one place would let macros become a lot more readable and you could refer back to the single variable definition file to see what is what. Currently the state of a gcode is always explicitly defined when it's called.

    That's one reason to have variables. Less effort, more control over system state (as it is defined in one place). and better readability.

    I'd be curious to see if people would find that more difficult or less difficult than it is now. And beyond the initial setup of the machine, you're not really editing those variables very often.

    Well, having many parametrised macros is certainly quite messy the bigger the number. And if someone does not like to use variables then they could still use the existing approach.

    A facility to make this possible is already planned!

    @dc42 can't wait to use it. Bedcenter is just an example, where it variables would be helpful. Running a z-probe repeatabilty test would be much easier to implement too. Let's assume I want to repeat the test in other places than bedcenter (to spot a mechanical issue). Again, I would need to create yet another macro to set the location of the probing to take place. With vars, one could

    ; StartProbeLocationMacro.g:
    Set probing location coords $X $Y at bedcenter
    Run repeatProbe.g and record results in log file
    Set probing location coords $X $Y at front left
    Run repeatProbe.g
    Set probing location coords $X $Y at front right 
    ......
    Reset probing location to default state $defaultBedCenterX $defaultBedCenterY
    
    ; StartProbeSpeedMacro.g:
    Set probing feedrate, speed, height
    Run repeatProbe.g
    Change probing parameter
    Run repeatProbe.g
    ....
    Reset probing params to default state
    

    This does seem like something that is more useful for a machine that is in development

    @SupraGuy Absolutely. Either to setup and fine tune the configuration during development, but also to perform quality control upon setup of a new machine or to run maintenance tests.

    As for the changes in the slicer as you pointed out:

    G1 BED_X_CENTRE BED_Y_CENTRE 5.0
    M280 PBL_TOUCH_PIN I1 S10

    Hmmm, true. I don't see a use case for that - yet. Both lines are likely to bo into the start or end code scripts. Plus, that could just as well be done with macros containing the variables. But then how can you instruct your slicer to run a macro (apart from onstart, onlayerchange etc events)

    As for the #define statements, I think the "gcode everywhere" approach is a MUCH better fit, due to adherence to coding standards and coherence.

    I think it is important to point out that the value of the variables can be altered by
    Mxx0 Kkeyname VNewValue

    Thanks everyone for your input!



  • @Googliola Check this out. I think it will deliver very close to what you're after.

    https://github.com/token47/duetcfgen created by @token47

    To quote:

    duetcfgen
    Duet Config Generator

    What this is NOT:

    • attempt to negate the RRF "All Gcode" filosophy
    • an attempt to create a Configuration.h (a la Marlin) for RRF

    What this IS:

    • a way to quickly edit all the files on the same place
    • a way to use variables for repetitive information
    • a way to use "vim" on my linux instead of the web editor

    It will:

    • compile a template, generating all the individual files
    • do variable replacement while compiling
    • upload all of them to the duet using ftp
    • optionally will reset the duet after uploading
    • can potentially download a backup of all files before uploading (currently broken)


  • @Phaedrux Thanks for the link. Indeed a simple and nice workaround. Unfortunately, I'm a Windows (8.1) and not a Linux person. But I starred the project and will check back.



  • There is no "right or wrong" answer to having (or not) variables in G-Code.

    At the same time, the NIST standards writers for G-Code chose "not". There is a fairly deep philosophy behind this: G-Code is/was intended to be a machine control language. It is philosophically not intended to be a programming language.

    Anyone who's coded, even the simplest languages, knows that programming languages that have conditional execution and variables can have many emergent behaviours. This is highly undesirable when controlling machines that can break themselves, break parts, and injure operators.

    G-Code is specifically limited to "deterministic" behaviors. Even allowing math on a single line is a bit debateable in this philosophy; it did make it into the final standard. Nonetheless, people tend to avoid it in truly deterministic G-Code.

    The assumption is that all conditional execution, repetition, etc. will be accomplished in the generator of the G-Code, not the G-Code itself.

    Again, no right/wrong here, but knowing the background philosophy may help explain why things that seem desirable in G-Code may have been intentionally excluded.


  • administrators

    This week I have been designing a mechanism for conditional and looping GCode with variables. I will publish the proposal soon.



  • @dc42 said in Make use of variables:

    This week I have been designing a mechanism for conditional and looping GCode with variables. I will publish the proposal soon.

    Sweet! I've been waiting for this feature.



  • @Googliola : For what it's worth, this seems to be a collection of shell scripts and python code. There is a Windows port of bash to execute the shell scripts, and python can be run on just about anything. You MIGHT need to do some translation of some of the different locations and so forth, but I see little reason why this can't work in a Windows environment, if you're willing to put up with some teething pains and see what needs to be changed.

    @Danal : Certainly fair point, until you get things like configuration files and macros in a "gcode everywhere" the idea that the generator should be doing all of the work is a good one. In this case though since the configuration files, and presumably most of the macros are going to be (for the largest part) hand-coded, with the exception of things like the RepRap Configuration Generator, it gets to be a bit of a pain.

    I was thinking about the things that I was talking about above, and I realized that gcc includes a facility for running things through its preprocessor without trying to run them through the actual compile process. It throws certain errors, as it expects C/C++ style comments, and not ones with a semicolon prefix, but that isn't too hard to handle.

    So for example, I have a file test.h which contains the following:

    #define DEFAULTPROBE P8 H8 F780 I1 T9000 R0.8 Z1
    #define MESHGRID X30:190 Y35:235 S20
    #define PROBEOFFSETS X0.0 Y0.0 Z-0.1

    Then there's a file called manualprobemacro.c (The .c is just more convenient for the preprocessor)

    ; manualprobe.g manually define a mesh grid without needing to probe manually 90 times

    #include "test.h"

    M558 P0
    G31 X0 Y0 Z0
    M557 X35:185 Y25:250 S75
    G29
    M558 DEFAULTPROBE
    G31 PROBEOFFSETS
    M557 MESHGRID

    Then I run gcc -E manualprobe.c

    I get some returns from gcc on stderror, since this really isn't C code, but on stdout I get

    4 "manualprobe.c" 2

    M558 P0
    G31 X0 Y0 Z0
    M557 X35:185 Y25:250 S75
    G29
    M558 P8 H8 F780 I1 T9000 R0.8 Z1
    G31 X0.0 Y0.0 Z-0.1
    M557 X30:190 Y35:235 S20

    Now I can probably do something with awk to fix or remove things like the # comment lines. I can probably have it redirect the .c files to .g versions which can then be uploaded en masse to the macro directory. I can probably set up a makefile so that I can just do something like make macros.

    Maybe if I ever think that I'll have more than a dozen such macros, I might go through the trouble of setting it up.


 

Looks like your connection to Duet3D was lost, please wait while we try to reconnect.