diff --git a/rclpy/timers/minimal_timer/README.md b/rclpy/timers/minimal_timer/README.md new file mode 100644 index 00000000..4094f457 --- /dev/null +++ b/rclpy/timers/minimal_timer/README.md @@ -0,0 +1,33 @@ +# Minimal Timer Example + +This example demonstrates how to create a simple timer node in ROS 2 using rclpy. + +## Overview + +ROS 2 timers allow nodes to execute a callback function periodically. +They are commonly used for tasks such as monitoring loops, periodic updates, +or scheduled processing. + +This example creates a node that executes a callback every second using +`create_timer()`. + +## Running the Example + +Run the node with: + +```bash +python3 minimal_timer.py + +``` + +You should see log messages printed once per second. + +## Example output + +Timer callback triggered +Timer callback triggered +Timer callback triggered + +## Key Concept + +`create_timer()` schedules a callback to be executed periodically. diff --git a/rclpy/timers/minimal_timer/minimal_timer.py b/rclpy/timers/minimal_timer/minimal_timer.py new file mode 100644 index 00000000..d0eee4bd --- /dev/null +++ b/rclpy/timers/minimal_timer/minimal_timer.py @@ -0,0 +1,29 @@ +import rclpy +from rclpy.node import Node + + +class MinimalTimer(Node): + + def __init__(self): + super().__init__('minimal_timer') + + timer_period = 1.0 + self.timer = self.create_timer(timer_period, self.timer_callback) + + def timer_callback(self): + self.get_logger().info('Timer callback triggered') + + +def main(args=None): + rclpy.init(args=args) + + minimal_timer = MinimalTimer() + + rclpy.spin(minimal_timer) + + minimal_timer.destroy_node() + rclpy.shutdown() + + +if __name__ == '__main__': + main()