Example topic listener
Minimal ROS 2 chatter subscriber used by the launch-script examples; importing defines the node, while the entry point initializes and spins ROS.
scripts.pdf-launch-scripts.simple_listener · Source
Examples and demos · Guide · Symbol index
How it is used
Use this small example to follow a ROS subscription from node creation to message handling. main() owns ROS startup and shutdown; ROS calls MinimalSubscriber.listener_callback() for each chatter message. This listener is separate from the EROBS task system.
Start here
Callable |
Purpose |
|---|---|
Initialize ROS, construct the listener, and spin until execution returns, then destroy the node and shut down ROS. |
|
Log the payload of each chatter message in an “I heard” line. |
Functions
main()
Initialize ROS, construct the listener, and spin until execution returns, then destroy the node and shut down ROS.
Entry point · Source
main(args=None)
Parameter |
Meaning |
|---|---|
|
Optional ROS initialization argument list; None uses process arguments. |
Returns: None after spin returns.
Errors: No try/finally: an exception or interrupt during spin can bypass explicit cleanup.
Side effects: Starts ROS and blocks processing subscriptions; normal completion destroys the node and ROS context.
MinimalSubscriber
A ROS node that subscribes to String messages on chatter and logs their contents.
Inherits from: Node.
MinimalSubscriber.listener_callback()
Log the payload of each chatter message in an “I heard” line.
Callback · Source
listener_callback(self, msg)
Parameter |
Meaning |
|---|---|
|
std_msgs.msg.String received on chatter; its data field is logged. |
Returns: None.
Errors: Malformed message/logger errors propagate.
Side effects: Writes an INFO ROS log.
Referenced in source (1)
MinimalSubscriber.__init__— scripts.pdf-launch-scripts.simple_listener
MinimalSubscriber.__init__()
Create minimal_subscriber and retain a String subscription on relative topic chatter with queue depth 10.
Lifecycle · Source
__init__(self)
Errors: ROS node or subscription creation errors propagate.
Side effects: Registers a ROS node and subscription.
Referenced in source (1)
main— scripts.pdf-launch-scripts.simple_listener