-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservoGPIO.py
46 lines (40 loc) · 1.41 KB
/
servoGPIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import RPi.GPIO as GPIO
import time
class ServoGPIO():
# GPIO pins for the different servos
signalBottom = 17
signalMiddle = 27
signalTop = 22
# Frequency of 50Hz gives period of 20 ms
frequency = 50
def __init__(self):
# Use Broadcom SOC channel numbers for pins
GPIO.setmode(GPIO.BCM)
# Set GPIO pins to optput
GPIO.setup(self.signalBottom, GPIO.OUT)
GPIO.setup(self.signalMiddle, GPIO.OUT)
GPIO.setup(self.signalTop, GPIO.OUT)
# Initialize all the PWMs with the frequency
self.pwmBottom = GPIO.PWM(self.signalBottom, self.frequency)
self.pwmMiddle = GPIO.PWM(self.signalMiddle, self.frequency)
self.pwmTop = GPIO.PWM(self.signalTop, self.frequency)
# Neutral for HS-322HD is 1.5ms
# 2.5% (0 degrees) 12.5% (180 degrees)
# Set all servos to starting position
self.pwmBottom.start(2.5)
self.pwmMiddle.start(2.5)
self.pwmTop.start(2.5)
time.sleep(2)
def servoGPIOCleanup(self):
GPIO.cleanup()
def servosSet(self, pwm1, pwm2, pwm3, dutyCycle):
pwm1.ChangeDutyCycle(dutyCycle)
pwm2.ChangeDutyCycle(dutyCycle)
pwm3.ChangeDutyCycle(dutyCycle)
time.sleep(1)
def main():
pablo = ServoGPIO()
pablo.servosSet(pablo.pwmBottom, pablo.pwmMiddle, pablo.pwmTop, 12.5)
pablo.servoGPIOCleanup()
if __name__ == "__main__":
main()