-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Tools/ros2/ardupilot_dds_tests/test/ardupilot_dds_tests/test_clock.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import rclpy | ||
from rosgraph_msgs.msg import Clock | ||
from builtin_interfaces.msg import Time | ||
|
||
|
||
class ClockSubscriber: | ||
def __init__(self): | ||
self.node = rclpy.create_node("clock_subscriber") | ||
self.subscription = self.node.create_subscription(Clock, "/ap/clock", self.clock_callback, 10) | ||
|
||
def clock_callback(self, msg): | ||
try: | ||
# Assert the information here | ||
assert isinstance(msg.clock, Time) | ||
assert isinstance(msg.clock.sec, int) | ||
assert isinstance(msg.clock.nanosec, int) | ||
|
||
print("Assertion passed. Received valid Clock message:") | ||
print(f"Seconds: {msg.clock.sec}") | ||
print(f"Nanoseconds: {msg.clock.nanosec}") | ||
|
||
except AssertionError as e: | ||
print(f"Assertion failed. Invalid Clock message: {e}") | ||
finally: | ||
# Stop the ROS node and exit the program | ||
rclpy.shutdown() | ||
|
||
|
||
def test_clock(args=None): | ||
rclpy.init(args=args) | ||
subscriber = ClockSubscriber() | ||
rclpy.spin(subscriber.node) |