Helpful GCode snippets

The following is copied from a reply to @Dgkeith237’s excellent post here:

I have added download links for the helpful GCode snippets.

That same thing has worked great for me. Here’s one way that I have simplified it. I originally created this for when working with cuts on full sheets. I have since realized it can be used for other cuts too.

I created some GCode files on my SD card to be used as a kind of “macro” approach. I did this because I wanted macros, but did not ever take time to figure out how to reprogram the Marlin firmware on the SKR to get macros there.

I created 4 different GCode files for probing.

  1. One is based on moving from the machine’s 0,0 home to the very corner of a full MDF 49x97 sheet, aligned perfectly with my spoil board, which is also a full 49x97 sheet. It moves to that corner spot, sets the work origin as 0,0 there, and then moves inward by a distance of x150 and y150, and then starts the Z probing sequence, down to my homemade “tiny touch plate.”

  2. Another is based on moving from the machine’s 0,0 home to the very corner of a full plywood 48x96 sheet, placed at exactly 1/2" inward from the edges of my spoil board (a full 49x97 sheet). It moves to that corner spot, sets the work origin as 0,0 there, and then moves inward by a distance of x150 and y150, and then starts the Z probing sequence, down to my homemade “tiny touch plate.”

  3. The third assumes I have manually moved the machine to where I would like a work origin set, and immediately sets the work origin as 0,0 there, and then moves inward by a distance of x150 and y150, and then starts the Z probing sequence, down to my homemade “tiny touch plate.”

  4. The fourth assumes I have a work origin already set, and I don’t want to change it. It does not set any 0,0, but simply moves inward by a distance of x150 and y150, and then starts the Z probing sequence, down to my homemade “tiny touch plate.”

HOW TO USE the first two…

Whenever I create vectors for cutting on MDF, I set them on a 49x97 rectangle, and output that as part of the vector file (SVG or DXF). Then in ESTLcam, that rectangle means I can easily get to a 0,0 point mutually shared between the cut job and the machine’s work origin.

For vectors for a cut job on sheet goods that are 48x96, I use a rectangle of that size.

It can be used for cuts on partial sheets, by simply positioning the partial sheet where its near corner (x-min and y-min corner) aligns with the near edges of the spoil board (for #1), or 1/2" inward from there (for #2).

This works well for me on a wide variety of cut jobs. I am sharing the GCode files if anyone wants them.

I am also adding some snippets I use for quickly parking my gantry on the far end (for most of you that would be your Y-max).

Please take note that I have my X and Y swapped, so you probably will need to edit the axes mentions accordingly.

USE WITH CAUTION. EDIT THE DISTANCES TO SUIT YOUR TABLE.

Note: the slight Z drop from Z-max before moving to park, is because my gantry, if left at full max height, would collide with a lumber rack on the wall at the far end of my LR3.

Probe 48x96 PLY 26.2, 21.2 set 0,0 - 250,250 (fast).gcode (628 Bytes)

Probe 49x97 MDF 13.5, 8.5 set 0,0 - 250,250 (fast).gcode (625 Bytes)

Probe any loc - no origin reset, 0,0 (fast, F900).gcode (521 Bytes)

Probe at any loc - 0,0 - 150,150 (fast, F900).gcode (517 Bytes)

The two snippets “park” the gantry.

Park Home Long (X2450).gcode (227 Bytes)

Park Home Short (X2000).gcode (226 Bytes)

The two snippets reverse the “park” and instead “home” the gantry.

Park Home Long (X2450).gcode (227 Bytes)

Park Home Short (X2000).gcode (226 Bytes)

1 Like

Reading @Dgkeith237 comments and your response with script triggered some ideas/information. I’ve used various scripts triggered in various places over my time with my MPCNCs, and I’ve spotted some g-code features that might be helpful. Consider this post my musings on saving positions and the use of g-code scripts/macros.

I’m actually interested if anyone has suggestions on how to simplify that process.

It is my understanding that, in firmwares specifically created for a CNC, the saving and restoring of positions is handled by workspaces. Marlin does have workspaces (see: G54-G59.3 - Workspace Coordinate System). What it doesn’t have is an interface on the host computer software to make using workspaces easy. It means you have to define your own process and scripts to use workspaces. I outlined in this post how I used workspaces to implement bitsetter functionality, but a much simpler setup could be used to just save and restore a position.

One cool thing about workspaces is that they are saved with a M500. This means the offsets that you are currently writing on a piece of paper could be “automatically” recorded for all your jobs just in case you need to return to the job origin (even if the electronics are cycled). If anyone thinks they want to go down this path, I can provide some ideas of the process and the g-code to make it happen.

You might also look at G60 and G61. These two g-code save and restore positions. This is a simpler mechanism than Workspaces, but this feature is not enabled in the V1 versions of Marlin. To enabled it, this area of configuration_adv.h needs to be edited:

//
// G60/G61 Position Save and Return
//
//#define SAVED_POSITIONS 1 // Each saved position slot costs 12 bytes

I think that G60 positions are saved to the EEPROM with an M500, but it is not something I’ve tested.

G-code scripts can be added to the V1 custom menu. This menu is available with an LCD display or in Marlin mode on the TFT. I’ve put a number of my scripts on this menu to make them easier to execute. Search for “#define CUSTOM_MENU_MAIN” to find the area to edit to add custom menu items.

A number of people on this list have implemented a pendent to provide physical “switches” to implement macros (and movement)…myself included. It is an easy Arduino project to read a 4x4 membrane switch array and execute a g-code script for each of the 16 buttons. And I think more than one membrane switch could be used (32 scripts at the push of a button anyone?). If anyone wants to go down this path, I’ll be glad to help.

I recently bumped into the M810-M819 g-codes. G-code macros can be attached to each of these g-codes. Using them would centralize the g-code scripts to a single file, and it would allow these scripts to be executed from a g-code sender, not just by running them from the SD card. They could also be run from the console on the TFT. Unfortunately, M810-M819 are not saved with an M500, but Marlin’s [Autostart functionality](Autostart | Marlin Firmware Autostart | Marlin Firmware) would allow you to repopulate these macros each time the control board is rebooted. Also unfortunately, the V1 versions of Marlin do not have this macros feature enabled. It is this section in configuration_adv.h that needs to be modified to enable g-code macros.

/**
* G-code Macros
*
* Add G-codes M810-M819 to define and run G-code macros.
* Macros are not saved to EEPROM.
*/
//#define GCODE_MACROS
#if ENABLED(GCODE_MACROS)
  #define GCODE_MACROS_SLOTS 5 // Up to 10 may be used
  #define GCODE_MACROS_SLOT_SIZE 50 // Maximum length of a single macro
#endif

Nice!