-
Notifications
You must be signed in to change notification settings - Fork 296
GCode Parsing
-
Note
Tormach now uses LinuxCNC.
-
G Code is a line-based static file format. It is first split on line endings (
\n
,\r
, or\r\n
), then each line is processed in turn. -
Each line breaks down into an optional "line number", zero or more "words", and zero or more "comments."
-
There are rules on what words can be on a line together, and what words are required with other words.
-
There are also "modal" words, which become implicit on later lines until they are replaced by another word of the same "modal group."
N…
where …
is a number from 0 - 99,999. This looks like a "word" but actually is not. It is optional but must be at the beginning of a line if at all.
Other than line numbers, a character followed by a number is considered a "word".
- Example words
-
-
G0
-
x15
-
Words can be rearranged as needed, and have the line will not change the meaning.
Note
|
There are already rules in place for what words are allowed on the same line. We are to fail the line (and the job?) if this is violated. |
Note
|
This table is directly copied from Table 4 of page 20 the NIST specification. |
The modal groups for G codes are: |
---|
group 1 = { |
group 2 = { |
group 3 = { |
group 5 = { |
group 6 = { |
group 7 = { |
group 8 = { |
group 10 = { |
group 12 = { |
group 13 = { |
The modal groups for M codes are: |
group 4 = { |
group 6 = { |
group 7 = { |
group 8 = { |
group 9 = { |
In addition to the above modal groups, there is a group for non-modal G codes: |
group 0 = { |
Anything between (
and )
are considered a comment. Any line can have multiple comments on the same line. If the close parenthesis is missing, it is supposed to be an error.
Note
|
We might ignore this case of the missing ) , but perhaps we shouldn’t. If there is a missing ) then it may indicate the line is malformed and won’t be interpreted int he way it was intended. The ; and \ characters both provide alternate mechanisms to end the line early if that was the intent.
|
Comments are not to be nested. In other words, the charater (
is not allowed after a (
and before a )
.
Any character after a ;
is considered a comment. This type of comment is not to be interpreted, but simply thrown away. We can effectively treat a ;
as a NULL
character and the end of the line.
Addition: Some malformed G Code generators wrongly use a %
character as an alternative to ;
. We will support that through complex heuristics to seperate it from our other uses of the %
character. Citation_Needed
Though not technically a "comment," if the first character is a /
then the line is optionally to be skipped if the block delete switch is on.
We are adding the concept of the "active comment" on top of the NIST comments. LinuxCNC has a similar concept (and is the origination of the term "active comments"), but we are intentionally limiting them to comment-wrapped-JSON.
For our active comments, we are limiting the active comments to JSON immediately wrapped in parenthesis (starting with ({
and ending with })
). Not space is allowed between the (
)
and the {
}
characters.
({ wait:{ io1:t }}) (1)
( {wait:null} ) (2)
-
Active comment. There are no spaces between beginning
(
and{
or between}
and)
. -
Normal commment, due to the space between the
(
and{
.
Following the rules of comments (active or not), we can have multiple comments. We can also have words before, after, and between comments. This means we can have multiple active comments as well.
In order to simplify the parsing and processing of the G Code line, we want to "normalize" it as a string of bytes into a simpler form. The trick of this is that we can’t accidentally change the "meaning" of the line in the process.
Note
|
It’s assumed that several of these steps may be done in parallel. I list them individually for clarity. |
-
Replace
;
and%
when found with aNULL
and ignore the rest of the string. -
Strip out
(
and everything up the the first following)
only when the first character after(
is not{
orM
followed byS
followed byG
. -
Since there can be multiple
MSG
comments, and we only want one. Throw away the rest.-
(Option 1) Count MSG comments in one pass, and strip all but the last one in a second pass. (This honors NIST’s statement that only the last one is to be kept.)
-
(Option 2) Ignoring the NIST spec, once we have seen one
MSG
comment, we delete the rest as normal comments.
-
-
Since there can be multiple active comments, and position relative to the G Code words doesn’t matter, we can merge them.
-
Move all active comments to the end of the line (buffer?) and all G Code words to the beginning.
NoteAt this point we will be parsing the JSON in-line. Parts of this string may get copied for use in a command buffer. We don’t need to worry about that here, though. -
Merge all active comments on a line into one JSON object, replacing the
)}{(
with a,
.
-
-
Capitalize all of the G Code words.
-
Remove extraneous zeros from numbers that would cause them to be treated as octal.
# Incoming raw line
N1 g0 (blah)({wait:{i01:n}}) x001(MSG Say this)({camera:1}) ; say something and wait
# First pass, strip extra bits
N1g0({wait:{i01:n}})x001(MSG Say this)({camera:1})
# Rearrange so words are at the front and comments we're keeping at the end.
# We also merge JSON comments
N1g0x001(MSG Say this)({wait:{i01:n},camera:1})
# Capitalize and strip leading zeros from the G Code words
N1G0X1(MSG Say this)({wait:{i01:n},camera:1})
# ^- GCode ^- MSG ^- JSON
# Now we are done. Not we maintained pointers to:
# The beginning of the G Code
# The MSG comment
# The active comment JSON
Getting Started Pages
- Home
- What is g2core?
- Who uses g2core?
- Jerk-Controlled Motion
- Getting Started with g2core
- Connecting to g2core
- Configuring g2core
- Flashing g2core
- Troubleshooting
Reference Pages
- Gcodes
- Mcodes
- Text Mode
- JSON Communications
- GPIO Digital IO
- Alarms & Exceptions
- Power Management
- Coordinate Systems
- Status Reports
- Status Codes
- G2 Communications
- Tool Offsets and Selection
- Probing
- Feedhold, Resume, Job Kill
- Marlin Compatibility
- 9 Axis UVW Operation
- gQuintic Specs
Discussion Topics
- Roadmap
- GPIO for 1.X Releases
- Toolheads
- Raster Streaming Prototol
- g2core REST Interface
- Gcode Parsing
- G2 3DP Dialect
- Consensus Gcode
- Digital DRO
- Overview of Motion Processing
Developer Pages
- Development & Contribution
- Branching and Release - DRAFT
- Getting Started with g2core Development
- Project Structure & Motate
- Compiling G2
- OSX w/Xcode
- OSX/Linux Command Line
- Windows10 w/AtmelStudio7
- Debugging G2 on OSX
- Board and Machine Profiles
- Arduino Due Pinout
- Arduino DUE External Interfaces
- Diagnostics
- Debugging w/Motate Pins
- Development Troubleshooting
- g2core Communications
- Git Procedures
- Windows 10 / VMware 8 Issues
- Dual Endpoint USB Internals
- G2core License
- VSCode Setup
- Compatibility Axioms
- Wiki History