Skip to content

Commit eb53850

Browse files
committed
[Bdt] Error code summaries for each loop
Signed-off-by: Greg Balke <gbalke@berkeley.edu> Topic: tools_error_summaries
1 parent d49ab03 commit eb53850

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

bd_tools/src/bd_tools/bin/control_motor.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,18 @@ def make_type(x, to_type):
5353

5454
boards.initMotor(client, board_ids)
5555

56-
def callback() -> bool:
56+
def callback() -> int:
5757
boards.clearWDGRST(client)
5858

5959
try:
6060
boards.driveMotor(client, board_ids, actuations, mode)
61-
except (comms.ProtocolError, comms.MalformedPacketError):
62-
return False
61+
except (comms.ProtocolError, comms.MalformedPacketError) as e:
62+
if "id: " in str(e):
63+
return int(str(e).split("id: ")[1][0])
64+
else:
65+
return -1
6366

64-
return True
67+
return 0
6568

6669
loop = utils.DebugLoop(callback, args.num_iters, iters_per_print=1000)
6770

bd_tools/src/bd_tools/bin/read_sensor.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def action(args):
8181

8282
num_boards = len(board_ids)
8383

84-
def callback() -> bool:
84+
def callback() -> int:
8585
boards.clearWDGRST(client)
8686

8787
try:
@@ -98,8 +98,12 @@ def callback() -> bool:
9898
print("Board:", bid, message.format(args.sensor, val))
9999

100100
except (comms.MalformedPacketError, comms.ProtocolError) as e:
101-
print(e)
102-
return False
101+
if "id: " in str(e):
102+
return int(str(e).split("id: ")[1][0])
103+
else:
104+
return -1
105+
106+
return 0
103107

104108
loop = utils.DebugLoop(
105109
callback=callback, num_iters=args.num_iters, iters_per_print=1000

bd_tools/src/bd_tools/utils.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Defines a set of convenient utilities to use in scripts."""
22
import time
33
from typing import Callable
4+
from collections import Counter
45

56

67
class DebugLoop:
@@ -17,41 +18,63 @@ def __init__(
1718
1819
Arguments:
1920
callback: called for each iteration of the loop. This callable
20-
takes no arguments and should return True if successful else,
21-
False.
21+
takes no arguments and should return 0 if successful and an
22+
error code otherwise (this will be used to report statistics).
2223
num_iters: number of iterations to run before exiting.
2324
iters_per_print: number of iterations between prints.
2425
"""
2526
self._callback = callback
2627
self._num_iters = num_iters
2728
self._iters_per_print = iters_per_print
2829

29-
self._errors = 0
30+
self._errors = {}
31+
self._cummulative_errors = Counter({})
3032
self._iters = 0
3133
self._iter_start_time = None
3234

3335
def _loop_func(self):
34-
self._errors += 0 if self._callback() else 1
36+
err = self._callback()
37+
assert type(err) is int
38+
39+
if err != 0:
40+
if err not in self._errors:
41+
self._errors[err] = 1
42+
else:
43+
self._errors[err] += 1
3544
self._iters += 1
3645

3746
if self._iters % self._iters_per_print == 0:
3847
self._print_func()
3948
self._iter_start_time = time.time()
4049

50+
def _print_error_codes(self, errors):
51+
total_errors = sum(errors.values())
52+
for error_code, count in sorted(errors.items()):
53+
code_errors = count / total_errors * 100
54+
print(
55+
f"Error code: {error_code} was {round(code_errors, 2)}% of "
56+
f"errors."
57+
)
58+
4159
def _print_func(self):
4260
now = time.time()
4361
diff = now - self._iter_start_time
4462

4563
freq = self._iters_per_print / diff
46-
error_rate = self._errors / self._iters_per_print * 100
64+
total_errors = sum(self._errors.values())
65+
error_rate = total_errors / self._iters_per_print * 100
4766
print(
4867
f"Loop frequency is {round(freq, 2)}hz at "
68+
f"({total_errors}/{self._iters_per_print})"
4969
f"{round(error_rate, 2)}% error rate."
5070
)
5171

72+
self._print_error_codes(self._errors)
73+
5274
# Reset statistics variables.
5375
self._start_time = now
54-
self._errors = 0
76+
self._cummulative_errors += Counter(self._errors)
77+
self._errors = {}
5578

5679
def loop(self):
5780
"""Loops the callback. Can be escaped with ctrl^c."""
@@ -67,4 +90,6 @@ def loop(self):
6790
f"Interrupted. Loop exiting. Completed {self._iters} "
6891
f"iterations."
6992
)
93+
print(f"Cummultive errors:")
94+
self._print_error_codes(self._cummulative_errors)
7095
pass

0 commit comments

Comments
 (0)