-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservoControl.py
More file actions
executable file
·81 lines (68 loc) · 2.08 KB
/
servoControl.py
File metadata and controls
executable file
·81 lines (68 loc) · 2.08 KB
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
78
79
80
81
# Servo Control
from __future__ import division
import time
# Import the PCA9685 module.
import Adafruit_PCA9685
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()
# Sets the frequency for the Adafruit PCA9685 servo hat.
pwm.set_pwm_freq(50)
# The Servo class is used to control a servo from the Adafruit PCA9685
# servo hat. Each instance of the class controls a single servo.
class Servo:
#
def __init__(self, Min, Max, Port):
self.min = Min
self.max = Max
self.curpos = self.min
self.port = Port
#
def minMax(self):
return '{} {}'.format(self.min, self.max)
#
def increase(self):
if self.curpos < self.max:
self.curpos = int(self.curpos + 1)
pwm.set_pwm(self.port, 0, self.curpos)
return True
else:
return False
#
def decrease(self):
if self.curpos > self.min:
self.curpos = int(self.curpos - 1)
pwm.set_pwm(self.port, 0, self.curpos)
return True
else:
return False
#
def increaseRate(self, value):
if self.curpos < self.max:
self.curpos = int(self.curpos + value)
if self.curpos > self.max:
self.curpos = self.max
pwm.set_pwm(self.port, 0, self.curpos)
return True
else:
return False
#
def decreaseRate(self, value):
if self.curpos > self.min:
self.curpos = int(self.curpos - value)
if self.curpos < self.min:
self.curpos = self.min
pwm.set_pwm(self.port, 0, self.curpos)
return True
else:
return False
#
def neutralPos(self):
neut = (self.min + self.max) / 2
self.curpos = int(neut)
pwm.set_pwm(self.port, 0, self.curpos)
def setMin(self):
self.curpos = self.min
pwm.set_pwm(self.port, 0, self.curpos)
def setMax(self):
self.curpos = self.max
pwm.set_pwm(self.port, 0, self.curpos)