I modified Marlin so it can use the built-in Auto Bed Leveling feature for use with my MPCNC. I have posted my modified Configuration.h and Marlin_main.cpp files, so you can try it out… CAUTION: Personally i have only dry run tested this myself, but, it seems to work fine (dry run, meaning i create the map with a board that was on a tilt, and after mapping it, i can move the spindle all around the work surface and it automatically keeps the Z to just touching the surface.)
Mapping the work surface is needed for several uses- 1) is PCB etching (carving) 2) V-Carving not so flat boards, especially v-carving smaller text, 3) etching (carving) onto acrylic, or glass. There may be other times it’s needed also, so, if you need it, this should work.
BUT, you would need to have your machine set up for it. I use Z-Max for homing my spindle up when issuing a Home command, and use Z-Min for the Touch Probe / Mapping Probe.
For mapping, I have a Vacuum shoe sweep that I attach with magnets to my spindle, so I printed a bracket to hold a micro sw and i take off the Vacuum shoe sweep and attach my Mapping Probe using those same magnet points. My sw sits right at or below (X Y aligned) the bit - so i don’t need an X or Y offset, but, if your probe sits off to a side, in the configuration.h you can set a Probe X or Y offset. You can also specify a Probe Z offset, but I programmed this so that the Z offset will be calculated Automatically when i do a G30 command to get the top of the surface work piece.
My GCODE Header work flow is as follows - in Gcode – without leveling:
; Beginning of Header
M80; Turn On Power Supply (PC ATX PS = Power for Steppers)
G90 ; ABSOLUTE MODE
G28 Z0 ; home to limit sw
G28 Y0 ; home to limit sw
G28 X0 ; home to limit sw
M3 S8500; turn Spindle On to Snnn RPM
; Move spindle to desired Start Pos - XY corner of the Work piece
; EDIT NEXT 2 LINES
G0 X20 Y40 F12000 ; xy corner of material
G0 Z132 F1200 ; top of material NOTE: in my set up, ZMAX == 210, so I am moving it down from 210 to 132 (This Value is obtained by running a G30 command and is different with each bit change and material height )
G92 X0 Y0 Z0 ; set current LOGICAL location to 0,0,0
G0 Z5.080 F1200; Spindle is now 5mm above the XY starting corner of the material.
;----- End Of Header
; — run the actual Gcode for Cutting ----
So, to use the Surface mapping, here is my new work flow, before I run the above GCode header and cutting file
G0 Xnn Ynn Fnn; send the spindle to the XY corner of the work material, such as X20 Y40
G92 X0 Y0; set logic Zero/ this is same as i do in my normal header
; Issue the Surface Map command and create a map of the work piece surface
G29 L0 F0 Rrr Bbb Xxx Yyy T; Where L and F set the starting corner of the work piece, I am using 0,0 for the start because i moved my spindle to that location and issued a G92 command to set that location as 0,0,0 for the cutting file. Rrr = Right (X) size of work area to map, Bbb= back or Y size of material to map, such as R200 B300 for a work material that is 200mm in the X direction by 300mm in the Y direction. And, i supply X and Y - which in the G29 command sets how many grid points our map will have. X4 Y5 will set a map with 4 points in the X direction and 5 in the Y, 20 points total. If T is given it will print the results out to the serial port so the host software will display the Map data once it is created. (I am currently using Repetier Host for sending the GCode files)
; I put in code so that this data, the LFRB and XY data, MUST be sent with the G29 command, or it will give an error and not create the map. I’ve included a CNC_LEVELING_VALUES define in the configuration.h file, you can comment that out if you don’t want that feature. The only exception is you can issue a “G29 T” and it will not do a map, but, will print out the current map to the serial port
; The G29 command will create the MAP - but, uses the Z Height at whatever Height the Probe is-- which will NOT be the same as the cutting Bit…
; so, after running G29 i move the spindle back to the material XY starting corner, which will be 0, 0 (since I used the G92 command to set 0,0 to the work piece corner)
G0 X0 Y0 F12000
; remove the Probe and if not already installed, install the router bit,
; then place the metal touch-plate at that location and attach the wire to the bit , and issue the G30 command.
This will do a single probe at the starting corner of the work piece.
I Modified the G30. As long as CNC_LEVELING is defined in the configuration.h file, the G30 command will get the height of the work surface, for the bit that is installed. In the Configuration.h file I have also added a define called ZERO_PLATE_THICKNESS which you set to the thickness of the metal touch plate that you are using.
; this way when we do a G30 cmnd it will calculate the Z height (-) (minus) the thickenss of the touchplate, so it will get the exact Z height of the work stock at the starting corner.
; the other I have the G30 command do, is it will calculate the difference between the Bit Z height and the Probed Z height, meaning it will automatically calculate the Probe Z offset value. And, it then goes through the Map data and applys that offset to the remaining Map data points. So, the map get re-aligned to the bit height all just by issuing the G30 command.
; after issuing this G30 we can then go ahead and run the Gcode script to cut the work piece,
; the Gcode can re-Home the spindle, etc, and run as normal, and the mapping data will still be there and mapping will still be enabled (as long as the CNC_LEVELING flag is defined in the configuration.h file).
; After the first bit is done cutting, if another bit is needed, like a rough cut, then final cut or additional cuts,
; go ahead and change the bit and move spindle to Starting corner of work piece (Logical 0,0) and issue another G30 with the touch plate in place and it will get the Z height with the new bit - (minus) the thickness of the touch plate, get the difference in height and apply that new difference to the map - again, and it is ready to cut with the second bit,. Again you can Home, etc, and cut away. An d, you can do this as many times as you need, the map data will not be disabled.
you can issue a G29 T to view the existing map data.
So far i have only DRY run it, i haven’t had time to put on a piece of wood and cut it, but, after mapping, I issue HOME and G30, i can use G0, G1 and move the spindle around (I had purposefully put a board on the work area on a tilt) and as you move around the spindle in the X Y directions, the Z tracks up and down as needed to keep the spindle level to the tilted surface.
I am uploading the Configuration.h and Marlin_main.cpp These are the files from the DualDual_1_1_5 iteration of marlin, that was made for the separate control of the X and Y stepper pairs.
I have noted where i did the changes for the CNC Leveling, so if you want to apply this to another version of Marlin, search for “CNC_Added” and copy the code in those areas to the .cpp file that you use. Same with the Configuration.h file. Let me know if this works for you.
Marlin_CNC_ABL.zip (130 KB)