-
Notifications
You must be signed in to change notification settings - Fork 0
/
REVModule.py
227 lines (177 loc) · 7.6 KB
/
REVModule.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from SDKDevice import REVMotor
import REVServo, REVADC, REVDIO, REVI2C
from REVConstants import *
##Note: Modules are hubs (lynx modules)
class Module:
"""Lynx Module device"""
def __init__(self, commObj, address, parent, errors, manualBulkread = True):
self.errorHandler = errors
self.commObj = commObj
self.address = address
self.parent = parent
self.motors = []
self.servos = []
self.i2cChannels = []
self.adcPins = []
self.dioPins = []
self.bulkInputData = None
self.analogInput = []
self.digitalInput = []
self.encoderPosition = []
self.encoderVelocity = []
self.motorIsOvercurrent = []
self.isBulkread = manualBulkread
def init_periphs(self):
"""Initalizes all devices (Motors, Servos, Digital I/O, and ADC"""
try:
for i in range(0, 4):
self.motors.append(REVMotor.Motor(self.commObj, i, self.address, self))
self.motors[-1].setMode(0, 1)
self.motors[-1].setPower(0)
self.i2cChannels.append(REVI2C.I2CChannel(self.commObj, i, self.address))
for j in range(0, 8):
self.dioPins.append(REVDIO.DIOPin(self.commObj, j, self.address))
for k in range(0, 6):
self.servos.append(REVServo.Servo(self.commObj, k, self.address, self))
self.servos[-1].init()
for l in range(0, 4):
self.adcPins.append(REVADC.ADCPin(self.commObj, l, self.address))
except:
self.errorHandler.throwError('Error while initializing device')
def killSwitch(self):
"""Disable everything for safe stop"""
for i in range(0, 4):
self.motors[i].disable()
for k in range(0, 6):
self.servos[k].disable()
def getParentStatus(self):
"""Get status of parent module (?)"""
return self.parent
def getAddress(self):
"""Get address of lynx module, functionally identical to getModuleAddress()"""
return self.address
def getStatus(self):
"""Get lynx module """
return self.commObj.getModuleStatus(self.address)
def getModuleAddress(self):
"""Get address of lynx module, functionally identical to getAddress()"""
return self.address
def sendKA(self):
"""Keep alive for lynx module"""
return self.commObj.keepAlive(self.address)
def sendFailSafe(self):
"""Failsafe for lynx module"""
self.commObj.failSafe(self.address)
def setAddress(self, newAddress):
"""Set the address of the lynx module and update all devices addresses"""
self.commObj.setNewModuleAddress(self.address, newAddress)
self.address = newAddress
for motor in self.motors:
motor.setDestination(newAddress)
for servo in self.servos:
servo.setDestination(newAddress)
for i2cChannel in self.i2cChannels:
i2cChannel.setDestination(newAddress)
for adcPin in self.adcPins:
adcPin.setDestination(newAddress)
for dioPin in self.dioPins:
dioPin.setDestination(newAddress)
def getInterface(self, interface):
"""Get the interface of the lynx module, direct serial or RS485 (?)"""
return self.commObj.queryInterface(self.address, interface)
def setLEDColor(self, red, green, blue):
"""Set the color of the LED on the lynx module"""
self.commObj.setModuleLEDColor(self.address, red, green, blue)
def getLEDColor(self):
"""Get the current RGB value of the lynx module"""
return self.commObj.getModuleLEDColor(self.address)
def setLEDPattern(self, pattern):
"""
Set a LED patern for the LED of the lynx module
Example:
from REVmessages import LEDPattern
hub = REVModules()
my_pattern = LEDPattern()
my_pattern.set_step(0, 255, 0, 0, 10) # set first step to red for 1 second
my_pattern.set_step(1, 0, 255, 0, 10) # set second step to green for 1 second
hub.REVModules[0].setLEDPattern(my_pattern)
hub.REVModules[0].keepAlive()
"""
return self.commObj.setModuleLEDPattern(self.address, pattern)
def setLogLevel(self, group, verbosity):
"""Set the logging level"""
self.commObj.debugLogLevel(self.address, group, verbosity)
def getBulkData(self):
"""Get bulk input data, analagous to FTC SDK bulkread"""
return self.commObj.getBulkInputData(self.address)
def enableCharging(self):
"""Enable Android Phone charging"""
self.commObj.phoneChargeControl(self.address, 1)
def disableCharging(self):
"""Disable Android Phone charging"""
self.commObj.phoneChargeControl(self.address, 0)
def chargingEnabled(self):
"""Check android phone charging state"""
return self.commObj.phoneChargeQuery(self.address)
def debugOutput(self, length, hint):
"""Debug the output"""
self.commObj.injectDataLogHint(self.address, length, hint)
def setAllDIO(self, values):
"""Set all Digital I/O"""
REVDIO.setAllDIOOutputs(self.address, values)
def getAllDIO(self):
"""Read all Digital I/O"""
return REVDIO.getAllDIOInputs(self.address)
def getVersionString(self):
"""Read the firmware version"""
versionRaw = '' + self.commObj.readVersionString(self.address)
versionStr = ''
for i in range(0, int(len(versionRaw) / 2)):
tmpHex = int(str(versionRaw)[i * 2] + str(versionRaw)[i * 2 + 1], 16)
versionStr = versionStr + chr(tmpHex)
return versionStr
def setIMUBlockReadConfig(self, startRegister, numberOfBytes, readInterval_ms):
"""Set the Configuration of IMU block read"""
REVI2C.imuBlockReadConfig(self.address, startRegister, numberOfBytes, readInterval_ms)
def getIMUBlockReadConfig(self):
"""Get the configuration of IMU block read"""
return REVI2C.imuBlockReadQuery(self.address)
def getBulkRead(self):
if self.bulkInputData != None:
return self.bulkInputData
else:
self.bulkInputData = self.getBulkData()
return self.bulkInputData
def invalidateBulkCache(self):
self.bulkInputData = None
self.analogInput = []
self.digitalInput = []
self.encoderPosition = []
self.encoderVelocity = []
self.motorIsOvercurrent = []
def parseBulkData(self):
read = self.getBulkRead()
for port in range(0,4):
self.analogInput[port] = int((read[port + ANALOG_OFFSET] & (1 << (16 - 1))))
for port in range(0,4):
self.digitalInput[port] = bool(read[DIGITAL_OFFSET] & 1)
for port in range(0,4):
self.encoderPosition[port] = int(read[port + POSITION_OFFSET])
for port in range(0,4):
self.encoderVelocity[port] = int((read[port + VELOCITY_OFFSET] & (1 << (16 - 1))))
for port in range(0,4):
self.motorIsOvercurrent[port] = bool(read[STATUS_OFFSET] & 1)
def isBulkread(self):
return self.isBulkread
def getAnalog(self, port):
return self.analogInput[port]
def getEncoderPosition(self, port):
return self.encoderPosition[port]
def getEncoderVelocity(self, port):
return self.encoderVelocity[port]
def getDigitalIO(self, port):
return self.digitalInput[port]
def getIsOverCurrent(self, port):
return self.motorIsOvercurrent[port]
def throwError(self, error):
self.errorHandler.throwError(error)