Sun, 22 Dec 2013 02:42:03 +0100
bin jetzt muede und mach dicht hier...
23 | 1 | """ |
2 | FreeSlot project | |
3 | Blackbox communication library | |
4 | """ | |
5 | ||
140
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
6 | import serial, sys, string, time, binascii |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
7 | |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
8 | # how often should a command retried when busy? |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
9 | RETRIES = 10 |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
10 | |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
11 | # define loglevels |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
12 | DEBUG = 20 |
77
cede78304992
finished live CLI, BB firmware improvements and fixes
Malte Bayer <mbayer@neo-soft.org>
parents:
57
diff
changeset
|
13 | LOGLEVEL = 20 |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
14 | def log(level, msg): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
15 | """ |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
16 | Logging output function |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
17 | """ |
77
cede78304992
finished live CLI, BB firmware improvements and fixes
Malte Bayer <mbayer@neo-soft.org>
parents:
57
diff
changeset
|
18 | if level <= LOGLEVEL: print msg |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
19 | |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
20 | class SerialCommunicator(): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
21 | def __init__(self, device, speed): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
22 | self.device = device |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
23 | self.speed = speed |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
24 | self.com = None |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
25 | self.connected = False |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
26 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
27 | def connect(self): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
28 | if self.connected: |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
29 | return True |
27
3e617fcf999a
added exception, started SlotCli class
Malte Bayer <mbayer@neo-soft.org>
parents:
26
diff
changeset
|
30 | try: |
150
cc8721bd2c03
bin jetzt muede und mach dicht hier...
Malte Bayer <mbayer@neo-soft.org>
parents:
145
diff
changeset
|
31 | self.com = serial.Serial(self.device, baudrate=self.speed, |
57
c2e2695c92fe
decreased blocking time for serial rx
Malte Bayer <mbayer@neo-soft.org>
parents:
50
diff
changeset
|
32 | xonxoff=0, timeout=0.1) |
27
3e617fcf999a
added exception, started SlotCli class
Malte Bayer <mbayer@neo-soft.org>
parents:
26
diff
changeset
|
33 | except serial.SerialException, err: |
3e617fcf999a
added exception, started SlotCli class
Malte Bayer <mbayer@neo-soft.org>
parents:
26
diff
changeset
|
34 | print err |
3e617fcf999a
added exception, started SlotCli class
Malte Bayer <mbayer@neo-soft.org>
parents:
26
diff
changeset
|
35 | sys.exit(1) |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
36 | self.connected = True |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
37 | return True |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
38 | |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
39 | def disconnect(self): |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
40 | self.com = None |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
41 | return True |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
42 | |
140
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
43 | def read(self, size = 1): |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
44 | return self.com.read(size) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
45 | |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
46 | def write(self, msg, getanswer=False): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
47 | self.com.write(msg + "\n") |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
48 | if getanswer: |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
49 | return self.readline() |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
50 | return None |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
51 | |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
52 | def readline(self): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
53 | answer = self.com.readline() |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
54 | return string.strip(answer, "\n") |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
55 | |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
56 | def query(self, msg): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
57 | retry = 0 |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
58 | response = self.write(msg, True) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
59 | while (retry < RETRIES) and (response == "BUSY"): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
60 | time.sleep(0.1) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
61 | response = self.write(msg, True) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
62 | retry += 1 |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
63 | log( DEBUG, "%i> %s\n< %s" % (retry, msg, response) ) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
64 | return response |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
65 | |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
66 | |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
67 | class Blackbox(): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
68 | def __init__(self): |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
69 | self.com = None |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
70 | self.info = None |
140
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
71 | self.log = open("serial.log", "w") |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
72 | |
86
79fb119cf3c3
added test option without communication
Malte Bayer <mbayer@neo-soft.org>
parents:
77
diff
changeset
|
73 | def readline(self): |
140
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
74 | if not self.com: return "" |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
75 | # TODO: Binaerprotokoll implementieren und als "alte" ASCII Antwort zurueckgeben! |
139
7127e7082ee0
switched firmware to binary protocol, TODO: client implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
116
diff
changeset
|
76 | # |
140
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
77 | line = self.com.readline() |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
78 | if line == "F:": |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
79 | # parse binary fuel info |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
80 | datalen = ord(self.com.read(1)) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
81 | if datalen != 7: |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
82 | self.log.write("F: ERROR, incorrect length header") |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
83 | return "" |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
84 | data = self.com.read(7) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
85 | if len(data) != 7: |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
86 | self.log.write("F: ERROR LEN%i = %s\n" % (datalen, repr(data))) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
87 | return "" |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
88 | slot = ord(data[0]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
89 | fuel = (ord(data[1]) * 256) + ord(data[2]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
90 | clk = (ord(data[3]) * 256*256*256) + (ord(data[4]) * 256*256) + (ord(data[5]) * 256) + ord(data[6]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
91 | self.com.readline() # clear to next linefeed |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
92 | line = "F:%i:%x:%x\n" % (slot, fuel, clk) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
93 | elif line == "L:": |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
94 | # parse binary lap info |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
95 | datalen = ord(self.com.read(1)) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
96 | if datalen != 12: |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
97 | self.log.write("L: ERROR, incorrect length header") |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
98 | return "" |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
99 | data = self.com.read(12) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
100 | if len(data) != 12: |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
101 | self.log.write("L: ERROR LEN%i = %s\n" % (datalen, repr(data))) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
102 | return "" |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
103 | track = ord(data[0]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
104 | laps = (ord(data[1]) * 256) + ord(data[2]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
105 | slot = ord(data[3]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
106 | diff = (ord(data[4]) * 256*256*256) + (ord(data[5]) * 256*256) + (ord(data[6]) * 256) + ord(data[7]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
107 | clk = (ord(data[8]) * 256*256*256) + (ord(data[9]) * 256*256) + (ord(data[10]) * 256) + ord(data[11]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
108 | self.com.readline() # clear to next linefeed |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
109 | line = "L:%i:%x:%i:%x:%x\n" % (track, laps, slot, diff, clk) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
110 | elif line == "RW:": |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
111 | # parse binary responsewire info |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
112 | datalen = ord(self.com.read(1)) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
113 | if datalen != 8: |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
114 | self.log.write("RW: ERROR, incorrect length header") |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
115 | return "" |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
116 | data = self.com.read(8) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
117 | if len(data) != 8: |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
118 | self.log.write("RW: ERROR LEN%i = %s\n" % (datalen, repr(data))) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
119 | return "" |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
120 | slot = ord(data[0]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
121 | track = ord(data[1]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
122 | sender = ord(data[2]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
123 | status = ord(data[3]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
124 | clk = (ord(data[4]) * 256*256*256) + (ord(data[5]) * 256*256) + (ord(data[6]) * 256) + ord(data[7]) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
125 | self.com.readline() # clear to next linefeed |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
126 | line = "RW:%i:%i:%i:%i:%x\n" % (slot, track, sender, status, clk) |
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
127 | self.log.write(line) |
145
80be5eb283d3
optimized fuses, rx binary crap filter
Malte Bayer <mbayer@neo-soft.org>
parents:
143
diff
changeset
|
128 | if line.find(chr(0)) == -1: |
80be5eb283d3
optimized fuses, rx binary crap filter
Malte Bayer <mbayer@neo-soft.org>
parents:
143
diff
changeset
|
129 | return line |
80be5eb283d3
optimized fuses, rx binary crap filter
Malte Bayer <mbayer@neo-soft.org>
parents:
143
diff
changeset
|
130 | else: |
80be5eb283d3
optimized fuses, rx binary crap filter
Malte Bayer <mbayer@neo-soft.org>
parents:
143
diff
changeset
|
131 | self.log.write("malformed RX\n") |
80be5eb283d3
optimized fuses, rx binary crap filter
Malte Bayer <mbayer@neo-soft.org>
parents:
143
diff
changeset
|
132 | return "" |
86
79fb119cf3c3
added test option without communication
Malte Bayer <mbayer@neo-soft.org>
parents:
77
diff
changeset
|
133 | |
79fb119cf3c3
added test option without communication
Malte Bayer <mbayer@neo-soft.org>
parents:
77
diff
changeset
|
134 | def query(self, msg): |
79fb119cf3c3
added test option without communication
Malte Bayer <mbayer@neo-soft.org>
parents:
77
diff
changeset
|
135 | if self.com: |
79fb119cf3c3
added test option without communication
Malte Bayer <mbayer@neo-soft.org>
parents:
77
diff
changeset
|
136 | return self.com.query(msg) |
79fb119cf3c3
added test option without communication
Malte Bayer <mbayer@neo-soft.org>
parents:
77
diff
changeset
|
137 | return "" |
79fb119cf3c3
added test option without communication
Malte Bayer <mbayer@neo-soft.org>
parents:
77
diff
changeset
|
138 | |
140
f910ad6ed0b6
client binary protocol implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
139
diff
changeset
|
139 | def connect(self, device="/dev/ttyUSB0", speed=57600): |
139
7127e7082ee0
switched firmware to binary protocol, TODO: client implementation
Malte Bayer <mbayer@neo-soft.org>
parents:
116
diff
changeset
|
140 | # old connection speed 57600 |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
141 | if self.com == None: |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
142 | self.com = SerialCommunicator(device, speed) |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
143 | if self.com.connected: |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
144 | self.com.disconnect() |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
145 | self.com.connect() |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
146 | self.info = self.readinfo() |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
147 | |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
148 | def disconnect(self): |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
149 | self.com.disconnect() |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
150 | |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
151 | def readinfo(self): |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
152 | """ |
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
153 | Read complete Information from connected box |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
154 | This does not include race+car status! |
25
646ee4dc3a6b
started communication class and CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
23
diff
changeset
|
155 | """ |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
156 | return None |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
157 | |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
158 | def progcar(self, carid, command, value): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
159 | """ |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
160 | Send program packets to specified car id |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
161 | valid command: speed, brake, fuel |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
162 | valid value: 4 bit integer (0..15) |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
163 | valid carid: 0..5 |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
164 | """ |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
165 | if (carid < 0) or (carid > 5): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
166 | return "ERR - invalid carid" |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
167 | cmd = -1 |
100
039ab094f79b
renamed --speed to --accel and added minimum value 6
Malte Bayer <mbayer@neo-soft.org>
parents:
86
diff
changeset
|
168 | if command == "accel": |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
169 | cmd = 0 |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
170 | if command == "brake": |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
171 | cmd = 1 |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
172 | if command == "fuel": |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
173 | cmd = 2 |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
174 | if (cmd == -1): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
175 | return "ERR - invalid command" |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
176 | if (value<0) or (value>15): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
177 | return "ERR - invalid value" |
100
039ab094f79b
renamed --speed to --accel and added minimum value 6
Malte Bayer <mbayer@neo-soft.org>
parents:
86
diff
changeset
|
178 | if command == "accel" and value < 6: |
039ab094f79b
renamed --speed to --accel and added minimum value 6
Malte Bayer <mbayer@neo-soft.org>
parents:
86
diff
changeset
|
179 | return "ERR - value too low" |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
180 | # transform value 10..15 to A..F |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
181 | if (value>9): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
182 | value = chr(ord("A") + (value-10)) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
183 | command = "P%i%s%i" % (cmd, value, carid) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
184 | response = self.com.query( command ) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
185 | return response |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
186 | |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
187 | def blinkcar(self, carid, blink): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
188 | """ |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
189 | Set car blinking state |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
190 | """ |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
191 | if (carid < 0) or (carid > 5): |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
192 | return "ERR - invalid carid" |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
193 | if blink: |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
194 | return self.com.query( "P48%i" % carid ) |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
195 | else: |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
27
diff
changeset
|
196 | return self.com.query( "P40%i" % carid ) |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
197 | |
143
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
198 | def unlimitedfuel(self, carid, boolval): |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
199 | """ |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
200 | Set unlimited fuel on/off for a car |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
201 | """ |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
202 | if (carid < 0) or (carid > 5): |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
203 | return "ERR - invalid carid" |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
204 | value = 0 |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
205 | if boolval: value = 1 |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
206 | return self.com.query( "U%i%i" % (carid, value) ) |
cb514887cf17
added unlimited fuel for profile (set fuel=0 to enable)
Malte Bayer <mbayer@neo-soft.org>
parents:
140
diff
changeset
|
207 | |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
208 | def speedlimit(self, carid, value): |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
209 | """ |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
210 | Set the maximum controller speed for a car |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
211 | Attention: this is software limited, this does not affect car acceleration! |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
212 | """ |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
213 | if (carid < 0) or (carid > 5): |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
214 | return "ERR - invalid carid" |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
215 | if (value<0) or (value>15): |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
216 | return "ERR - invalid value" |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
217 | # transform value 10..15 to A..F |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
218 | if (value>9): |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
219 | value = chr(ord("A") + (value-10)) |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
220 | return self.com.query( "L%i%s" % (carid, value) ) |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
221 | |
50
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
222 | def speedminimum(self, carid, value): |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
223 | """ |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
224 | Set the minimzm controller speed for a car |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
225 | """ |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
226 | if (carid < 0) or (carid > 5): |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
227 | return "ERR - invalid carid" |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
228 | if (value<0) or (value>15): |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
229 | return "ERR - invalid value" |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
230 | # transform value 10..15 to A..F |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
231 | if (value>9): |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
232 | value = chr(ord("A") + (value-10)) |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
233 | return self.com.query( "S%i%s" % (carid, value) ) |
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
234 | |
116
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
235 | def fueldivisor(self, value): |
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
236 | """ |
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
237 | Set the minimzm controller speed for a car |
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
238 | """ |
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
239 | if (value<0) or (value>255): |
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
240 | return "ERR - invalid value" |
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
241 | return self.com.query( "F:%s" % (value) ) |
c2fc650cc48f
added option to disable freeslot fuel logic
Malte Bayer <mbayer@neo-soft.org>
parents:
100
diff
changeset
|
242 | |
50
84b8ab4cd79e
implemented new --drive command to CLI
Malte Bayer <mbayer@neo-soft.org>
parents:
36
diff
changeset
|
243 | |
26
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
244 | def setmode(self, mode): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
245 | """ |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
246 | Switch the Blackbox mode |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
247 | Valid modes are: idle, prepare, race |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
248 | 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
|
249 | polling is required |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
250 | """ |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
251 | return True |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
252 | |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
253 | def getmode(self): |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
254 | self.readinfo() |
4af697fa5ea9
added some communication definitions
Malte Bayer <mbayer@neo-soft.org>
parents:
25
diff
changeset
|
255 | return self.info["mode"] |