File tree Expand file tree Collapse file tree
rclpy/topics/minimal_timer Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Minimal Timer Example
2+
3+ This example demonstrates how to create a simple timer node in ROS 2 using rclpy.
4+
5+ ## Overview
6+
7+ ROS 2 timers allow nodes to execute a callback function periodically.
8+ They are commonly used for tasks such as monitoring loops, periodic updates,
9+ or scheduled processing.
10+
11+ This example creates a node that executes a callback every second using
12+ ` create_timer() ` .
13+
14+ ## Running the Example
15+
16+ Run the node with:
17+
18+ ``` bash
19+ python3 minimal_timer.py
20+
21+ ```
22+
23+ You should see log messages printed once per second.
24+
25+ ## Example output
26+
27+ Timer callback triggered
28+ Timer callback triggered
29+ Timer callback triggered
30+
31+ ## Key Concept
32+
33+ ` create_timer() ` schedules a callback to be executed periodically.
Original file line number Diff line number Diff line change 1+ import rclpy
2+ from rclpy .node import Node
3+
4+
5+ class MinimalTimer (Node ):
6+
7+ def __init__ (self ):
8+ super ().__init__ ('minimal_timer' )
9+
10+ timer_period = 1.0
11+ self .timer = self .create_timer (timer_period , self .timer_callback )
12+
13+ def timer_callback (self ):
14+ self .get_logger ().info ('Timer callback triggered' )
15+
16+
17+ def main (args = None ):
18+ rclpy .init (args = args )
19+
20+ minimal_timer = MinimalTimer ()
21+
22+ rclpy .spin (minimal_timer )
23+
24+ minimal_timer .destroy_node ()
25+ rclpy .shutdown ()
26+
27+
28+ if __name__ == '__main__' :
29+ main ()
You can’t perform that action at this time.
0 commit comments