Skip to content

Commit 394fd70

Browse files
committed
Add minimal rclpy timer example demonstrating create_timer() usage
1 parent 503696e commit 394fd70

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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()

0 commit comments

Comments
 (0)