GCode Tutorial: Building Circles
In the previous GCode tutorial, we covered the creation of GCodes using Python, and introduced the basic structure of a script that uses GCode objects as a tool for creating GCode files. In this tutorial, we’ll move from Para01.py to Para02.py in the first GCode Script Pack. In this file, we’ll start using the math library to create circles from straight GCodes.
First, a bit of bookkeeping. Using trigonometric functions means getting back floating-point values with lots of decimal places of precision, but in a GCode file it’s usually a bad idea to have more than a few decimal places of precision, since it bloats serial operations and finer than 10um precision isn’t attainable on most current hardware anyway. So we need a rounding function to truncate these floating-point numbers. My friend Ryan Vilbrandt was kind enough to lend me his:

This is called from the GCode string output function later to make sure that the GCodes sent to the file are tidy.
In the next step, we’ll use the parametric formula of a circle:
X = cos (T)
Y = sin (T)
Inside the Zsteps loop from the previous example, we put a loop for Tsteps equal to two pi radians (360 degrees) minus a little bit. Here we do 62 steps of 0.1 radian each:

Smaller steps will make for shorter GCodes and a more perfectly circular curve, and larger ones will even produce visibly flat sides to the cylinder. This script can be used to make shapes like octagons and pentagons by reducing the number of steps to the number of desired sides. Additionally, by entering an integer multiplier into one or both of the trigonometric function arguments, this script can generate lisajous patterns.
There are a lot of shapes that can be build based off even this simple example– scripting GCodes directly isn’t going to work for every application, but it’s a great way to build certain classes of objects, and doesn’t require learning how to use a 3D modeling tool.


























