At the monthly DIY CNC night at PS:One some people were complaining about the CNC Toolchains they were using with their ShapeOkos. I decided to write a blog post on the process of hacking or tweaking your CNC toolchain.
A toolchain is the series software programs you use to get a completed product. I will use CNC routing as an example throughout this post, but other digital fabrication processes have their own toolchains and can be tweaked using the same process. My toolchain goal was to be able to use my commercial CAM programs, use a state of the art machine controller like Marlin and have a nice GUI interface, like PrintRun.
The purpose of this post is to show how to hack a toolchain. I am not suggesting that any of these programs are better than any other programs. This just shows you the steps typically done to tweak your existing toolchain. Here is a typical CNC routing toolchain.
- CAD – This is the software where you draw or model your part. For the ShapeOko that would typically be a 2D vector drawing. AutoCAD, InkScape, CorelDRAW are typical programs I use. The only requirement is that it exports to a format that the next link in the chain can handle. Most people export in DFX format, but SVG, AI, EPS are also popular.
- CAM – This is where you determine how the item will be machined. For example: Cut this shape with a 3mm bit to a depth of 2mm in one clockwise path. This then outputs machine code that the machine to cut. It is usually G-Code. I use programs from Vectric, like Cut2D, V-Carve and Aspire.
- Machine Interface – This is the software that lets your computer talk to the CNC machnie. It could be a simple as a text prompt like GRBL uses or a GUI like PrintRun.
- Machine Controller – This is the software that runs on the machine. In this case it is software that runs on an Arduino. If you use EMC2 or Mach3, the machine controller and machine interface are typically the same program.
With the ShapeOko, most people are using GRBL. (Garble? Gerbil?). I have not hacked GRBL in a while, but last time I looked, it did not compile straight from the Arduino IDE and the pace of new versions was slower than some other controller projects. The pace of development and new features in the 3D printer area appears to be faster and Marlin is very popular right now.
Programs like Marlin are well commented easy to modify, but if you hack the actual code, you will need to re-hack it every time a new version comes out. My goal was to only touch the configuration file. That way upgrading to the latest version is very easy.
I don’t own a ShapeOko, so I used my ORD Bot to simulate a CNC router. This ORD Bot was using a RAMPS 1.4 controller. It already had the three axes and I attached a simple 12V fan to simulate a spindle.
Since Marlin is a 3D printer controller, spindle control is not included, so you need to find a work around. All of the M and G codes implemented by the software can be found in the main Marlin.pde file. For a good reference on G-Codes, check out my G Code commentor page. It will show you the meaning of most codes.
The typical spindle control codes are M03 for on and M05 for off. Neither of these are present in Marlin, but the cooling fan controls (M106 and M107) looked like they would work well. I could have simply added the M03,M05 codes into Marlin, but that would create a customized version, which is what I wanted to avoid. I did the M03/M05 to M106/M107 swap in the post processor.
The fan in Marlin can also be speed controlled which is a cool feature for a spindle. The RAMPS board has many options to hook up a fan, but I chose to use the D9 connection because it is 12V and can drive 5amps which would be perfect for a relay to run a dremel tool. BTW: Don’t try PWM speed control through a mechanical relay
With this method I only needed to modify one line in my Marlin configuration.h file. The motherboard type needs to be 33 so that D9 is used for a fan and not a second extruder. That is it for Marlin…compile and upload.
#define MOTHERBOARD 33
The CAM software still wants to send M03 to turn on the spindle. That needs to change to the fan control codes. No two CNC machines are exactly the same, so CAM programs use a file called a post processor to tweak the G-Code that is produced. The post processor file tells the CAM software what flavor of G-code to use for each operation. There is no standardized format for post processor files. They are unique to each CAM program, but they are pretty easy to figure out and most CAM programs have a guide on how to use them. I use CAM programs from Vectric and their guide to editing a post processor is here.
This is how the header section of my Marlin post processor looks. The stuff in the quotes what is outputed. This is the how each G-Code file will start. The lines starting with “;” are just comments about the file and are ignored by Marlin. The items in square brackets are variables supplied by the CAM program. The first line of G-Code starts with G90. I have added comments to each line. The line M106 is where the fan (spindle) turns on. The [S] variable sets the PWM speed. It comes from the speed you set when you create the toolpath in the CAM software. If you don’t supply this it will run full speed or if you supply it, it should be 0-255 for 0 to full speed. I would change the line to “M106 ; turn on spindle full speed” if you were using a relay and did not have speed control.
“; [TP_FILENAME] “
“; File created: [DATE] – [TIME]“
“; for Marlin by Bart”
“; Material Size”
“; X= [XLENGTH], Y= [YLENGTH], Z= [ZLENGTH];”
“G90 ; Absolute mode”
“G21 ; Use millimeter mode”
“G01 [ZH] ; go to z rapid height”
“M106 [S]; turn on spindle”
“G04 S3; dwell/wait for spindle to reach speed”
“G01 [XH] [YH] [F] ; Go to XY home”
“G01 [ZH] ;goto safe z”
“M107; turn off spindle”
“G01 [XH] [YH] ; goto xy home”
This is the G-Code that will result. In this case I have my spindle speed set for 60.
; Profile 3
; File created: Friday, August 10, 2012 – 11:42 AM
; for Marlin by Bart
; Material Size
; X= 100.000, Y= 100.000, Z= 10.000;
G90 ; Absolute mode
G21 ; Use millimeter mode
G01 Z20.000 ; go to z rapid height
M106 S60; turn on spindle
G04 S3; dwell/wait for spindle to reach speed
G01 X0.000 Y0.000 F1800.0 ; Go to XY home
Here are some screen shots from my CAM program. I made 2 toolpaths. The first is a square with the spindle set to a speed of 60. The second is a circle with a ramping plunge move and a speed of 255.
This is the GUI interface you will have with the machine controller using PrintRun. You will have to ignore the heater controls, but the interface is nice for jogging and you can see the macro buttons I have added to limit manual typing.
BTW: The other neat trick about this hack is you could quickly attach a 3D print head to the ShapeOko and print without any changes to the controller or controller software.
Here is a video of the ORD Bot standing in for a CNC router. Here are the sequence of events.
- Spindle (fan) turns on at speed 60
- It waits 3 seconds for the spindle to reach speed.
- The Z drops down to the rapid movement height from the safe Z height (start)
- It plunges into the material.
- It cuts the square.
- It goes up to the rapid Z height and moves to the XY start of the circle.
- Spindle speeds up to full.
- It does a ramping plunge move down.
- It completes the circle at the bottom.
- It return to the safe Z height.