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:
~/change_state(lifecycle_msgs/srv/ChangeState)Requests a state transition for the lifecycle node
~/get_state(lifecycle_msgs/srv/GetState)Retrieves the current state of the lifecycle node
~/get_available_states(lifecycle_msgs/srv/GetAvailableStates)Lists all possible states the lifecycle node can be in
~/get_available_transitions(lifecycle_msgs/srv/GetAvailableTransitions)Lists all available state transitions from the current state
~/get_transition_graph(lifecycle_msgs/srv/GetAvailableTransitions)Retrieves the complete state transition graph
Lifecycle States and Transitions
Primary States
Unconfigured - Initial state after node creation
Inactive - Configured but not yet active
Active - Fully operational, publishing messages
Finalized - Terminal state after shutdown
State Transitions
The node implements the following lifecycle callbacks:
on_configure()- Called during transition from unconfigured → inactiveCreates the lifecycle publisher on topic
lifecycle_chatterCreates a 1 Hz timer for publishing messages
Returns
SUCCESSto complete transition to inactive state
on_activate()- Called during transition from inactive → activeEnables the lifecycle publisher (handled automatically by parent class)
Logs activation event
Returns
SUCCESSto complete transition to active state
on_deactivate()- Called during transition from active → inactiveDisables the lifecycle publisher (handled automatically by parent class)
Logs deactivation event
Returns
SUCCESSto complete transition to inactive state
on_cleanup()- Called during transition from inactive → unconfiguredDestroys the timer
Destroys the publisher
Returns
SUCCESSto complete transition to unconfigured state
on_shutdown()- Called during transition to finalized stateCleans up timer and publisher resources
Can be called from any state
Returns
SUCCESSto 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)