Macro Creation Tutorial

Part 4: Default Parameters

One drawback to supplying a parameter arbitrarily in a macro is that it can
be referenced without a value. This could result in undesirable behavior or
and error at runtime if it is not handled. One way to check to see if a
parameter exits is simply to evaluate it.

	{% if PA %}
	{% if params.PA %}

Gcode parameters are accessed in macros through either directly referencing
the parameter name or by using the params collection. There are some
nuances to using the params collection in that, it will only have values that
are passed in through the gcode command. Whereas directly referencing
the parameter name can allow us to get a default value for ‘default_parameter_PA’

Jinja affords us the use of certain filters for things like rounding and typing (more
on that later). There is also a default() filter which can be used instead of
declaring a default_parameter_PA.

	{ params.PA|default(.06) }

So if we pass the a parameter through for PA and we want to set pressure
advance in our start print macro, we can check for the parameter and then
call the SET_PRESSURE_ADVANCE command.

	SET_PRESSURE_ADVANCE ADVANCE={ params.PA|default(.06) }

Alternatively you can give it a default value using the default_parameter
config option for the gcode macro.

	[gcode_macro start_print]
	default_parameter_PA: 0.06
	gcode:

For this example, we are adding a Pressure Advance setting to our
start print macro so that the PA can be set based on a particular filament

	[gcode_macro start_print]
	default_parameter_PA: 0.06
	gcode:
		M109 S{params.TOOL_TEMP}		# Heat the tool to temperature and wait
		
		{% if printer.homed_axes != 'XYZ' %}
			G28							#Home All Axes
		{% endif %}
	
		SET_PRESSURE_ADVANCE ADVANCE={PA}
	
		G92 E0 							# Reset Extruder
		G1 Z2.0 F3000 					# Move Z Axis to travel height
		G1 X0.1 Y20 Z0.2 F5000.0 		# Move to start position
		G1 X0.1 Y200.0 Z0.2 F1500.0 E15 # Draw the first line
		G1 X0.4 Y200.0 Z0.2 F5000.0 	# Move to side a little
		G1 X0.4 Y20 Z0.3 F1500.0 E30 	# Draw the second line
		G92 E0 							# Reset Extruder
		G1 Z2.0 F3000 					# Move Z Axis up to travel height
5 Likes