-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo.py
39 lines (29 loc) · 961 Bytes
/
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
import time
from gpiozero import Servo as GPIOZeroServo
from gpiozero.pins.pigpio import PiGPIOFactory
#set GPIO Pins
GPIO_PILOT = 16
class Servo():
#place servo in left position
def rotate_to_left(self):
self.servo.max()
time.sleep(0.5)
#place servo in right position
def rotate_to_right(self):
self.servo.min()
time.sleep(0.5)
#place servo in middle position
def rotate_to_middle(self):
self.servo.mid()
time.sleep(0.5)
def is_rotated_left(self):
return self.servo.value == 1.0
def is_rotated_right(self):
return self.servo.value == -1.0
def is_rotated_middle(self):
return self.servo.value == 0.0 or self.servo.value==-0.0
def __init__(self, gpio_number):
# initialize servo
factory = PiGPIOFactory()
self.servo = GPIOZeroServo(gpio_number, pin_factory=factory)
self.servo.mid()