ROS2如何查看某个主题是由哪个节点发布的

方法一

ros2 topic info /xx_topic -v

结果:
在这里插入图片描述

方法二

直接运行

rqt_graph

注意图中的选项,如果只有发布节点而没有订阅节点,那么一定不要勾选Dead sinks
在这里插入图片描述
Dead Sink: 当一个节点订阅了一个主题但没有正确处理接收到的消息,或者节点已经停止运行而未取消订阅时,我们可能会将该节点称为接收端的“死坑”或“dead sink”。这意味着尽管有消息发布到该主题,但由于没有活动的订阅者进行处理,这些消息实际上被忽略了,造成了一种资源浪费。

Leaf Topic: 指的是没有其他话题依赖于它的消息输出的主题。也就是说,这个主题可能被节点订阅并处理,但它不作为数据源供其他话题使用。在话题层级关系上,它处于叶子节点的位置,没有子节点。

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在同一个ROS2节点中包含多个订阅者和发布者的多线程功能,可以使用ROS2提供的多线程库来实现。 具体步骤如下: 1. 创建一个ROS2节点,并在其中创建多个订阅者和发布者。 2. 使用C语言的多线程库,如pthread,创建多个线程。 3. 在每个线程中,分别订阅发布相应的消息。 4. 在每个线程中,使用ROS2提供的rcl_wait函数来等待消息到达。 5. 在每个线程中,使用ROS2提供的rcl_take函数来获取消息并处理。 6. 在每个线程中,使用ROS2提供的rcl_publish函数来发布消息。 7. 确保在每个线程中使用适当的同步机制,以避免竞争条件和死锁等问题。 8. 在程序结束时,使用ROS2提供的rcl_shutdown函数来关闭节点并释放资源。 以下是一个简单的示例代码,演示了如何在同一个ROS2节点中使用多线程处理多个订阅者和发布者: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include "rcl/rcl.h" #include "std_msgs/msg/int32.h" rcl_node_t node; rcl_subscription_t sub1, sub2; rcl_publisher_t pub1, pub2; void* thread1(void* arg) { rcl_wait_set_t wait_set; rcl_ret_t ret; std_msgs__msg__Int32 msg; rcl_wait_set_init(&wait_set, 2, 0, 0, 0, 0, rcl_get_default_allocator()); while(1) { rcl_wait(&wait_set, RCL_MS_TO_NS(10)); if(rcl_wait_set_is_ready(&wait_set, &sub1)) { ret = rcl_take(&sub1, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 1 received message: %d\n", msg.data); msg.data *= 2; rcl_publish(&pub1, &msg, NULL); } } if(rcl_wait_set_is_ready(&wait_set, &sub2)) { ret = rcl_take(&sub2, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 1 received message: %d\n", msg.data); msg.data *= 2; rcl_publish(&pub2, &msg, NULL); } } } rcl_wait_set_fini(&wait_set); return NULL; } void* thread2(void* arg) { rcl_wait_set_t wait_set; rcl_ret_t ret; std_msgs__msg__Int32 msg; rcl_wait_set_init(&wait_set, 2, 0, 0, 0, 0, rcl_get_default_allocator()); while(1) { rcl_wait(&wait_set, RCL_MS_TO_NS(10)); if(rcl_wait_set_is_ready(&wait_set, &sub1)) { ret = rcl_take(&sub1, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 2 received message: %d\n", msg.data); msg.data *= 3; rcl_publish(&pub1, &msg, NULL); } } if(rcl_wait_set_is_ready(&wait_set, &sub2)) { ret = rcl_take(&sub2, &msg, NULL, NULL); if(ret == RCL_RET_OK) { printf("Thread 2 received message: %d\n", msg.data); msg.data *= 3; rcl_publish(&pub2, &msg, NULL); } } } rcl_wait_set_fini(&wait_set); return NULL; } int main(int argc, char** argv) { rcl_init_options_t options = rcl_get_zero_initialized_init_options(); rcl_init_options_init(&options, rcl_get_default_allocator()); rcl_init(argc, argv, &options, &node); rcl_subscription_init(&sub1, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "topic1", NULL); rcl_subscription_init(&sub2, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "topic2", NULL); rcl_publisher_init(&pub1, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "output1", NULL); rcl_publisher_init(&pub2, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "output2", NULL); pthread_t t1, t2; pthread_create(&t1, NULL, thread1, NULL); pthread_create(&t2, NULL, thread2, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); rcl_subscription_fini(&sub1, &node); rcl_subscription_fini(&sub2, &node); rcl_publisher_fini(&pub1, &node); rcl_publisher_fini(&pub2, &node); rcl_node_fini(&node); rcl_shutdown(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值