-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo.py
63 lines (52 loc) · 1.63 KB
/
servo.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''Class to run servo motor.'''
import RPi.GPIO as gpio
import time
class Servo():
# Configures servoPin as output pin for servo.
def __init__(self,servoPin):
self.servoPin = servoPin
gpio.setwarnings(False)
gpio.setmode(gpio.BCM)
gpio.setup(self.servoPin,gpio.OUT)
# Starts pwm for servo control at servoPin.
def start(self,pwmFreq):
self.pwmFreq = int(pwmFreq)
self.pwm = gpio.PWM(self.servoPin,self.pwmFreq)
self.pwm.start(1)
# Sends servo rotor to specified angle.
def goto(self,angle):
self.step = (angle*0.055)+2
self.pwm.ChangeDutyCycle(self.step)
# Runs servo between two angles to and fro once.
def sweep(self,startAngle,endAngle,stepDelay): #Angle in degree from 0 to 180.
self.sAngle = startAngle
self.eAngle = endAngle
self.delay = stepDelay
for i in range(self.sAngle,self.eAngle):
step = (i*0.055)+2
self.pwm.ChangeDutyCycle(step)
time.sleep(self.delay)
for j in range(self.eAngle,self.sAngle,-1):
step = (j*0.055)+2
self.pwm.ChangeDutyCycle(step)
time.sleep(self.delay)
# Moves a servo once between defined positions.
def slide(self,startAngle,endAngle,stepDelay):
self.sAngle = startAngle
self.eAngle = endAngle
self.delay = stepDelay
if self.sAngle < self.eAngle:
for ii in range(self.sAngle,self.eAngle):
step = (ii*0.055)+2
self.pwm.ChangeDutyCycle(step)
time.sleep(self.delay)
elif self.sAngle > self.eAngle:
for ii in range(self.sAngle,self.eAngle,-1):
step = (ii*0.055)+2
self.pwm.ChangeDutyCycle(step)
time.sleep(self.delay)
if __name__ == "__main__":
servo1 = Servo(18)
servo1.start(50)
servo1.sweep(60,120,0.05)
servo1.slide(0,180,0.05)