ROS通信机制——python实现

27 篇文章 0 订阅
6 篇文章 0 订阅

一、普通话题通信

1. 创建发布者

注意:不要在开头添加注释,下面代码第一行是指定编译器,第二行是防止因为代码中的中文注释而出现乱码

#! /usr/bin/env python  
#coding:utf-8

#导包
import rospy  
from std_msgs.msg import String

if __name__=="__main__":
    #初始化ROS节点
    rospy.init_node("talker")
    #创建发布者
    pub=rospy.Publisher("Matlab",String,queue_size=10)
    #创建消息类型
    msg=String()
    #设置发布排频率
    rate=rospy.Rate(1)
    count=0
    #休眠1秒,防止出现前面的数据没有接受到
    rospy.sleep(1)
    #循环发布数据,其中rospy.is_shutdown()的意思是只要节点关闭就返回true
    while not rospy.is_shutdown():
        count+=1
        #str(count)可以将count转变为字符串
        msg.data="hello"+str(count)
        pub.publish(msg)
        #rospy.loginfo()相当于C++版本里面的ROS_INFO()
        rospy.loginfo("I talk %s",msg.data)
        #休眠
        rate.sleep()

2. 创建订阅者

注意:不要在开头添加注释,下面代码第一行是指定编译器,第二行是防止因为代码中的中文注释而出现乱码

#! /usr/bin/env python  
#coding:utf-8

#导包
import rospy
from std_msgs.msg import String

def Callback(msg):
    rospy.loginfo("I hear %s",msg.data)

if __name__=="__main__":
    #初始化ROS节点
    rospy.init_node("listener")
    #创建订阅者
    sub=rospy.Subscriber("Matlab",String,Callback,queue_size=10)
    #回调函数
    #spin
    rospy.spin()

3.添加可执行权限

注意:一定要在scripts文件夹下打开终端

4. 编译配置

在Cmakelist中添加如下代码:

catkin_install_python(PROGRAMS
  scripts/talker.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

catkin_install_python(PROGRAMS
  scripts/listener.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

5. 执行

二、自定义话题通信

1. 准备工作

先写好自定义的消息,然后再编译,这时会生成相应的头文件。

打开lib下面的python2.7/dist-package....,鼠标放在这个文件上然后打开终端。

 在终端中输入pwd,然后赋值路径

将路径赋值到setting.json中的“python.autoComplete.extraPaths” 中,保存。

2. 编写发布者

#! /usr/bin/env python  
#coding:utf-8

#导包
import rospy
from learning_communication.msg import person

if __name__ == "__main__":
    #初始化节点
    rospy.init_node("talkermsg")
    #创建发布者
    pub=rospy.Publisher("Matlab",person,queue_size=10)
    #创建消息对象
    per=person()
    per.age=10
    per.name="zhangpeng"
    per.height=180
    #设置频率
    rate=rospy.Rate(1)
    while not rospy.is_shutdown():
        pub.publish(per)
        rospy.loginfo("I am talking %s %d %d",per.name,per.age,per.height)
        per.age+=1
        rate.sleep()

3. 编写接收者

#! /usr/bin/env python  
#coding:utf-8

import rospy
from learning_communication.msg import person

def Callback(per):
    rospy.loginfo("I hear %s %d %d",per.name,per.age,per.height)

if __name__ == "__main__":

    rospy.init_node("listenermsg")
    sub=rospy.Subscriber("Matlab",person,Callback,queue_size=10)
    rospy.spin()

4. 配置相关的文件

 在CmakeList中添加:

catkin_install_python(PROGRAMS
  scripts/talker.py
  scripts/listener.py
  scripts/talkermsg.py
  scripts/listenermsg.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

5. 添加可执行权限

注意:一定要在scripts文件夹下打开终端

6. 编译运行

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值