Grbl to Snapmaker Marlin g-code converter

(Minor update Dec. 29, 2020: Added the ability to optionally accept leading zeros in some M commands, so the script should now recognize M03, M05, M06 as equivalent to M3, M5, M6.)

The attachment is a Perl script designed to take g-code produced by Carbide Create or other programs that can only output in the grbl dialect and transform it into something usable by the Snapmaker2. (I’ll put it on GitHub or Heptapod or somewhere eventually.)

Be warned that this is a very early-stage prototype! It has been tested on one file containing a limited set of commands only! (Luban seemed to understand the output, though, so it should be processing the basic G0/G1/M3/M5 commands needed for CNC somewhat correctly.)

Right now what this thing needs is testing. Throw your grbl at it, watch to see if it complains about “Unknown command [foo]”, import the results into Luban (or a more sophisticated simulator, if you have one), and see if the toolpath makes sense.

In order to test this, you will need a Perl interpreter. Mac OSX and Linux should have one preinstalled; for Windows, you’ll need Strawberry Perl (ActiveState Perl should also work if you already have it installed for some reason). I can’t be absolutely certain this will work on Windows as-is—Perl is perfectly portable 90% of the time, and trips over its own feet the other 10%. If it fails to run for you (or produces output with messed-up linebreaks), tell me, and I’ll see if I can sort it out.

Usage: perl grbltomarlin.pl [grbl file]

The output will automatically go to a file called [grblfile].marlin.cnc

grbltomarlin.zip (1.9 KB)

(Not how I expected to be spending my Saturday afternoon. Oh, well.)

5 Likes

Dear @ElloryJaye,
Thanks a lot for your effort and time.
I was just about to make a similar script myself, but then I stumpled upon your post.
It works nicely - but I needed to make a small adjustment: the nc-file output by my Carbide Create (build 514) uses “M03”-commands and not “M3” to start the spindle.
Thanks again,
Lars

When is this needed?
Because I’ve used several programs that I’ve selected ‘grbl’ as the output and they’ve worked perfectly with my A350. (only for CNC)

-S

I’ve only tried Carbide Create - and there it is needed.

GRBL supports motion modes where lines do not contain a G command, such as

X100
X150 Y10
Y20 F3000

Carbide create, and some other software packages, generate that style of gcode.

A simple regex is enough for 90% of the cases, and this is a more thorough parser to capture edge cases regex is not enough for.

My solution to this was just recompile the firmware and enable GCODE_MOTION_MODES, so a parser is not required. With GCODE_MOTION_MODES enabled lines starting with X Y Z E, and F are valid gcode that will be parsed.

@larsbw: Thank you for your feedback. I’ll patch the script to accept an optional 0 in M3 and M5 commands when I have a moment.

The idea was, as brent113 says, to deal with “abbreviated” grbl g-code in such a way that posters who are coming from a nontechnical background or are just wary of turning their Snapmaker into a paperweight by messing around with the firmware might be able to apply the fix. I do a lot of regex-based document parsing in my day job, so this was easy enough to tinker up.

I also figured I was going to have to familiarize myself with g-code sooner or later—very little laser or CNC software is available for Linux, and I expect that I am going to have to, at some point, somewhere, work around or patch a broken program. This was a practice exercise that I hope will be of use to people other than myself.

This was like a dream come true. GRBL to MARLIN!!

Using the standalone version of perl after the command prompt:
Keyed in:
grbltomarlin.pl grblfile.nc marlin.cnc
(where grblfile.nc is input, marlin.cnc is output)

I am presented with a selection on how to open the ‘pl’ file, when I select notepad what opens is the ‘pl’ source code.

It seemed so easy and self explanatory, what am I doing wrong?

You need to install a Perl interpreter. The usual one for Windows is linked in my opening post. Your command line is then:

perl grbltomarlin.pl grblfile.nc

The output will then appear as grblfile.marlin.cnc (or maybe grblfile.nc.marlin.cnc —I forget how I handled extensions.)

Finally got it to run.
grblfile.nc (13.3 KB)

Threw a lot of errors.
Please see attached source file & errors.nc.
errors.nc (1.2 KB)

Right, I’ll have a look. Might not be until this weekend, though.

Thanks, I appreciate your time & efforts

Okay, it looks like most of the errors the script generated were places where I should have allowed for zeroes or spaces but didn’t, plus my first encounter with the alternate comment syntax. I’ve fixed those (your file will produce one note about the unsupported pause command near the end, which you can safely ignore), and Luban accepted the output file, although I have no idea whether it looks the way you intended.

You can download the revised script from Upload files for free - grbltomarlin.zip - ufile.io (sorry—I tried to upload it to the board, but nothing happened as far as I could see).

Thanks for your work on this.

Tried it on a different image. 3 different nc files, each still getting errors.

Heart2 Step1 gets a tool change.
Heart2 Step2 & Heart2 Step3 only 1 tool.

I am not very familiar with GCode, but it looks like ‘M0’ is unconditional stop, ‘T’ is tool change & ‘S’ is spindle speed.

Looking at the errors and cnc output file - M0 & T are ignored but thrown as an error so there is not ‘pause’ for a tool, change, and S10000 is also ignored but replaced with S83.xxxx, so speed will be 83RPM.
Test Files.rar (188.6 KB)

Snapmaker does spindle speeds in percentages (don’t ask me why), with a max RPM of 12000, so that S83 should be correct (10000/12000 = 5/6 = 0.833333 . . .) Tool changes weren’t supported by the Snapmaker when I wrote the script (and I think still aren’t), and I’m not sure how usable M0 is in general—the handset is weird about pauses—but I’ll attempt to find the applicable code in the Snapmaker Github repositories and see.

Thanks, I broke the tool change file into 2 pieces. I guess the file would need a ‘pause’ then the handset a resume/continue that could reliably be used.

Again thanks for the work om this. I’m going to try it in practice this weekend.

Jay