1

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.

KMC
  • 137
  • 1
  • 5
  • 6
    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 07: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 08:04
  • Have you ever tried writing an interpreter? The interpreter is inside a microcontroller in the CNC lathe. – user253751 Apr 05 '18 at 23:54
  • Aside: Python is a programming language. Code is Data. – Caleth Aug 16 '21 at 10:01

4 Answers4

8

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.

Alexander
  • 3,562
  • 1
  • 19
  • 24
Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479
  • 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 07:59
  • 1
    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](https://github.com/grbl/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 05 '18 at 23:56
  • @KMC: it is proprietary software. Implementations are highly specific to the mechanical design of each machine. This is why it is not published: it is guarded as a secret, and the machine vendor only makes the guarantee that when the G-code input is provided, that the machine will perform as intended. – rwong Jan 08 '21 at 20:23
5

The question shows a fundamental misunderstanding of "code", compilers, interpreters, etc.

Take a BMP image file. It consists of a short header and then a bunch of pixels.

The hex code 0000FF (yes, it's backwards) means to call a function to insert a red pixel. Where can we find the pixel codes? Is there an ISO standard C/C++ library that the bitmap file calls from?

No, there isn't! The bitmap file is a bitmap file. It doesn't know about functions. It only knows about pixels. It doesn't say "call the red pixel function", it only says "red pixel". If Microsoft Paint wants to use a red pixel function, that's Microsoft Paint's problem - nothing to do with the file.

In the same way, G-Code doesn't care about functions. G00 means "rapid movement to point". It doesn't mean "call the rapid movement to point function".

If you are designing a machine that processes G-Code, you get to decide how to implement this. You could write:

if(command == "G00") {
    // code for rapid movement to point
} else if(command == "G01") {
    // code for precise movement to point
} else if(...etc...

Or you could write this:

if(command == "G00") {
    doRapidMovementToPoint(); // code is inside function
} else if(command == "G01") {
    doPreciseMovementToPoint(); // code is inside function
} else if(...etc...

Or you could write this:

if(commandChar == 'G' && gCommands[commandNumber]) {
    (*gCommands[commandNumber])(); // call through function pointer
} else if(...etc...

In Marlin, here is the code which splits apart a G-Code command into variables, and here is the code which processes the command after it's been split apart. No magic. Just if and switch statements.

As you can see, in Marlin they mostly wrote a function for each G-Code command. But it doesn't have to be that way. E.g. we see case 90: set_relative_mode(false); break; instead of case 90: G90(); break;. And we see that G00 and G01 share the same function but check the code number again.

user253751
  • 4,864
  • 3
  • 20
  • 27
0

In any firmware of a machine like a 3D printer which uses G-codes, there is a file that includes the configuration file and subfiles exist that assign each G-code to a command; a lot of programming pages exist but the configuration file include/call them so usually you cannot see it unless if you searched in other firmware folders

0

Another way to think about this is that G-code is NOT a program in any conventional sense.

It is a list of instructions on how to draw something with tools. It doesn't use function calls the way a programming language might.

It has instructions like, pick up this tool, or move to a location, or draw a line, or draw a curve, or draw a circle, etc.

You ask where the code is that this calls? G-code doesn't call code. It says to do something. The code to implement that action is in the firmware of the controller. The firmware is specific to the machine, as a kind of baked in knowledge. In the case of Marlin, the marlin code itself is the firmware, and includes all the actions supported.

Asking "where is the standard library" is kind of like if I asked you to draw a circle and you asked me for instructions on how to draw a circle. Don't you just intuitively know how to draw a circle? Marlin bakes in that intuition.

The closest you would get to a "standard library" is the list of G-codes a particular firmware supports. Here is a list of supported marlin G-codes. Note that not all of these are supported in every version of Marlin, and when Marlin is compiled into the binary loaded into the machine as firmware, some of these can be disabled or otherwise tweaked to best fit the hardware available.

user10489
  • 257
  • 2
  • 8