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节点. 我们使用 ...
随机推荐
- logstash采集tomcat日志、mysql错误日志
input{ file { path => "/opt/Tomcat7.0.28/logs/*.txt" start_position => "beginni ...
- hdu1864最大报销额(01背包)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=187#problem/G 该题要注意的就是每张单子A种类的总和不能大与600,同 ...
- zabbix 微信报警脚本
不知道是什么原因直接用Python脚本zabbix无法执行脚本,需要一个shell来启动 #! /bin/bash userid=$ content=$ python /data/zabbix/ale ...
- [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Keras 源码分析
. │ activations.py │ callbacks.py │ constraints.py │ initializations.py │ metrics.py │ models.py │ o ...
- 2018-2019-2 网络对抗技术 20165324 Exp3:免杀原理与实践
2018-2019-2 网络对抗技术 20165324 Exp3:免杀原理与实践 免杀原理及基础问题回答 免杀 1. 一般是对恶意软件做处理,让它不被杀毒软件所检测.也是渗透测试中需要使用到的技术. ...
- Python基础socket编程
Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...
- Catch all the latest Jordan Release Dates
In case y'all missed yesterday's news, Air Jordan 13 Olive 2018 officially unveiled their 2017 Holid ...
- 浅谈Java中的初始化和清理
引言 这篇文章我们主要介绍Java初始化和清理的相关内容,这些内容虽然比较基础,但是还是在这边做一个简单的总结,方便以后查阅. 初始化过程 Java尽力保证:所有变量在使用之前都会得到恰当的初始化(对 ...
- 浅谈Android View的定位
引言 今天我们来介绍Android坐标系统和View的定位,当然也会介绍View的滑动相关话题.下面让我们开始介绍吧. View的基础知识 View是Android中所有控件的基类,无论是TextVi ...