Skip to content

Commit

Permalink
feat(fw_update): add flag for listing online nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
hashemmm96 committed Oct 15, 2024
1 parent e2136ae commit 4a4b8d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
49 changes: 26 additions & 23 deletions rover_py/flasher/flasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,32 @@ def __init__(self, ch, config=None):
self.ch.setBusOutputControl(canlib.Driver.NORMAL)
self.ch.busOn()

def detect_online_nodes(self):
self.ch.write(rover.set_action_mode(mode=rover.ActionMode.FREEZE))

self.ch.iocontrol.flush_rx_buffer()
self.ch.writeWait(
rover.give_base_number(response_page=1), self.default_timeout_ms
)
time.sleep(0.1) # Allow time to respond

# Check responses
while True:
try:
frame = self.ch.read(timeout=10)
if frame:
self.online_node_ids.add(frame.id - rover.BASE_NUMBER)

except (canlib.exceptions.CanTimeout, canlib.exceptions.CanNoMsg):
break

return self.online_node_ids

def run(self):
if not self.config:
raise ValueError(f"run method requires config parameter")

self.__detect_online_nodes()
self.detect_online_nodes()

# Flash all online nodes
for node in self.config.get_nodes():
Expand All @@ -33,7 +54,7 @@ def run(self):
self.__restart_all()

def run_single(self, id, binary_file=None, config_file=None):
self.__detect_online_nodes()
self.detect_online_nodes()

if id not in self.online_node_ids:
raise ValueError(
Expand All @@ -54,7 +75,7 @@ def run_single(self, id, binary_file=None, config_file=None):

def format_fs(self, id):
prefix = f"node {id}"
self.__detect_online_nodes()
self.detect_online_nodes()

if id not in self.online_node_ids:
raise ValueError(
Expand All @@ -79,7 +100,7 @@ def enter_recovery_mode(self, binary_file, config_file):
# Send default letter. Send should succeed only if node is on the bus again.
self.ch.writeWait(rover.default_letter(), 30_000) # 30 second delay
self.__enter_bootloader(0)
self.__detect_online_nodes()
self.detect_online_nodes()
id = self.online_node_ids.pop()
binary = _read_binary(binary_file)
config = _read_json(config_file)
Expand Down Expand Up @@ -169,6 +190,7 @@ def __block_transfer(self, envelope, binary):
chunks[-1] += [0] * (chunk_size - last_chunk_length)

current_page_number = 3

batch_size = 128 # Set to avoid TX buffer overflow

try:
Expand Down Expand Up @@ -201,25 +223,6 @@ def __block_transfer(self, envelope, binary):
if int.from_bytes(received.data[1:3], byteorder="little") != 0x0000:
raise RuntimeError("Got wrong bundle request, wanted 0x0000")

def __detect_online_nodes(self):
self.ch.write(rover.set_action_mode(mode=rover.ActionMode.FREEZE))

self.ch.iocontrol.flush_rx_buffer()
self.ch.writeWait(
rover.give_base_number(response_page=1), self.default_timeout_ms
)
time.sleep(0.1) # Allow time to respond

# Check responses
while True:
try:
frame = self.ch.read(timeout=10)
if frame:
self.online_node_ids.add(frame.id - rover.BASE_NUMBER)

except (canlib.exceptions.CanTimeout, canlib.exceptions.CanNoMsg):
break

def __enter_bootloader(self, id):
try:
# Restart target
Expand Down
16 changes: 14 additions & 2 deletions rover_py/fw_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def parse_args():
"--bitrate",
default="125k",
choices=["125k", "250k", "500k", "1m"],
help="Use this option if you have changed the default bitrate",
help="use this option if you have changed the default bitrate",
)
parser.add_argument(
"-l", "--list-online", action="store_true", help="list online nodes"
)

subparsers = parser.add_subparsers(required=False, dest="subcommand")
Expand Down Expand Up @@ -115,7 +118,16 @@ def run_flasher(args):
try:
print("Running flasher...")

if args.subcommand == "single":
if args.list_online:
f = flasher.Flasher(ch)
node_ids = f.detect_online_nodes()
print("Found nodes:")
for id in node_ids:
print(f" {id}: {rover.City(id).name}")

sys.exit(0)

elif args.subcommand == "single":
run_single(ch, args)

elif args.subcommand == "recover":
Expand Down

0 comments on commit 4a4b8d4

Please sign in to comment.