-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maestro.py
176 lines (158 loc) · 5.79 KB
/
Maestro.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import termios
import time
import glob
import logging
class Maestro:
def __init__(self, serial, servos=24, resetPin=255, deviceNumber=255, CRCEnabled=False):
self.serial = serial
self.resetPin = resetPin
self.deviceNumber = deviceNumber
self.servos = servos
self.targets = {}
self.moveStatus = {}
self.registered = {}
for i in range(self.servos):
self.targets[i] = 0
self.moveStatus[i] = 0
self.registered[i] = False
self.stream = None
self.begin()
def begin(self):
self.Close()
devices = sorted(glob.glob("/dev/serial/by-id/usb*Pololu*Maestro*" + self.serial + "*"))
for device in devices:
try:
logging.info("Using Maestro servo controller on " + device + ".")
self.stream = os.open(device, os.O_RDWR | os.O_NOCTTY)
options = termios.tcgetattr(self.stream)
options[0] &= ~(termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IXON | termios.IXOFF)
options[1] &= ~(termios.ONLCR | termios.OCRNL)
options[3] &= ~(termios.ECHO | termios.ECHONL | termios.ICANON | termios.ISIG | termios.IEXTEN)
termios.tcsetattr(self.stream, termios.TCSANOW, options)
break
except:
self.stream = None
logging.warning("Unable to connect to Maestro servo controller")
def Close(self):
if self.stream == None:
return
try:
os.close(self.stream)
except:
pass
self.stream = None
def Reset(self):
if self.resetPin != 255:
pass
#digitalWrite(self.resetPin, LOW)
#pinMode(self.resetPin, OUTPUT)
#delay(1)
#pinMode(self.resetPin, INPUT)
#delay(200)
def Disable(self, channel):
if self.stream == None:
self.begin()
try:
os.write(self.stream, "".join(map(chr, [0x84, channel, 0, 0])))
#os.flush(self.stream)
except:
self.Close()
def Enable(self, channel):
if self.stream == None:
self.begin()
try:
self.SetTarget(channel, self.targets[channel])
except:
self.Close()
def SetTarget(self, channel, target):
if self.stream == None:
self.begin()
if self.targets[channel] == target:
return
try:
self.moveStatus[channel] = -1
self.targets[channel] = target
target = int(target * 4)
os.write(self.stream, "".join(map(chr, [0x84, channel, target & 0x7F, (target >> 7) & 0x7F])))
#os.flush(self.stream)
except:
self.Close()
def SetSpeed(self, channel, speed):
if self.stream == None:
self.begin()
try:
speed = int(speed * 4)
os.write(self.stream, "".join(map(chr, [0x87, channel, speed & 0x7F, (speed >> 7) & 0x7F])))
#os.flush(self.stream)
except:
self.Close()
def SetAcceleration(self, channel, acceleration):
if self.stream == None:
self.begin()
try:
acceleration = int(acceleration * 4)
os.write(self.stream, "".join(map(chr, [0x89, channel, acceleration & 0x7F, (acceleration >> 7) & 0x7F])))
#os.flush(self.stream)
except:
self.Close()
def GetPosition(self, channel):
if self.stream == None:
self.begin()
try:
os.write(self.stream, "".join(map(chr, [0x90, channel])))
#os.flush(self.stream)
r1 = os.read(self.stream, 1)
r2 = os.read(self.stream, 1)
return (ord(r1) + 256 * ord(r2)) / 4
except:
self.Close()
return 0
def Update(self):
if self.stream == None:
return
try:
for i in range(self.servos):
if self.registered[i]:
if self.targets[i] != 0 and self.moveStatus[i] < 0:
p = self.GetPosition(i)
if p != 0 and self.targets[i] == p:
self.moveStatus[i] = time.time()
elif self.moveStatus[i] > 0:
if self.moveStatus[i] + 0.5 < time.time():
#self.Disable(i)
self.moveStatus[i] = 0
except:
self.Close()
class MaestroServo:
def __init__(self, maestro, channel, minimum=1000, maximum=2000, speed=0, acceleration=0):
self.maestro = maestro
self.channel = channel
self.minimum = minimum
self.maximum = maximum
self.speed = speed
self.acceleration = acceleration
self.maestro.registered[channel] = True
self.maestro.SetSpeed(channel, speed)
self.maestro.SetAcceleration(channel, acceleration)
def SetTarget(self, target):
self.Enable()
self.maestro.SetSpeed(self.channel, self.speed)
self.maestro.SetAcceleration(self.channel, self.acceleration)
self.maestro.SetTarget(self.channel, int(self.minimum + target * (self.maximum - self.minimum)))
#self.maestro.SetAcceleration(self.channel, self.acceleration)
#self.maestro.SetSpeed(self.channel, self.speed)
def Open(self):
self.SetTarget(1)
def Close(self):
self.SetTarget(0)
def Enable(self):
self.maestro.Enable(self.channel)
def Disable(self):
self.maestro.Disable(self.channel)
def Toggle(self):
mid = (self.minimum + self.maximum) / 2
if self.maestro.targets[self.channel] > mid:
self.SetTarget(0)
else:
self.SetTarget(1)