-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServoKit_Mock.py
67 lines (57 loc) · 2.37 KB
/
ServoKit_Mock.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
class ServoKit(object):
default_angle = 90
panPort = 0
tiltPort = 1
tiltAngle = 90
panAngle = 90
def __init__(self, num_ports):
print(' ...Initializing "' + str(self) + '"...')
def setAngle(self, port, angle):
if port == 0:
self.panAngle = angle
if port == 1:
self.tiltAngle = angle
def reset(self, port):
if port == 0:
self.panAngle = self.default_angle
if port == 1:
self.tiltAngle = self.default_angle
def getAngle(self, port):
if port == 0:
return self.panAngle
if port == 1:
return self.tiltAngle
def setActuatorState(self, value):
# Value should contain a delmited command, which
# must be passed in one of the following forms:
# pan=+5 -- adds 5 degrees to the current pan angle...
# tilt=-5 -- subtracts 5 degrees tfromo the current tilt angle...
# pan=reset -- resets the pan to default angle...
# tilt=97 -- sets the tile angle to exactly 97 degrees...
cmdAction = value.split('=')
portToUse = -1
print(cmdAction[0])
print(cmdAction[1])
# Determine which port we're trying to access...
if cmdAction[0] == "tilt":
portToUse = self.tiltPort
if cmdAction[0] == "pan":
portToUse = self.panPort
# Determine the nature of the command we're trying to execute...
if str(cmdAction[1]) == "reset":
print('Resetting "' + cmdAction[0] + '" servo...')
self.reset(portToUse)
if str(cmdAction[1]) == "test":
print('Test called for "' + cmdAction[0] + '" servo...')
self.reset(0)
self.reset(1)
if cmdAction[1].startswith('+') or cmdAction[1].startswith('-'):
currentAngle = self.getAngle(portToUse)
targetAngle = currentAngle + int(cmdAction[1])
print('Modifying "' + cmdAction[0] + '" angle by ' + cmdAction[1] + ' to ' + str(targetAngle) + '...')
self.setAngle(portToUse, targetAngle)
if cmdAction[1].isnumeric():
self.setAngle(portToUse, int(cmdAction[1]))
return { 'tilt' : str(self.tiltAngle), 'pan': str(self.panAngle)}
def getCurrentState(self):
return { 'tilt' : str(self.tiltAngle), 'pan': str(self.panAngle) }