echo_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from __future__ import absolute_import
3 
4 import functools
5 
6 """
7  A very simple echo ROS node class.
8  - echo from topic to echo_topic
9  - echo service
10 """
11 
12 import roslib
13 import rospy
14 import std_msgs.msg as std_msgs
15 
16 
17 # TODO : get rid of this somehow ( dynamic generation or integration of more basic services in ROS )
18 from pyros_test.srv import StringEchoService
19 
20 
21 class EchoNodeArgumentNotFound(Exception):
22  pass
23 
24 
25 class EchoNode(object):
26  def __init__(self):
27  rospy.loginfo('String Echo node started. [' + rospy.get_name() + ']')
28 
29  topic_name = rospy.get_param("~topic_name", "topic")
30  print 'Parameter {0!s} has value {1!s}'.format(rospy.resolve_name('~topic_name'), topic_name)
31  if topic_name == "":
32  raise EchoNodeArgumentNotFound("{0} parameter not found".format(rospy.resolve_name('~topic_name')))
33 
34  echo_topic_name = rospy.get_param("~echo_topic_name", "echo_topic")
35  print 'Parameter {0!s} has value {1!s}'.format(rospy.resolve_name('~echo_topic_name'), echo_topic_name)
36  if echo_topic_name == "":
37  raise EchoNodeArgumentNotFound("{0} parameter not found".format(rospy.resolve_name('~echo_topic_name')))
38 
39  echo_service_name = rospy.get_param("~echo_service_name", "echo_service")
40  print 'Parameter {0!s} has value {1!s}'.format(rospy.resolve_name('~echo_service_name'), echo_service_name)
41  if echo_service_name == "":
42  raise EchoNodeArgumentNotFound("{0} parameter not found".format(rospy.resolve_name('~echo_service_name')))
43 
44  # TODO parameter topic type to reuse this for *any* msg type
45 
46  pub = rospy.Publisher(echo_topic_name, std_msgs.String, queue_size=1)
47 
48  # building callback
49  echo = functools.partial(self.topic_callback, data_type=std_msgs.String, pub=pub)
50  sub = rospy.Subscriber(topic_name, std_msgs.String, echo)
51 
52  srv = rospy.Service(echo_service_name, StringEchoService, self.service_callback)
53 
54 
55  # a callback that just echoes the message ( if it doesnt come from me )
56  def topic_callback(self, data, data_type, pub):
57  # extract data
58  print "==> echoing {d} ".format(d=data)
59  pub.publish(data=data.data) # data member is needed because it s the only std_msgs.String.__slots__
60  # BAD ROS API is not symmetrical
61  # TODO : generic way to forward any msgtype safely
62  pass
63 
64 
65  def service_callback(self, data):
66  # extract data
67  print "==> echoing {d} ".format(d=data)
68  return data.request # why specific field here as well ? "request" / response but no "data" ?
69  # TODO : generic way to forward any msgtype safely
70 
71 
72  def spin(self):
73  rospy.spin()
def service_callback(self, data)
Definition: echo_node.py:65
def topic_callback(self, data, data_type, pub)
Definition: echo_node.py:56


pyros_test
Author(s): AlexV
autogenerated on Mon Jun 10 2019 14:20:23