Python为ROS编写一个简单的发布者和订阅者

Python为ROS编写一个简单的发布者和订阅者

1.创建工作空间

1.1建立文件夹hello_rospy,再在该目录下建立子目录src,并创建工作空间

mkdir -p ~/hello_rospy/src
cd ~/hello_rospy/src
catkin_init_workspace

1.2 编译

cd ~/hello_rospy/
catkin_make

1.3设置运行环境

echo "source ~/hello_rospy/devel/setup.bash" >> ~/.bashrc

1.4 检查运行环境(可以省略)

echo #ROS_PACKAGE_PATH

如果成功的话,输出内容应该包含,本空间的” …/devel/setup.bash"文件。

2.创建beginner_tutorials包

在src目录下创建beginner_tutorials包

cd ~/hello_rospy/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

3.编写发布者程序和订阅者程序

在beginner_tutorials 文件夹下面创建scripts文件夹,并在scripts文件夹下新建talker.py和listener.py两个文件

roscd beginner_tutorials/ 
mkdir scripts
cd scripts

在scripts目录下新建talker.py文件,写入如下内容:

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String
def talker():
     pub = rospy.Publisher('chatter', String, queue_size=10)
     rospy.init_node('talker', anonymous=True)
     rate = rospy.Rate(10) # 10hz
     while not rospy.is_shutdown():
         hello_str = "hello world %s" % rospy.get_time()
         rospy.loginfo(hello_str)
         pub.publish(hello_str)
         rate.sleep()

if __name__ == '__main__':
     try:
         talker()
     except rospy.ROSInterruptException:
         pass

在scripts目录下新建listener.py文件,写入如下内容:

#!/usr/bin/env python
 import rospy
 from std_msgs.msg import String

 def callback(data):
     rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)

 def listener():

     # In ROS, nodes are uniquely named. If two nodes with the same
     # node are launched, the previous one is kicked off. The
     # anonymous=True flag means that rospy will choose a unique
     # name for our 'listener' node so that multiple listeners can
     # run simultaneously.
     rospy.init_node('listener', anonymous=True)

     rospy.Subscriber("chatter", String, callback)

     # spin() simply keeps python from exiting until this node is stopped
     rospy.spin()

 if __name__ == '__main__':
     listener()

更改文件权限为可执行文件

chmod +x talker.py
chmod +x listener.py

4. 编译

(Python编写可以省略)修改Cmakelist.txt为如下:
如果是按照上面编写的,Cmakelist.txt中的内容步需要修改

cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
)
catkin_package()

回到工作空间运行,catkin_make,编译:

catkin_make

5. 运行

cd   #回到梗目录
roscore  #启动ROS
rosrun beginner_tutorials talker.py
rosrun beginner_tutorials listener.py

运行结果:

在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ROS(Robot Operating System)是一个用于构建机器人应用程序的开源框架。它提供了一系列工具、库和约定,用于简化机器人软件开发的过程。ROS中,多线程发布订阅是一常见的通信机制,用于实现不同节点之间的数据传。 在ROS中,发布者(Publisher)节点责将数据发布到特定的主题(Topic),而订阅者(Subscriber)节点则负责从主题中接收数据。多线程发布订阅允许多订阅者同时接收来自同一个主题的数据,从而提高系统的并发性能。 在Python中使用ROS进行多线程发布订阅,可以按照以下步骤进行: 1. 导入所需的ROS库和模块: ```python import rospy from std_msgs.msg import String ``` 2. 初始化ROS节点: ```python rospy.init_node('node_name') ``` 3. 创建发布者对象,并指定要发布的主题和消息类型: ```python pub = rospy.Publisher('topic_name', String, queue_size=10) ``` 4. 创建订阅者回调函数,用于处理接收到的消息: ```python def callback(data): rospy.loginfo("Received: %s", data.data) ``` 5. 创建订阅者对象,并指定要订阅的主题和消息类型,以及回调函数: ```python sub = rospy.Subscriber('topic_name', String, callback) ``` 6. 编写发布数据的逻辑,并使用发布者对象发布消息: ```python rate = rospy.Rate(10) # 设置发布频率为10Hz while not rospy.is_shutdown(): msg = "Hello ROS!" pub.publish(msg) rate.sleep() ``` 以上是使用ROS进行多线程发布订阅的基本步骤。通过创建发布者订阅者对象,并在回调函数中处理接收到的消息,可以实现节点之间的数据传输和通信。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值