Advertisement
qianzhe

TestCode for Puppet Show 2

Aug 10th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. # External module imports
  2. from __future__ import division
  3. import RPi.GPIO as GPIO
  4. import time
  5. import pygame
  6.  
  7. # Import the PCA9685 module.
  8. import Adafruit_PCA9685
  9.  
  10. # Initialise the PCA9685 using the default address (0x40).
  11. pwm = Adafruit_PCA9685.PCA9685()
  12.  
  13. # GPIO Pin Definitons:
  14. beastStart = 5  # IR Module pin for start position for the Beast (character)
  15. beastShow = 6  #IR Module pin where the Beast (Character) would be at when he appears.
  16. belleShow = 13  #IR Module pin where the Belle (Character) would be at when she appears.
  17. belleStart = 19  # IR Module pin for start position for Belle (character)
  18.  
  19. # PCA9685 Pin Definitions
  20. beastLeg = 0
  21. beastArm = 1
  22. belleBody = 2
  23. belleArm = 3
  24. gboxMotor = 4  # Servo for the gearbox - This is for the one with the motor
  25. gboxNMotor = 5  # Servo for the gearbox - This is for the one WITHOUT the motor.
  26. beastM_Left = 6  # PWM Pin to send to the Motor Driver.
  27. beastM_Right = 7  # PWM Pin to send to the Motor Driver.
  28. belleM_Left = 8  # PWM Pin to send to the Motor Driver.
  29. belleM_Right = 9  # PWM Pin to send to the Motor Driver.
  30. screen_Left = 10  # PWM Pin to send to the Motor Driver.
  31. screen_Right = 11  # PWM Pin to send to the Motor Driver.
  32. led_red = 12  # To send to High power LED Driver.
  33. led_green = 13  # To send to High power LED Driver.
  34. led_blue = 14  # To send to High power LED Driver.
  35. led_white = 15  # To send to High power LED Driver.
  36.  
  37. # Configure min and max servo pulse lengths
  38. servo_min = 320  # Min pulse length out of 4096
  39. servo_max = 600  # Max pulse length out of 4096
  40. led_low = 0
  41. led_high = 3072
  42.  
  43. # Pin Setup:
  44. GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
  45. GPIO.setup(beastStart, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
  46. GPIO.setup(beastShow, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
  47. GPIO.setup(belleShow, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
  48. GPIO.setup(belleStart, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
  49.  
  50. # NOTE: Servos work with these values:
  51. # Pulses are at a 50/60Hz interval
  52. # Pulse high vary between 1 to 2 miliseconds, i.e. centre should be 1.5ms
  53.  
  54. # Helper function to make setting a servo pulse width simpler.
  55. def set_servo_pulse(channel, pulse):
  56.     pulse_length = 1000000    # 1,000,000 us per second
  57.     pulse_length //= 60       # 60 Hz
  58.     print('{0}us per period'.format(pulse_length))
  59.     pulse_length //= 4096     # 12 bits of resolution
  60.     print('{0}us per bit'.format(pulse_length))
  61.     pulse *= 1000
  62.     pulse //= pulse_length
  63.     pwm.set_pwm(channel, 0, pulse)
  64.  
  65. # Set frequency to 60hz, good for servos.
  66. pwm.set_pwm_freq(60)
  67.  
  68.  
  69.  
  70.  
  71.  
  72. # Alternatively specify a different address and/or bus:
  73. # pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)
  74.  
  75.  
  76. print("Here we go! Press CTRL+C to exit")
  77. try:
  78.  
  79.     pwm.set_pwm(led_white, 0, led_high)
  80.     while GPIO.input(beastStart):
  81.         pwm.set_pwm(beastArm, 0, servo_min)
  82.         time.sleep(1)
  83.         pwm.set_pwm(beastArm, 0, servo_max)
  84.         time.sleep(1)
  85.         pwm.set_pwm(beastM_Right, 0, 256)
  86.  
  87.     pwm.set_pwm(beastArm, 0, servo_max)
  88.     pwm.set_pwm(beastM_Right, 0, 0)
  89.     print("stop")
  90.     pwm.set_pwm(led_white, 0, led_low)
  91.  
  92.  
  93.  
  94.             # if GPIO.input(butPin): # button is released
  95.         #     pwm.set_pwm(belle_Body, 0, servo_min)
  96.         #     pwm.set_pwm(led_white, 0, led_low)
  97.         #     pwm.set_pwm(led_red, 0, led_low)
  98.         #     print("Go to servo min, light min")
  99.         #     time.sleep(3)
  100.         # else: # button is pressed:
  101.         #     print("mowing on to second step")
  102.         #     pwm.set_pwm(belle_Body, 0, servo_max)
  103.         #     pwm.set_pwm(led_white, 0, led_high)
  104.         #     pwm.set_pwm(led_red, 0, led_high)
  105.         #     print("Go to servo max, light max")
  106.         #     time.sleep(3)
  107. except KeyboardInterrupt: #If CTRL+C is pressed, exit cleanly:
  108.     pwm.set_pwm(beastArm, 0, servo_min)
  109.     pwm.set_pwm(led_white, 0, 0)
  110.     pwm.set_pwm(led_red, 0, 0)
  111.     GPIO.cleanup() # cleanup all GPIO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement