For the Professional Maker: Python for physical computing applications
July 20, 2018
Interestingly, the basics of router ?G-code? isn?t that complicated, simply telling your cutting head to move from one place to another. In fact, it?s possible?and very tedious?to write it by hand.
Way back in 2012, I was becoming more and more involved with making various projects in my garage. I even had a small CNC router, which allowed me to cut items out automatically using code generated CAD/CAM software. Interestingly, the basics of router “G-code” isn’t that complicated, simply telling your cutting head to move from one place to another. In fact, it’s possible—and very tedious—to write it by hand.
To generate this type of code automatically in the way I wanted, I turned to the Python programming language, which meant learning it from scratch. My method of choice was a book entitled Learn Python the Hard Way by Zed A. Shaw. It was free for version 2.7 at the time, and the tools to run were already available on the Linux computer that I was using. The book takes a very hands-on approach, giving you short (at first) lessons to complete, a format that worked very well for me.
Now the course has been updated to Python 3, and is no longer free. At about $30, however, it’s still a bargain compared to most textbooks. Authors certainly deserve to get paid for their work, and if you’re going to spend the many hours required to learn a programming language, this would seem to be a worthy investment. Notably, per the book’s preface, Windows 10 is fully supported in the newest version, helpful for readers that don’t run Mac or Linux-based systems.
[Tiger Paw recreated with my Python “Pixel Machining” routine]
As for my experience, I admittedly didn’t get through the entire book, but got far enough to write a very basic CAM program. While that sounds impressive, the program was very basic indeed, using the Python Image Library (PIL) to translate an image into points to be plunge milled with my CNC router. I even modified it later to implement servo control for a rather strange glowing laser setup using a board called the pyMCU. Now retired, the pyMCU made a nice pass-through for physical I/O, allowing you to extend the code onto your computer for real-world use.
At the time, the pyMCU seemed like a very innovative product, but you might note that today Raspberry Pi boards come with Python pre-installed, and can do the same sort of physical computing operations. In fact, since this $5 to $35 device acts as a full computer itself, you don’t have to occupy your laptop or other device to work with it, just a keyboard, mouse, and monitor. Or just a WiFi connection if you prefer to connect remotely. If that doesn’t quite work for you, there are also Python implementations meant to run on a microcontroller, in a similar way to how Arduino boards are programmed in a version of C. If this sounds interesting, be sure to give CircuitPython or MicroPython a look.
[Circuit Python. Image via Adafruit.]
Between the interesting implementations for physical computing, as well as the more general computing applications not covered here, Python would be a great language to learn. From my experience with 2.7, it’s also quite easy to pick up, at least to the level where you can make useful applications for yourself!