为什么当我在ros2中使用launch时输出会阻塞

问题描述 投票:0回答:1

我是ros2的新玩家。有一天,我创建了两个名为 topic_helloword_pub 和 topic_helloworld_sub 的节点以及一个名为 simple.launch.py 的启动文件来启动节点。当我使用 ros2 launch 启动节点时,终端中没有错误或警告,但它会阻塞,没有任何内容输出直到我按“ctrl+c”停止它。停止后,每个节点将输出一条消息并结束但是,当我使用 ros2 run 分别启动节点时,一切都运行良好。我想知道为什么以及如何修复它。 我使用windows和foxie版本的ros2。 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_once(node)                                
    node.destroy_node()                              
    rclpy.shutdown()                                 

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_once(node)                                
    node.destroy_node()                             
    rclpy.shutdown()                                 

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', 
        ),
    ])

在终端输出

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"
launch ros2
1个回答
0
投票

您的问题是由于在代码中使用 rclpy.spin_once(node) 而不是 rclpy.spin(node) 引起的。

当您使用 rclpy.spin_once(node) 时,您的节点仅执行一个处理周期,然后关闭,因此您只能在按 Ctrl+C 后看到该消息。当您使用 rclpy.spin(node) 时,节点将继续运行并处理事件,直到您停止它。

    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()
© www.soinside.com 2019 - 2024. All rights reserved.