/bot.py

https://bitbucket.org/GloryFish/puppybot
Python | 102 lines | 55 code | 26 blank | 21 comment | 1 complexity | cbd78e77832fb1c88ccd8ee99f94e898 MD5 | raw file
  1. #! /usr/bin/python
  2. import cmd
  3. import logging
  4. import time
  5. import RPi.GPIO as GPIO
  6. import dispenser
  7. import indicator
  8. import speaker
  9. class BotCommander(cmd.Cmd):
  10. """Command processor for the puppy bot."""
  11. def preloop(self):
  12. """Hook method executed once when cmdloop() is called.
  13. Sets up some initial settings.
  14. """
  15. self.intro = 'PuppyBot v2.0'
  16. self.prompt = '> '
  17. # Configure logging
  18. logging.basicConfig(level=logging.INFO)
  19. self.logger = logging.getLogger('puppybot')
  20. # Configure RPi.GPIO
  21. GPIO.setwarnings(False)
  22. GPIO.setmode(GPIO.BOARD)
  23. # Physical treat dispenser
  24. self.dispenser = dispenser.Dispenser('puppybot')
  25. self.dispenser.start()
  26. # Indicator lights
  27. self.indicator = indicator.Indicator(redchannel=18, greenchannel=16)
  28. self.indicator.start()
  29. self.indicator.green_on()
  30. # Speaker
  31. self.speaker = speaker.Speaker()
  32. def do_beep(self, line):
  33. self.speaker.beep()
  34. def do_treat(self, line):
  35. """Trigger the treat dispenser and update indicator lights."""
  36. self.indicator.red_on()
  37. self.speaker.beep()
  38. self.dispenser.give_treat()
  39. self.indicator.green_on()
  40. def do_setservo(self, line):
  41. """Set a servo to a specific value.
  42. Syntax: setservo [number] [value]
  43. """
  44. args = line.split()
  45. servo = int(args[0])
  46. value = int(args[1])
  47. self.logger.info('servo: %i, value: %i', servo, value)
  48. self.dispenser.set_servo(servo, value)
  49. def do_blink(self, line):
  50. """Blink all lights on and off for testing"""
  51. self.indicator.all_on()
  52. time.sleep(1)
  53. self.indicator.all_off()
  54. time.sleep(1)
  55. self.indicator.all_on()
  56. time.sleep(1)
  57. self.indicator.all_off()
  58. def do_exit(self, line):
  59. """Quit PuppyBot, safely deactivating the system."""
  60. return self.quit()
  61. def help_exit(self):
  62. print "Quit PuppyBot, safely deactivating the system."
  63. def do_EOF(self, line):
  64. return self.quit()
  65. def quit(self):
  66. """Shut down the bot cleanly and clean up external resources."""
  67. self.dispenser.stop()
  68. self.indicator.stop()
  69. GPIO.cleanup()
  70. return True
  71. if __name__ == '__main__':
  72. BotCommander().cmdloop()