grblHAL / core

grblHAL core code and master Wiki
Other
334 stars 88 forks source link

Enable parameter substitution in MSG comments #587

Closed skasti closed 1 month ago

skasti commented 1 month ago

In many cases it is necessary to communicate something to an operator which contains a dynamic value, and being able to use the MSG comments for this seems natural.

The reason for not using DEBUG comments is that those are used for debugging, and create a lot of noise for the operator. So the operator should be able to disable DEBUG output and still get the messages they need

Example output with debug output enabled:

G65P200T2 
[MSG:Find pickup]
[MSG:Slot 0: 2 T:2]
[MSG:Returning 0]
[MSG:Tool T2 already stored in slot 1 from the back]

Example output with debug output disabled:

G65P200T2
[MSG:Put tool T2 in slot 1 from the back]

P200 is a macro that finds an available slot in my tool rack, tells the operator to put it there, and assigns the tool to the slot so that it knows where to find it.

terjeio commented 1 month ago

please move substitute_parameters() to the same #if #endif block where read_parameter () is located and mark it as static

skasti commented 1 month ago

please move substitute_parameters() to the same #if #endif block where read_parameter () is located and mark it as static

Hm, read_parameter seems to be in the #if #endif block for NGC_EXPRESSIONS_ENABLE. Does this mean I should put this feature behind that flag, and not NGC_PARAMETERS_ENABLE ?

I would have thought that NGC_PARAMETERS_ENABLE would be more appropriate for both of these functions, since they revolve around parameters and not expressions? Though I do see that a sub-section of read_parameters does call ngc_eval_expression πŸ€”

Will put my stuff in the NGC_EXPRESSIONS_ENABLE-block for now, since that is at least consistent

terjeio commented 1 month ago

FYI NGC_PARAMETERS_ENABLE was added to have basic support for G65 (with no parameter passing) for the 128K STM32F1xx MCUs, this since adding expressions would otherwise overflow flash. Perhaps a bad choice of name for the symbol... Anyway if NGC_EXPRESSIONS_ENABLE is true NGC_PARAMETERS_ENABLE will always be as well.

skasti commented 1 month ago

FYI NGC_PARAMETERS_ENABLE was added to have basic support for G65 (with no parameter passing) for the 128K STM32F1xx MCUs, this since adding expressions would otherwise overflow flash. Perhaps a bad choice of name for the symbol... Anyway if NGC_EXPRESSIONS_ENABLE is true NGC_PARAMETERS_ENABLE will always be as well.

Aaah, that makes senseπŸ˜… I thought maybe parameters might have been a thing outside of using expressions/G65, so that you could have used them in "normal" g-code somehow.

terjeio commented 1 month ago

FYI you introduced a memory leak in MSG substitution... I will fix this in the next commit and add also support for PRINT messages and formatting of values.

skasti commented 1 month ago

Oh no, I am so sorry! 😞 Please point out how, if possible, so I can hopefully learn πŸ˜…

terjeio commented 1 month ago

if(message && *message == NULL && !strncmp(comment, "(MSG,", 5) && (*message = malloc(len))) {

This line allocates memory for a copy of the MSG message, and your code in substitute_parameters() overwrites the *message pointer without freeing the already allocated memory. I changed the code to not allocate memory initally when expressions is enabled.

skasti commented 1 month ago

ah, I did not notice that this line did any memory allocation. I thought it only did the same thing as the debug-one πŸ˜… I guess then the #else-block needs to include that malloc in order to work properly?