建立文件夹hello_rospy,再在该目录下建立子目录src,cd到该src目录,运行如下命令创建工作包

catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

pkg代表所属包名,name是文件名,type是脚本文件名称,output指定输出到屏幕

<node pkg="xsens_drive" name="mtnode" type="mtnode.py" output="screen">
</node>

cd到src上层目录,编译并且source

cd ..
catkin_make
. devel/setup.bash

编写订阅者程序

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=)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate() # 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()

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

3. 编译

修改Cmakelist.txt为如下:

cmake_minimum_required(VERSION 2.8.)
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即可

4. 运行

在3个终端中分别执行如下命令

roscore
rosrun beginner_tutorials talker.py
rosrun beginner_tutorials listener.py

结果:

python ros 创建节点订阅robot_pose的更多相关文章

  1. python ros 关闭节点

    def myhook(): print "shutdown time!" rospy.on_shutdown(myhook) 或 rospy.signal_shutdown(rea ...

  2. ROS学习笔记四:用C++编写ROS发布与订阅

    1 创建并编译功能包 1.1 创建功能包 在工作空间的 src 目录下创建功能包: $ cd ~/dev/catkin_ws/src $ catkin_create_pkg chapter2_tuto ...

  3. 编写ros串口节点,使用官方serial包

    参考http://www.roswiki.com/read.php?tid=557&fid=39 1.通过sudo apt-get install ros-<distro>-ser ...

  4. 理解ROS的节点(NODE)

    经过前面的学习,我们已经知道了如何构建一个ROS的包,这篇博客将介绍ROS中的节点的概念. 在继续之前,请按ctrl+alt+t打开一个终端,在里面输入: sudo apt-get install r ...

  5. 用Python从零开始创建区块链

    本文主要内容翻译自Learn Blockchains by Building One 本文原始链接,转载请注明出处. 作者认为最快的学习区块链的方式是自己创建一个,本文就跟随作者用Python来创建一 ...

  6. (六)ROS话题---节点之间通信的方式

    1. 理解 ROS 话题: (Ctrl+Alt+T 打开一个新终端) 运行下面的命令: $ roscore (Ctrl+Alt+T 打开一个新终端) $ rosrun turtlesim turtle ...

  7. dubbo源码解析-zookeeper创建节点

    前言 在之前dubbo源码解析-本地暴露中的前言部分提到了两道高频的面试题,其中一道dubbo中zookeeper做注册中心,如果注册中心集群都挂掉,那发布者和订阅者还能通信吗?在上周的dubbo源码 ...

  8. Mininet python代码创建拓扑、交互式界面创建主机、交换机

          python代码创建拓扑: from mininet.net importMininet net =Mininet() # Creating nodes in the network. c ...

  9. Python -二叉树 创建与遍历算法(很详细)

    树表示由边连接的节点.它是一个非线性的数据结构.它具有以下特性. 一个节点被标记为根节点. 除根节点之外的每个节点都与一个父节点关联. 每个节点可以有一个arbiatry编号的chid节点. 我们使用 ...

随机推荐

  1. 深入理解Fabric环境搭建的详细过程(转)

    前面的准备工作我就不用多说了,也就是各种软件和开发环境的安装,安装好以后,我们git clone下来最新的代码,并切换到v1.0.0,并且下载好我们需要使用的docker镜像,也就是到步骤6,接下来我 ...

  2. [LeetCode] 836. Rectangle Overlap_Easy

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  3. jmeter bean shell断言加密的响应信息(加密接口测试二)

    断言加密的响应信息 1.在http请求-->添加-->断言-->bean shell 断言 import com.changfu.EncryptAndDecryptInterface ...

  4. html10

    1.动画fadeIn() - 淡入fadeOut() - 淡出 -通过改变透明度(opacity隐藏之后依旧占着位置)实现节点的显示和隐藏show() - 显示hide() - 隐藏 -同时改变节点的 ...

  5. Lintcode: Heapify && Summary: Heap

    Given an integer array, heapify it into a min-heap array. For a heap array A, A[0] is the root of he ...

  6. malloc 实现原理

    1. Reference: 如何实现一个malloc http://blog.codinglabs.org/articles/a-malloc-tutorial.html 2.

  7. sql distinct去除重复

    distinct select  distinct * from  table1 或者用 group  by

  8. 026-chmod命令

    语法# chmod [ 选项参数 ]  选择修改权限的对象   权限的改变   目标文件 语义:对哪些目标文件的哪些权限进行修改. (1)# chmod -R ugo +r /home/apple.将 ...

  9. 017-linux正则表达式

    一.单字符表示:1.特定字符:某个具体的字符. '1' 'a' '\.'2.范围内单个字符:单个字符[] [0-9] [259] [a-z] [abc] [A-Z] [ABC] [a-zA-Z] [, ...

  10. mov指令

    一.规定 1.立即数不能作为目的操作数,如mov 110H,AX 2.立即数不能直接传给段寄存器,如mov DS,110H 2.两个操作数不能同时为段寄存器,如mov ES,DS 3.两个操作数不能同 ...