python ros 创建节点订阅robot_pose
建立文件夹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的更多相关文章
- python ros 关闭节点
def myhook(): print "shutdown time!" rospy.on_shutdown(myhook) 或 rospy.signal_shutdown(rea ...
- ROS学习笔记四:用C++编写ROS发布与订阅
1 创建并编译功能包 1.1 创建功能包 在工作空间的 src 目录下创建功能包: $ cd ~/dev/catkin_ws/src $ catkin_create_pkg chapter2_tuto ...
- 编写ros串口节点,使用官方serial包
参考http://www.roswiki.com/read.php?tid=557&fid=39 1.通过sudo apt-get install ros-<distro>-ser ...
- 理解ROS的节点(NODE)
经过前面的学习,我们已经知道了如何构建一个ROS的包,这篇博客将介绍ROS中的节点的概念. 在继续之前,请按ctrl+alt+t打开一个终端,在里面输入: sudo apt-get install r ...
- 用Python从零开始创建区块链
本文主要内容翻译自Learn Blockchains by Building One 本文原始链接,转载请注明出处. 作者认为最快的学习区块链的方式是自己创建一个,本文就跟随作者用Python来创建一 ...
- (六)ROS话题---节点之间通信的方式
1. 理解 ROS 话题: (Ctrl+Alt+T 打开一个新终端) 运行下面的命令: $ roscore (Ctrl+Alt+T 打开一个新终端) $ rosrun turtlesim turtle ...
- dubbo源码解析-zookeeper创建节点
前言 在之前dubbo源码解析-本地暴露中的前言部分提到了两道高频的面试题,其中一道dubbo中zookeeper做注册中心,如果注册中心集群都挂掉,那发布者和订阅者还能通信吗?在上周的dubbo源码 ...
- Mininet python代码创建拓扑、交互式界面创建主机、交换机
python代码创建拓扑: from mininet.net importMininet net =Mininet() # Creating nodes in the network. c ...
- Python -二叉树 创建与遍历算法(很详细)
树表示由边连接的节点.它是一个非线性的数据结构.它具有以下特性. 一个节点被标记为根节点. 除根节点之外的每个节点都与一个父节点关联. 每个节点可以有一个arbiatry编号的chid节点. 我们使用 ...
随机推荐
- spring的bean容器加载
1.在单独使用的时候可以通过ClassPathXmlApplicationContext(“配置文件.xml”);来启动容器. 2.在MVC下是通过启动servlet容器,初始化DispatcherS ...
- PAT 1024 Palindromic Number[难]
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- soapUI-Groovy Script
1.1.1 Groovy Script soapUI通过以groovy语言编写的脚本来大量支持您的项目. Groovy脚本TestSteps可用于向功能TestCase添加任意功能. 脚本断言用于任 ...
- SQL CASE 多条件用法
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN ...
- c++虚函数重写的权限问题
cbase.h: #ifndef CBASE_H #define CBASE_H #include<iostream> using std::cout; using std::endl; ...
- linux 启动引导流程
课程大纲: Linux引导流程 Linux运行级别 Linux启动服务管理 GRUB配置与应用 启动故障分析与解决 系统引导流程 1.固件firmware(CMOS(固化在硬件上的程序与硬件统称)/B ...
- linux文件系统软链接硬链接
引子 目前,UNIX的文件系统有很多种实现,例如UFS(基于BSD的UNIX文件系统).ext3.ext4.ZFS和Reiserfs等等. 不论哪一种文件系统,总是需要存储数据.硬盘的最小存储单位是扇 ...
- wcf 服务器无法处理请求由于内部错误
The server was unable to process the request due to an internal error. For more information about t ...
- 80. Remove Duplicates from Sorted Array II(双指针)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON FastThreshold1
zw版[转发·台湾nvp系列Delphi例程]HALCON FastThreshold1 procedure TForm1.Button1Click(Sender: TObject);var img ...