I've been experimenting with adding a few fun features to my CNC setup. I settled on a plan to add an arduino-based board to provide the following:
I used a Pololu A-Star 32U4 Micro, connected to the Raspberry Pi using USB. This creates a new serial port on the Pi that I can use to communicate between the two controllers. I then leave the Pi powered on all the time, and only switch on/off the tinyg when not in use.
It is still a work in progress, but it has been working nicely for a few weeks and I'm definitely liking it. Here is a quick video showing its current state:
Disclaimer: If you build this, please be careful with the automatic spindle control. When changing bits you should remove power to the trim router just as a precaution. Also, make sure the gcode you send has the proper M3 (spindle on) and M5 (spindle off) commands. I've noticed that most CAM software adds these, but some (JSCut in particular) seem not to so you have to add them manually. Or, you could type them in the serial console in Chilipeppr to manually power up/down as needed.
The light is simply a small portion (two LEDs) of an Addressable RGB 30-LED Strip, although you could use a higher-density version of the strip to have more LEDs. I soldered a three-conductor stranded cable to the short LED strip and ran it up through an unused mounting hole on the bottom of the spindle plate.
I took advantage of a threaded hole on the trim router base to secure the cable to protect against tugging on the soldered connections:
The arduino is connected as follows:
There are two relays - one for the spindle, and one for the dust vacuum. The arduino senses when the tinyg has power by connecting to the 3.3v output, and knows when the spindle is to turn on from the "Spin" output. Power for the arduino and lights comes from the USB connection to the Pi.
Note that I did things a little different for my build: I only have one relay for the spindle, and the vacuum is controlled using a wireless chip not shown above to communicate with a vacuum system I previously built. I have tried to leave those details and code out of what I post here to simplify things, but please note that means I have not tested it exactly as described here.
The code (still a work in progress) can be downloaded from git at:
https://github.com/treib/cnc_light_astar_nowireless
I now leave the Pi powered up all the time, and only have the CNC (tinyg and spindle) on the power strip that I switch on and off. I run a python script on the Pi to monitor the serial connection to the Arduino and start/stop the Serial Port JSON Server based on whether the tinyg is powered up. This allows it to start fresh each time I power up the CNC by the power strip.
I started by downloading the current version of serial-port-json-server to the pi's home directory, as described under Raspberry Pi Config, and I created a symbolic link as follows:
ln -s serial-port-json-server_linux_arm/serial-port-json-server serial-port-json-server
Here is the code for the current version of the python script. Store this at path: /home/pi/cncbot
A quick way to download the above file to the pi is to run the following (all one line):
curl -O https://gist.githubusercontent.com/treib/8f14c49587fbd41b2d39/raw/28274f73c76a7bdb2da7b46471dd80170004706e/cncbot
To make the script executable:
sudo chmod +x /home/pi/cncbot
To install this to run when the Pi boots, I did the usual init.d stuff:
First, create a file /etc/init.d/cncbot with the following contents:
As a shortcut, I simply did the following to download the above to the home directory (on one line):
curl -O https://gist.githubusercontent.com/treib/c1083095e8a52f7953f2/raw/dc4d319756807b70a40a1bb9afa8f0e9c506d2e3/cncbot-initd
And then I copied it to the right location:
sudo cp cncbot-initd /etc/init.d/cncbot
Now make the script executable:
sudo chmod +x /etc/init.d/cncbot
And finally, to set it to run at startup, type:
sudo update-rc.d cncbot defaults
You could reboot now to see if it works by typing:
sudo shutdown -r now
Or, you can run it directly from the command line with:
sudo /etc/init.d/cncbot start
If for some reason in the future you don't want it to start at boot, you can remove it with:
sudo update-rc.d -f
cncbot remove
Note that if you already had Serial Port JSON Server starting at boot, turn that off first so that we won't end up with two running.