Combine Subscriber and Publisher in Python, ROS

This article will describe an example of Combining Subscriber and Publisher in Python in ROS programming.

This is very useful in ROS development.

We will also discuss briefly how to build and modify a catkin package which is written by Python.

  • Create a catkin package with the command: catkin_create_pkg, under the path: ~/catkin_ws/src
  • Build it with the command: catkin_make, under the path: ~/catkin_ws/
  • Source the catkin setup file under devel folder:
    $ source ~/catkin_ws/devel/setup.bash
  • modify the Python scripts file under the path: ~/catkin_ws/src/<pkg_name>/scripts/nodexxx.py
  • chmod +x nodexxx.py
  • Run this package by Command:  rosrun package_name nodexxx.py
  • Modify the CMakefile.txt for Python: Writing a ROS Python Makefile

More about Create and Build catkin ROS package: This blog

The sourcecode for this Combining Subscriber and Publisher in Python is here:

#!/usr/bin/env python
# License removed for brevity
"""
learn to write Subscriber and Listener in one python script.
Function Style
Author: Sonic http://blog.csdn.net/sonictl
Date: Feb 29, 2016
"""
import rospy
from std_msgs.msg import String def callback(data):
rospy.loginfo(rospy.get_caller_id() + "callback:I heard %s", data.data)
#resp_str = "resp_str: I heard: " + data.data
talker(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. http://blog.csdn.net/sonictl
rospy.init_node('responsor', anonymous=True) rospy.Subscriber("uc0Response", String, callback) # spin() simply keeps python from exiting until this node is stopped
rospy.spin() def talker(data):
pub = rospy.Publisher('uc0Command', String, queue_size=10)
rospy.loginfo("talker:I heard %s", data.data) #while not rospy.is_shutdown():
resp_str = "resp_str: I heard: " + data.data
rospy.loginfo(resp_str)
if data.data == "cigit-pc\n" :
pub.publish(resp_str)
else:
rospy.loginfo("invalid seri data:" + data.data) if __name__ == '__main__':
#listener()
try:
listener()
#talker()
except rospy.ROSInterruptException:
pass

20160614: I have to paste the next code for an other instance because this should be very good for learners:

#!/usr/bin/env python

""" odom_ekf.py - Version 0.1 2012-07-08
Republish the /robot_pose_ekf/odom_combined topic which is of type
geometry_msgs/PoseWithCovarianceStamped as an equivalent message of
type nav_msgs/Odometry so we can view it in RViz.
Created for the Pi Robot Project: http://www.pirobot.org
Copyright (c) 2012 Patrick Goebel. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.5 This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at: http://www.gnu.org/licenses/gpl.html """ import roslib; roslib.load_manifest('rbx1_nav')
import rospy
from geometry_msgs.msg import PoseWithCovarianceStamped
from nav_msgs.msg import Odometry class OdomEKF():
def __init__(self):
# Give the node a name
rospy.init_node('odom_ekf', anonymous=False) # Publisher of type nav_msgs/Odometry
self.ekf_pub = rospy.Publisher('output', Odometry) # Wait for the /odom topic to become available
rospy.wait_for_message('input', PoseWithCovarianceStamped) # Subscribe to the /robot_pose_ekf/odom_combined topic
rospy.Subscriber('input', PoseWithCovarianceStamped, self.pub_ekf_odom) rospy.loginfo("Publishing combined odometry on /odom_ekf") def pub_ekf_odom(self, msg):
odom = Odometry()
odom.header = msg.header
odom.child_frame_id = 'base_footprint'
odom.pose = msg.pose self.ekf_pub.publish(odom) if __name__ == '__main__':
try:
OdomEKF()
rospy.spin()
except:
pass

This program above is for Transfer a
PoseWithCovarianceStamped typed topic "input" into
Odometry typed topic which is named "output".

We can learn how to initialize a node, declare a publisher, subscriber and how to use
wait_for_message() function. The callback function pub_ekf_odom is called when input has data, and we can see how this function works to convert the data from PoseWithCovarianceStamped
into Odometry.

ROS 进阶学习笔记(13) - Combine Subscriber and Publisher in Python, ROS的更多相关文章

  1. ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - ROSMapModify - ROS地图修改

    ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - 2 - MapModify地图修改 We can use gmapping model to genera ...

  2. ROS 进阶学习笔记(12) - Communication with ROS through USART Serial Port

    Communication with ROS through USART Serial Port We always need to communicate with ROS through seri ...

  3. ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM

    (写在前面: 这里参考rbx书中第八章和ROS社区教程进行学习,先看社区教程) ===  Doing the Turtlebot Navigation   === ref ros wiki: http ...

  4. ROS进阶学习笔记(10)- 搭建自己的Turtlebot(5) - Interactive Makers

    用interactive_makers控制Turtlebot移动 interactive_makers 是Willow Garage公司开发的一个虚拟控制工具,可通过鼠标在虚拟环境中的操作,完成实际机 ...

  5. Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法

    Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法 这篇笔记将介绍如何使用Ext.Net GridPanel 中使用Sorter. 默认情况下,Ext.Net GridP ...

  6. SQL反模式学习笔记13 使用索引

    目标:优化性能 改善性能最好的技术就是在数据库中合理地使用索引.  索引也是数据结构,它能使数据库将指定列中的某个值快速定位在相应的行. 反模式:无规划的使用索引 1.不使用索引或索引不足 2.使用了 ...

  7. golang学习笔记13 Golang 类型转换整理 go语言string、int、int64、float64、complex 互相转换

    golang学习笔记13 Golang 类型转换整理 go语言string.int.int64.float64.complex 互相转换 #string到intint,err:=strconv.Ato ...

  8. springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定

    springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...

  9. Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...

随机推荐

  1. java jvm设置http代理参数

    -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=1080

  2. 排序算法<No.5>【堆排序】

    算法,是系统软件开发,甚至是搞软件的技术人士的核心竞争力,这一点,我坚信不疑.践行算法实践,已经有一段时间没有practise了,今天来一个相对麻烦点的,堆排序. 1. 什么是堆(Heap) 这里说的 ...

  3. 【java】进制转换

    进制的表现形式: 十进制:0-9 ,满10 进1 八进制:0-7,满8进1,用0开头表示 十六进制:0-9,A-F,满16进1,用0x开头表示 十进制转换二进制: 原理:对十进制数进行除2运算,如37 ...

  4. Consul+upsync+Nginx实现动态负载均衡 摘自https://blog.csdn.net/qq_29247945/article/details/80787014

    传统感念:每次修改完nginx配置文件,要重启nginx 动态感念:每次修改完nginx配置信息,不需要重启,nginx实时读取配置信息. Nginx: 反向代理和负载均衡 Consul:是用go编写 ...

  5. jquery位置问题

    在页面中添加jquery时,一定要把jquery放在其他js文件前面!不然会出现"$ is not defined", 导致js无法正常运行.

  6. paramiko 实现ssh登录和sftp登录

    简单ssh登录 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddP ...

  7. 研究js特效巩固JavaScript知识

    400多个JavaScript特效大全,包含全部源代码和详细代码说明,不可多得 JavaScript实现可以完全自由拖拽的效果,带三个范例    http://www.sharejs.com/show ...

  8. 微信小程序客服消息使用

    客服消息使用 为丰富小程序的服务能力,提高服务质量,微信为小程序提供客服消息能力,以便小程序用户可以方便快捷地与小程序服务提供方进行沟通. xiaokefu.com.cn 功能介绍 用户可使用小程序客 ...

  9. 禅道在docker上部署与迁移

    一.禅道部署 1.下载地址 禅道开源版:   http://dl.cnezsoft.com/zentao/docker/docker_zentao.zip 数据库用户名: root,默认密码: 123 ...

  10. 01-TCP/IP概述

    TCP/IP 概述 允许不同厂家的各种型号的计算机使用不同操作系统互相进行通信 真正的开放系统 "全球互联网"或"因特网"的基础 2.分层 网络协议通常分不同层 ...