-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservo3.py
45 lines (34 loc) · 843 Bytes
/
servo3.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
#!/usr/bin/env python
#
# Demonstrates Servo control
#
# MUST use pin 18
import time
def setupServo():
with open("/sys/class/pwm/pwm0/mode", "w") as f:
f.write("servo")
def toggleServo(enabled):
value = "0"
if enabled:
value = "1"
with open("/sys/class/pwm/pwm0/active", "w") as f:
f.write(value)
# Duty cycle can vary between 1-180
def setServoPosition(pos):
if pos <= 0:
pos = 1
elif pos > 180:
pos = 180
with open("/sys/class/pwm/pwm0/servo", "w") as f:
f.write(str(pos))
def servoTest():
setupServo()
toggleServo(True)
while True:
for i in range(0, 32, 1):
print ("Servo position (degrees): ",i)
setServoPosition(i)
time.sleep(.2)
toggleServo(False)
if __name__ == "__main__":
servoTest()