Skip to content

Commit bcf66c1

Browse files
committed
this is pretty nice now
1 parent 5c5a56c commit bcf66c1

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

selfdrive/debug/can_print_changes.py

+32-13
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
import cereal.messaging as messaging
88
from tools.lib.logreader import logreader_from_route_or_segment
99

10+
RED = '\033[91m'
11+
CLEAR = '\033[0m'
1012

11-
def update(msgs, bus, low_to_high, high_to_low, quiet=False):
13+
def update(msgs, bus, dat, low_to_high, high_to_low, quiet=False):
1214
for x in msgs:
1315
if x.which() != 'can':
1416
continue
1517

1618
for y in x.can:
1719
if y.src == bus:
18-
i = int.from_bytes(y.dat, byteorder='big')
20+
dat[y.address] = y.dat
1921

22+
i = int.from_bytes(y.dat, byteorder='big')
2023
l_h = low_to_high[y.address]
2124
h_l = high_to_low[y.address]
2225

@@ -34,35 +37,51 @@ def update(msgs, bus, low_to_high, high_to_low, quiet=False):
3437

3538

3639
def can_printer(bus=0, init_msgs=None, new_msgs=None):
37-
logcan = messaging.sub_sock('can')
40+
logcan = messaging.sub_sock('can', timeout=10)
3841

42+
dat = defaultdict(int)
3943
low_to_high = defaultdict(int)
4044
high_to_low = defaultdict(int)
4145

4246
if init_msgs is not None:
43-
update(init_msgs, bus, low_to_high, high_to_low, quiet=True)
47+
update(init_msgs, bus, dat, low_to_high, high_to_low, quiet=True)
48+
49+
low_to_high_init = low_to_high.copy()
50+
high_to_low_init = high_to_low.copy()
4451

4552
if new_msgs is not None:
46-
update(new_msgs, bus, low_to_high, high_to_low)
53+
update(new_msgs, bus, dat, low_to_high, high_to_low)
4754
else:
4855
# Live mode
49-
while 1:
50-
can_recv = messaging.drain_sock(logcan, wait_for_one=True)
51-
update(can_recv, bus, low_to_high, high_to_low)
56+
try:
57+
while 1:
58+
can_recv = messaging.drain_sock(logcan)
59+
update(can_recv, bus, dat, low_to_high, high_to_low)
60+
time.sleep(0.02)
61+
except KeyboardInterrupt:
62+
pass
63+
64+
print("\n\n")
65+
for addr in sorted(dat.keys()):
66+
init = low_to_high_init[addr] & high_to_low_init[addr]
67+
now = low_to_high[addr] & high_to_low[addr]
68+
d = now & ~init
69+
if d == 0:
70+
continue
71+
b = d.to_bytes(len(dat[addr]), byteorder='big')
72+
byts = ''.join([(c if c == '0' else f'{RED}{c}{CLEAR}') for c in str(binascii.hexlify(b))[2:-1]])
73+
print(f"{hex(addr).ljust(6)}({str(addr).ljust(4)})", byts)
5274

5375

5476
if __name__ == "__main__":
5577
desc = """Collects messages and prints when a new bit transition is observed.
5678
This is very useful to find signals based on user triggered actions, such as blinkers and seatbelt.
5779
Leave the script running until no new transitions are seen, then perform the action."""
58-
5980
parser = argparse.ArgumentParser(description=desc,
6081
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
61-
6282
parser.add_argument("--bus", type=int, help="CAN bus to print out", default=0)
63-
64-
parser.add_argument("init", type=str, help="Route or segment to initialize with")
65-
parser.add_argument("comp", type=str, help="Route or segment to compare against init")
83+
parser.add_argument("init", type=str, nargs='?', help="Route or segment to initialize with")
84+
parser.add_argument("comp", type=str, nargs='?', help="Route or segment to compare against init")
6685

6786
args = parser.parse_args()
6887

0 commit comments

Comments
 (0)