Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # External module imports
- from __future__ import division
- import RPi.GPIO as GPIO
- import time
- import pygame
- # Import the PCA9685 module.
- import Adafruit_PCA9685
- # Initialise the PCA9685 using the default address (0x40).
- pwm = Adafruit_PCA9685.PCA9685()
- # GPIO Pin Definitons:
- beastStart = 5 # IR Module pin for start position for the Beast (character)
- beastShow = 6 #IR Module pin where the Beast (Character) would be at when he appears.
- belleShow = 13 #IR Module pin where the Belle (Character) would be at when she appears.
- belleStart = 19 # IR Module pin for start position for Belle (character)
- # PCA9685 Pin Definitions
- beastLeg = 0
- beastArm = 1
- belleBody = 2
- belleArm = 3
- gboxMotor = 4 # Servo for the gearbox - This is for the one with the motor
- gboxNMotor = 5 # Servo for the gearbox - This is for the one WITHOUT the motor.
- beastM_Left = 6 # PWM Pin to send to the Motor Driver.
- beastM_Right = 7 # PWM Pin to send to the Motor Driver.
- belleM_Left = 8 # PWM Pin to send to the Motor Driver.
- belleM_Right = 9 # PWM Pin to send to the Motor Driver.
- screen_Left = 10 # PWM Pin to send to the Motor Driver.
- screen_Right = 11 # PWM Pin to send to the Motor Driver.
- led_red = 12 # To send to High power LED Driver.
- led_green = 13 # To send to High power LED Driver.
- led_blue = 14 # To send to High power LED Driver.
- led_white = 15 # To send to High power LED Driver.
- # Configure min and max servo pulse lengths
- servo_min = 320 # Min pulse length out of 4096
- servo_max = 600 # Max pulse length out of 4096
- led_low = 0
- led_high = 3072
- # Pin Setup:
- GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
- GPIO.setup(beastStart, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
- GPIO.setup(beastShow, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
- GPIO.setup(belleShow, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
- GPIO.setup(belleStart, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
- # NOTE: Servos work with these values:
- # Pulses are at a 50/60Hz interval
- # Pulse high vary between 1 to 2 miliseconds, i.e. centre should be 1.5ms
- # 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)
- # Set frequency to 60hz, good for servos.
- pwm.set_pwm_freq(60)
- # Alternatively specify a different address and/or bus:
- # pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)
- print("Here we go! Press CTRL+C to exit")
- try:
- pwm.set_pwm(led_white, 0, led_high)
- while GPIO.input(beastStart):
- pwm.set_pwm(beastArm, 0, servo_min)
- time.sleep(1)
- pwm.set_pwm(beastArm, 0, servo_max)
- time.sleep(1)
- pwm.set_pwm(beastM_Right, 0, 256)
- pwm.set_pwm(beastArm, 0, servo_max)
- pwm.set_pwm(beastM_Right, 0, 0)
- print("stop")
- pwm.set_pwm(led_white, 0, led_low)
- # if GPIO.input(butPin): # button is released
- # pwm.set_pwm(belle_Body, 0, servo_min)
- # pwm.set_pwm(led_white, 0, led_low)
- # pwm.set_pwm(led_red, 0, led_low)
- # print("Go to servo min, light min")
- # time.sleep(3)
- # else: # button is pressed:
- # print("mowing on to second step")
- # pwm.set_pwm(belle_Body, 0, servo_max)
- # pwm.set_pwm(led_white, 0, led_high)
- # pwm.set_pwm(led_red, 0, led_high)
- # print("Go to servo max, light max")
- # time.sleep(3)
- except KeyboardInterrupt: #If CTRL+C is pressed, exit cleanly:
- pwm.set_pwm(beastArm, 0, servo_min)
- pwm.set_pwm(led_white, 0, 0)
- pwm.set_pwm(led_red, 0, 0)
- GPIO.cleanup() # cleanup all GPIO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement