electrosoup.com

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

index.xml (15518B)


      1 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
      2 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
      3   <channel>
      4     <title>Lasers on Electrosoup</title>
      5     <link>https://electrosoup.com/tags/lasers/</link>
      6     <description>Recent content in Lasers on Electrosoup</description>
      7     <generator>Hugo -- gohugo.io</generator>
      8     <language>en-US</language>
      9     <managingEditor>christianermann@gmail.com (Christian Ermann)</managingEditor>
     10     <webMaster>christianermann@gmail.com (Christian Ermann)</webMaster>
     11     <lastBuildDate>Wed, 23 Oct 2024 16:29:40 -0700</lastBuildDate>
     12     <atom:link href="https://electrosoup.com/tags/lasers/index.xml" rel="self" type="application/rss+xml" />
     13     <item>
     14       <title>Writing a G-code Interpreter</title>
     15       <link>https://electrosoup.com/posts/gcode-interpreter/</link>
     16       <pubDate>Wed, 23 Oct 2024 16:29:40 -0700</pubDate><author>christianermann@gmail.com (Christian Ermann)</author>
     17       <guid>https://electrosoup.com/posts/gcode-interpreter/</guid>
     18       <description>&lt;p&gt;I&amp;rsquo;m currently working on a control system for a laser galvanometer (galvo) that&#xA;I want to use in a stereolithography (SLA) resin printer. I don&amp;rsquo;t want to write&#xA;a custom slicer, as I feel like model slicing can be incredibly complicated and&#xA;existing free software (Cura, Slic3r, etc.) has many advanced features I don&amp;rsquo;t&#xA;want to reimplement. However, this means my control system needs to understand&#xA;G-code, the language that these slicers output.&lt;/p&gt;</description>
     19       <content:encoded><![CDATA[<p>I&rsquo;m currently working on a control system for a laser galvanometer (galvo) that
     20 I want to use in a stereolithography (SLA) resin printer. I don&rsquo;t want to write
     21 a custom slicer, as I feel like model slicing can be incredibly complicated and
     22 existing free software (Cura, Slic3r, etc.) has many advanced features I don&rsquo;t
     23 want to reimplement. However, this means my control system needs to understand
     24 G-code, the language that these slicers output.</p>
     25 <p>As stated on the <a href="https://reprap.org/wiki/G-code">reprap wiki</a>, &ldquo;G-code is a
     26 list of fields that are separated by white spaces or line breaks. A field can
     27 be interpreted as a command, parameter, or for any other special purpose.&rdquo; For
     28 now, we&rsquo;ll ignore any special purposes. Here is an example of G-code that
     29 describes a movement of the tool/hotend/laser to a specific location:</p>
     30 
     31 
     32 
     33 
     34 
     35 <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>
     36 <pre><code>1. Retrieve the next white-space delimited token.
     37 2. Accumulate tokens until two separate command tokens are found.
     38 </code></pre>
     39 <p>This second step is important because we can&rsquo;t begin executing a command until
     40 we&rsquo;ve found all of its parameters and we don&rsquo;t know if we&rsquo;ve found all of its
     41 parameters until we find the next command.</p>
     42 <p>Once a command and all of its parameters have been parsed, it is passed to the
     43 controller which updates the state of the machine. For each command that the
     44 controller receives, it has to update its internal state, and in the case of
     45 physical movement commands, send the proper signal to the laser galvo. Most
     46 physical movement commands utilize interpolation so a timed control loop is
     47 utilized to ensure the laser follows as close to the intended path as possible.</p>
     48 <h2 id="system-diagram">System Diagram</h2>
     49 
     50 
     51 
     52 <div class="goat svg-container ">
     53   
     54     <svg
     55       xmlns="http://www.w3.org/2000/svg"
     56       font-family="Menlo,Lucida Console,monospace"
     57       
     58         viewBox="0 0 384 249"
     59       >
     60       <g transform='translate(8,16)'>
     61 <path d='M 80,16 L 296,16' fill='none' stroke='currentColor'></path>
     62 <path d='M 16,64 L 112,64' fill='none' stroke='currentColor'></path>
     63 <path d='M 256,64 L 368,64' fill='none' stroke='currentColor'></path>
     64 <path d='M 128,80 L 240,80' fill='none' stroke='currentColor'></path>
     65 <path d='M 16,96 L 112,96' fill='none' stroke='currentColor'></path>
     66 <path d='M 256,96 L 368,96' fill='none' stroke='currentColor'></path>
     67 <path d='M 80,144 L 296,144' fill='none' stroke='currentColor'></path>
     68 <path d='M 0,192 L 120,192' fill='none' stroke='currentColor'></path>
     69 <path d='M 256,192 L 368,192' fill='none' stroke='currentColor'></path>
     70 <path d='M 136,208 L 240,208' fill='none' stroke='currentColor'></path>
     71 <path d='M 0,224 L 120,224' fill='none' stroke='currentColor'></path>
     72 <path d='M 256,224 L 368,224' fill='none' stroke='currentColor'></path>
     73 <path d='M 0,192 L 0,224' fill='none' stroke='currentColor'></path>
     74 <path d='M 16,64 L 16,96' fill='none' stroke='currentColor'></path>
     75 <path d='M 64,32 L 64,48' fill='none' stroke='currentColor'></path>
     76 <path d='M 64,160 L 64,176' fill='none' stroke='currentColor'></path>
     77 <path d='M 112,64 L 112,96' fill='none' stroke='currentColor'></path>
     78 <path d='M 120,192 L 120,224' fill='none' stroke='currentColor'></path>
     79 <path d='M 256,64 L 256,96' fill='none' stroke='currentColor'></path>
     80 <path d='M 256,192 L 256,224' fill='none' stroke='currentColor'></path>
     81 <path d='M 312,32 L 312,48' fill='none' stroke='currentColor'></path>
     82 <path d='M 312,112 L 312,128' fill='none' stroke='currentColor'></path>
     83 <path d='M 368,64 L 368,96' fill='none' stroke='currentColor'></path>
     84 <path d='M 368,192 L 368,224' fill='none' stroke='currentColor'></path>
     85 <path d='M 312,48 L 312,56' fill='none' stroke='currentColor'></path>
     86 <path d='M 312,104 L 312,112' fill='none' stroke='currentColor'></path>
     87 <path d='M 64,48 L 64,56' fill='none' stroke='currentColor'></path>
     88 <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>
     89 <path d='M 64,176 L 64,184' fill='none' stroke='currentColor'></path>
     90 <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>
     91 <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>
     92 <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>
     93 <path d='M 80,16 A 16,16 0 0,0 64,32' fill='none' stroke='currentColor'></path>
     94 <path d='M 296,16 A 16,16 0 0,1 312,32' fill='none' stroke='currentColor'></path>
     95 <path d='M 80,144 A 16,16 0 0,0 64,160' fill='none' stroke='currentColor'></path>
     96 <path d='M 312,128 A 16,16 0 0,1 296,144' fill='none' stroke='currentColor'></path>
     97 <text text-anchor='middle' x='24' y='212' fill='currentColor' style='font-size:1em'>c</text>
     98 <text text-anchor='middle' x='32' y='84' fill='currentColor' style='font-size:1em'>t</text>
     99 <text text-anchor='middle' x='32' y='212' fill='currentColor' style='font-size:1em'>o</text>
    100 <text text-anchor='middle' x='40' y='84' fill='currentColor' style='font-size:1em'>o</text>
    101 <text text-anchor='middle' x='40' y='212' fill='currentColor' style='font-size:1em'>n</text>
    102 <text text-anchor='middle' x='48' y='84' fill='currentColor' style='font-size:1em'>k</text>
    103 <text text-anchor='middle' x='48' y='212' fill='currentColor' style='font-size:1em'>t</text>
    104 <text text-anchor='middle' x='56' y='84' fill='currentColor' style='font-size:1em'>e</text>
    105 <text text-anchor='middle' x='56' y='212' fill='currentColor' style='font-size:1em'>r</text>
    106 <text text-anchor='middle' x='64' y='84' fill='currentColor' style='font-size:1em'>n</text>
    107 <text text-anchor='middle' x='64' y='212' fill='currentColor' style='font-size:1em'>o</text>
    108 <text text-anchor='middle' x='72' y='84' fill='currentColor' style='font-size:1em'>i</text>
    109 <text text-anchor='middle' x='72' y='212' fill='currentColor' style='font-size:1em'>l</text>
    110 <text text-anchor='middle' x='80' y='84' fill='currentColor' style='font-size:1em'>z</text>
    111 <text text-anchor='middle' x='80' y='212' fill='currentColor' style='font-size:1em'>l</text>
    112 <text text-anchor='middle' x='88' y='84' fill='currentColor' style='font-size:1em'>e</text>
    113 <text text-anchor='middle' x='88' y='212' fill='currentColor' style='font-size:1em'>e</text>
    114 <text text-anchor='middle' x='96' y='84' fill='currentColor' style='font-size:1em'>r</text>
    115 <text text-anchor='middle' x='96' y='212' fill='currentColor' style='font-size:1em'>r</text>
    116 <text text-anchor='middle' x='128' y='132' fill='currentColor' style='font-size:1em'>c</text>
    117 <text text-anchor='middle' x='136' y='132' fill='currentColor' style='font-size:1em'>o</text>
    118 <text text-anchor='middle' x='144' y='4' fill='currentColor' style='font-size:1em'>m</text>
    119 <text text-anchor='middle' x='144' y='132' fill='currentColor' style='font-size:1em'>m</text>
    120 <text text-anchor='middle' x='144' y='196' fill='currentColor' style='font-size:1em'>c</text>
    121 <text text-anchor='middle' x='152' y='4' fill='currentColor' style='font-size:1em'>o</text>
    122 <text text-anchor='middle' x='152' y='132' fill='currentColor' style='font-size:1em'>m</text>
    123 <text text-anchor='middle' x='152' y='196' fill='currentColor' style='font-size:1em'>t</text>
    124 <text text-anchor='middle' x='160' y='4' fill='currentColor' style='font-size:1em'>r</text>
    125 <text text-anchor='middle' x='160' y='132' fill='currentColor' style='font-size:1em'>a</text>
    126 <text text-anchor='middle' x='160' y='196' fill='currentColor' style='font-size:1em'>r</text>
    127 <text text-anchor='middle' x='168' y='4' fill='currentColor' style='font-size:1em'>e</text>
    128 <text text-anchor='middle' x='168' y='68' fill='currentColor' style='font-size:1em'>t</text>
    129 <text text-anchor='middle' x='168' y='132' fill='currentColor' style='font-size:1em'>n</text>
    130 <text text-anchor='middle' x='168' y='196' fill='currentColor' style='font-size:1em'>l</text>
    131 <text text-anchor='middle' x='176' y='68' fill='currentColor' style='font-size:1em'>o</text>
    132 <text text-anchor='middle' x='176' y='132' fill='currentColor' style='font-size:1em'>d</text>
    133 <text text-anchor='middle' x='184' y='4' fill='currentColor' style='font-size:1em'>p</text>
    134 <text text-anchor='middle' x='184' y='68' fill='currentColor' style='font-size:1em'>k</text>
    135 <text text-anchor='middle' x='184' y='196' fill='currentColor' style='font-size:1em'>s</text>
    136 <text text-anchor='middle' x='192' y='4' fill='currentColor' style='font-size:1em'>a</text>
    137 <text text-anchor='middle' x='192' y='68' fill='currentColor' style='font-size:1em'>e</text>
    138 <text text-anchor='middle' x='192' y='132' fill='currentColor' style='font-size:1em'>w</text>
    139 <text text-anchor='middle' x='192' y='196' fill='currentColor' style='font-size:1em'>i</text>
    140 <text text-anchor='middle' x='200' y='4' fill='currentColor' style='font-size:1em'>r</text>
    141 <text text-anchor='middle' x='200' y='68' fill='currentColor' style='font-size:1em'>n</text>
    142 <text text-anchor='middle' x='200' y='132' fill='currentColor' style='font-size:1em'>/</text>
    143 <text text-anchor='middle' x='200' y='196' fill='currentColor' style='font-size:1em'>g</text>
    144 <text text-anchor='middle' x='208' y='4' fill='currentColor' style='font-size:1em'>a</text>
    145 <text text-anchor='middle' x='208' y='196' fill='currentColor' style='font-size:1em'>n</text>
    146 <text text-anchor='middle' x='216' y='4' fill='currentColor' style='font-size:1em'>m</text>
    147 <text text-anchor='middle' x='216' y='132' fill='currentColor' style='font-size:1em'>p</text>
    148 <text text-anchor='middle' x='216' y='196' fill='currentColor' style='font-size:1em'>a</text>
    149 <text text-anchor='middle' x='224' y='4' fill='currentColor' style='font-size:1em'>s</text>
    150 <text text-anchor='middle' x='224' y='132' fill='currentColor' style='font-size:1em'>a</text>
    151 <text text-anchor='middle' x='224' y='196' fill='currentColor' style='font-size:1em'>l</text>
    152 <text text-anchor='middle' x='232' y='4' fill='currentColor' style='font-size:1em'>?</text>
    153 <text text-anchor='middle' x='232' y='132' fill='currentColor' style='font-size:1em'>r</text>
    154 <text text-anchor='middle' x='240' y='132' fill='currentColor' style='font-size:1em'>a</text>
    155 <text text-anchor='middle' x='248' y='132' fill='currentColor' style='font-size:1em'>m</text>
    156 <text text-anchor='middle' x='256' y='132' fill='currentColor' style='font-size:1em'>s</text>
    157 <text text-anchor='middle' x='272' y='84' fill='currentColor' style='font-size:1em'>a</text>
    158 <text text-anchor='middle' x='272' y='212' fill='currentColor' style='font-size:1em'>l</text>
    159 <text text-anchor='middle' x='280' y='84' fill='currentColor' style='font-size:1em'>c</text>
    160 <text text-anchor='middle' x='280' y='212' fill='currentColor' style='font-size:1em'>a</text>
    161 <text text-anchor='middle' x='288' y='84' fill='currentColor' style='font-size:1em'>c</text>
    162 <text text-anchor='middle' x='288' y='212' fill='currentColor' style='font-size:1em'>s</text>
    163 <text text-anchor='middle' x='296' y='84' fill='currentColor' style='font-size:1em'>u</text>
    164 <text text-anchor='middle' x='296' y='212' fill='currentColor' style='font-size:1em'>e</text>
    165 <text text-anchor='middle' x='304' y='84' fill='currentColor' style='font-size:1em'>m</text>
    166 <text text-anchor='middle' x='304' y='212' fill='currentColor' style='font-size:1em'>r</text>
    167 <text text-anchor='middle' x='312' y='84' fill='currentColor' style='font-size:1em'>u</text>
    168 <text text-anchor='middle' x='320' y='84' fill='currentColor' style='font-size:1em'>l</text>
    169 <text text-anchor='middle' x='320' y='212' fill='currentColor' style='font-size:1em'>g</text>
    170 <text text-anchor='middle' x='328' y='84' fill='currentColor' style='font-size:1em'>a</text>
    171 <text text-anchor='middle' x='328' y='212' fill='currentColor' style='font-size:1em'>a</text>
    172 <text text-anchor='middle' x='336' y='84' fill='currentColor' style='font-size:1em'>t</text>
    173 <text text-anchor='middle' x='336' y='212' fill='currentColor' style='font-size:1em'>l</text>
    174 <text text-anchor='middle' x='344' y='84' fill='currentColor' style='font-size:1em'>o</text>
    175 <text text-anchor='middle' x='344' y='212' fill='currentColor' style='font-size:1em'>v</text>
    176 <text text-anchor='middle' x='352' y='84' fill='currentColor' style='font-size:1em'>r</text>
    177 <text text-anchor='middle' x='352' y='212' fill='currentColor' style='font-size:1em'>o</text>
    178 </g>
    179 
    180     </svg>
    181   
    182 </div>
    183 <h2 id="source-code">Source Code</h2>
    184 <p>The source code for the G-code interpreter can be found
    185 <a href="https://git.christianermann.dev/gcode-interpreter">here</a>. You may have issues
    186 accessing the link if your ISP doesn&rsquo;t support IPv6 yet.</p>
    187 <p>Also, the names in the code are different than what&rsquo;s shown in the diagram, so
    188 here&rsquo;s the mapping to help out with understanding the code:</p>
    189 <table>
    190   <thead>
    191       <tr>
    192           <th style="text-align: left">Diagram</th>
    193           <th style="text-align: left">Source File</th>
    194       </tr>
    195   </thead>
    196   <tbody>
    197       <tr>
    198           <td style="text-align: left">tokenizer</td>
    199           <td style="text-align: left">token.c</td>
    200       </tr>
    201       <tr>
    202           <td style="text-align: left">accumulator</td>
    203           <td style="text-align: left">command.c</td>
    204       </tr>
    205       <tr>
    206           <td style="text-align: left">controller</td>
    207           <td style="text-align: left">machine.c</td>
    208       </tr>
    209   </tbody>
    210 </table>
    211 <p>I&rsquo;ll try to keep this updated if the mapping changes as I continue to work on
    212 this project.</p>
    213 ]]></content:encoded>
    214     </item>
    215   </channel>
    216 </rss>