Sat, 03 Dec 2011 09:16:39 +0100
added some communication definitions
23 | 1 | """ |
2 | FreeSlot project | |
3 | Blackbox communication library | |
4 | """ | |
5 | ||
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
6 | from serial import Serial |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
7 | |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
8 | class SerialCommunicator(): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
9 | def __init__(self, device, speed): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
10 | self.device = device |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
11 | self.speed = speed |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
12 | self.com = None |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
13 | self.connected = False |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
14 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
15 | def connect(self): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
16 | if self.connected: |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
17 | return True |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
18 | self.com = Serial(self.device, baudrate=self.speed, xonxoff=0, timeout=1) |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
19 | self.connected = True |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
20 | return True |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
21 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
22 | def disconnect(self): |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
23 | self.com = None |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
24 | return True |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
25 | |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
26 | def write(self, msg, getanswer=False): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
27 | self.com.write(msg + "\n") |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
28 | if getanswer: |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
29 | return self.readline() |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
30 | return None |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
31 | |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
32 | def readline(self): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
33 | answer = self.com.readline() |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
34 | return string.strip(answer, "\n") |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
35 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
36 | class Blackbox(): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
37 | def __init__(self): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
38 | self.com = None |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
39 | self.info = None |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
40 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
41 | def connect(self, device="/dev/ttyUSB0", speed=115200): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
42 | if self.com == None: |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
43 | self.com = SerialCommunicator(device, speed) |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
44 | if self.com.connected: |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
45 | self.com.disconnect() |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
46 | self.com.connect() |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
47 | self.info = self.readinfo() |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
48 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
49 | def disconnect(): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
50 | self.com.disconnect() |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
51 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
52 | def readinfo(): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
53 | """ |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
54 | Read complete Information from connected box |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
55 | This does not include race+car status! |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
56 | """ |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
57 | return None |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
58 | |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
59 | def progcar(self, carid, command, value): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
60 | """ |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
61 | Send program packets to specified car id |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
62 | valid command: speed, brake |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
63 | valid value: 4 bit integer (0..15) |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
64 | """ |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
65 | return True |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
66 | |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
67 | def setmode(self, mode): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
68 | """ |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
69 | Switch the Blackbox mode |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
70 | Valid modes are: idle, prepare, race |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
71 | note: box will permanently send status info in race mode, so no |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
72 | polling is required |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
73 | """ |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
74 | return True |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
75 | |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
76 | def getmode(self): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
77 | self.readinfo() |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
78 | return self.info["mode"] |