A simple 2D CNC controller code with Phidgets stepper motor controller
The hobbiest and DIYer which builds his or her own CNC milling machine often gets stuck when it comes to the software and and electronics needed to actually move it. And while there are hundreds of examples on sites like instructables.com (like this, and this ) of how to build the machine itself, there is often very little in the way of how to give the machine any real functionality.
Having built a 2D CNC machine which is actually a pen lotter/laser cutter, and written code for it in open source packages R and Python I thought I would share it.
First of all... the hardware you need is:
- CNC machine built with 6 wire stepper motors (sorry, up to you to figure this part out.
- Phidgets stepper motor controller ~$70 - some google shopping results.
- Connect your phidgets board and stepper motors (see this eHow article for figuring out which wires go where)
And the software that you'll need is:
- The Gimp Image Editor (or any image editor that produces .jpg files)
- R statistical processing language
- Python
- The supplied libraries with the phidgets board
Finally you'll need these scripts that I've written: CNC-stepper-coords (Python file) pic2coordpaths (R script) p2cFuncs (R script) dataproc (R script) : you may need to change some of the relative pathing.
1. Start with a drawing of the desired object in Gimp... keep it in black and white no aliasing for best results.
2. Size the drawing such that it is true to life size (i.e. 1 inch in picture = 1 inch in real life). Set the pixels/inch such that the desired resolution is achieved.
3. Export the image as a .jpg
3. Now open up the R script "pic2coordpaths.r" and set the variables according to your setup. The variable should be fairly clear, and the most important of which is the "steps2steps" variable. This variable indicates how many steps = 1 pixel in the picture.
The math is as follows: given that 1 px = 1/100 inch and 400 steps per motor RPM, and a threaded rod with 18 threads per inch.
steps2steps = 400 steps/Rev * 18 Rev / inch * .01 inch / px= 72 steps / px
4. This script will process as many pictures as much the string of the name that you give it in line 40.
5. Startup up R, install package "biOps" (if it doesn't exist already on your system) and then run "source('pics2coordpath.r')"
6. The resulting .csv files are sets of coordinates (in #of steps) for the outline of the drawing (automatically detected).
7. Now edit CNC-stepper-coords.py line 117 for your .csv coordinates file.
8. Plug in the phidgets board via USB into your computer, launch Python and run the script... your CNC will move accordingly!


