ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Through a bit more experimenting, the combination of #1 and #2 did the trick. It still seems like there might be a cleaner way.

class Base(object):
   def __init__(self):
      service = rospy.Service('~get_state', ...) 

class Node1(Base):
  def __init__(self):
      rospy.init_node('node1')
      super(Node1, self).__init__()

Note that init_node is called before Service and the Service includes a tilde (~).

That seems consistent with the docs here to keep the Service private to the node... http://wiki.ros.org/Names

Through a bit more experimenting, the combination of #1 and #2 did the trick. It still seems like there might be a cleaner way.

class Base(object):
   def __init__(self):
      service = rospy.Service('~get_state', ...) 

class Node1(Base):
  def __init__(self):
      rospy.init_node('node1')
      super(Node1, self).__init__()

Note that init_node is called before the Service is created in the super class and the further Service includes a tilde (~).

(~). That seems consistent with the docs here to keep the Service private to the node... http://wiki.ros.org/Names

Through a bit more experimenting, the combination of #1 and #2 did the trick. It still seems like there might be a cleaner way.

# src/foo.py
class Base(object):
   def __init__(self):
      service = rospy.Service('~get_state', ...) 

# nodes/node1.py
import foo
class Node1(Base):
Node1(foo.Base):
  def __init__(self):
      rospy.init_node('node1')
      super(Node1, self).__init__()

Note that init_node is called before the Service is created in the super class and further Service includes a tilde (~). That seems consistent with the docs to keep the Service private to the node... http://wiki.ros.org/Names

Through The answer is simply to include a bit tilde (~) when creating the service. A good practice would be to call init_node from the main block to prevent the mistake of including more experimenting, than one node in a single process. There isn't much benefit in calling the combination of #1 and #2 did init_node from within the trick. It still seems like there might be a cleaner way.class __init__.

# src/foo.py
class Base(object):
   def __init__(self):
      service = rospy.Service('set_state', ...)    # results in /set_state
      service = rospy.Service('~get_state', ...)    # results in /node1/get_state (as desired)

# nodes/node1.py
import foo
class Node1(foo.Base):
  def __init__(self):
      rospy.init_node('node1')
super(Node1, self).__init__()

if __name__ == '__main__':
   super(Node1, self).__init__()
rospy.init_node('node1')

Note that init_node is called before the Service is created in the super class and further Service includes a tilde (~). That seems consistent with the docs to keep the Service private to the node... node's namespace... http://wiki.ros.org/Names

The answer is simply to include a tilde (~) when creating the service. A good practice would be to call init_node from the main block to prevent the mistake of including more than one node in a single process. There isn't much benefit in calling the init_node from within the class __init__.

# src/foo.py
class Base(object):
   def __init__(self):
      service = rospy.Service('set_state', ...)    # results in /set_state
      service = rospy.Service('~get_state', ...)   # results in /node1/get_state (as desired)

# nodes/node1.py
import foo
class Node1(foo.Base):
  def __init__(self):
      super(Node1, self).__init__()

if __name__ == '__main__':
    rospy.init_node('node1')
    node = Node1()
    ...

That seems consistent with the docs to keep the Service private to the node's namespace... http://wiki.ros.org/Names

The answer is simply to include a tilde (~) when creating the service. A good practice would be to call init_node from the main block to prevent the mistake of including more than one node in a single process. There isn't much benefit in calling the init_node from within the class __init__.

# src/foo.py
class Base(object):
   def __init__(self):
      service = rospy.Service('set_state', ...)    # results in /set_state
      service = rospy.Service('~get_state', ...)   # results in /node1/get_state (as desired)

# nodes/node1.py
import foo
class Node1(foo.Base):
  def __init__(self):
      super(Node1, self).__init__()

if __name__ == '__main__':
    rospy.init_node('node1')
rospy.init_node('node1')  # moved out of Node1.__init__
    node = Node1()
    ...

That seems consistent with the docs to keep the Service private to the node's namespace... http://wiki.ros.org/Names