Skip to content

Commit a54bdca

Browse files
authored
fix bad timestamp for API print event (#140)
* prevent latest timestamp from going back in time It was possible for an analog message to enter the message processing queue and have its older timestamp assigned as the latest timestamp. If the timing was just right, this could occasionally cause the get_timestamp() function to produce an out of sequence/incorrect timestamp. This commit fixes the problem by only updating the latest timestamp with the message's timestamp, if the message's timestamp is newer.
1 parent 130e263 commit a54bdca

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

source/communication/pycboard.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -481,21 +481,23 @@ def process_data(self):
481481
ID = int.from_bytes(content_bytes[:2], "little")
482482
data = array(self.sm_info.analog_inputs[ID]["dtype"], content_bytes[2:])
483483
content = (ID, data)
484-
message_sum = sum(message[:8]) + sum(data)
484+
msg_sum = sum(message[:8]) + sum(data)
485485
else:
486-
message_sum = sum(message)
486+
msg_sum = sum(message)
487487
# Process message.
488-
if checksum == message_sum & 0xFFFF: # Checksum OK.
489-
self.last_message_time = time.time()
490-
self.timestamp = int.from_bytes(message[:4], "little")
488+
if checksum == (msg_sum & 0xFFFF): # Checksum OK.
489+
msg_timestamp = int.from_bytes(message[:4], "little")
490+
if msg_timestamp > self.timestamp:
491+
self.last_message_time = time.time()
492+
self.timestamp = msg_timestamp
491493
if msg_type in (MsgType.EVENT, MsgType.STATE):
492494
content = int(content_bytes.decode()) # Event/state ID.
493495
elif msg_type in (MsgType.PRINT, MsgType.WARNG):
494496
content = content_bytes.decode() # Print or error string.
495497
elif msg_type == MsgType.VARBL:
496498
content = content_bytes.decode() # JSON string
497499
self.sm_info.variables.update(json.loads(content))
498-
new_data.append(Datatuple(time=self.timestamp, type=msg_type, subtype=msg_subtype, content=content))
500+
new_data.append(Datatuple(time=msg_timestamp, type=msg_type, subtype=msg_subtype, content=content))
499501
else: # Bad checksum
500502
new_data.append(
501503
Datatuple(time=self.get_timestamp(), type=MsgType.WARNG, content="Bad data checksum.")
@@ -526,7 +528,7 @@ def trigger_event(self, event_name, source="u"):
526528
def get_timestamp(self):
527529
"""Get the current pyControl timestamp in ms since start of framework run."""
528530
seconds_elapsed = time.time() - self.last_message_time
529-
return self.timestamp + round(1000 * (seconds_elapsed))
531+
return self.timestamp + round(1000 * seconds_elapsed)
530532

531533
def send_serial_data(self, data, command, cmd_type=""):
532534
"""Send data to the pyboard while framework is running."""

0 commit comments

Comments
 (0)