definin a macro with variables

hello everyone,
I am working on Marlin program I made some changes for xyz prop and I do not make a definition as #define PRB_GCODE_END "G92 X-" XOFF "Y" XOFF "Z" ZOFF "\ n" to send the final xyz coordinates as gcode at the end of the promulgation procedure. is required. If anyone has any idea how to use float type variables during macro definition please help.

void _user_gcode_exe(const char * const cmd) {
lcd_return_to_status();
enqueue_and_echo_commands_P(cmd);
}

void user_gcode_exe_1() {
float XOFF = (25.00 + PRB_XOFFSET + (TOOL_DIA / 2.00));
float YOFF = (25.00 - (PRB_YOFFSET + (TOOL_DIA / 2.00)));
float ZOFF = (10.00 + PRB_ZOFFSET);

#define _PRB_GCODE_END "G92 X-" XOFF " Y" XOFF " Z" ZOFF "\ n"

#define PRB_GCODE "M117 Z-XY Probing now..\nG38.2 F120 Z-20.00\nG92 X0.00 Y.00 Z0.00\nG0 Z5.00 F1200\nG0 X-20.00 F1200\nG0 Z-5.00 F1200\nG38.2 F120 X25.00\nG92 X0.00\nG0 X-5.00 Y-25.00 F1200\nG0 X15.00 F1200\nG38.2 F120 Y25.00\nG92 Y0.00\nG0 Y-5.00 F1200\nG0 Z10.00 F1200\nG0 X-25.00 Y25.00 F1200\n"

#define MSG__OK "M117 Z-XY Probing done..\n"
_user_gcode_exe(PSTR(PRB_GCODE PRB_GCODE_END MSG__OK));

}

In the 'C' and 'C++' language you have to tell the compiler what it should do.
You have to convert the float into text and then combine them.

  1. The Arduino String object can be used: String() - Arduino Reference.
  2. An other way is to bypass the String() object and use a number Serial.print() calls.
  3. Or you can use a seperate buffer with dtostrf() and then combine them with strcat().

By the way, I'm not sure if I like those many #define. When using a Arduino Mega board, then it is better to make 'const' 'PROGMEM' variables.

Hello there,
Thank you for your interest.
All operations related to String () are not allowed in Marlin software, especially when sending gcode command. However, it is only allowed to send gcode commands with macros defined by #define. In other words, it is not possible for gcode commands to be created as string () first and then sent by #define. When defining the macro, variable values should be put into this definition.

fatihcem:
it is only allowed to send gcode commands with macros defined by #define.

not clear what you mean by "#define"

#define is a C pre-processor operation that defines a symbol (e.g. MyDef(x)) with the text that #define associates it with. both the c-preprocessor directives (e.g. #define) and the symbol do not appear in the the code processed by the compiler.

#define PRB_GCODE_END "G92 X-" XOFF " Y" YOFF " Z" ZOFF "\ n"
the problem is here.
XOFF , YOFF, ZOFF is float and calculated before PRB_GCODE_END defination
and should be put as a float value into PRB_GCODE_END definition.

fatihcem:
All operations related to String () are not allowed in Marlin software, especially when sending gcode command. However, it is only allowed to send gcode commands with macros defined by #define. In other words, it is not possible for gcode commands to be created as string () first and then sent by #define. When defining the macro, variable values should be put into this definition.

I'm pretty sure this is nonsense. If it can be sent from a #defined macro, then it can more than likely be sent from a variable. Show us the prototype for the function that does the sending.

fatihcem:
_user_gcode_exe(PSTR(PRB_GCODE PRB_GCODE_END MSG__OK));

The PSTR() in this line is storing a text literal in program (flash) memory. This means that the arguments must be quoted text litetals, so you cannot use variables, and the text must be known at compile time.

fatihcem:
#define PRB_GCODE_END "G92 X-" XOFF " Y" YOFF " Z" ZOFF "\ n"

all the pre-processor does is replace any appearance of PRB_GCODE_END in the code with "G92 X-" XOFF " Y" YOFF " Z" ZOFF "\ n"

this occurs at compile, not run time

fatihcem, do you have experience in a scripting language ?
The original 'C' language was written for the possibilities of the processors at that time. The assembly code for the processor can almost be seen right through the statements. The 'C++' language has very nice additions, but the compiler does not do something automatically for you. You have to tell the compiler what it should do.

gcjr:
all the pre-processor does is replace any appearance of

PRB_GCODE_END

in the code with

"G92 X-" XOFF " Y" YOFF " Z" ZOFF "\ n"

this occurs at compile, not run time

The variable values must be in quotes. The command I noted above depends on the compiler concatenating all quoted text into a single text literal.

output from gcc -c -E of the code in the original post

# 1 "fat.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "fat.cpp"

void _user_gcode_exe(const char * const cmd) {
      lcd_return_to_status();
      enqueue_and_echo_commands_P(cmd);
    }

  void user_gcode_exe_1() {
    float XOFF = (25.00 + PRB_XOFFSET + (TOOL_DIA / 2.00));
    float YOFF = (25.00 - (PRB_YOFFSET + (TOOL_DIA / 2.00)));
    float ZOFF = (10.00 + PRB_ZOFFSET);






    _user_gcode_exe(PSTR("M117 Z-XY Probing now..\nG38.2 F120 Z-20.00\nG92 X0.00 Y.00 Z0.00\nG0 Z5.00 F1200\nG0 X-20.00 F1200\nG0 Z-5.00 F1200\nG38.2 F120 X25.00\nG92 X0.00\nG0 X-5.00 Y-25.00 F1200\nG0 X15.00 F1200\nG38.2 F120 Y25.00\nG92 Y0.00\nG0 Y-5.00 F1200\nG0 Z10.00 F1200\nG0 X-25.00 Y25.00 F1200\n" PRB_GCODE_END "M117 Z-XY Probing done..\n"));

}

gcjr:
output from gcc -c -E of the code in the original post

# 1 "fat.cpp"

1 ""

1 ""

1 "fat.cpp"

void _user_gcode_exe(const char * const cmd) {
      lcd_return_to_status();
      enqueue_and_echo_commands_P(cmd);
    }

void user_gcode_exe_1() {
    float XOFF = (25.00 + PRB_XOFFSET + (TOOL_DIA / 2.00));
    float YOFF = (25.00 - (PRB_YOFFSET + (TOOL_DIA / 2.00)));
    float ZOFF = (10.00 + PRB_ZOFFSET);

_user_gcode_exe(PSTR("M117 Z-XY Probing now..\nG38.2 F120 Z-20.00\nG92 X0.00 Y.00 Z0.00\nG0 Z5.00 F1200\nG0 X-20.00 F1200\nG0 Z-5.00 F1200\nG38.2 F120 X25.00\nG92 X0.00\nG0 X-5.00 Y-25.00 F1200\nG0 X15.00 F1200\nG38.2 F120 Y25.00\nG92 Y0.00\nG0 Y-5.00 F1200\nG0 Z10.00 F1200\nG0 X-25.00 Y25.00 F1200\n" PRB_GCODE_END "M117 Z-XY Probing done..\n"));

}

Note the define starts with an underscore.

david_2018:
Note the define starts with an underscore.

?

our practice at work was to prefix all static variables and funcitons with a "" and i've name null state machine functions "___()"

gcjr:
?

our practice at work was to prefix all static variables and funcitons with a "" and i've name null state machine functions "___()"

In this case, the substitution is never made because the define starts with an underscore, the statement where it is used lacks the underscore.

Koepel:
fatihcem, do you have experience in a scripting language ?
The original 'C' language was written for the possibilities of the processors at that time. The assembly code for the processor can almost be seen right through the statements. The 'C++' language has very nice additions, but the compiler does not do something automatically for you. You have to tell the compiler what it should do.

So, have you any idea for this . how can we make a macro defination (using with #define) with variables ??

While defining PRB_GCODE_END, ensuring that float type variables are included in this definition. For example: Considering that the variables XOFF, YOFF and ZOFF are calculated as 12.85, 11.25 and 10.50 respectively. PRB_GCODE_END should occur as follows.
G92 X-12.85 Y 11.25 Z10.50 \ n

david_2018:
Note the define starts with an underscore.

yes, writing this way is not a problem. Problem: While defining PRB_GCODE_END, ensuring that float type variables are included in this definition. For example: Considering that the variables XOFF, YOFF and ZOFF are calculated as 12.85, 11.25 and 10.50 respectively. PRB_GCODE_END should occur as follows. G92 X-12.85 Y11.25 Z10.50 \n

david_2018:
Note the define starts with an underscore.

yes, writing this way is not a problem. Problem: While defining PRB_GCODE_END, ensuring that float type variables are included in this definition. For example: Considering that the variables XOFF, YOFF and ZOFF are calculated as 12.85, 11.25 and 10.50 respectively. PRB_GCODE_END should occur as follows.

G92 X-12.85 Y 11.25 Z10.50 \ n

fatihcem:
So, have you any idea for this . how can we make a macro defination (using with #define) with variables ??

While defining PRB_GCODE_END, ensuring that float type variables are included in this definition. For example: Considering that the variables XOFF, YOFF and ZOFF are calculated as 12.85, 11.25 and 10.50 respectively. PRB_GCODE_END should occur as follows.
G92 X-12.85 Y 11.25 Z10.50 \ n

In this case, the value of the variables must be known at compile time. The _user_gcode_exe() function takes an argument that is a text string stored in program memory, which cannot be altered at run time. Also, the #define must be one or more quoted texts, so the float value must be surrounded by quotes.

I'm not where I can look at the Marlin source code go see if _user_gcode_exe has an overload for char* instead of const char*, which would let you use a char array in ram.

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