2021-09-16 ROS2教程(二)-创建发布者和订阅者(C++和Python版)

这篇博客详细介绍了如何在ROS2的foxy版本中,使用C++和Python创建发布者和订阅者。首先,通过`ros2 pkg create`命令创建cpp版功能包,并获取发布者和订阅者代码,添加必要的依赖并进行编译运行。接着,创建Python版功能包,同样获取发布者和订阅者代码,添加依赖并编译运行。提供了官方和中文翻译参考链接,帮助理解每个步骤。
摘要由CSDN通过智能技术生成

参考

官方参考:
http://docs.ros.org/en/foxy/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html

中文翻译参考
https://zhuanlan.zhihu.com/p/353892419
https://zhuanlan.zhihu.com/p/353896706

C++版

注意工作空间不要命名太长,编译的时候会找不到功能包

1.0创建的功能包(cpp版)

ros2 pkg create --build-type ament_python py_pubsub

在这里插入图片描述

1.1获取发布者代码

cd cpp_pubsub/src
wget -O publisher_member_function.cpp https://raw.githubusercontent.com/ros2/examples/foxy/rclcpp/topics/minimal_publisher/member_function.cpp

在这里插入图片描述

1.2添加发布者的依赖

#package.xml
<depend>rclcpp</depend>
<depend>std_msgs</depend>

#CMakeLists.txt
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

add_executable(talker src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp std_msgs)

install(TARGETS
  talker
  DESTINATION lib/${PROJECT_NAME})

在这里插入图片描述在这里插入图片描述

1.3-获取订阅者代码及添加依赖

wget -O subscriber_member_function.cpp https://raw.githubusercontent.com/ros2/examples/foxy/rclcpp/topics/minimal_subscriber/member_function.cpp


add_executable(listener src/subscriber_member_function.cpp)
ament_target_dependencies(listener rclcpp std_msgs)

install(TARGETS
  talker
  listener
  DESTINATION lib/${PROJECT_NAME})

在这里插入图片描述在这里插入图片描述

1.4-编译运行订阅发布

rosdep install -i --from-path src --rosdistro foxy -y
colcon build --packages-select cpp_pubsub
. install/setup.bash
ros2 run cpp_pubsub talker
ros2 run cpp_pubsub listener

在这里插入图片描述

Python版

2.0创建功能包获取订阅发布(Python)

#在自己的工作空间下创建功能包
ros2 pkg create --build-type ament_python py_pubsub

#在src/py_pubsub/py_pubsub文件夹下放入
wget https://raw.githubusercontent.com/ros2/examples/foxy/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_member_function.py

wget https://raw.githubusercontent.com/ros2/examples/foxy/rclpy/topics/minimal_subscriber/examples_rclpy_minimal_subscriber/subscriber_member_function.py

在这里插入图片描述

2.1添加依赖

#package.xml
<exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend>

#setup.py
entry_points={
        'console_scripts': [
                'talker = py_pubsub.publisher_member_function:main',
                'listener = py_pubsub.subscriber_member_function:main',
        ],
},

在这里插入图片描述

2.2编译运行

rosdep install -i --from-path src --rosdistro foxy -y
colcon build --packages-select py_pubsub
. install/setup.bash
ros2 run py_pubsub talker
ros2 run py_pubsub listener

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在同一个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; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值