An inspection review may need to pair each camera frame with the most recent recorded robot state available at the frame time. The camera and robot may log at different rates, with occasional gaps. Choosing the closest timestamp in either direction can pull a later measurement into the review. Carrying the previous state through a long gap can also create a pairing that looks plausible but is too old for the inspection.
A backward timestamp match selects the latest state at or before each frame time. An age limit leaves the frame unmatched when that state is older than the review allows. This is an offline way to reconstruct what the recorded state stream indicated. It can help diagnose inspection logs, and it makes no estimate of the robot's true pose between measurements.
Use a fixture with a gap and a future neighbor
This example uses integer milliseconds on a common time axis. Each index identifies a row in a separate state table. The 30 ms age limit is for the fixture. A real inspection needs its own tolerance derived from the capture timing, motion and measurement requirements.
from bisect import bisect_right
def pose_row_at(pose_ms, frame_ms, max_age_ms):
index = bisect_right(pose_ms, frame_ms) - 1
if index < 0:
return None
age_ms = frame_ms - pose_ms[index]
return index if age_ms <= max_age_ms else None
pose_ms = [100, 120, 170]
frames = [95, 120, 149, 150, 151, 168, 171]
matches = [pose_row_at(pose_ms, frame, 30) for frame in frames]
assert matches == [None, 1, 1, 1, None, None, 2]
assert pose_row_at([100, 120, 120, 170], 120, 30) == 2
The frame at 95 ms precedes every state sample. At 120 ms, the frame matches the sample with the same time. A frame at 149 ms uses the state at 120 ms, even though the future sample at 170 ms is closer. At 150 ms the state age is exactly 30 ms, so it passes. At 151 ms the state is 31 ms old, and the frame is unmatched. The frame at 168 ms also stays unmatched even though a new state will arrive just two milliseconds later. At 171 ms the new sample is available and only one millisecond old.
The search assumes the timestamps are sorted in ascending order. Sort the state rows and their timestamps together so that the index still points to the right record. The last assertion illustrates a deterministic tie rule. When two states have the same timestamp, this code selects the last one in the sorted sequence. Keep a source sequence number if order reflects how the logger emitted the rows. If the duplicates conflict and the true order is unknown, flag the timestamp for review.
Decide which time the match represents
Comparing the camera and robot integers only makes sense after establishing a shared time base. Verify the units, counter origin, and clock mapping before the join. A camera timestamp may describe exposure while receiving software records the arrival time. A robot record may similarly carry the controller's measurement time or the host's receipt time. Record the convention used. If the question is which state the inspection application had already received, use receipt time and account for how receipt is logged. A controller measurement can have an earlier event time even when its record arrived late.
Separate state streams before matching. Each station and robot should only receive matches from its own state history. Clock resets and reboots can also require separate segments, particularly when a device resets a local counter. For table pipelines, pandas documents a backward direction, optional grouping keys, an exact-time option, and a tolerance in merge_asof. Confirm the required sort order for the merge key when building the grouped input.
Keep the match and its evidence together
Log the frame timestamp, selected state timestamp, state age, and row or sequence ID. Preserve unmatched outcomes so a reviewer can tell whether a frame had no previous record or had only an older record beyond the allowed age. Monitor the distribution of state age for each station during a review. A group of failures after a logger pause can suggest a data coverage issue, and should be traceable to the original records.
Include at least one frame before the first sample, an exact-time match, an age-limit boundary and a frame just before a later sample in the regression case. If the pipeline supports multiple stations, test that a nearby state from another station never fills a gap in the target station. These checks reproduce mistakes that evenly spaced fixtures with synchronized camera and robot logs rarely expose.
Sources
Python standard library, bisect
https://docs.python.org/3/library/bisect.html
pandas API reference, merge_asof
https://pandas.pydata.org/docs/reference/api/pandas.merge_asof.html