Declaring variables in config.g
-
@nightowl999 said in Declaring variables in config.g:
You declare global variables (and set the initial value) like this:
global savedSpindleSpeed = 0
You assign a new value to an existing variables like this:
set global.savedSpindleSpeed = 1 ; set a new value
You access global variables like this:
if {global.savedSpindleSpeed} = 0 echo "global.savedSpindleSpeed is zero" else echo "global.savedSpindleSpeed is not zero"
I declare all my global variables in config.g - for example:
; **************************************************************************************************** ; control variables ; **************************************************************************************************** global g_z_home = 10 ; can be used to always move Z to known height global g_z_datum_set = false ; can be used to prevent unneeded setting of Z=0 datum global g_probe_trigger_height = 613 ; set as appropriate (100 to 999) (divided by 100) global g_probe_dive_height = 4 ; set as appropirate global g_probe_count = 2 ; determines how z probing is done (1 to 10) global g_probe_tolerance = 30 ; determines how z probing is done (10 to 100) (divided by 1000) global g_probe_average = false ; determines how z probing is done global g_probe_mounted = false ; track state of euclid probe (mounted/unmounted) global g_probe_clearance = 15 ; use to insure bed is clear of dock global g_level_mode = "3 point" ; determines if 3 or 4 points are used for leveling global g_mesh_taper = 0 ; global g_mesh_scale_x = 100 ; in percent (20 to 100) global g_mesh_scale_y = 100 ; in percent (20 to 100) global g_mesh_points_x = 21 ; in points ( 2 to 21) global g_mesh_points_y = 21 ; in points ( 2 to 21) global g_print_beg_prime = 30 ; default - set desired in filament config.g global g_print_beg_retract = 0 ; default - set desired in filament config.g global g_print_beg_level_bed = false ; controls leveling of bed in print_begin.g global g_print_beg_load_mesh = true ; controls loading of heightmap in print_begin.g global g_print_pause_retract = 0 ; default - set desired in filament config.g global g_print_end_retract = 0 ; default - set desired in filament config.g global g_print_end_delay_enable = false ; heater turn off delay enable/disable global g_print_end_delay_time = 6 ; heater turn off delay time (increments of 10 seconds) global g_print_end_delay_counter = 0 ; heater turn off delay counter global g_pa_setting = 0 ; used for pressure advance testing global g_pa_increment = 0 ; used for pressure advance testing global g_pa_layer_count = 0 ; used for pressure advance testing global g_pa_layer_counter = 0 ; used for pressure advance testing
When you start to use variables in expressions sometimes it can be confusing as to what goes where.
Frederick
-
@fcwilt That's really helpful, than you Frederick.
This is how I've declared the variable in config.g:
... global savedSpindleSpeed = 0 ...
and this is how (it's been suggested) I should access it...
... if state.currentTool >= 0 & tools[state.currentTool].spindle >= 0 set global.savedSpindleSpeed = tools[state.currentTool].spindleRpm ...
I'm assuming "state.currentTool" is built-in, but there are no squiggly brackets anywhere!
Is that what I'm missing, do you think, as this is on a CNC?
EDIT: I've just noticed "[state.currentTool].spindle >=0," yet the second part shows "tools[state.currentTool].spindleRpm". Should they both be ".spindleRpm"?
-
@nightowl999
Your expressions in your condition should be enclosed in brackets.
As you are using CNC mode, you would need to use curly brackets.if {state.currentTool >= 0} & {tools[state.currentTool].spindleRpm >= 0}
Also you are correct, it should be spindleRpm in both instances.
You can check the correct syntax and work out which object model values to use by making use of the object model browser plugin in DWC.This will let you see the object model and drill down to get the info you need.
If you select a value, the correct path can be copied to the clipboard using the copy button at top right. -
@owend So the original line should be changed to this:
if {state.currentTool >= 0} & {tools[state.currentTool].spindleRpm >= 0} set {global.savedSpindleSpeed} = {tools[state.currentTool].SpindleRpm} ...
... or should the lower line be enclosed within a single pair of curly parentheses?
As an aside, is "SpindleRPM" the same as "spindlerpm" and SPINDLERPM?
Thanks
-
@nightowl999
No, you only need the curly brackets when doing a comparison using OR or AND
This groups and separates the parts of the equations
You don't need them when setting the value of the variable.if {global.A = global.B} & {global.B<global.C} set global.D = global.B
if {global.A = global.B} || {global.B<global.C} set global.D = global.B
You also need them when you must calculate something as part of a standard command such as using a variable in a speed setting
G1 X100 Y100 F{global.D}
-
@nightowl999 said in Declaring variables in config.g:
As an aside, is "SpindleRPM" the same as "spindlerpm" and SPINDLERPM?
No they are not the same.
The object model values and variable names are case sensitive. -
@owend Well, I'm definitely missing something, because the resume.g file doesn't restart the spindle, when I expect it should.
This is the declared variable in config.g:
global savedSpindleSpeed = 0
This is my pause.g file:
G1 Z{max(move.axes[2].userPosition+5,move.axes[2].max-5)} ; move the Z axis to a safe height if {state.currentTool >= 0} & {tools[state.currentTool].spindleRpm >= 0} set global.savedSpindleSpeed = tools[state.currentTool].spindleRpm ; save current spindle speed G1 X273.5 Y560 F2400 ; move XY to a safe place M5 ; turn the spindle off echo "Spindle speed saved at", global.savedSpindleSpeed, "RPM" ; spindle speed saved
...and this my resume.g:
if {state.currentTool >= 0} & {tools[state.currentTool].spindleRpm >= 0} M3 S{global.savedSpindleSpeed} ; resume saved spindle speed G4 S1 ; wait 1 second to allow the spindle to spin up echo "Spindle speed resumed at ", global.savedSpindleSpeed, "RPM" G1 R1 X0 Y0 F2400 ; go above the paused XY position
This should be easy with all the help I've had, but what have I missed..?
-
@nightowl999 said in Declaring variables in config.g:
Can you try this to ensure that the condition is being met.
if {state.currentTool >= 0} & {tools[state.currentTool].spindleRpm >= 0} M3 S{global.savedSpindleSpeed} ; resume saved spindle speed G4 S1 ; wait 1 second to allow the spindle to spin up echo "Spindle speed resumed at ", global.savedSpindleSpeed, "RPM" G1 R1 X0 Y0 F2400 else echo "condition not met. Spindle speed remains at " ^ tools[state.currentTool].spindleRpm ^ "RPM"
-
I can, @owend, but running pause.g indicates the spindle speed of 0RPM is saved, not the 18,000RPM it's running at, so it seems to be an issue with pause.g?
-
@OwenD here we are, the text from the Console:
27/05/2022, 19:43:24 M0 Printing paused at X128.8 Y168.9 Z64.0 Cancelled printing file 0:/gcodes/Calibration Toolpath.gcode, print time was 0h 0m 27/05/2022, 19:43:12 Spindle speed saved at 0 RPM 27/05/2022, 19:43:10 M25 Resume state saved 27/05/2022, 19:43:08 Printing resumed 27/05/2022, 19:42:56 M24 Error: in file macro line 14 column 63: meta command: array index out of bounds 27/05/2022, 19:42:52 Printing paused at X125.6 Y125.8 Z65.0 27/05/2022, 19:42:40 Spindle speed saved at 0 RPM 27/05/2022, 19:42:37 M25 Resume state saved 27/05/2022, 19:42:29 M32 "0:/gcodes/Calibration Toolpath.gcode" File 0:/gcodes/Calibration Toolpath.gcode selected for printing 27/05/2022, 19:42:16 File 0:/gcodes/Calibration Toolpath.gcode will print in 0h 3m plus heating time 27/05/2022, 19:42:15 M37 P"0:/gcodes/Calibration Toolpath.gcode" Simulating print of file 0:/gcodes/Calibration Toolpath.gcode 27/05/2022, 19:42:04 G10 L20 P1 Origin of workplace 1: X97.00 Y94.00 Z67.00 27/05/2022, 19:41:37 g1 x147 27/05/2022, 19:41:20 g1 y84 27/05/2022, 19:41:06 g1 y660 27/05/2022, 19:40:34 Connected to weeble
-
@nightowl999 said in Declaring variables in config.g:
Error: in file macro line 14 column 63: meta command: array index out of bounds
This will be the problem.
See the other thread. -
This is Line 14 from resume.g...
echo "condition not met. Spindle speed remains at " ^ tools[state.currentTool].spindleRpm ^ "RPM"
with the same error when I changed it to:
echo "condition not met. Spindle speed remains at ", tools[state.currentTool].spindleRpm, "RPM"
-
@nightowl999
See the other thread.
I suspect you have no tool defined when the resume starts.