Skip to content

Instantly share code, notes, and snippets.

@kosuke55
Created April 11, 2022 07:02
Show Gist options
  • Save kosuke55/01ca70c0fd598510375193ff50d5eb6c to your computer and use it in GitHub Desktop.
Save kosuke55/01ca70c0fd598510375193ff50d5eb6c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from rosgraph_msgs.msg import Clock
from rclpy.qos import QoSProfile, QoSReliabilityPolicy
class JumpbackDetector(Node):
def __init__(self):
super().__init__('jumpback_detector')
self.clock = None
qos_profile = QoSProfile(depth=1)
qos_profile.reliability = QoSReliabilityPolicy.BEST_EFFORT
self._sub_clock = self.create_subscription(
Clock, '/clock', self._on_clock, qos_profile)
def _on_clock(self, msg):
self.clock = msg.clock
if __name__ == '__main__':
rclpy.init()
node = JumpbackDetector()
previous_time = None
while True:
rclpy.spin_once(node, timeout_sec=2)
current_time = node.clock.sec + node.clock.nanosec / 1000000000.
if previous_time is not None:
if current_time < previous_time:
print("Jumpback {} -> {}".format(previous_time, current_time))
previous_time = current_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment