Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import pigpio
- GPIO.setmode(GPIO.BCM)
- from time import sleep
- import sys, tty, termios, time
- import time
- GPIO.setwarnings(False)
- pi = pigpio.pi()
- user_gpio = 0-31.
- pulsewidth = 0, 500-2500
- # Variables
- delay = 0.02
- steps = 200
- refresh_period = 0.02
- # Enable servo pins and variables
- spwm = 1500
- epwm = 1500
- wpwm = 1500
- g1pwm = 1200
- g2pwm = 1800
- nspwm = 1500
- nepwm = 1500
- nwpwm = 1500
- ng1pwm = 1200
- ng2pwm = 1800
- shoulderpin = 18
- elbowpin = 12
- wristpin = 16
- grip1pin = 20
- grip2pin = 21
- GPIO.setup(shoulderpin, GPIO.OUT)
- GPIO.setup(elbowpin, GPIO.OUT)
- GPIO.setup(wristpin, GPIO.OUT)
- GPIO.setup(grip1pin, GPIO.OUT)
- GPIO.setup(grip2pin, GPIO.OUT)
- # Enable pins for IN1-4 to control step sequence in stepper motor
- coil_1_pin = 23
- coil_2_pin = 24
- coil_3_pin = 22
- coil_4_pin = 17
- # Set pin states for stepper motor
- GPIO.setup(coil_1_pin, GPIO.OUT)
- GPIO.setup(coil_2_pin, GPIO.OUT)
- GPIO.setup(coil_3_pin, GPIO.OUT)
- GPIO.setup(coil_4_pin, GPIO.OUT)
- # The getch method can determine which key has been pressed
- # by the user on the keyboard by accessing the system files
- # It will then return the pressed key as a variable
- def getch():
- fd = sys.stdin.fileno()
- old_settings = termios.tcgetattr(fd)
- try:
- tty.setraw(sys.stdin.fileno())
- ch = sys.stdin.read(1)
- finally:
- termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
- return ch
- # This section of code defines the methods used to move
- # the stepper turning motor and servos used for manipulating
- # the arm. These definitions are called later by keypress events.
- def turn_right():
- GPIO.output(23, 1)
- sleep(0.02)
- GPIO.output(23, 0)
- GPIO.output(24, 1)
- sleep(0.02)
- GPIO.output(24, 0)
- GPIO.output(22, 1)
- sleep(0.02)
- GPIO.output(22, 0)
- GPIO.output(17, 1)
- sleep(0.02)
- GPIO.output(17, 0)
- def turn_left():
- GPIO.output(17, 1)
- sleep(0.02)
- GPIO.output(17, 0)
- GPIO.output(22, 1)
- sleep(0.02)
- GPIO.output(22, 0)
- GPIO.output(24, 1)
- sleep(0.02)
- GPIO.output(24, 0)
- GPIO.output(23, 1)
- sleep(0.02)
- GPIO.output(23, 0)
- def shoulder_up(spwm = spwm, nspwm = nspwm):
- for spwm in range(1000, 2000, 20):
- pi.set_servo_pulsewidth(shoulderpin, spwm)
- time.sleep(0.4)
- pi.set_servo_pulsewidth(shoulderpin, 0)
- def shoulder_down(spwm = spwm, nspwm = nspwm):
- spwm = nspwm
- pi.set_servo_pulsewidth(shoulderpin, spwm)
- time.sleep(0.4)
- pi.set_servo_pulsewidth(shoulderpin, 0) # off
- nspwm = spwm + 20
- def elbow_up():
- epwm = epwm + 20
- pi.set_servo_pulsewidth(elbowpin, 1500)
- time.sleep(0.4)
- pi.set_servo_pulsewidth(elbowpin, 0) # off
- def elbow_down():
- pi.set_servo_pulsewidth(elbowpin, 2000)
- time.sleep(0.4)
- pi.set_servo_pulsewidth(elbowpin, 0) # off
- def wrist_clockwise():
- pi.set_servo_pulsewidth(wristpin, 1000)
- time.sleep(0.4)
- pi.set_servo_pulsewidth(wristpin, 0) # off
- def wrist_counterclockwise():
- pi.set_servo_pulsewidth(wristpin, 1200)
- time.sleep(0.4)
- pi.set_servo_pulsewidth(wristpin, 0) # off
- def grip_closed():
- pi.set_servo_pulsewidth(grip1pin, 1450)
- pi.set_servo_pulsewidth(grip2pin, 1650)
- time.sleep(0.2)
- pi.set_servo_pulsewidth(grip1pin, 0) # off
- pi.set_servo_pulsewidth(grip2pin, 0) # off
- def grip_open():
- pi.set_servo_pulsewidth(grip1pin, 1800)
- pi.set_servo_pulsewidth(grip2pin, 1200)
- time.sleep(0.2)
- pi.set_servo_pulsewidth(grip1pin, 0) # off
- pi.set_servo_pulsewidth(grip2pin, 0) # off
- # Infinite loop that will not end until the user presses the
- # exit key
- while True:
- # Keyboard character retrieval method is called and saved
- # into variable
- char = getch()
- # The shoulder will move down when the "w" key is pressed
- if(char == "w"):
- shoulder_down()
- # The shoulder will move up when the "s" key is pressed
- if(char == "s"):
- shoulder_up()
- # The "a" key will turn the arm left
- if(char == "a"):
- turn_left()
- # The "d" key will turn the arm right
- if(char == "d"):
- turn_right()
- # The "r" key will move the elbow down
- if(char == "r"):
- elbow_down()
- # The "f" key will move the elbow up
- if(char == "f"):
- elbow_up()
- # The "q" key will turn the wrist counterclockwise
- if(char == "q"):
- wrist_counterclockwise()
- # The "e" key will turn the wrist clockwise
- if(char == "e"):
- wrist_clockwise()
- # The "o" key will open the gripper
- if(char == "o"):
- grip_open()
- # The "p" key will close the gripper
- if(char == "p"):
- grip_closed()
- # The "x" key will break the loop and exit the program
- if(char == "x"):
- print("Program Ended")
- break
- # At the end of each loop
- # The keyboard character variable will be set to blank, ready
- # to save the next key that is pressed
- char = ""
- # Program will cease all GPIO activity before terminating
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement