How can I add auto bed leveling before print?

I want to add auto bed leveling before each print. When I enable auto bed level in configuration.h, it only shows auto bed in menu. I found this code in cardreader.cpp

void CardReader::openAndPrintFile(const char *name) {
char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
sprintf_P(cmd, PSTR("M23 %s"), name);
for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
enqueue_and_echo_command(cmd);
enqueue_and_echo_commands_P(PSTR("M24"));
}


and changed it to

void CardReader::openAndPrintFile(const char *name) {
char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
sprintf_P(cmd, PSTR("M23 %s"), name);
for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
enqueue_and_echo_command("G28");
enqueue_and_echo_command("G29");
enqueue_and_echo_command(cmd);
enqueue_and_echo_commands_P(PSTR("M24"));
}


Now before each print, the printer does auto bedding two times but when print starts the auto bedding is ignored and printer acts like before doing auto bed.

I'm using Marlin Firmware 1.1.0.

• Why don't you just add the auto leveling commands to your start G-code (in your slicer)? – Tom van der Zanden Jun 18 '17 at 19:43
• I want it to be automatic, some times I only have g-code file and it's not possible to edit all of them – Hadi Barak Jun 18 '17 at 19:53
• @HadiBarak If configured correctly, you will have an option to auto level the bed before manually starting a print. This will be under Prepare -> bed leveling (or whatever the option is called). Adding it to the start G-code is the best solution. But make sure you put it after G28 (auto homing) You are not able to automatically run the command when printing something because this is controlled by the starting G-code. – Granny Nov 15 '17 at 10:03
• @HadiBarak you can edit Gcode with any TXT editor, is just needed to add G29 after G28. – Fernando Baltazar Jun 10 '19 at 21:02

Rather than modifying the firmware to handle this, have you considered a pre-processing script on your computer, greping for a G29 in the G-code, then adding a G28/G29 pair at the start of the file if no G29 is found?

• After re-reading the question, it seems this is maybe the only way to solve the problems in this case, unless Octoprint can do the same as a preprocess. – Sean Houlihane Aug 26 '18 at 8:48
• @SeanHoulihane I've added an answer that describes how to do that with the use of OctoPrint – 0scar Aug 29 '18 at 15:26

Rather than placing the G28 (Home) and G29 (bed level) in the configuration, I would place it in the G-code generating slicer as pre-print code. This will automatically add this to the start of any G-code sliced, enforcing the homing and leveling whenever the G-code is run.

Another answer that does exactly what you want involves the use of a print server. A print server is an application that runs the instructions to the printer over a USB connection from another device, this can be your computer/laptop, or a dedicated Raspberry Pi (a small and affordable computer). One such application described here further is OctoPrint (this may very well be done with other applications, but this needs to be checked first!), this print server application allows integration of many third party plug-ins next to the extensive feature set it already has out-of-the-box. One such feature is GCODE scripts (intently spelled this way to match the option in the Octoprint settings menu); this screenshot shows some details:

As can be seen from the image, there are specific "events" available to process G-code commands at specific event occurences like e.g. just before the print starts. You could use that envent to insert your leveling commands.

Please note that in the image you will find strange G-code commands like OCTO100 and OCTO110 which is a feature of the plugin called "GCODE System Commands" which allows running shell scripts to schedule the fan. I just kill the power to the fan when the printer is idle to get rid of the noise when the printer is just idling, the fan is only needed when the hotend is at elevated temperature.

With Marlin 1.1.0, you can automatically run a G-Code file when powering on the printer with a SD card already present.

Add a file named auto0.g at the root of your card, containing the following G-Code:

G28 ;Auto-homing
G29 ;Bed leveling


Normally the bed leveling map should be reused for all subsequent prints, until the printer is turned off.

It is possible to provide up to 10 files, from auto0.g to auto9.g.

I'm not that fluent in G-code, but at Modern Machine Shop: Understanding G27, G28, G29 and G30 I found these descriptions of the G28 and G29 codes you added:

• G28: For any axis letter addresses included in the G28 command, the machine first will move (at rapid) to an intermediate position in those axes. Then, it will rapid to the zero return position in the commanded axes.

• G29: G29 is also a two-step command. First, it causes the machine to move (in the axes commanded) to the intermediate position used in the most recent G28 command. Second, it causes the machine to move to the position included in the G29 command.

So it sounds to me like you only need one of those two commands. It seems that the code you modified runs separately on startup, rather than for each separate print. I would first try your modification with each G command separately, so you can see exactly what the difference is on your hardware.

I don't know my way around the code involved, but it looks like you'd need to make the modification in another place, such as just before whatever loop sends successive commands to the printer.

Hope that's helpful despite my limited fluency...

• These descriptions you found relate to the G28/G29 commands on professional CNC machines. In 3D printer firmware, they have been (mis-)appropriated for other uses. G28 performs a homing sequence to the endstops (this kind of - but not really - corresponds to your description), while G29 initiates auto-bed leveling. – Tom van der Zanden Oct 14 '17 at 15:45