-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathotto_servo.py
77 lines (67 loc) · 2.09 KB
/
otto_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#-- A simple class for controlling hobby servos. Modeled after the ESP8266 Arduino Servo Driver
#-- OttDIY Python Project, 2020
import machine
try:
from esp32 import Servo as espServo
useServo = True
except ImportError:
"""This version of esp32 doesn't support Servo, use PWM instead"""
def espServo(_arg):
print("espServo not defined")
raise ImportError
useServo = False
class Servo:
def __init__(self, freq = 50, min_us = 1000, max_us = 2000, max_ang = 180):
global useServo
self.min_us = min_us
self.max_us = max_us
self.freq = freq
self.max_ang = max_ang
self.pin = None
if useServo:
self.servo = None
else:
self.pwm = None
self._attached = False
def attach(self, pin):
global useServo
self.pin = machine.Pin(pin)
if useServo:
self.servo = espServo(self.pin)
else:
self.pwm = machine.PWM(self.pin, freq = self.freq)
self._attached = True
def detach(self):
global useServo
if useServo:
self.servo.deinit()
else:
self.pwm.deinit()
self._attached = False
def attached(self):
return self._attached
def write_us(self, us):
"""Set the signal to be ``us`` microseconds long. Zero disables it."""
global useServo
if useServo:
self.servo.duty(us)
else:
"""PWM uses duty as a value from 0-1024"""
duty = int(us / (1000000 / self.freq / 1024))
self.pwm.duty(duty)
def write(self, degrees):
"""Move to the specified angle in ``degrees``."""
degrees = degrees % 360
if degrees < 0:
degrees += 360
if degrees > 180:
degrees = 180
total_range = self.max_us - self.min_us
us = self.min_us + total_range * degrees // self.max_ang
self.write_us(us)
def __deinit__(self):
global useServo
if useServo:
self.servo.deinit()
else:
self.pwm.deinit()