Go to the documentation of this file.00001
00002 from __future__ import absolute_import
00003
00004 import functools
00005
00006 """
00007 A very simple echo ROS node class.
00008 - echo from topic to echo_topic
00009 - echo service
00010 """
00011
00012 import roslib
00013 import rospy
00014 import std_msgs.msg as std_msgs
00015
00016
00017
00018 from pyros_test.srv import StringEchoService
00019
00020
00021 class EchoNodeArgumentNotFound(Exception):
00022 pass
00023
00024
00025 class EchoNode(object):
00026 def __init__(self):
00027 rospy.loginfo('String Echo node started. [' + rospy.get_name() + ']')
00028
00029 topic_name = rospy.get_param("~topic_name", "topic")
00030 print 'Parameter {0!s} has value {1!s}'.format(rospy.resolve_name('~topic_name'), topic_name)
00031 if topic_name == "":
00032 raise EchoNodeArgumentNotFound("{0} parameter not found".format(rospy.resolve_name('~topic_name')))
00033
00034 echo_topic_name = rospy.get_param("~echo_topic_name", "echo_topic")
00035 print 'Parameter {0!s} has value {1!s}'.format(rospy.resolve_name('~echo_topic_name'), echo_topic_name)
00036 if echo_topic_name == "":
00037 raise EchoNodeArgumentNotFound("{0} parameter not found".format(rospy.resolve_name('~echo_topic_name')))
00038
00039 echo_service_name = rospy.get_param("~echo_service_name", "echo_service")
00040 print 'Parameter {0!s} has value {1!s}'.format(rospy.resolve_name('~echo_service_name'), echo_service_name)
00041 if echo_service_name == "":
00042 raise EchoNodeArgumentNotFound("{0} parameter not found".format(rospy.resolve_name('~echo_service_name')))
00043
00044
00045
00046 pub = rospy.Publisher(echo_topic_name, std_msgs.String, queue_size=1)
00047
00048
00049 echo = functools.partial(self.topic_callback, data_type=std_msgs.String, pub=pub)
00050 sub = rospy.Subscriber(topic_name, std_msgs.String, echo)
00051
00052 srv = rospy.Service(echo_service_name, StringEchoService, self.service_callback)
00053
00054
00055
00056 def topic_callback(self, data, data_type, pub):
00057
00058 print "==> echoing {d} ".format(d=data)
00059 pub.publish(data=data.data)
00060
00061
00062 pass
00063
00064
00065 def service_callback(self, data):
00066
00067 print "==> echoing {d} ".format(d=data)
00068 return data.request
00069
00070
00071
00072 def spin(self):
00073 rospy.spin()