Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions rclpy/timers/minimal_timer/README.md
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions rclpy/timers/minimal_timer/minimal_timer.py
Original file line number Diff line number Diff line change
@@ -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()