Skip to content
Browse files

Added some fun code from the cat feeder project.

  • Loading branch information...
1 parent 18d8e87 commit d8ac4e5d6efa99c19ea4c3d73d27dd10f12923bb @videoman committed Mar 28, 2013
Showing with 397 additions and 0 deletions.
  1. +89 −0 Feed_my_cat.py
  2. +161 −0 Feed_my_human-button.py
  3. +106 −0 Feed_my_human.py
  4. +21 −0 PWM-piezo.py
  5. +20 −0 Servo_test.py
View
89 Feed_my_cat.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+#
+# Example for Hack Factory Raspberry Pi Class.
+# David M. N. Bryan, dave@drstrangelove.net
+#
+# This is licend under creative commons license:
+# http://creativecommons.org/licenses/by-nc-sa/3.0/
+# Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
+#
+# This needs more work. The code for the Humans appears to have gotten a higher priorty.
+#
+
+from RPIO import PWM
+import time
+import RPi.GPIO as GPIO
+
+Servo1Pin=23
+Servo2Pin=18
+
+BeeperPin=24
+GPIORed_PIN=17
+GPIOGreen_PIN=27
+
+# Setup the OUTPUTS , beeper & LEDS
+GPIO.setmode(GPIO.BCM)
+GPIO.setup(BeeperPin, GPIO.OUT)
+GPIO.setup(GPIORed_PIN, GPIO.OUT)
+GPIO.setup(GPIOGreen_PIN, GPIO.OUT)
+
+GPIO.output(BeeperPin, False)
+GPIO.output(GPIORed_PIN, False)
+GPIO.output(GPIOGreen_PIN, False)
+
+servo = PWM.Servo()
+
+def servo_CW(ServoPIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ # This rotates the servo CW.
+ servo.set_servo(ServoPIN, 1200)
+ time.sleep(SleepTime)
+ servo.stop_servo(ServoPIN)
+ time.sleep(.25)
+
+def dual_servo_CW(Servo1PIN,Servo2PIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ # This rotates the servo CW.
+ servo.set_servo(Servo1PIN, 1200)
+ servo.set_servo(Servo2PIN, 1200)
+ time.sleep(SleepTime)
+ servo.stop_servo(Servo1PIN)
+ servo.stop_servo(Servo2PIN)
+ time.sleep(.25)
+
+def servo_CCW(ServoPIN,SleepTime):
+ # Set servo on Servo1Pin to 2000s (2.0ms)
+ # This rotates the servo CounterCW
+ servo.set_servo(ServoPIN, 2000)
+ time.sleep(SleepTime)
+ # Clear servo on Servo1Pin
+ servo.stop_servo(ServoPIN)
+ time.sleep(.25)
+
+def dual_servo_CCW(Servo1PIN,Servo2PIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ # This rotates the servo CW.
+ servo.set_servo(Servo1PIN, 2000)
+ servo.set_servo(Servo2PIN, 2000)
+ time.sleep(SleepTime)
+ servo.stop_servo(Servo1PIN)
+ servo.stop_servo(Servo2PIN)
+ time.sleep(.25)
+
+# Clear servo on Servo1Pin
+#servo.stop_servo(Servo1Pin)
+#servo.stop_servo(Servo2Pin)
+
+while True:
+
+ GPIO.output(GPIORed_PIN, True)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.75)
+ GPIO.output(BeeperPin, False)
+
+ dual_servo_CW(Servo1Pin,Servo2Pin,2)
+ #servo_CW(Servo1Pin,2)
+ #servo_CCW(Servo1Pin,2)
+ #servo_CW(Servo2Pin,2)
+ #dual_servo_CCW(Servo1Pin,Servo2Pin,2)
+ #servo_CCW(Servo2Pin,2)
View
161 Feed_my_human-button.py
@@ -0,0 +1,161 @@
+#!/usr/bin/env python
+#
+# Example for Hack Factory Raspberry Pi Class.
+# David M. N. Bryan, dave@drstrangelove.net
+#
+# This is licend under creative commons license:
+# http://creativecommons.org/licenses/by-nc-sa/3.0/
+# Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
+
+# Import the PWM library so we can control the servos.
+from RPIO import PWM
+# It's a timeywimey thing.
+import time
+# Import the Raspberry Pi GPIO library.
+import RPi.GPIO as GPIO
+
+# I have the Servero input ping (White wires) setup
+# for the fllowing pins
+Servo1Pin=18
+Servo2Pin=23
+
+# I have a pizeo beeper to announce when I'm going to
+# hurl food at you. puny human.
+BeeperPin=24
+
+# Left Switch has an LED pin, and a Switch input pin
+GPIO_ButtonL_LED_PIN=4
+GPIO_ButtonL_PIN=17
+
+# Right Switch has an LED pin, and a Switch input pin
+GPIO_ButtonR_LED_PIN=27
+GPIO_ButtonR_PIN=22
+
+# Tell the GPIO who is what,EG: OUTPUTS , beeper, LEDS, inputs
+GPIO.setmode(GPIO.BCM)
+GPIO.setup(BeeperPin, GPIO.OUT)
+GPIO.setup(GPIO_ButtonL_LED_PIN, GPIO.OUT)
+GPIO.setup(GPIO_ButtonL_PIN, GPIO.IN)
+GPIO.setup(GPIO_ButtonR_LED_PIN, GPIO.OUT)
+GPIO.setup(GPIO_ButtonR_PIN, GPIO.IN)
+
+# Set the beeper&LED pins low, so they don't light or sweek on boot.
+GPIO.output(BeeperPin, False)
+GPIO.output(GPIO_ButtonL_LED_PIN, False)
+GPIO.output(GPIO_ButtonR_LED_PIN, False)
+
+# Setup the servo PWM library
+servo = PWM.Servo()
+
+# I'm goint to make some functions that do the servo rotation,
+# so I don't have to change a ton of code later on if I need to.
+
+# This rotates the feeder wheel Clock Wise (from left to right)
+def servo_CW(ServoPIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ servo.set_servo(ServoPIN, 1200)
+ time.sleep(SleepTime)
+ servo.stop_servo(ServoPIN)
+ time.sleep(.25)
+
+# This rotates both feeder wheels Clock Wise (from left to right)
+# at the same time
+def dual_servo_CW(Servo1PIN,Servo2PIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ # This rotates the servo CW.
+ servo.set_servo(Servo1PIN, 1200)
+ servo.set_servo(Servo2PIN, 1200)
+ time.sleep(SleepTime)
+ servo.stop_servo(Servo1PIN)
+ servo.stop_servo(Servo2PIN)
+ time.sleep(.25)
+
+# This rotates the feeder wheel Counter Clock Wise (from right to left)
+def servo_CCW(ServoPIN,SleepTime):
+ # Set servo on Servo1Pin to 2000s (2.0ms)
+ servo.set_servo(ServoPIN, 2000)
+ time.sleep(SleepTime)
+ # Clear servo on Servo1Pin
+ servo.stop_servo(ServoPIN)
+ time.sleep(.25)
+
+# This rotates both feeder wheels Counter Clock Wise (from right to left)
+# at the same time
+def dual_servo_CCW(Servo1PIN,Servo2PIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ servo.set_servo(Servo1PIN, 2000)
+ servo.set_servo(Servo2PIN, 2000)
+ time.sleep(SleepTime)
+ servo.stop_servo(Servo1PIN)
+ servo.stop_servo(Servo2PIN)
+ time.sleep(.25)
+
+# I created a function to feed the "thing" from the approiate side, based
+# on the user input. This function calls the servo funcations.
+def feed_thing(HopperSide,FeedTime):
+ if(HopperSide == "Left"):
+ GPIO.output(GPIO_ButtonL_LED_PIN, False)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.10)
+ GPIO.output(BeeperPin, False)
+ GPIO.output(GPIO_ButtonL_LED_PIN, True)
+ print "Ok, food is coming out the left bin!"
+ servo_CW(Servo1Pin,FeedTime)
+
+ if(HopperSide == "Right"):
+ GPIO.output(GPIO_ButtonR_LED_PIN, False)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.10)
+ GPIO.output(BeeperPin, False)
+ GPIO.output(GPIO_ButtonR_LED_PIN, True)
+ print "Ok, food is coming out the right bin!"
+ servo_CCW(Servo2Pin,FeedTime)
+
+ if(HopperSide == "Both"):
+ GPIO.output(GPIO_ButtonL_LED_PIN, False)
+ GPIO.output(GPIO_ButtonR_LED_PIN, False)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.25)
+ GPIO.output(BeeperPin, False)
+ time.sleep(.25)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.25)
+ GPIO.output(BeeperPin, False)
+ time.sleep(.25)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.25)
+ GPIO.output(BeeperPin, False)
+ GPIO.output(GPIO_ButtonL_LED_PIN, True)
+ GPIO.output(GPIO_ButtonR_LED_PIN, True)
+ print "Ok, food is coming out at both ends!"
+ #servo_CCW(Servo2Pin,FeedTime)
+ dual_servo_CW(Servo1Pin,Servo2Pin,FeedTime)
+
+# Lets send out two beeps so that the user knows that we are ready to
+# dispense some food.
+GPIO.output(BeeperPin, True)
+time.sleep(.25)
+GPIO.output(BeeperPin, False)
+time.sleep(.25)
+GPIO.output(BeeperPin, True)
+time.sleep(.25)
+GPIO.output(BeeperPin, False)
+
+# This is the main loop where we wait for stuff to happen!
+while True:
+ # First off, lets turn the LEDs for the buttons on!
+ GPIO.output(GPIO_ButtonL_LED_PIN, True)
+ GPIO.output(GPIO_ButtonR_LED_PIN, True)
+
+ # Now lets evaluate if a button is bushed...
+ # If both buttons are pushed, call the feed_thing function, feeding both hoppers for X seconds.
+ if ( GPIO.input(GPIO_ButtonL_PIN) == True and GPIO.input(GPIO_ButtonR_PIN) == True ):
+ feed_thing("Both",5)
+ # If only the left button is pushed, feed from the left hopper for half a second.
+ elif ( GPIO.input(GPIO_ButtonL_PIN) == True ):
+ feed_thing("Left",.5)
+ # If only the right button is pushed, feed from the right hopper for half a second.
+ elif ( GPIO.input(GPIO_ButtonR_PIN) == True ):
+ feed_thing("Right",.5)
+
+# If nothing to do, go back to the loop... forever...
View
106 Feed_my_human.py
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+#
+# Example for Hack Factory Raspberry Pi Class.
+# David M. N. Bryan, dave@drstrangelove.net
+#
+# This is licend under creative commons license:
+# http://creativecommons.org/licenses/by-nc-sa/3.0/
+# Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
+
+from RPIO import PWM
+import time
+import RPi.GPIO as GPIO
+
+Servo1Pin=18
+Servo2Pin=23
+
+BeeperPin=24
+GPIO_ButtonL_LED_PIN=4
+GPIO_ButtonL_PIN=17
+
+GPIO_ButtonR_LED_PIN=27
+GPIO_ButtonR_PIN=22
+
+# Setup the OUTPUTS , beeper & LEDS
+GPIO.setmode(GPIO.BCM)
+GPIO.setup(BeeperPin, GPIO.OUT)
+GPIO.setup(GPIO_ButtonL_LED_PIN, GPIO.OUT)
+GPIO.setup(GPIO_ButtonL_PIN, GPIO.IN)
+GPIO.setup(GPIO_ButtonR_LED_PIN, GPIO.OUT)
+GPIO.setup(GPIO_ButtonR_PIN, GPIO.IN)
+
+GPIO.output(BeeperPin, False)
+GPIO.output(GPIO_ButtonL_LED_PIN, False)
+GPIO.output(GPIO_ButtonR_LED_PIN, False)
+
+servo = PWM.Servo()
+
+def servo_CW(ServoPIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ # This rotates the servo CW.
+ servo.set_servo(ServoPIN, 1200)
+ time.sleep(SleepTime)
+ servo.stop_servo(ServoPIN)
+ time.sleep(.25)
+
+def dual_servo_CW(Servo1PIN,Servo2PIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ # This rotates the servo CW.
+ servo.set_servo(Servo1PIN, 1200)
+ servo.set_servo(Servo2PIN, 1200)
+ time.sleep(SleepTime)
+ servo.stop_servo(Servo1PIN)
+ servo.stop_servo(Servo2PIN)
+ time.sleep(.25)
+
+def servo_CCW(ServoPIN,SleepTime):
+ # Set servo on Servo1Pin to 2000s (2.0ms)
+ # This rotates the servo CounterCW
+ servo.set_servo(ServoPIN, 2000)
+ time.sleep(SleepTime)
+ # Clear servo on Servo1Pin
+ servo.stop_servo(ServoPIN)
+ time.sleep(.25)
+
+def dual_servo_CCW(Servo1PIN,Servo2PIN,SleepTime):
+ # Set servo on Servo1Pin to 1200us (1.2ms)
+ # This rotates the servo CW.
+ servo.set_servo(Servo1PIN, 2000)
+ servo.set_servo(Servo2PIN, 2000)
+ time.sleep(SleepTime)
+ servo.stop_servo(Servo1PIN)
+ servo.stop_servo(Servo2PIN)
+ time.sleep(.25)
+
+# Clear servo on Servo1Pin
+#servo.stop_servo(Servo1Pin)
+#servo.stop_servo(Servo2Pin)
+
+
+
+while True:
+ GPIO.output(GPIO_ButtonL_LED_PIN, True)
+ GPIO.output(GPIO_ButtonR_LED_PIN, True)
+
+ bin_name = raw_input('Do you require food from the (L)eft or (R)ight bin? []')
+
+ if bin_name == "L" or bin_name == "l" or bin_name == "left" or bin_name == "Left":
+ GPIO.output(GPIO_ButtonL_LED_PIN, False)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.25)
+ GPIO.output(BeeperPin, False)
+ GPIO.output(GPIO_ButtonL_LED_PIN, True)
+ servo_CCW(Servo1Pin,2)
+ elif bin_name == "R" or bin_name == "r" or bin_name == "Right" or bin_name == "right":
+ GPIO.output(GPIO_ButtonR_LED_PIN, False)
+ GPIO.output(BeeperPin, True)
+ time.sleep(.25)
+ GPIO.output(BeeperPin, False)
+ GPIO.output(GPIO_ButtonR_LED_PIN, True)
+ servo_CW(Servo2Pin,2)
+ elif bin_name == "B" or bin_name == "b" or bin_name == "Both" or bin_name == "both":
+ dual_servo_CW(Servo1Pin,Servo2Pin,2)
+ servo_CCW(Servo1Pin,2)
+ servo_CW(Servo2Pin,2)
+ dual_servo_CCW(Servo1Pin,Servo2Pin,2)
+ servo_CCW(Servo2Pin,2)
View
21 PWM-piezo.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+from RPIO import PWM
+import time
+
+BeeperPIN=24
+
+# Setup PWM and DMA channel 0
+PWM.setup()
+PWM.init_channel(0)
+
+# Add some pulses to the subcycle
+PWM.add_channel_pulse(0, BeeperPIN, 10, 10)
+time.sleep(2)
+PWM.add_channel_pulse(0, BeeperPIN, 100, 50)
+time.sleep(2)
+
+# Stop PWM for specific GPIO on channel 0
+PWM.clear_channel_gpio(0, BeeperPIN)
+
+# Shutdown all PWM and DMA activity
+PWM.cleanup()
View
20 Servo_test.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+from RPIO import PWM
+import time
+
+Servo1Pin=23
+Servo2Pin=18
+
+servo = PWM.Servo()
+
+# Set servo on Servo1Pin to 1200us (1.2ms)
+#servo.set_servo(Servo1Pin, 1200)
+
+
+# Set servo on Servo1Pin to 2000s (2.0ms)
+servo.set_servo(Servo1Pin, 2000)
+
+time.sleep(5)
+
+# Clear servo on Servo1Pin
+servo.stop_servo(Servo1Pin)

0 comments on commit d8ac4e5

Please sign in to comment.
Something went wrong with that request. Please try again.