echo_node.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
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 # TODO : get rid of this somehow ( dynamic generation or integration of more basic services in ROS )
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         # TODO parameter topic type to reuse this for *any* msg type
00045 
00046         pub = rospy.Publisher(echo_topic_name, std_msgs.String, queue_size=1)
00047 
00048         # building callback
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     # a callback that just echoes the message ( if it doesnt come from me )
00056     def topic_callback(self, data, data_type, pub):
00057         # extract data
00058         print "==> echoing {d} ".format(d=data)
00059         pub.publish(data=data.data)  # data member is needed because it s the only std_msgs.String.__slots__
00060         # BAD ROS API is not symmetrical
00061         # TODO : generic way to forward any msgtype safely
00062         pass
00063 
00064 
00065     def service_callback(self, data):
00066         # extract data
00067         print "==> echoing {d} ".format(d=data)
00068         return data.request  # why specific field here as well ? "request" / response but no "data" ?
00069         # TODO : generic way to forward any msgtype safely
00070 
00071 
00072     def spin(self):
00073         rospy.spin()


pyros_test
Author(s): AlexV
autogenerated on Sat Jun 8 2019 20:51:06