Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/openmv/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, port, baudrate=921600,
self.verinfo = {}
self.transport = None
self.frame_event = False
self.resync_pending = False

def __enter__(self):
"""Context manager entry"""
Expand Down Expand Up @@ -82,6 +83,14 @@ def _send_cmd_wait_resp(self, opcode, channel=0, data=b''):
if not self.is_connected():
raise OMVException("Not connected")

# The device announced a soft reboot: its protocol state (sequence,
# capabilities, dynamic channels) is reset once it is back. Start the
# next command with a resync so both sides agree again.
if self.resync_pending:
# Clear first: _resync() negotiates capabilities through this function.
self.resync_pending = False
self._resync()

# Special handling for reset commands - they never return
if opcode in [Opcode.SYS_RESET, Opcode.SYS_BOOT]:
self.transport.send_packet(opcode, channel, 0, data)
Expand Down Expand Up @@ -109,6 +118,11 @@ def _handle_event(self, channel_id, event):
# Handle system events
if event == EventType.SOFT_REBOOT:
logging.info("🔥 Soft Reboot triggered")
self.resync_pending = True
# In-flight packets from the rebooting device restart at 0.
self.transport.reset_sequence()
# The reboot drops every dynamic channel: refresh the list.
self.pending_channel_events += 1
elif event == EventType.CHANNEL_REGISTERED:
self.pending_channel_events += 1
elif channel_id in self.channels_by_id:
Expand Down Expand Up @@ -334,6 +348,10 @@ def stop(self):
# Stop running script
stdin_id = self.get_channel(name="stdin")
self._channel_ioctl(stdin_id, ChannelIOCTL.STDIN_STOP)
# Stopping a running script soft-reboots the device. Without events
# there is no SOFT_REBOOT notification, so assume a resync is needed.
if not self.caps['events']:
self.resync_pending = True

@retry_if_failed
def exec(self, script):
Expand Down
13 changes: 9 additions & 4 deletions src/openmv/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ def _check_crc(self, crc, buffer, crc_size=16):

def _check_seq(self, sequence, expected_sequence, opcode, flags):
"""Check if sequence is valid or sequence checking is disabled"""
# ACK/NAK packets are accepted with any sequence number: after the
# device resets its protocol state (e.g. a soft reboot) its sequence
# restarts at 0, and the NAK it sends for our out-of-sequence command
# is the only way to learn that a resync is needed.
return (not self.seq_enabled or
(flags & Flags.EVENT) or
(flags & Flags.RTX) or
(flags & (Flags.EVENT | Flags.RTX | Flags.ACK | Flags.NAK)) or
sequence == expected_sequence or
opcode == Opcode.PROTO_SYNC)

Expand Down Expand Up @@ -211,11 +214,13 @@ def recv_packet(self, poll_events=False):
else:
self.send_packet(packet['opcode'], packet['channel'], Flags.ACK)

# Handle event packets
# Handle event packets. Events do not extend the receive timeout:
# a device that keeps emitting events (e.g. stdout notifications
# every 50 ms while its output is unread) must not keep a command
# waiting for its response forever.
if packet['flags'] & Flags.EVENT:
self.event_callback(packet['channel'], 0xFFFF if not packet['length']
else struct.unpack('<H', packet['payload'])[0])
start_time = time.time()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some testing and I can see the issue, if you configure logging you can see the events, so analysis got that right. However, this change seems to be all that's needed to make the script work/recover. To me it seems that all other changes and the firmware PR are not needed. Can you reproduce any other issue with just this fix without anything else?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that line alone still loses the script 1 in 4 times, it just stops the hang.

From Claude:

Measured on an N6 with stock firmware, 20 stop()+exec() cycles on a running script, only the timeout line applied: 15 pass after the 1s timeout + resync, 5 lose the script. The camera keeps ACKing RESET/WRITE/EXEC for ~10 ms after it sends SOFT_REBOOT, the write lands in the stdin buffer the reboot wipes, every command is acknowledged and the host thinks it worked. With the NAK accepted at any sequence number the resync happens on the NAK instead of after the timeout. 1.0.7: 12/12 hang. This branch: 19/20 at 0.6s/cycle, 1 timeout on a slow reboot.

continue

# Update sequence after each packet (including fragments)
Expand Down
Loading