I hate using the control panel of my 3D printer to tweak its settings. Sometimes when I need to preheat the hotend or the bed, I have to navigate through a bunch of menus using an encoder that is too outdated in this age of voice control and AI.
This project will let you control your 3D printer using a Raspberry Pi and the power of your voice!
It also has some handy features such as active power monitoring, and even shuts the printer down when it exceeds a preset current limit. (specially if there's a nasty electrical short that can lead to fire)
MATRIX VoiceMATRIX Voice is a development board for building sound driven behaviors and interfaces. You might already know that the RPi lacks a vital component needed for voice control..a Mircophone! This development board has an array of 8 audio sensors that'll pick up your voice from any direction. It also has an Spartan 6 FPGA that can be used for advanced voice algorithms.
Check out the matrix voice here.
The Snips AI Voice Platform is a decentralized, private-by-design voice assistant. If you've used services like Amazon Alexa and Google Home, you might know that voice is processed in the cloud. However, Snips processes the user’s voice directly on-device. Anyone can easily build CUSTOM voice assistants in a matter of minutes using their easy to use web interface. You can provide your own training data, and then install the assistant on your hardware and run it completely on the edge.
This video will give you a quick intro to Snips.
HardwareAside from the MATRIX Voice board, I will be using my trusty Raspberry Pi 3 Model B. I also purchased a cheap modbus enabled power meter from ebay which would allow me to monitor the input voltage, AC power and current consumption of my 3D printer.
You will also need a RS485 to TTL converter, a Relay Module and some wires.
Installing Libraries for Matrix VoiceDetailed instructions can be found here. https://docs.snips.ai/articles/raspberrypi/matrix
Run the following commands in your Raspberry Pi's terminal to add the MATRIX repository & key and update your repository packages.
curl https://apt.matrix.one/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.matrix.one/raspbian $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/matrixlabs.list
sudo apt-get update
sudo apt-get upgrade
Note :It is essential that you do the apt-get upgrade prior to installing the matrixio-kernel-modules. I skipped this the first time and ran into some issues later.
Reboot your device.
sudo reboot
Install the MATRIX Kernel modules. Aside from a few other changes, this allows the microphones on your MATRIX device to register as an ALSA microphone on your Raspberry Pi.
sudo apt install matrixio-kernel-modules
Reboot your device again
sudo reboot
Installing SnipsNote - A more detailed set of instructions can be found at : https://docs.snips.ai/getting-started/quick-start-raspberry-pi
Prior to installing snips, you will have to install Raspbian on your Raspberry Pi. You can follow the instructions on this link to install Raspbian on your board.
While Raspbian is being set up, be sure set up a Snips console account at https://console.snips.ai and install Git and NodeJS on your computer. You will need the Node Package Manger (NPM) to install the Sam Command Line Interface. Sam allows you to setup snips on your raspberry pi using the terminal of your own pc. However, you will need a remote access tool like puTTY later on. (if you're using Windows)
Follow the instructions on this link to setup Node and the NPM on your windows PC. (For Linux users : https://tecadmin.net/install-latest-nodejs-npm-on-ubuntu/)Once that's done, you can use NPM to install Sam on your computer.
npm install -g snips-sam
Make sure that both your PC and Raspberry Pi is connected to the same WiFi network, open up command prompt and run
sam devices
to view the list of available devices. However, my raspberry pi didn't show up when I tried this, so I ended up using the IP address of my Pi to connect to it.
Executing the following command will install all the repositories required to run Snips, and the voice platform itself on your Pi.
sam init
Once it's done, don't forget to login to your snips console account using
sam login
With Snips properly installed, the next step is to edit the snips.toml
file for configuring the mics.
From a terminal session on your Raspberry Pi, run the following:
sudo nano /etc/snips.toml
Scroll down to where you see [snips-audio-server]
and replace
# mike = "Built-in Microphone"
with the following:
mike = "MATRIXIO-SOUND: - (hw:2,0)"
This video explains how to create custom assistants using the Snips console.
I created two separate apps for the two main functionalities of this project.
Creating an app is very straightforward. For every app you have intents, which are like specific actions you want the voice assistant to recognize. As an example, my 3dprinter_basicControls app has 3 intents, which are used to set the temperature, move the hot end and home the printer.
Lets check out one of these intents in detail.
(All the info about creating intents can be found here. https://docs.snips.ai/articles/console/actions/set-intents)
On the right side we have the training examples. You can provide different ways the same command can be issued to the assistant. The highlighted words are 'slots', which can later be used in our action code. In this case, I have two slots for this intent. One is temperature and the other is the endpoint (hotend or bed).
I have published both apps used here to the snips appstore, so you can easily add them to your project!
If you only plan on justcontrolling your 3D printer, then this part is not necessary. Since I also wanted to some power monitoring, I used a spare electrical socket and a sun box to create a special plug base. This added an extra layer of safety since no electrical connections are exposed.
Wiring diagram is as follows.
In order for the Raspberry Pi to actually do something, such as telling you the weather forecast, we will have to write some handler code. This is code which is executed when a certain event happens on the Snips platform, such as when an intent has been detected.
For this I used one of the python templates provided by snips.Link : https://docs.snips.ai/articles/platform/create-an-app/python-template
Out of these, I chose https://github.com/snipsco/snips-app-template-py. You can download this to your Raspberry Pi using Git and begin writing your handler code in action-app_template.py
git clone https://github.com/snipsco/snips-app-template-py
cd to the directory and run./setup.sh to install the required library (hermes-python>=0.8)
I'll briefly explain some important components of the action code I wrote.
import serial
import time
ser = serial.Serial()
ser.port='/dev/ttyUSB2'
ser.baudrate=115200
ser.open()
ser.write("\r\n\r\n".encode()) # Hit enter a few times to wake the printer
time.sleep(2) # Wait for printer to initialize
ser.flushInput()
pyserial is used to send gcode to the 3D printer. If you get an error complaining that /dev/tty/USB1 is not found, make sure you change this piece of code.
If you also plan on using the energy meter, make sure you have this part setup correctly as well.
import minimalmodbus
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1, mode = 'rtu') # port name, slave address - Use multiple objects to talk with multiple power meters
instrument.serial.baudrate = 9600
I am using DF0 pin of the MATRIX Voice expansion header to control the relay. This pin has to be configured in the following way.
relayPin = 0
gpio.setFunction(relayPin, 'DIGITAL')
gpio.setMode(relayPin, "output")
gpio.setDigital(relayPin,1)
You can tell the assistant to call a certain function when it recognizes an intent. This is done in the master_intent_callback function.
def master_intent_callback(self,hermes, intent_message):
self.showlights()
coming_intent = intent_message.intent.intent_name
#========================== Power Monitor Functions ========================= #
if coming_intent == 'yasaspeiris:getVoltage':
self.getVolts_callback(hermes, intent_message)
Replace yasaspeiris with your username for snips console
We'll now look at one function each from the powerMonitor app and the 3d printer controller app.
def getVolts_callback(self, hermes, intent_message):
hermes.publish_end_session(intent_message.session_id, "")
print ('[Received] intent: {}'.format(intent_message.intent.intent_name))
device = None
if intent_message.slots:
device = intent_message.slots.deviceName.first().value
if device not in availableDevice:
device = None
if device is None:
reply = "No device specified"
else:
reply = "Voltage received by "+str(device) +" is " + str(self.get_volts()) + " Volts"
hermes.publish_start_session_notification(intent_message.site_id, reply,"snips_power_app")
hermes.publish_end_session(intent_message.session_id, reply)
This function will first print the received intent for debugging purposes. It will then check if there is a valid value for the devices slot. (You can expand this code to support multiple devices) It will then call the get_volts() function, which will communicate with the energy meter via modbus and return the voltage value.
Now lets look at one of the call backs for 3D printer controls.
elif coming_intent == 'yasaspeiris:goHome':
self.goHome_callback(hermes, intent_message)
This callback is used to home the 3D printer.
def goHome_callback(self, hermes, intent_message):
hermes.publish_end_session(intent_message.session_id, "")
reply = "Homing all axes"
command = 'G28\n'
self.printserial(command)
hermes.publish_start_session_notification(intent_message.site_id, reply,"snips_power_app")
hermes.publish_end_session(intent_message.session_id, reply)
This function will use the printserial function to encode the G28 gcode command and send it to the 3D printer via serial.
The entire action code is attached with this project.
DemonstrationThe following video contains demos of the power monitoring part and the printer voice controls (such as movement, homing, setting temperatures etc.)
ConclusionYou can easily modify this project to send any kind of GCODE to the printer! And if you chose to add the power monitoring part, you printer would be safer since our voice assistant will be continuously monitoring it for any current spikes.
I would also like to point out that the MATRIX Voice board is pretty good at picking up sound, even in a noisy environment like this. These technologies are great starting points for anyone interested in audio processing!
Comments