forked from robotika/osgar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
can.py
419 lines (364 loc) · 11.1 KB
/
can.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/python
"""
CAN bus interface over serial port.
usage:
can.py [<file to parse> | [modules to watch (default all)]]
"""
import serial
import datetime
import time
import os
import os.path
import socket
import subprocess
import sys
class LogEnd(Exception):
pass
class LogIt():
"Log communication via serial com"
def __init__( self, com ):
self._com = com
self._logFile = None
self.relog( 'logs/b' )
def relog( self, prefix ):
today = datetime.date.today()
t = time.localtime()[3:6]
suffix = "%02d%02d%02d_%02d%02d%02d.log" % ( today.year % 100, today.month, today.day, t[0], t[1], t[2] )
if prefix.endswith(suffix):
# hack for already whole name - note, that 1s delay will cause detection failure
filename = prefix
else:
filename = prefix + suffix
self._logFile = open( filename, "wb" )
print "LogIt:", filename
return filename
def read( self, numChars ):
s = self._com.read( numChars )
for ch in s:
self._logFile.write( chr(0x01) )
self._logFile.write( ch )
self._logFile.flush()
return s
def write( self, chars ):
for ch in chars:
self._logFile.write( chr(0x00) )
self._logFile.write( ch )
self._logFile.flush()
self._com.write( chars )
#-------------------------------------------------------------------
class ReplayLog():
"Read & verify log"
def __init__( self, filename, assertWrite=True ):
print "ReplyLog", filename
self._logFile = open( filename, "rb" )
self.assertWrite = assertWrite
def read( self, numChars ):
s = []
for i in range(numChars):
marker = self._logFile.read(1)
if not marker:
raise LogEnd()
assert( marker == chr(0x01) )
s.append(self._logFile.read(1))
if not s[-1]:
raise LogEnd()
return ''.join(s)
def write( self, chars ):
for ch in chars:
marker = self._logFile.read(1)
if not marker:
raise LogEnd()
assert( marker == chr(0x00) )
verifyCh = self._logFile.read(1)
if not verifyCh:
raise LogEnd()
if self.assertWrite:
assert( verifyCh == ch )
#-------------------------------------------------------------------
class ReplayLogInputsOnly():
"Read & verify log"
def __init__( self, filename ):
print "ReplyLogInputOnly", filename
self._logFile = open( filename, "rb" )
def read( self, numChars ):
s = []
for i in range(numChars):
while( self._logFile.read(1) not in [chr(0x01), ''] ):
c = self._logFile.read(1) # skip write output
if not c:
raise LogEnd()
assert( i == 0 ) # the packets should be complete
s.append(self._logFile.read(1))
if not s[-1]:
raise LogEnd()
return ''.join(s)
def write( self, chars ):
pass
#-------------------------------------------------------------------
class DummyMemoryLog():
"Helper for parsing logs"
def __init__( self ):
self.data = 10*[0xFF] # workaround for initial sync
def read( self, numChars ):
s = ""
if self.data:
s = "".join( [chr(x) for x in self.data[:numChars]] )
self.data = self.data[numChars:]
return s
def write( self, chars ):
pass
#-------------------------------------------------------------------
class RTSerial():
"Handle i/o via real-time serial pipe"
# An OSError (32: Broken Pipe) occures when the real-time serial
# process (rt42) has died. This process dies if we stop reading data
# from it for a while (eg. during a restart) and the communication
# buffer gets full. => the communication may need a restart,
# which this class performs in such a case.
def __init__( self, com ):
self.sub = None
self.com = com
self.__connect()
def __connect( self ):
sys.stderr.write("RTSerial - connect ...\n")
self.__disconnect()
self.sub = subprocess.Popen(["/home/robot/system/rt42"])
time.sleep(0.1)
self._fd = os.open( self.com, os.O_RDWR )
def __disconnect( self, sleep = False ):
if self.sub:
# os.close( self._fd )
if sleep:
time.sleep(0.1)
# self.sub.kill() # not available in Python 2.5
#killCmd = ["kill", "-9", "%d" % self.sub.pid]
pid = self.sub.pid
del self.sub
self.sub = None
os.close( self._fd )
del self._fd
self._fd = None
time.sleep(1.0)
killCmd = ["kill", "-9", "%d" % pid]
print killCmd
subprocess.call( killCmd )
time.sleep(1.0)
print "killed"
def __del__( self ):
self.__disconnect(sleep=True)
def read( self, numChars ):
if self.sub and self.sub.returncode:
print self.sub.returncode
s = []
n = 0
while n < numChars:
try:
rec = os.read( self._fd, numChars - len(s) )
s.append(rec)
n += len(rec)
except OSError: # Broken Pipe
self.__connect()
return ''.join(s)
def write( self, chars ):
try:
os.write( self._fd, chars )
except OSError: # Broken pipe
self.__connect()
#-------------------------------------------------------------------
class CAN():
def __init__(self, com = None, defaultDTR = False, verbose = 1, skipInit = False):
self.verbose = verbose
self.skipInit = skipInit
if com:
useDTR = defaultDTR
self.com = com
else:
if os.name == 'nt': # windows (could be also used sys.platform == 'win32'
ser = serial.Serial('COM20',115200,dsrdtr=0) # alt COM9
useDTR = True
else:
# ser = RTSerial('/dev/rtp0') # alt /dev/ttyS0
ser = serial.Serial('/dev/ttyS0', 115200, dsrdtr=1)
ser.setRTS()
useDTR = True #False
if useDTR:
ser.setDTR(0)
self.com = LogIt( ser )
if useDTR:
assert( self.readPacket() == (0xFE,[0x10]) ) # wait for BootUp message
if not skipInit:
self.syncFF()
self.sendControl( 0x31 ) # start bridge
def __del__(self):
if not self.skipInit:
self.sendControl( 0x30 ) # stop bridge
del self.com
print "CAN terminated"
def syncFF(self):
"synchronize CANBridge communication"
self.com.write(chr(0xff)*10)
i = 0
while i < 10:
s = self.com.read(1)
if ord(s[0]) == 0xFF:
i += 1
else:
i = 0
if self.verbose > 0:
print "syncFF OK"
def sendControl(self, command):
self.com.write(chr(0xfe)+chr(command))
def readPacket(self):
b = 0xFF
while b == 0xFF:
b = ord( self.com.read( 1 ) )
header = [b]
header.append( ord( self.com.read( 1 ) ) )
rtr=(header[1]>>4)&0x1
len=(header[1])&0x0f
id=((header[0])<<3)|(((header[1])>>5)&0x1f)
if rtr:
if header[0]==0xfe:
if header[1] == 0x10:
print "BRIDGE: boot-up message"
elif header[1] == 0x30:
print "BRIDGE: connected to CAN, error active"
else:
print "Error:%x%x"%(header[0],header[1])
return header[0], [header[1]]
else:
print "RTR:", id, "len=", len
return id,[len]
else:
#print "id ",id,"len",len,"rtr ",rtr
data=[ord(x) for x in self.com.read(len)]
return id,data
def sendData(self,id,data):
header=[(id>>3)&0xff,(id<<5)&0xe0|(len(data)&0xf)]
packet="".join([chr(x) for x in header+data])
self.com.write(packet)
def printPacket( self, id, data ):
print hex(id), ":", data
def sendOperationMode( self ):
self.sendData(0,[1,0])
def sendPreoperationMode( self ):
self.sendData(0,[128,0])
def collectPeriodPackets( self ):
packets = {}
id = 0
while id != 0x281: #id!=128:
id,data=self.readPacket()
print id, data
packets[ id ] = data
return packets
def relog( self, prefix ):
if "relog" in dir(self.com):
return self.com.relog( prefix )
def resetModules( self, configFn=None ):
print "Reset all modules"
self.sendData( 0, [129,0] ) # reset all
ackBootup = [] # Heart Beat 0, bootup, after reset
ackPreop = [] # HB 127
ackOp = [] # HB 5
while len( ackBootup ) == 0 or len( ackBootup ) > len( ackPreop ):
id, data = self.readPacket()
if (id & 0xF80) == 0x700:
nodeID = id & 0x7F
if data[0] == 0:
print "Started module", nodeID
ackBootup.append( nodeID )
if data[0] == 127:
if nodeID in ackBootup:
print "Module", nodeID, "in preoperation."
ackPreop.append( nodeID )
else:
print "WARNING!!! Module", nodeID, "preop BEFORE bootup!!!"
if configFn:
print "--- Extra Configuration ---"
configFn( self )
print "------- Switch to Operation mode --------"
self.sendOperationMode()
while len( ackPreop ) > len( ackOp ):
id, data = self.readPacket()
if (id & 0xF80) == 0x700:
nodeID = id & 0x7F
if data[0] == 5:
if nodeID in ackPreop:
print "Module", nodeID, "in operation."
ackOp.append( nodeID )
else:
print "WARNING!!! Module", nodeID, "op BEFORE preop!!!"
print "collecting some packets ..."
countHB = 0
while countHB < len( ackOp) * 3: # ie approx 3s
id, data = self.readPacket()
if (id & 0xF80) == 0x700:
countHB += 1
if countHB % len( ackOp ) == 0:
print countHB/len( ackOp ), '...'
assert( len(data) == 1 )
if data[0] != 5:
nodeID = id & 0x7F
print 'ERROR - module', nodeID, 'data', data
return ackOp
def main():
can = CAN()
can.sendOperationMode()
print can.collectPeriodPackets()
print can.collectPeriodPackets()
print can.collectPeriodPackets()
can.sendPreoperationMode()
def parseFileG( filename, watchModules = [] ):
# dump log file
dummy = DummyMemoryLog()
can = CAN( com=dummy )
file = open( filename, "rb" )
d = []
prevIo = 2
binData = [ord(x) for x in file.read()] + [2,0] # dummy termination
pairs = zip( binData[::2], binData[1::2] )
for io, data in pairs:
if io == prevIo:
d.append( data )
else:
if prevIo == 0:
# parse send
while d:
assert( len(d) >= 2 )
if d[0] == 0xFE:
print "OutFE:", hex(d[0]), hex(d[1])
dataSize = 0
else:
id = (int(d[0])<<3) | (d[1]>>5)
dataSize = d[1] & 0x0F
if not watchModules or (id & 0x7F) in watchModules:
yield 0, id, d[2:2+dataSize]
d = d[2+dataSize:]
elif prevIo == 1:
assert( len(d) >= 2 )
assert( dummy.data == [] )
dummy.data = d
while dummy.data:
if dummy.data == 10*[0xFF]:
# skip syncFF without new packets
dummy.data = []
break
a,b = can.readPacket()
if not watchModules or (a&0x7F) in watchModules:
yield 1, a, b
d = [data]
prevIo = io
file.close()
def parseFile( filename, watchModules = [] ):
for io, id, data in parseFileG( filename, watchModules ):
if io == 0:
print "------------"
print "Out:", hex(id), data
elif io == 1:
print hex(id), ":", data
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
parseFile( sys.argv[1], [int(x) for x in sys.argv[2:]] )
else:
main()