/
servo.py
62 lines (46 loc) · 1.34 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author : Original author WindVoiceVox
# Original Author Github: https://github.com/WindVoiceVox/Raspi_SG90
# http://elecrow.com/
import RPi.GPIO as GPIO
import time
import sys
class sg90:
def __init__( self, direction):
self.pin = 22
GPIO.setmode( GPIO.BCM )
GPIO.setup( self.pin, GPIO.OUT )
self.direction = int( direction )
self.servo = GPIO.PWM( self.pin, 50 )
self.servo.start(0.0)
def cleanup( self ):
self.servo.ChangeDutyCycle(self._henkan(0))
time.sleep(0.3)
self.servo.stop()
GPIO.cleanup()
def currentdirection( self ):
return self.direction
def _henkan( self, value ):
return 0.05 * value + 7.0
def setdirection( self, direction, speed ):
for d in range( self.direction, direction, int(speed) ):
self.servo.ChangeDutyCycle( self._henkan( d ) )
self.direction = d
time.sleep(0.1)
self.servo.ChangeDutyCycle( self._henkan( direction ) )
self.direction = direction
def main():
s = sg90(0)
try:
while True:
print("Turn left ...")
s.setdirection( 100, 10 )
time.sleep(0.5)
print("Turn right ...")
s.setdirection( -100, -10 )
time.sleep(0.5)
except KeyboardInterrupt:
s.cleanup()
if __name__ == "__main__":
main()