Intro: Dog Mood Detector (Raspberry Pi)
This Instructable is designed to take the sounds a dog makes and determine if they should be approached or not with indicator LED's. Most dog owners know their pets and can read the signals that they give off so this Instructable is mostly directed toward strangers that may come in contact with your dog.
Step 1: Parts
You will need:
- Raspberry Pi
- Red/Green LEDs (X2)
- Keyboard/mouse with USB
- WiFi Dongle
- External USB Microphone
- 330 ohm Resistor (X2)
- External Raspberry Pi Power Supply
- Dog Collar
Step 2: Procedure
The first step to beginning this project is to observe the behaviors and patterns associated with your dog. You can click HERE for a simple guide on what you should be looking for. In my case, my dog howls whenever she is excited or happy to see someone and barks intermittently when nervous or aggravated. In the next few steps I will explain how to modify my program in order to accommodate the behaviors of your dog.
Step 3: Programming
Below is the Python program I used for my dog. In the next step I will explain how to modify the program for your dog's behavior. Don't run the program just yet as it won't work until you do the next step.
<p>#!/usr/bin/python<br>import pyaudio import sys import thread from time import sleep from array import array import RPi.GPIO as GPIO</p><p>bark=0 howl=False hold=0 barkLength=5 delay=0 wait = 2 flag = 0 red = 7 green=5 exitFlag = False</p><p>def toggleLightRed(c): GPIO.setmode(GPIO.BOARD) GPIO.setup(red,GPIO.OUT) GPIO.output(c,True) sleep(10) GPIO.output(c,False) print("Red toggled")</p><p>def toggleLightGreen(c): GPIO.setmode(GPIO.BOARD) GPIO.setup(green,GPIO.OUT) GPIO.output(c,True) sleep(10) GPIO.output(c,False) print("Green toggled")</p><p>def main(): global bark global howl global hold global barkLength global delay global flag global red global green</p><p> chunk = 8192 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 threshold = 3000 max_value = 0 p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, output=True, frames_per_buffer=chunk) GPIO.setmode(GPIO.BOARD) GPIO.setup(red,GPIO.OUT) GPIO.setup(green,GPIO.OUT) try: print "Detection initialized" while True: try: data = stream.read(chunk) except IOError as ex: if ex != pyaudio.paInputOverflowed: raise data = '\x00' * chunk as_ints = array('h', data) max_value = max(as_ints) while max_value>threshold: delay=0 hold=hold+1 try: data = stream.read(chunk) except IOError as ex: if ex != pyaudio.paInputOverflowed: raise data = '\x00' * chunk as_ints = array('h', data) max_value = max(as_ints) if hold>=barkLength: howl=True print "HOWL DETECTED" toggleLightGreen(green) GPIO.cleanup() elif hold>0 and hold<barklength: ="" bark+="1" print="" "bark="" detected"="" togglelightred(red)="" gpio.cleanup()="" hold="0" delay+="1" except="" (keyboardinterrupt,="" systemexit):="" "\rexiting"="" stream.stop_stream()="" stream.close()="" p.terminate()="" gpio.cleanup()<="" p=""></barklength:></p><p>if __name__ == '__main__': main()</p>
Step 4: Troubleshooting and Modifying
Before you run the program you will need to type the following into the Terminal to install PyAudio:
<p>sudo apt-get intall python-pyaudio</p>
After this you can test-run the program to make sure it is functioning properly.
You may experience the following error:
IOError: [Errno Input overflowed] -9981
To fix this, simply increase the number assigned to the variable chunk until the error no longer appears.
The variable barkLength dictates the number of times the program loops before a noise is no longer seen as a bark, but as a howl. If your dogs howls but does so only for a short burst then you should decrease this number.
The variables red and green refer to the output ports that will be used for the LED indicators. These can be changed to fit your needs.
Although I don't use the variable delay actively in my program, it can be observed to indicate the frequency of barking or howling.
The variable hold refers to how many times the program loops where the noise level is above the threshold and is used to determine if a howl is taking place. This variable should not be modified in any way as howls are detected through the manipulation of the barkLength variable.
The threshold variable can be lowered if the bark is not very loud or raised if there is background noise that could be misinterpreted as noise emanating from the dog.
Step 5: Hardware
It is at this point that you should have a fully functioning program that displays to the screen what the hardware will be doing. This point in the project is where you should decide whether this will be a stationary microphone that is set in one place (ex. a place in the house where the dog normally frequents or comes into contact with visitors) or if the project will be minimized and attached to the dog's collar to provide immediate feedback to the person interacting with the dog.
The easiest thing to do is to leave everything on a breadboard since there will be no movement that could detach wires. Connect the cathodes of the Red LEDs to the Ground pin on the Raspberry Pi and the Anodes either through a 330 ohm resistor or straight into pin 7 of the Raspberry Pi. Do the same with the Green LEDs but connect the Anodes to pin 5. Run the program and you should have a completed project once you have modified the code to your needs.
I would recommend completing the stationary version first just to ensure that everything is working and then move on from there by soldering together everything so that no wires will become unconnected from the dog's movement.
Attach the LEDs to the collar so that they are both in a comfortable position and are able to be seen by someone as they approach.
Next connect your external power supply such as the one seen HERE to the raspberry pi and secure it to the collar in a manner that is comfortable for the dog.
Once everything is secured in place go ahead and run the program, attach the collar to the dog, and you're done!