• 初始化 ROS 系统
  • 在 ROS 网络内广播我们将要在 chatter 话题上发布 std_msgs/String 类型的消息

  • 以每秒 10 次的频率在 chatter 上发布消息

在 beginner_tutorials package 里创建 script/talker.py 文件:

#!/usr/bin/env python
# license removed for brevity
rospy
String

talker():
10)
True)
# 10hz
is_shutdown():
get_time()
hello_str)
hello_str)
sleep()

':
try:
talker()
ROSInterruptException:
pass

 

 赋予文件执行权限:

$ chmod +x listener.py

2.编写订阅器

  

  • 初始化ROS系统
  • 订阅 chatter 话题

  • 进入自循环,等待消息的到达
  • 当消息到达,调用 chatterCallback() 函数

 

在 beginner_tutorials 目录下创建 script/listener.py 文件:

#!/usr/bin/env python
rospy
String

data):
data)
    
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.
True)

callback)

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

':
listener()

 赋予文件执行权限:

$ chmod +x listener.py

 

3.编译

  • 在catkin_ws目录下运行 catkin_make

 

 4.测试结果:

Ros学习——Python发布器publisher和订阅器subscriber

 

相关文章:

  • 2022-12-23
  • 2021-11-11
  • 2021-06-24
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-12-31
猜你喜欢
  • 2022-02-10
  • 2022-02-15
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2022-01-23
  • 2021-07-28
相关资源
相似解决方案