RepRap Firmware macros

From RepRap
Jump to: navigation, search

Principle of RepRap Firmware macros operation

Most RepRap Firmware board operations are handled by macros files stored on the SD card.

  • A macro is a text file containing a sequence of G-codes
  • Firmware use macros with predefined names located in /sys directory
  • Files located in /macros directory are presented to user in DWC panel, whatever their extension
  • User defined macros could be set in any directory, including /sys
  • A macro can call another macro located in any directory
  • A mistake in the syntax of a macro may hold the printer

The G-codes set of RepRap Firmware is evolving and the description below apply to the most recent firmwares, from DC42 or Chrishamm forks. Note that many listed command may not work with the firmware as delivered with your board (notably Replikeo boards) and you may update the firmware for proper operation.

The below macros are given as examples, but you shall have a look on specific SD images to establish your own macro set.

Some listed macros are single line and fairly basic, but creating these macros once prevent searching the documentation month later when you will have to redo part of the commissioning or calibration and forgot the commands. It is a sort of always available documentation. It is recommended to create you whole macros set before starting the commissioning.

System macros


This is called at board startup. The printer configuration is defined in the main macro config.g. Configuration and update of this file is detailed in other pages.

Homing: homeall.g and homedelta.g

This macro is called by G28 and home the printer. The homedelta.g macro is called if the printer configuration is defined as delta (M665 command). Example for a cartesian printer (here an Ormerod).

;macro /sys/homeall.g  homing for a cartesian 
G91 			; set movement to relative mode
G1 Z5 F200 		; move Z up 5mm
G90 			; set movement to absolute mode
M558 P1 		; set probe to unmodulated mode
G1 X-240 F2000 S1 	; home X
G92 X0 			; set position to X=0
G1 X3 F200 		; move axis away from X=0
G1 X-5 F200 S1 		; home again, slower
G92 X0 			; set position to X=0
G1 X15 F2000 		; ADJUST the X value to put the nozzle on the edge of the bed
G92 X0 			; set position to X=0
M558 P2			; set probe to modulated mode
G1 X45 F2000 		; move X, ADJUST the X value to get Z probe over target
G92 Y0 			; set position to Y=0
G1 Y-240 F2000 S1 	; home Y
G92 Y0 			; set position to Y=0
G1 Y3 F200 		; move Y axis out
G1 Y-5 F200 S1 		; home Y, slower
G92 Y0 			; set position to Y=0
G1 Y3 F2000 		; ADJUST the Y value to put the nozzle on the edge of the bed
G92 Y0 			; set position to Y=0
G1 Y0 F2000 		; move Y, ADJUST the Y value to get Z probe over target
G30 			; home Z, using values from G31 in config.g
G1 Z5 F200 		; Move Z up to Z=5
G1 X0 Y0 F2000 		; Move to X=0 Y=0 
G1 Z0 F200 		; Move to Z=0

Example for a delta

;macro /sys/homedelta.g  Homing file for RepRapFirmware on delta printer
G91				; use relative positioning
G1 S1 X520 Y520 Z520 F8000	; move all carriages up 520mm – S1 moves X,Y,Z carriages and detect stops; use F500 for commissioning (low speed)  - beware max carriage travel > max height
G1 S2 X-4 Y-4 Z-4 F1000		; move all tower carriage down 4mm, no stop detection
G1 S1 X8 Y8 Z8 F500		; move carriages up 8mm, stopping at the endstops
G1 Z-10	F2000			; down a few mm so that we can centre the head
G90				; back to absolute positioning
G1 X0 Y0 F2000			; centre the head
G1 Z20 F12000			; move head 20mm above plate

There are also standard system macros for independent axis: homex.g, homey.g and homez.g

Homing without end switches

With digital potentiometer allowing stepper current adjustment by software, it is possible to stall the motors on movement mechanical stops, so removing need for end stops switches and improving reliability. Experience show that a too low current drive to difficulties for the stepper to ‘re-hook’ to steps and large rebounds. Increasing current to a minimal value reduce the rebound. You shall experiment for own steppers, with minimum current values around 30% of nominal current. Also, the mechanical stop shall not be too springy e.g. felt strips may be better than rubber. The precision is not very good (0.2 to 0.5 mm) but that shall be corrected by the calibration run. For any reason, the S2 parameters don’t effectively prevent stop detection, so the below routine only works on machine NOT equipped with switches. The new G-code M913 defining current percentage is available from DC42 v1.14.

;macro /sys/homedelta.g  Homing file without stop switches (board with digipots)
    	    	    	; - WONT'T WORK if you do have switches
G91			; use relative positioning
M913 X50 Y50 Z50    	; Reduce current to a percentage on the current as defined by M906 (here 50%)
			;- resulting current shall be above 30% of stepper RATED current.
			; Too low current makes carriage jumping.
G1 S2 X520 Y520 Z520 F4000 ; move all carriages up 520mm S2 no stop detection 
    	    	    	;- beware max carriage travel > max height
G92 X0 Y0 Z380		; defines the position when homed  - required while there is no endstops 
    	    	    	;- beware asymmetry of your stops
G1 Z-10 F100		; down a few mm so that we can centre the head
G90			; back to absolute positioning
G1 X0 Y0 F2000		; centre the head and set a reasonable feed rate
M913 X100 Y100 Z100	; return to normal current
G1 Z20 F12000		; set head 20mm  above bed


This macro is called by code 'G32' and do automatic calibration. It also call homing macro (G28) if homing is not already done. Example of calibration for a delta printer with mechanical sensing (like the Fisher). The H parameter is the sensor offset for each point. It is here used instead of the general 'G31 Znn' parameter which is common to all probe points. A negative value correspond to a height over the bed. Example for a Cartesian (using M557 in config.g instead may be more simple than a bed.g macro)

; macro /sys/bed.g  Auto calibration routine for a Cartesian printer: 1 to 5 probe points
M561	; clear any bed transform, otherwise homing may be at the wrong height
G1 Z5 F350
G1 X45 Y0 F2000	; move head
G30 P0 Z-100000	; probe
G1 Z5 F350	; rise head  
G1 X45 Y180 F2000
G30 P1 Z-100000
G1 Z5 F350
G1 X210 Y180 F2000
G30 P2 Z-100000
G1 Z5 F350
G1 X210 Y0 F2000
G30 P3 S Z-100000 ; probe and calibrate with 4 parameters (as there is 4 points)
G4 P1500 ;Leave the probe there to give the web interface time to report the plane points
G1 Z10 F350

Example for a delta

; macro /sys/bed.g  Auto calibration routine for a delta printer with offset values (H param)
; different for each probe point (mechanical sensing). You can have as many probe points
; as you want (least square root average), use at least 3 points more than parameter number.
; This does a single iteration of auto calibration. Run this file multiple times until
; the values converge. Then transfer values from M666 and M665 to your config.g file.

M561			; clear any bed transform, otherwise homing may be at the wrong height
M201 X500 Y500 Z500	; reduce acceleration

; Probe the bed and do auto calibration 
G1 X-64.95 Y-37.5 Z3  F12000 	;go above first point 
G30 P0 X-69 Y-37.5  H-0.43 Z-99999	; X tower
G30 P1 X69 Y-37.5   H-0.45 Z-99999	; Y tower
G30 P2 X1 Y78 	    H-0.40 Z-99999	; Z tower 
G30 P3 X-34 Y-18.75 H-0.34 Z-99999	; half way to X tower
G30 P4 X34 Y-18.75  H-0.40 Z-99999	; half way to Y tower 
G30 P5 X4 Y41 	    H-0.30 Z-99999	; half way to Z tower
G30 P6 X2 Y2 	    H-0.37 Z-99999 S4 ; centre, and auto-calibrate for 4 parameters

M201 X6000 Y6000 Z6000 ; reset acceleration - Warning! supersede values set in config.g

G91; relative positioning
G1 S1 Z40 F15000	; go part way up 
G90; back to absolute positioning

Machines like Kossel with mechanically deployed Z-probe also calls deploying and retract macros, see [1]

Tool selection

For each tool, there is three standard macros while changing or selecting tool

example : "Tfree1.g". This could be used to wait for printing temperature, set idling temperature, move the head off the print, retract, un-retract, clean tool or any other action.

Macros are called on tool change or selection:

  • TpreN.g is run before tool N is set active
  • TpostN.g is run after tool N is set active
  • TfreeN.g is run before tool N is freed

Note than on the Chrishamm fork, if these macros are not present, there will be an error message. There is no error message on DC42 fork.

Pause and resume a print: pause.g and resume.g

  • pause.g is called before pausing a print, pause ordered by M25 (pause from control panel, not from G-code file)
  • resume.g is called before resuming a print, resume ordered by M24
  • cancel.g is called if the print is cancelled

Stop a print: stop.g and cancel.g

  • stop.g is called while M0 is called. Without this macro all heating loops are shutdown. If M0 is received while a print is paused, cancel.g is run instead of stop.g.

Set of macros for filament detection

Using M581/M582 (from DC42 V1.13), you can implement simple filament detection.

In addition, it is possible to add a push-button in parallel which will allow user to stop a print from the machine itself.

In the following example, the detection switch is normally closed, so circuit is closed while there is no filament. It is connected to the E1 end stop between signal and ground pins, so switch closing lower the signal. So the trigger is on falling edge.

;/sys/start.g  Executed at print start (add 'M98 P/sys/start.g' in your slicer start code)
M581 E1 S0 T2	; Trigger 2 rised if filament is missing. Switch on E1 connect
		; signal and ground pin when there is no filament, so trigger 
		; on falling edge.  Other extruder switch or user accessible
		; pushbutton (closed on action) can be paralleled 
M582 T2  	; Check if the Filament absence is detected NOW
; M581 E0 S0 T3	; If endstop 0 is available (no Z-probe), trigger resume print on E0 activation 

; /sys/cancel.g
M581 S-1 T2  ; neutralise trigger 2  while cancelling print
M581 S-1 T3  ; neutralise trigger 3  while cancelling print
;Next is non standard and unrelated to filament detection
M140 S55     ; Set the bed temperature and don't wait for it
M104 S155    ; set the current hotend temperature to 155 and don't wait for it

M581 S-1 T2  ; neutralise trigger 2  while stopping print
M581 S-1 T3  ; neutralise trigger 3  while stopping print

M300 S300 P500   ; Beep 
M300 S1000 P1000 ; Beep 
M117 No filament or User pause ; Message on DWC or Panel
;M84 E0:1  	 ; Turn extruders motors off - Don't work
M25		 ; Pause print (and run pause.g before)

;/sys/trigger3.g  Option for a 'resume' button
M24 		 ; Resume print (and run resume.g before)

M581 S-1 T2 ; neutralise trigger 2  while pausing print - to avoid multiple triggers
G1 E-4 F2500    ; retract 4mm
M84 E0:1        ; Turn extruders motors off
;Next is unrelated to filament detection
G91      	; relative moves
G1 Z50 F5000    ; raise nozzle 50mm
G90             ; absolute moves
G1 X0 Y85 F5000 ; move head out of the way of the print

; /sys/resume.g
M581 E1 S0 T2   ; Reactivate trigger 2 
M582 T2         ; Check again there is filament 
;Next is unrelated to filament detection
G1 R1 Z2 F5000  ; move to 2mm above resume point
G1 R1           ; lower nozzle to resume point
G1 E4 F2500     ; undo the retraction

You shall not use this code ‘as is’ but adapt it to your existing pause/resume/stop macros.

It works properly, with bips and message on DWC, but was not tested on PanelDue.

Using macros

Name and extension

Names are case sensitive. You can use any file extension (.g, .gcode, .txt) or none at all, but it is preferable to stuck to ‘.g’ with an editor doing syntax highlighting.

In a macro file, comments are preceded by “;” character. They could be after the command or alone in line.

Call another macro

While in a macro, you can call another macro. Macros are searched in /sys directory and it is recommended to always specify explicitly the path.

M98 P/macros/mymacro.txt ;P define the parameter and is not part of the name

Some parameters are restored while returning from a macro, however, these are not well documented. So, you shall take care to restore them manually (notably motor current, acceleration). However, this drive to the problem that the actual value is the one decided at the end of the macro and not the value defined in config.g, so you shall take care to track all restored values in your macros.


Calling an invalid G-code may sometimes hang the firmware and lock your system. If the error is in the start-up process, the only solution is to remove the SD card and edit files to correct the bug. An error can be in a macro automatically called by the system.

Care shall be taken to not create a recursive loop, e.g. a homing macro calling another home. This creates a "Stack overflow!" error.

Useful user macros

Load filament

example for a Bowden tube installation with 680mm total length. This for tool 0, you may create a macro for each extruder/tool.

;macro /macros/_Load_filament.g 
T0		; Select tool
M109 S200	; set the current hotend temperature to 200 and wait for it
G1 E640 F3600	; extrude 640mm at heigh speed 
G1 E40 F200	; extrude 40mm at low speed
M84 P3 E0	; turn extruder motor off so the user can feed by hand - change for another tool

Unload filament

Example for tool 0: you may do another macro for other tools

;macro /macros/_Unload_filament.g
T0		;Select tool
;M109 S100	;set hotend temperature to 100 and wait for it – NOT USED as if hotend is hot, it wait for cooling which slow the process, hence heat temperature request shall be done manually before running this macro.
G1 E-5 F500	;Retract 5mm filament at low speed 
G1 E-680 F3600	;Retract 680mm filament at high speed
M84 E0 		;turn extruder motor off to let user unload if filament locked - 
T99		;deselect tool (useful ?)

Prepare printer

example for PLA preparation with a heated bed

;macro /macros/__prepare_PLA.g  prepare the machine after a cold start for PLA printing
T0			; define tool
M140 S55 		; Set the bed temperature and don't wait for it
M104 S150		; set the current hot-end temperature to 150°C and don't wait for it
G28			; home
G32			; calibrate during heating phase
M98 P/macros/brush.g  	; call cleaning macro if it exists
M109 S150	; set the current hot-end temperature to 150°C and wait for it - M104 don't wait 
G32		; 2nd calibration taking into account heating  - at 150°C to save surface like 
		; ‘BuildTak’, which don’t accept sensing at high temperature. Low temperature
		; sensing is inaccurate due to filament blob on the hot-end.
M109 S190	; set the current hot-end temperature to 190°C and wait for it - M104 don't wait 
M190 S55 	; Set the bed temperature at 55°C and wait for it

Extruder(s) motor(s) off

;macro /macros/E_motors_off.g
M84 E0:1:2:3:4

Head cleaning

The limit of accessible area may be defined in ‘config.g’ (M665 command for delta). You may want to go outside the printing area to clean the head. This need caution and on a delta to not impact the structure and requires reduction of speeds and accelerations to avoid step loss. The proper values will be restored at the end of the macro, but it is important to note that the restored values will supersedes the values defined in the config.g file, which could trouble the user.

Example for a delta machine with a bed 300mm diameter

;macro /macros/brush.g  Brush the head on a fixed brush
M201 X1000 Y1000 Z800	; reduce accelerations outside normal area 
			; – required for a delta at low arm angle - 
M564 S0			; can go outside the limits defined in M665
G0 X-52 Y127  Z2 F8000	;go to origin point, within authorized area, higher than brush
G1 X-77 Y140  F1500	; 1rst point outside
G1 Z0.5			; lower head to brush. in this case, the brush is at same height as bed
G1 X-97 Y126  F1500 
G1 X-78 Y139  F1500
G1 X-98 Y125  F1500
G1 X-77 Y140  F1500
G1 X-97 Y126  F1500
G1 X-77 Y140  F1500 	
G1 X-97 Y126  F1500 
G1 X-78 Y139  F1500
G1 Z2  F1500
G0 X-52 Y127  F1500	; Safe area, where speed could be increased 
M564 S1			; set again the safe area limits
G0 X0 Y0 Z50  F8000	; back to center
M201 X6000 Y6000 Z3000	; set again accelerations – Warning! that supersedes values in config.g

Slow retraction

In case of jam caused by foreign matter, you may want to remove very slowly the filament at low temperature to get the guilty element. Appropriate temperature for PLA is around 100°C, may be a few degrees lower

M109 S100		; set the current hotend temperature to 100 and wait for it
M302 P1			; allow cold extrusion for P1 extruder
G1 E-280 F15		; very slow retract - it takes ages
G1 E-650 F3000		; fast retract
M84 E0:1:2:3:4		; Disconnect extruder motors
M302 P0			; disallow cold extrusion for P1 extruder

Activate output/buzzer

Some output pins could be activated via code M42. By example a buzzer could be connected on Wired Duet extension connector pin 32, which is signal PB16 and Duet digital pin 67. Note that Duet WiFi expansion connector have other pin layout.

/macros/Beep_3s.g ; activate buzzer for 3seconds. Digital pin 67 = extension connector pin 32
M42 P67 S1 ; Beeper on
G4 P3000   ; wait 3000 msec (= 3 sec)
M42 P67 S0 ; Beeper off

Make sound to confirm proper operation


It is possible to make sound using motors. The frequency is the step frequency, so moving at a defined speed give a note. However, the frequency for a given speed depends from the step/mm ratio and each sound file shall be adapted for a given step/mm ratio. Another solution is to temporarily modify the step/mm configuration to use a known command set. As most printers driven by a duet haven’t any local screen nor diagnostic LED, the sound may indicate proper startup.

You may call a separate macro as the last command of your config file, e.g.

M98 P/macros/startmusic.g ; call a sound file to acknowledge the startup 

You could create a music file by using this converter: On a delta printer, you shall move in Z only, to have all your motor running simultaneously with the effector speed equal to the carriage speed. On a Cartesian printer, you can use whatever axis, provided the step/mm is accurate. It is wise to limit the travel, so the converter will make back and forth travel within the defined range. The file provided by the converter shall be modified:

  • Replace all G01 by G1
  • Comments shall be prefixed by “;”
  • add "G92 X0 Y0 Z0" at the beginning of the code, to define the origin as current position, as you have not yet homed the machine
  • add "G91" to set absolute moves.

A better control on a delta may be to move each carriage independently, hence checking driver and stepper independently. A movement on 'XYZ' columns instead of 'XYZ' user space is done while adding S2 parameter to the G1 command. Unfortunately, this does only work in relative mode, so you shall convert the file from absolute to relative. Use a spreadsheet program to help conversion.

Examples and converter

100 step/mm -> example 16 teeth GT2-2, micro-step 1/16, 1.8° stepper Z travel up total 8mm. Works ok for 200 step/mm (same with 0.9° steppers). You could use it 'as is' by modifying temporarily the step/mm of your machine with M92: Set axis steps per unit, then resetting original value.

8 first notes of French children song 'Quand on fait des crêpes chez nous'

;  MIDI-converter version: 2.5 
; File: crepes.mid  ;Breaks disabled ;Steps per millimeter for Z-axis: 100
G21 ; units are mm - supersedes former value, may not be needed
G90 ; Absolute positioning - supersedes former value, shall be the default except for extruder
G94 ; feed rate in units/minute, shall be the default
G92 X0 Y0 Z0; set origin to current position, required as machine is not homed
G1 Z1.23600000 F197.76000000
G1 Z1.60312500 F176.22000000
G1 Z2.42712500 F197.76000000
G1 Z3.30012500 F209.52000000
G1 Z4.28012500 F235.20000000
G1 Z5.10412500 F197.76000000
G1 Z6.41212500 F156.96000000
G1 Z7.64812500 F197.76000000
M84; shut down motors

for 87 steps/mm (like Fisher delta) sound shall be

G1 Z1.42068966 F227.31034483
G1 Z1.84267241 F202.55172414
G1 Z2.78979885 F227.31034483
G1 Z3.79324713 F240.82758621
G1 Z4.91968391 F270.34482759
G1 Z5.86681034 F227.31034483
G1 Z7.37025862 F180.41379310
G1 Z8.79094828 F227.31034483

You can hear the above example at the end of this film or same film in another format

Here is a small Libre Office calc sheet File:Convert

It converts G-codes according a given step/mm ratio of the first 8 notes of the children song 'Quand on fait des crêpes'. This calc sheet does not use any libre office macro.

Commissioning macros

Allow cold extrusion

; /macros/Allow_cold_extrusion.g
T0  ;select tool 0 – note that its starts hotend heating
M302 P1 ;allow cold extrusion

Extrude a given filament length

To calibrate your extruder, you shall extrude a given length and measure the actual extruded filament.

;macro /macros/Extrude_1000mm.g  Extrude 1000m of filament for calibration
T0  ;select tool 0 – note that start the hotend heating.
M302 P1 ;allow cold extrusion
G1 E1000 F3600 
;new step/mm = old*1000/Measured
;example 1024 measured with old steps = 310 -> new = 310*1000/1024 = 302.7

Move a motor on delta printer

On delta printers, the X,Y and Z movements does not correspond to a motor movement, while for commissioning, you may want to operate each motor independently. in G1 code 'S2' is used to select motor instead movement.

; macros/X_carriage_up_10mm.g  Commissioning - test of displacement column by column of 10mm, up. 
G91		; use relative positioning
G1 S2 X10 F300	; move carriage of X column 10mm up

Initial calibration

Calibration report

you can use M122 for reporting height of the sensing point – relative to previous geometrical alignment. A calibration MODIFY the geometrical alignment, and this is an iterative process. Another solution is to create a macro identical to bed.g, but with S-1 parameters, which will report height without doing a calibration. This can be useful to test repeatability of sensing.

Calibration help for mechanical sensing

In case of mechanical sensing, the offset for each point may vary for two reasons:

  • The force needed for actuation differ for each point depending relative position of sensors and hotend
  • The machine deformation for a given vertical force vary for each position,

So, you shall defined individually offset for each sensing point with the sheet paper method. To help this tedious process, it is useful to makes macros. Unfortunately, pausing don’t work in a macro if you are not printing, so independent macros are needed for each point. Writing all those macros ease the calibration process and is worth the task.

; /macros/P0.g – go to P0 Probing point
G90 		; absolute position
M564 S0 	; // allow travel outside envelope  - needed for movement below bed level (sub 0)
G1 Z2 		;raise 2mm above bed
G1 X-69 Y-37 F4000 ;move to P0
G1 Z0.25	; lower the hotend 0.25mm  above bed 

In addition to site licence GFDL1.2, this page is also released under license CC BY-SA 4.0