lifecycle_talker

This file is ai generated. Do not edit this file directly. Instead, edit the node source code and run the generate-node-docs command to update this file.

Description

A demonstration lifecycle node that publishes string messages to illustrate ROS 2 lifecycle management in Python. The node implements the managed lifecycle pattern, transitioning through various states (unconfigured, inactive, active, finalized) and only publishing messages when in the active state.

Publishers

  • lifecycle_chatter (example_interfaces/msg/String)

    • Lifecycle-managed publisher that sends sequential “Lifecycle HelloWorld” messages with an incrementing counter

    • Only publishes when the node is in the active state

    • Publishing rate: 1 Hz

Services

As a lifecycle node, this node automatically provides the following lifecycle management services:

Lifecycle States and Transitions

Primary States

  1. Unconfigured - Initial state after node creation

  2. Inactive - Configured but not yet active

  3. Active - Fully operational, publishing messages

  4. Finalized - Terminal state after shutdown

State Transitions

The node implements the following lifecycle callbacks:

  • on_configure() - Called during transition from unconfigured → inactive

    • Creates the lifecycle publisher on topic lifecycle_chatter

    • Creates a 1 Hz timer for publishing messages

    • Returns SUCCESS to complete transition to inactive state

  • on_activate() - Called during transition from inactive → active

    • Enables the lifecycle publisher (handled automatically by parent class)

    • Logs activation event

    • Returns SUCCESS to complete transition to active state

  • on_deactivate() - Called during transition from active → inactive

    • Disables the lifecycle publisher (handled automatically by parent class)

    • Logs deactivation event

    • Returns SUCCESS to complete transition to inactive state

  • on_cleanup() - Called during transition from inactive → unconfigured

    • Destroys the timer

    • Destroys the publisher

    • Returns SUCCESS to complete transition to unconfigured state

  • on_shutdown() - Called during transition to finalized state

    • Cleans up timer and publisher resources

    • Can be called from any state

    • Returns SUCCESS to complete transition to finalized state

Transition Commands

The node can be controlled using the lifecycle service client or CLI:

# Get current state
ros2 lifecycle get /lc_talker

# Configure the node
ros2 lifecycle set /lc_talker configure

# Activate the node
ros2 lifecycle set /lc_talker activate

# Deactivate the node
ros2 lifecycle set /lc_talker deactivate

# Cleanup (go back to unconfigured)
ros2 lifecycle set /lc_talker cleanup

# Shutdown the node
ros2 lifecycle set /lc_talker shutdown

Example Usage

Running the Node Directly

Start the lifecycle talker node:

ros2 run lifecycle_py lifecycle_talker

The node starts in the unconfigured state and will not publish messages until activated.

Controlling the Node Lifecycle

In separate terminals, control the lifecycle:

# Terminal 1: Run the node
ros2 run lifecycle_py lifecycle_talker

# Terminal 2: Configure the node
ros2 lifecycle set /lc_talker configure

# Terminal 3: Activate the node (starts publishing)
ros2 lifecycle set /lc_talker activate

# Terminal 4: Monitor the messages
ros2 topic echo /lifecycle_chatter

Using the Launch File

Launch the complete lifecycle demo with talker, listener, and service client:

ros2 launch lifecycle_py lifecycle_demo_launch.py

This launch file starts:

  • The lifecycle talker node

  • A lifecycle listener node (from the lifecycle package)

  • A lifecycle service client (from the lifecycle package) for automated state transitions

Example Output

When inactive:

[INFO] [lc_talker]: Lifecycle publisher is inactive. Messages are not published.

When active:

[INFO] [lc_talker]: Lifecycle publisher is active. Publishing: [Lifecycle HelloWorld #0]
[INFO] [lc_talker]: Lifecycle publisher is active. Publishing: [Lifecycle HelloWorld #1]
[INFO] [lc_talker]: Lifecycle publisher is active. Publishing: [Lifecycle HelloWorld #2]

Programmatic State Transitions

You can also interact with the lifecycle services programmatically:

from lifecycle_msgs.srv import ChangeState, GetState
from lifecycle_msgs.msg import Transition
import rclpy

# Get current state
get_state_client = node.create_client(GetState, '/lc_talker/get_state')
request = GetState.Request()
future = get_state_client.call_async(request)

# Change state to configure
change_state_client = node.create_client(ChangeState, '/lc_talker/change_state')
request = ChangeState.Request()
request.transition.id = Transition.TRANSITION_CONFIGURE
future = change_state_client.call_async(request)

# Change state to activate
request.transition.id = Transition.TRANSITION_ACTIVATE
future = change_state_client.call_async(request)