Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bad timestamp for API print event #140

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

alustig3
Copy link
Contributor

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.

The error can be reproduced with the files below:

task.py

from pyControl.utility import *
from devices import Breakout_1_2, Poke, Rotary_encoder

bb = Breakout_1_2()  # breakout board
center_poke = Poke(bb.port_2, rising_event="center_in")
running_wheel = Rotary_encoder(
    name="running_wheel",
    sampling_rate=40,
)

states = ["stateA"]
events = ["center_in"]

initial_state = "stateA"

v.api_class = "my_api"


def stateA(event):
    pass

my_api.py

from source.gui.api import Api


class my_api(Api):
    def __init__(self):
        pass

    def process_data_user(self, data):
        new_events = [new_event.name for new_event in data["events"]]
        for event in new_events:
            if event == "center_in":
                self.print_message("hi")

The log should show that the api print event occurs about 1ms after the poke event. As seen below, sometimes the timestamp for the api print is wrong.
CleanShot 2025-03-20 at 17 56 07

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.
@ThomasAkam
Copy link
Collaborator

Thanks Andy. As the time_of_processing variable is only used at line 492 can we simplify that bit of code slightly to:

                if checksum == (msg_sum & 0xFFFF):  # Checksum OK.
                    msg_timestamp = int.from_bytes(message[:4], "little")
                    if msg_timestamp > self.timestamp:
                        self.last_message_time = time.time()
                        self.timestamp = msg_timestamp

@alustig3
Copy link
Contributor Author

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants