The G-Code and M-Code that we used to instruct CNC lathe, 3d-Printers and engraving machines, to my understanding, is not a programming language, but a scripting language like Python where scripts such as G00, T01, M18 etc would instruct the micro-controller to call a functions (e.g. some numerical mathematics).

Where are these functions or library of functions? Is there a ISO standard C/C++ libraries that these G-Code call from?

I am trying to understand how a machine "understands" the G-code and where it is fetching the series of logics and movement that the G-code points to. Take Marlin firmware for example. It supposed to parse the G-code and instruct the stepper motors accordingly, but I do not fine the mathematics and the logic by going through the codes. It seems the MCU magically understands what G-Code is and compile the machine binaries and output the respective digital signal.

I believe I am either misunderstanding or missing some information, which I do not find in wikipedia.

  • 5
    Perhaps I'm missing something, but what does this have to do with software design? – Lightness Races in Orbit Feb 28 '16 at 21:34
  • 1
    Are you looking for a link to a specification of G-code, a book about how compilers and interpreters work, or an explanation of how Marlin firmware works? (unfortunately, none of those questions are on-topic here; Google is better at answering them anyway) – Ixrec Feb 28 '16 at 21:43
  • 1
    This is related to software design. I think viewers have been mistaken or I'm not phrasing it appropriately. Say, if I call G67 and the tool path supposed to move in arc or certain degree, there must exist a program that instruct the hardware to move in a circular motion. Where is this program? – KMC Feb 29 '16 at 7:54
  • 1
    @lxrec, it is related to design of firmware, which is a program. When the firmware (Marlin) called a G-code. That G-Code is just a text. I find no logic behind the firmware that can related a particular G-Code to a certain linear or geometrical path (which is math written as a program function). That intrigues me as the path movement of the G-Code seems not to be in the firmware but it should. – KMC Feb 29 '16 at 8:04
  • Have you ever tried writing an interpreter? The interpreter is inside a microcontroller in the CNC lathe. – user253751 Apr 5 '18 at 23:54

G-code was created to be extremely easy to parse by devices with extremely limited computing resources. It's almost more of a data file format than a programming language. There is no "compilation" step. It's interpreted as it is read, line by line, with a small buffer to avoid mechanical issues from timing latency. There's also no "standard library." Firmware typically has to be recompiled for each different combination of microcontroller and motor hardware used, and it takes quite a bit of work to even support what might seem like those minor variations.

In the case of the Marlin firmware, inside the Marlin_main.cpp you have a get_command() function that keeps a queue filled with the commands, and you have a process_next_command() that contains a massive switch statement to pull the next command from the queue and call the appropriate function.

As far as what those individual functions do, that depends a lot on what kind of hardware you have connected, but if you know you have a certain clock rate, a certain type of stepper motors, connected to certain axes, with a certain resolution, connected to certain pins, you can work out the right pins to toggle at the right time to say, move the head in a straight line from a to b along the x axis with a certain speed. From there it's really just a big grind to implement all the different required commands with the correct timing.

| improve this answer | |
  • I learned something from this answer but this is not what I originally asked for. Please see comments. If I call a G code to instruct the machine to move in a certain geometrical path - that geometrical path has to be a program with some mathematics somewhere. When the G code interpreted, this program of geometrical path is then called - where is this program? – KMC Feb 29 '16 at 7:59
  • This "program" is just temporary state in the memory of the G-code interpreter program. You can find open source G-code interpreters (e.g. GRBL) and have a look at how they work if you want. – Jules Feb 29 '16 at 10:36
  • @KMC, I pointed you straight to the code in the Marlin firmware. It's not some super-heavy math. It's basically setting different speeds of the motors, or different settings of the sensors, or different modes of the program. – Karl Bielefeldt Feb 29 '16 at 13:15
  • @KMC Why does a path have to be a program? A path is just a list of coordinates. Is an image file (.bmp) a program? – user253751 Apr 5 '18 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.