electrosoup.com

git clone git://git.electrosoup.com/electrosoup.com
Log | Files | Refs

index.html (18730B)


      1 <!DOCTYPE html>
      2 <html lang="en-US">
      3 
      4 <head>
      5   <meta http-equiv="X-Clacks-Overhead" content="GNU Terry Pratchett" />
      6   <meta charset="utf-8">
      7   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      8 <link rel="shortcut icon" href="https://electrosoup.com/favicon.png" />
      9 <title>Writing a G-code Interpreter | Electrosoup</title>
     10 <meta name="title" content="Writing a G-code Interpreter" />
     11 <meta name="description" content="I&rsquo;m currently working on a control system for a laser galvanometer (galvo) that
     12 I want to use in a stereolithography (SLA) resin printer. I don&rsquo;t want to write
     13 a custom slicer, as I feel like model slicing can be incredibly complicated and
     14 existing free software (Cura, Slic3r, etc.) has many advanced features I don&rsquo;t
     15 want to reimplement. However, this means my control system needs to understand
     16 G-code, the language that these slicers output." />
     17 <meta name="author" content="" />
     18 <meta name="keywords" content="G-Code,Lasers,C," />
     19 
     20 
     21 
     22 
     23 
     24 
     25   
     26   <meta property="og:url" content="https://electrosoup.com/posts/gcode-interpreter/">
     27   <meta property="og:site_name" content="Electrosoup">
     28   <meta property="og:title" content="Writing a G-code Interpreter">
     29   <meta property="og:description" content="I’m currently working on a control system for a laser galvanometer (galvo) that I want to use in a stereolithography (SLA) resin printer. I don’t want to write a custom slicer, as I feel like model slicing can be incredibly complicated and existing free software (Cura, Slic3r, etc.) has many advanced features I don’t want to reimplement. However, this means my control system needs to understand G-code, the language that these slicers output.">
     30   <meta property="og:locale" content="en_US">
     31   <meta property="og:type" content="article">
     32     <meta property="article:section" content="posts">
     33     <meta property="article:published_time" content="2024-10-23T16:29:40-07:00">
     34     <meta property="article:modified_time" content="2024-10-23T16:29:40-07:00">
     35     <meta property="article:tag" content="G-Code">
     36     <meta property="article:tag" content="Lasers">
     37     <meta property="article:tag" content="C">
     38 
     39 
     40   
     41   
     42   <meta name="twitter:card" content="summary">
     43   <meta name="twitter:title" content="Writing a G-code Interpreter">
     44   <meta name="twitter:description" content="I’m currently working on a control system for a laser galvanometer (galvo) that I want to use in a stereolithography (SLA) resin printer. I don’t want to write a custom slicer, as I feel like model slicing can be incredibly complicated and existing free software (Cura, Slic3r, etc.) has many advanced features I don’t want to reimplement. However, this means my control system needs to understand G-code, the language that these slicers output.">
     45 
     46 
     47   
     48   
     49   <meta itemprop="name" content="Writing a G-code Interpreter">
     50   <meta itemprop="description" content="I’m currently working on a control system for a laser galvanometer (galvo) that I want to use in a stereolithography (SLA) resin printer. I don’t want to write a custom slicer, as I feel like model slicing can be incredibly complicated and existing free software (Cura, Slic3r, etc.) has many advanced features I don’t want to reimplement. However, this means my control system needs to understand G-code, the language that these slicers output.">
     51   <meta itemprop="datePublished" content="2024-10-23T16:29:40-07:00">
     52   <meta itemprop="dateModified" content="2024-10-23T16:29:40-07:00">
     53   <meta itemprop="wordCount" content="443">
     54   <meta itemprop="keywords" content="G-Code,Lasers,C">
     55 
     56 
     57   <link href="/herman.min.css" rel="stylesheet">
     58 
     59   
     60     
     61     <link href="/syntax.min.css" rel="stylesheet">
     62   
     63 
     64   
     65 
     66   
     67 </head>
     68 
     69 <body>
     70   <header><a class="skip-link" href="#main-content">Skip to main content</a>
     71 
     72 <nav>
     73   <a href="/">Main</a>
     74 
     75   <a href="/posts/">Posts</a>
     76 
     77   <a href="https://git.electrosoup.com">Code</a>
     78 
     79   <a href="https://christianermann.dev">Resume</a>
     80 
     81 
     82 
     83 
     84 
     85 
     86 
     87 
     88 
     89 </nav>
     90 </header>
     91   <main id="main-content">
     92 
     93 <h1>Writing a G-code Interpreter</h1>
     94 <p class="byline">
     95   
     96   <time datetime='2024-10-23' pubdate>
     97     2024-10-23
     98   </time>
     99   
    100 </p>
    101 
    102 <content>
    103   <p>I&rsquo;m currently working on a control system for a laser galvanometer (galvo) that
    104 I want to use in a stereolithography (SLA) resin printer. I don&rsquo;t want to write
    105 a custom slicer, as I feel like model slicing can be incredibly complicated and
    106 existing free software (Cura, Slic3r, etc.) has many advanced features I don&rsquo;t
    107 want to reimplement. However, this means my control system needs to understand
    108 G-code, the language that these slicers output.</p>
    109 <p>As stated on the <a href="https://reprap.org/wiki/G-code">reprap wiki</a>, &ldquo;G-code is a
    110 list of fields that are separated by white spaces or line breaks. A field can
    111 be interpreted as a command, parameter, or for any other special purpose.&rdquo; For
    112 now, we&rsquo;ll ignore any special purposes. Here is an example of G-code that
    113 describes a movement of the tool/hotend/laser to a specific location:</p>
    114 
    115 
    116 
    117 
    118 
    119 <pre tabindex="0"><code class="language-gcode" data-lang="gcode">    G1 X10.4 Y7.4 Z8.3</code></pre><p>I was able to parse commands with parameters using a two-stage procedure:</p>
    120 <pre><code>1. Retrieve the next white-space delimited token.
    121 2. Accumulate tokens until two separate command tokens are found.
    122 </code></pre>
    123 <p>This second step is important because we can&rsquo;t begin executing a command until
    124 we&rsquo;ve found all of its parameters and we don&rsquo;t know if we&rsquo;ve found all of its
    125 parameters until we find the next command.</p>
    126 <p>Once a command and all of its parameters have been parsed, it is passed to the
    127 controller which updates the state of the machine. For each command that the
    128 controller receives, it has to update its internal state, and in the case of
    129 physical movement commands, send the proper signal to the laser galvo. Most
    130 physical movement commands utilize interpolation so a timed control loop is
    131 utilized to ensure the laser follows as close to the intended path as possible.</p>
    132 <h2 id="system-diagram">System Diagram</h2>
    133 
    134 
    135 
    136 <div class="goat svg-container ">
    137   
    138     <svg
    139       xmlns="http://www.w3.org/2000/svg"
    140       font-family="Menlo,Lucida Console,monospace"
    141       
    142         viewBox="0 0 384 249"
    143       >
    144       <g transform='translate(8,16)'>
    145 <path d='M 80,16 L 296,16' fill='none' stroke='currentColor'></path>
    146 <path d='M 16,64 L 112,64' fill='none' stroke='currentColor'></path>
    147 <path d='M 256,64 L 368,64' fill='none' stroke='currentColor'></path>
    148 <path d='M 128,80 L 240,80' fill='none' stroke='currentColor'></path>
    149 <path d='M 16,96 L 112,96' fill='none' stroke='currentColor'></path>
    150 <path d='M 256,96 L 368,96' fill='none' stroke='currentColor'></path>
    151 <path d='M 80,144 L 296,144' fill='none' stroke='currentColor'></path>
    152 <path d='M 0,192 L 120,192' fill='none' stroke='currentColor'></path>
    153 <path d='M 256,192 L 368,192' fill='none' stroke='currentColor'></path>
    154 <path d='M 136,208 L 240,208' fill='none' stroke='currentColor'></path>
    155 <path d='M 0,224 L 120,224' fill='none' stroke='currentColor'></path>
    156 <path d='M 256,224 L 368,224' fill='none' stroke='currentColor'></path>
    157 <path d='M 0,192 L 0,224' fill='none' stroke='currentColor'></path>
    158 <path d='M 16,64 L 16,96' fill='none' stroke='currentColor'></path>
    159 <path d='M 64,32 L 64,48' fill='none' stroke='currentColor'></path>
    160 <path d='M 64,160 L 64,176' fill='none' stroke='currentColor'></path>
    161 <path d='M 112,64 L 112,96' fill='none' stroke='currentColor'></path>
    162 <path d='M 120,192 L 120,224' fill='none' stroke='currentColor'></path>
    163 <path d='M 256,64 L 256,96' fill='none' stroke='currentColor'></path>
    164 <path d='M 256,192 L 256,224' fill='none' stroke='currentColor'></path>
    165 <path d='M 312,32 L 312,48' fill='none' stroke='currentColor'></path>
    166 <path d='M 312,112 L 312,128' fill='none' stroke='currentColor'></path>
    167 <path d='M 368,64 L 368,96' fill='none' stroke='currentColor'></path>
    168 <path d='M 368,192 L 368,224' fill='none' stroke='currentColor'></path>
    169 <path d='M 312,48 L 312,56' fill='none' stroke='currentColor'></path>
    170 <path d='M 312,104 L 312,112' fill='none' stroke='currentColor'></path>
    171 <path d='M 64,48 L 64,56' fill='none' stroke='currentColor'></path>
    172 <polygon points='80.000000,48.000000 68.000000,42.400002 68.000000,53.599998' fill='currentColor' transform='rotate(90.000000, 64.000000, 48.000000)'></polygon>
    173 <path d='M 64,176 L 64,184' fill='none' stroke='currentColor'></path>
    174 <polygon points='80.000000,176.000000 68.000000,170.399994 68.000000,181.600006' fill='currentColor' transform='rotate(90.000000, 64.000000, 176.000000)'></polygon>
    175 <polygon points='248.000000,80.000000 236.000000,74.400002 236.000000,85.599998' fill='currentColor' transform='rotate(0.000000, 240.000000, 80.000000)'></polygon>
    176 <polygon points='248.000000,208.000000 236.000000,202.399994 236.000000,213.600006' fill='currentColor' transform='rotate(0.000000, 240.000000, 208.000000)'></polygon>
    177 <path d='M 80,16 A 16,16 0 0,0 64,32' fill='none' stroke='currentColor'></path>
    178 <path d='M 296,16 A 16,16 0 0,1 312,32' fill='none' stroke='currentColor'></path>
    179 <path d='M 80,144 A 16,16 0 0,0 64,160' fill='none' stroke='currentColor'></path>
    180 <path d='M 312,128 A 16,16 0 0,1 296,144' fill='none' stroke='currentColor'></path>
    181 <text text-anchor='middle' x='24' y='212' fill='currentColor' style='font-size:1em'>c</text>
    182 <text text-anchor='middle' x='32' y='84' fill='currentColor' style='font-size:1em'>t</text>
    183 <text text-anchor='middle' x='32' y='212' fill='currentColor' style='font-size:1em'>o</text>
    184 <text text-anchor='middle' x='40' y='84' fill='currentColor' style='font-size:1em'>o</text>
    185 <text text-anchor='middle' x='40' y='212' fill='currentColor' style='font-size:1em'>n</text>
    186 <text text-anchor='middle' x='48' y='84' fill='currentColor' style='font-size:1em'>k</text>
    187 <text text-anchor='middle' x='48' y='212' fill='currentColor' style='font-size:1em'>t</text>
    188 <text text-anchor='middle' x='56' y='84' fill='currentColor' style='font-size:1em'>e</text>
    189 <text text-anchor='middle' x='56' y='212' fill='currentColor' style='font-size:1em'>r</text>
    190 <text text-anchor='middle' x='64' y='84' fill='currentColor' style='font-size:1em'>n</text>
    191 <text text-anchor='middle' x='64' y='212' fill='currentColor' style='font-size:1em'>o</text>
    192 <text text-anchor='middle' x='72' y='84' fill='currentColor' style='font-size:1em'>i</text>
    193 <text text-anchor='middle' x='72' y='212' fill='currentColor' style='font-size:1em'>l</text>
    194 <text text-anchor='middle' x='80' y='84' fill='currentColor' style='font-size:1em'>z</text>
    195 <text text-anchor='middle' x='80' y='212' fill='currentColor' style='font-size:1em'>l</text>
    196 <text text-anchor='middle' x='88' y='84' fill='currentColor' style='font-size:1em'>e</text>
    197 <text text-anchor='middle' x='88' y='212' fill='currentColor' style='font-size:1em'>e</text>
    198 <text text-anchor='middle' x='96' y='84' fill='currentColor' style='font-size:1em'>r</text>
    199 <text text-anchor='middle' x='96' y='212' fill='currentColor' style='font-size:1em'>r</text>
    200 <text text-anchor='middle' x='128' y='132' fill='currentColor' style='font-size:1em'>c</text>
    201 <text text-anchor='middle' x='136' y='132' fill='currentColor' style='font-size:1em'>o</text>
    202 <text text-anchor='middle' x='144' y='4' fill='currentColor' style='font-size:1em'>m</text>
    203 <text text-anchor='middle' x='144' y='132' fill='currentColor' style='font-size:1em'>m</text>
    204 <text text-anchor='middle' x='144' y='196' fill='currentColor' style='font-size:1em'>c</text>
    205 <text text-anchor='middle' x='152' y='4' fill='currentColor' style='font-size:1em'>o</text>
    206 <text text-anchor='middle' x='152' y='132' fill='currentColor' style='font-size:1em'>m</text>
    207 <text text-anchor='middle' x='152' y='196' fill='currentColor' style='font-size:1em'>t</text>
    208 <text text-anchor='middle' x='160' y='4' fill='currentColor' style='font-size:1em'>r</text>
    209 <text text-anchor='middle' x='160' y='132' fill='currentColor' style='font-size:1em'>a</text>
    210 <text text-anchor='middle' x='160' y='196' fill='currentColor' style='font-size:1em'>r</text>
    211 <text text-anchor='middle' x='168' y='4' fill='currentColor' style='font-size:1em'>e</text>
    212 <text text-anchor='middle' x='168' y='68' fill='currentColor' style='font-size:1em'>t</text>
    213 <text text-anchor='middle' x='168' y='132' fill='currentColor' style='font-size:1em'>n</text>
    214 <text text-anchor='middle' x='168' y='196' fill='currentColor' style='font-size:1em'>l</text>
    215 <text text-anchor='middle' x='176' y='68' fill='currentColor' style='font-size:1em'>o</text>
    216 <text text-anchor='middle' x='176' y='132' fill='currentColor' style='font-size:1em'>d</text>
    217 <text text-anchor='middle' x='184' y='4' fill='currentColor' style='font-size:1em'>p</text>
    218 <text text-anchor='middle' x='184' y='68' fill='currentColor' style='font-size:1em'>k</text>
    219 <text text-anchor='middle' x='184' y='196' fill='currentColor' style='font-size:1em'>s</text>
    220 <text text-anchor='middle' x='192' y='4' fill='currentColor' style='font-size:1em'>a</text>
    221 <text text-anchor='middle' x='192' y='68' fill='currentColor' style='font-size:1em'>e</text>
    222 <text text-anchor='middle' x='192' y='132' fill='currentColor' style='font-size:1em'>w</text>
    223 <text text-anchor='middle' x='192' y='196' fill='currentColor' style='font-size:1em'>i</text>
    224 <text text-anchor='middle' x='200' y='4' fill='currentColor' style='font-size:1em'>r</text>
    225 <text text-anchor='middle' x='200' y='68' fill='currentColor' style='font-size:1em'>n</text>
    226 <text text-anchor='middle' x='200' y='132' fill='currentColor' style='font-size:1em'>/</text>
    227 <text text-anchor='middle' x='200' y='196' fill='currentColor' style='font-size:1em'>g</text>
    228 <text text-anchor='middle' x='208' y='4' fill='currentColor' style='font-size:1em'>a</text>
    229 <text text-anchor='middle' x='208' y='196' fill='currentColor' style='font-size:1em'>n</text>
    230 <text text-anchor='middle' x='216' y='4' fill='currentColor' style='font-size:1em'>m</text>
    231 <text text-anchor='middle' x='216' y='132' fill='currentColor' style='font-size:1em'>p</text>
    232 <text text-anchor='middle' x='216' y='196' fill='currentColor' style='font-size:1em'>a</text>
    233 <text text-anchor='middle' x='224' y='4' fill='currentColor' style='font-size:1em'>s</text>
    234 <text text-anchor='middle' x='224' y='132' fill='currentColor' style='font-size:1em'>a</text>
    235 <text text-anchor='middle' x='224' y='196' fill='currentColor' style='font-size:1em'>l</text>
    236 <text text-anchor='middle' x='232' y='4' fill='currentColor' style='font-size:1em'>?</text>
    237 <text text-anchor='middle' x='232' y='132' fill='currentColor' style='font-size:1em'>r</text>
    238 <text text-anchor='middle' x='240' y='132' fill='currentColor' style='font-size:1em'>a</text>
    239 <text text-anchor='middle' x='248' y='132' fill='currentColor' style='font-size:1em'>m</text>
    240 <text text-anchor='middle' x='256' y='132' fill='currentColor' style='font-size:1em'>s</text>
    241 <text text-anchor='middle' x='272' y='84' fill='currentColor' style='font-size:1em'>a</text>
    242 <text text-anchor='middle' x='272' y='212' fill='currentColor' style='font-size:1em'>l</text>
    243 <text text-anchor='middle' x='280' y='84' fill='currentColor' style='font-size:1em'>c</text>
    244 <text text-anchor='middle' x='280' y='212' fill='currentColor' style='font-size:1em'>a</text>
    245 <text text-anchor='middle' x='288' y='84' fill='currentColor' style='font-size:1em'>c</text>
    246 <text text-anchor='middle' x='288' y='212' fill='currentColor' style='font-size:1em'>s</text>
    247 <text text-anchor='middle' x='296' y='84' fill='currentColor' style='font-size:1em'>u</text>
    248 <text text-anchor='middle' x='296' y='212' fill='currentColor' style='font-size:1em'>e</text>
    249 <text text-anchor='middle' x='304' y='84' fill='currentColor' style='font-size:1em'>m</text>
    250 <text text-anchor='middle' x='304' y='212' fill='currentColor' style='font-size:1em'>r</text>
    251 <text text-anchor='middle' x='312' y='84' fill='currentColor' style='font-size:1em'>u</text>
    252 <text text-anchor='middle' x='320' y='84' fill='currentColor' style='font-size:1em'>l</text>
    253 <text text-anchor='middle' x='320' y='212' fill='currentColor' style='font-size:1em'>g</text>
    254 <text text-anchor='middle' x='328' y='84' fill='currentColor' style='font-size:1em'>a</text>
    255 <text text-anchor='middle' x='328' y='212' fill='currentColor' style='font-size:1em'>a</text>
    256 <text text-anchor='middle' x='336' y='84' fill='currentColor' style='font-size:1em'>t</text>
    257 <text text-anchor='middle' x='336' y='212' fill='currentColor' style='font-size:1em'>l</text>
    258 <text text-anchor='middle' x='344' y='84' fill='currentColor' style='font-size:1em'>o</text>
    259 <text text-anchor='middle' x='344' y='212' fill='currentColor' style='font-size:1em'>v</text>
    260 <text text-anchor='middle' x='352' y='84' fill='currentColor' style='font-size:1em'>r</text>
    261 <text text-anchor='middle' x='352' y='212' fill='currentColor' style='font-size:1em'>o</text>
    262 </g>
    263 
    264     </svg>
    265   
    266 </div>
    267 <h2 id="source-code">Source Code</h2>
    268 <p>The source code for the G-code interpreter can be found
    269 <a href="https://git.christianermann.dev/gcode-interpreter">here</a>. You may have issues
    270 accessing the link if your ISP doesn&rsquo;t support IPv6 yet.</p>
    271 <p>Also, the names in the code are different than what&rsquo;s shown in the diagram, so
    272 here&rsquo;s the mapping to help out with understanding the code:</p>
    273 <table>
    274   <thead>
    275       <tr>
    276           <th style="text-align: left">Diagram</th>
    277           <th style="text-align: left">Source File</th>
    278       </tr>
    279   </thead>
    280   <tbody>
    281       <tr>
    282           <td style="text-align: left">tokenizer</td>
    283           <td style="text-align: left">token.c</td>
    284       </tr>
    285       <tr>
    286           <td style="text-align: left">accumulator</td>
    287           <td style="text-align: left">command.c</td>
    288       </tr>
    289       <tr>
    290           <td style="text-align: left">controller</td>
    291           <td style="text-align: left">machine.c</td>
    292       </tr>
    293   </tbody>
    294 </table>
    295 <p>I&rsquo;ll try to keep this updated if the mapping changes as I continue to work on
    296 this project.</p>
    297 
    298 </content>
    299 <p>
    300   
    301     <a class="blog-tags" href="/tags/g-code/">#g-code</a>
    302   
    303     <a class="blog-tags" href="/tags/lasers/">#lasers</a>
    304   
    305     <a class="blog-tags" href="/tags/c/">#c</a>
    306   
    307 </p>
    308 
    309 
    310   <p>
    311     <a href='mailto:christianermann@gmail.com?subject=Reply%20to%20"Writing%20a%20G-code%20Interpreter"'>
    312       Reply to this post by email ↪
    313     </a>
    314   </p>
    315 
    316 
    317 
    318   </main>
    319   <footer><footer>
    320     
    321         <a href="https://git.electrosoup.com">Code</a>.
    322     
    323         <a href="https://christianermann.dev">Resume</a>.
    324     
    325     <a href='https://electrosoup.com/index.xml'>RSS</a>.
    326     <p>Copyright &copy; 2026 Christian Ermann. | Made with <a href="https://github.com/clente/hugo-bearcub">Bear Cub</a></p>
    327 </footer>
    328 </footer>
    329 
    330     
    331 </body>
    332 
    333 </html>