Skip to content

Instantly share code, notes, and snippets.

@MiraWelner
Last active Aug 29, 2020
Embed
What would you like to do?
Controls a servo hand
import RPi.GPIO as GPIO
from time import sleep
import os
UP = 110
PINKY = 60 #in the hand i used, they pinky would always extend a lot idk why
DOWN = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.OUT)
GPIO.setup(3, GPIO.OUT)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
finger1=GPIO.PWM(2, 50)
finger1.start(0)
finger2=GPIO.PWM(3, 50)
finger2.start(0)
finger3=GPIO.PWM(4, 50)
finger3.start(0)
finger4=GPIO.PWM(5, 50)
finger4.start(0)
def SetAngle(finger, pin, angle):
duty = angle / 18 + 2
GPIO.output(pin, True)
finger.ChangeDutyCycle(duty)
sleep(0.1)
GPIO.output(pin, False)
finger.ChangeDutyCycle(0)
def Pose(first, second, third, fourth):
SetAngle(finger1, 2, first)
SetAngle(finger2, 3, second)
SetAngle(finger3, 4, third)
SetAngle(finger4, 5, fourth)
def openHand():
Pose(UP, UP, UP, PINKY)
def pinch():
Pose(DOWN, UP, UP, PINKY)
def tripod():
Pose(DOWN, DOWN, UP, PINKY)
def pointer():
Pose(UP,DOWN,DOWN,DOWN)
def fist():
Pose(DOWN,DOWN,DOWN,DOWN)
i = 'open'
while(True):
i = input()
if i == 'open':
openHand()
elif i == 'pinch':
pinch()
elif i == 'tripod':
tripod()
elif i == 'pointer':
pointer()
elif i == 'fist':
fist()
elif i == 'q':
finger4.stop()
finger3.stop()
finger2.stop()
finger1.stop()
GPIO.cleanup()
os._exit(0)
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment