import RPi.GPIO as GPIO
import time as time
import os
import Adafruit_PCA9685
import sys
import RPi.GPIO as GPIO
import time
import pygame
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.LOW)
GPIO.setup(19,GPIO.LOW)
pwm = Adafruit_PCA9685.PCA9685()
headMid = 350
headRight = 250
headleft = 500
headChannel = 3
rightArmMid = 400
rightArmDown = 150
rightArmUp = 600
rightArmChannel = 2
leftArmMid = 400
leftArmDown = 600
leftArmUp = 150
leftArmChannel = 1
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
# Helper function to make setting a servo pulse width simpler.
def set_servo_pulse(channel, pulse):
pulse_length = 1000000 # 1,000,000 us per second
pulse_length //= 60 # 60 Hz
print('{0}us per period'.format(pulse_length))
pulse_length //= 4096 # 12 bits of resolution
print('{0}us per bit'.format(pulse_length))
pulse *= 1000
pulse //= pulse_length
pwm.set_pwm(channel, 0, pulse)
def allOff():
pwm.set_pwm(headChannel, 0, 0)
pwm.set_pwm(leftArmChannel, 0, 0)
pwm.set_pwm(rightArmChannel, 0, 0)
def testHead():
pwm.set_pwm(headChannel, 0, headRight)
time.sleep(0.5)
pwm.set_pwm(headChannel, 0, 0)
time.sleep(2)
pwm.set_pwm(headChannel, 0, headMid)
time.sleep(0.5)
pwm.set_pwm(headChannel, 0, 0)
time.sleep(2)
pwm.set_pwm(headChannel, 0, headleft)
time.sleep(0.5)
pwm.set_pwm(headChannel, 0, 0)
time.sleep(2)
pwm.set_pwm(headChannel, 0, headMid)
time.sleep(0.8)
pwm.set_pwm(headChannel, 0, 0)
def testRightArm():
pwm.set_pwm(rightArmChannel, 0, rightArmUp)
time.sleep(0.8)
pwm.set_pwm(rightArmChannel, 0, rightArmDown)
time.sleep(0.4)
pwm.set_pwm(rightArmChannel, 0, 0)
def testLeftArm():
pwm.set_pwm(leftArmChannel, 0, leftArmUp)
time.sleep(0.8)
pwm.set_pwm(leftArmChannel, 0, leftArmDown)
time.sleep(0.4)
pwm.set_pwm(leftArmChannel, 0, 0)
def alive():
pygame.mixer.music.load(open("/home/pi/Desktop/Robot/sounds/alive.mp3", "rb"))
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(1)
pwm.set_pwm(leftArmChannel, 0, leftArmUp)
pwm.set_pwm(rightArmChannel, 0, rightArmUp)
pwm.set_pwm(leftArmChannel, 0, leftArmDown)
pwm.set_pwm(rightArmChannel, 0, rightArmDown)
time.sleep(0.8)
allOff()
def dance():
pygame.mixer.music.load(open("/home/pi/Desktop/Robot/sounds/dance.mp3", "rb"))
pygame.mixer.music.play()
for x in range(0,7):
pwm.set_pwm(leftArmChannel, 0, leftArmUp)
pwm.set_pwm(rightArmChannel, 0, rightArmDown)
pwm.set_pwm(headChannel, 0, headleft)
time.sleep(0.8)
pwm.set_pwm(headChannel, 0, 0)
pwm.set_pwm(leftArmChannel, 0, leftArmDown)
pwm.set_pwm(rightArmChannel, 0, rightArmUp)
pwm.set_pwm(headChannel, 0, headRight)
time.sleep(0.8)
allOff()
pwm.set_pwm(headChannel, 0, headMid)
time.sleep(0.8)
pwm.set_pwm(headChannel, 0, 0)
def hey():
pygame.mixer.music.load(open("/home/pi/Desktop/Robot/sounds/hey.mp3", "rb"))
pygame.mixer.music.play()
time.sleep(0.2)
pwm.set_pwm(rightArmChannel, 0, rightArmUp)
time.sleep(0.8)
pwm.set_pwm(rightArmChannel, 0, rightArmDown)
time.sleep(0.4)
pwm.set_pwm(rightArmChannel, 0, 0)
allOff()
def moveForward():
GPIO.output(26, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
def moveLeft():
GPIO.output(26, GPIO.LOW)
def moveRight():
GPIO.output(19, GPIO.LOW)
def stop():
GPIO.output(26, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
stop()
pygame.init()
# sets the window title
pygame.display.set_caption(u'Keyboard events')
# sets the window size
pygame.display.set_mode((400, 400))
pygame.mixer.init()
alive()
pwm.set_pwm(headChannel, 0, headMid)
pwm.set_pwm(rightArmChannel, 0, rightArmDown)
pwm.set_pwm(leftArmChannel, 0, leftArmDown)
while True:
for event in pygame.event.get():
if event.type in (pygame.KEYDOWN, pygame.KEYUP):
key_name = pygame.key.name(event.key)
key_name = key_name.upper()
if event.type == pygame.KEYDOWN:
# prints on the console the key pressed
print (u'"{}" key pressed'.format(key_name))
if key_name =="UP":
moveForward()
elif key_name == "RIGHT":
moveRight()
elif key_name == "LEFT":
moveLeft()
elif key_name == "[*]":
dance()
elif key_name == "[/]":
hey()
elif key_name == "ENTER":
pygame.quit()
GPIO.cleanup()
break
elif event.type == pygame.KEYUP:
if key_name =="UP":
stop()
elif key_name == "RIGHT":
stop()
elif key_name == "LEFT":
stop()
GPIO.cleanup()
Comments