lieocc
lieocc

Reputation: 1

why the output blocks when i use launch in ros2

I am a new player of ros2. And one day i create two nodes called topic_helloword_pub and topic_helloworld_sub and a launch file called simple.launch.py to start the nodes.There are no mistake or warning in the terminal when i use ros2 launch to start the nodes,but it blocks,nothing output until i press 'ctrl+c' stoping it.After stoping,each node will output one message and end However,when i use ros2 run starting the nodes respectively,everything is running well.I want to know why and how to fix it. I use windows and foxy version of ros2. code of topic_helloword_pub

import rclpy                                     
from rclpy.node import Node                      
from std_msgs.msg import String                  


class PublisherNode(Node):
    
    def __init__(self, name):
        super().__init__(name)                                    
        self.pub = self.create_publisher(String, "chatter", 10)   
        self.timer = self.create_timer(0.5, self.timer_callback)  
        
    def timer_callback(self):                                     
        msg = String()                                            
        msg.data = 'Hello World'                                 
        self.pub.publish(msg)                                     
        self.get_logger().info('Publishing: "%s"' % msg.data)     
        
def main(args=None):                                 
    rclpy.init(args=args)                           
    node = PublisherNode("topic_helloworld_pub")     
    rclpy.spin(node)                                
    node.destroy_node()                              
    rclpy.shutdown()                                 

code of topic_helloworld_sub

import rclpy                                     
from rclpy.node   import Node                    
from std_msgs.msg import String                  


class SubscriberNode(Node):
    
    def __init__(self, name):
        super().__init__(name)                                   
        self.sub = self.create_subscription(\
            String, "chatter", self.listener_callback, 10)        

    def listener_callback(self, msg):                             
        self.get_logger().info('I heard: "%s"' % msg.data)        
        
def main(args=None):                                
    rclpy.init(args=args)                            
    node = SubscriberNode("topic_helloworld_sub")    
    rclpy.spin(node)                                
    node.destroy_node()                             
    rclpy.shutdown()                                 

code of simple.launch.py

from launch import LaunchDescription           
from launch_ros.actions import Node            

def generate_launch_description():             
    return LaunchDescription([                 
        Node(                                  
            package='launch_test',          
            executable='topic_helloworld_pub', 
        ),
        Node(                                  
            package='launch_test',          
            executable='topic_helloworld_sub', 
        ),
    ])

output in the terminal

G:\python-work\ROS2\Lab1>ros2 launch launch_test simple.launch.py  
[INFO] [launch]: All log files can be found below C:\Users\Administrator\.ros\log\2024-07-04-16-51-07-430723-MS-UISXVFQQVNRF-11664
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [topic_helloworld_pub.EXE-1]: process started with pid [4524]
[INFO] [topic_helloworld_sub.EXE-2]: process started with pid [15504]
[WARNING] [launch]: user interrupted with ctrl-c (SIGINT)
[ERROR] [topic_helloworld_pub.EXE-1]: process has died [pid 4524, exit code 3221225786, cmd 'G:\python-work\ROS2\Lab1\install\launch_test\lib\launch_test\topic_helloworld_pub.EXE --ros-args'].
[topic_helloworld_pub.EXE-1] [INFO] [1720083068.578953900] [topic_helloworld_pub]: Publishing: "Hello World"

[ERROR] [topic_helloworld_sub.EXE-2]: process has died [pid 15504, exit code 3221225786, cmd 'G:\python-work\ROS2\Lab1\install\launch_test\lib\launch_test\topic_helloworld_sub.EXE --ros-args'].
[topic_helloworld_sub.EXE-2] [INFO] [1720083068.579490800] [topic_helloworld_sub]: I heard: "Hello World"

Upvotes: 0

Views: 213

Answers (1)

Daniil
Daniil

Reputation: 1

Your problem arises from using rclpy.spin_once(node) instead of rclpy.spin(node) in your code.

When you use rclpy.spin_once(node), your node performs only one processing cycle, then shuts down, and therefore you only see the message after pressing Ctrl+C. When you use rclpy.spin(node), the node continues to run and process events until you stop it.

    import rclpy                                     
    from rclpy.node import Node                      
    from std_msgs.msg import String                  
    
    
    class PublisherNode(Node):
        
        def __init__(self, name):
            super().__init__(name)                                    
            self.pub = self.create_publisher(String, "chatter", 10)   
            self.timer = self.create_timer(0.5, self.timer_callback)  
            
        def timer_callback(self):                                     
            msg = String()                                            
            msg.data = 'Hello World'                                 
            self.pub.publish(msg)                                     
            self.get_logger().info('Publishing: "%s"' % msg.data)     
            
    def main(args=None):                                 
        rclpy.init(args=args)                           
        node = PublisherNode("topic_helloworld_pub")     
        rclpy.spin(node)  # Use rclpy.spin() instead of rclpy.spin_once()                              
        node.destroy_node()                              
        rclpy.shutdown()                                 
    
    if __name__ == "__main__":
        main()

Upvotes: 0

Related Questions