Skip to content
Browse files

added node to forward messages between two topics

  • Loading branch information...
1 parent e82f955 commit 57335be57db5421a5811f453b7f2dde7599b7574 @evanw committed Apr 17, 2012
Showing with 83 additions and 0 deletions.
  1. +29 −0 nodes/link.py
  2. +10 −0 nodes/ride.py
  3. +6 −0 srv/LinkCreate.srv
  4. +6 −0 srv/LinkDestroy.srv
  5. +12 −0 test/test_link.launch
  6. +20 −0 test/test_link.py
View
29 nodes/link.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+import roslib; roslib.load_manifest('ride')
+import getopt
+import rospy
+import sys
+
+def load_msg_type(type):
+ '''Load a message type object from a string'''
+ package, message = type.split('/')
+ roslib.load_manifest(package)
+ module = __import__(package + '.msg._' + message)
+ return getattr(getattr(module.msg, '_' + message), message)
+
+def main():
+ # Parse command line arguments
+ args = dict(getopt.gnu_getopt(sys.argv, '', ['type=', 'from=', 'to='])[0])
+ type = args['--type']
+ from_topic = args['--from']
+ to_topic = args['--to']
+
+ # Create the link node
+ rospy.init_node('link')
+ type = load_msg_type(type)
+ pub = rospy.Publisher(to_topic, type)
+ sub = rospy.Subscriber(from_topic, type, lambda x: pub.publish(x))
+ rospy.spin()
+
+if __name__ == '__main__':
+ main()
View
10 nodes/ride.py
@@ -331,6 +331,14 @@ def package_list(request):
return ride.srv.PackageListResponse(request.id, packages)
+def link_create(request):
+ '''implements the /ride/link/create service'''
+ return ride.srv.LinkCreateResponse(request.id, False)
+
+def link_destroy(request):
+ '''implements the /ride/link/destroy service'''
+ return ride.srv.LinkDestroyResponse(request.id, False)
+
def main():
# Load cached information (packages and topics)
global packages, topics_path, topics
@@ -360,6 +368,8 @@ def main():
rospy.Service('/ride/node/list', ride.srv.NodeList, node_list)
rospy.Service('/ride/node/output', ride.srv.NodeOutput, node_output)
rospy.Service('/ride/package/list', ride.srv.PackageList, cached_package_list)
+ rospy.Service('/ride/link/create', ride.srv.LinkCreate, link_create)
+ rospy.Service('/ride/link/destroy', ride.srv.LinkDestroy, link_destroy)
rospy.spin()
if __name__ == '__main__':
View
6 srv/LinkCreate.srv
@@ -0,0 +1,6 @@
+int32 id
+string from
+string to
+----
+int32 id
+bool valid
View
6 srv/LinkDestroy.srv
@@ -0,0 +1,6 @@
+int32 id
+string from
+string to
+----
+int32 id
+bool valid
View
12 test/test_link.launch
@@ -0,0 +1,12 @@
+<launch>
+ <node name="a" pkg="ride" type="test_link.py" output="screen">
+ <remap from="in" to="a_in" />
+ <remap from="out" to="a_out" />
+ </node>
+ <node name="b" pkg="ride" type="test_link.py" output="screen">
+ <remap from="in" to="b_in" />
+ <remap from="out" to="b_out" />
+ </node>
+ <node name="a_to_b" pkg="ride" type="link.py" output="screen" args="--type=std_msgs/String --from=a_out --to=b_in" />
+ <node name="b_to_a" pkg="ride" type="link.py" output="screen" args="--type=std_msgs/String --from=b_out --to=a_in" />
+</launch>
View
20 test/test_link.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+import roslib; roslib.load_manifest('ride')
+import rospy
+import std_msgs.msg
+
+def callback(x):
+ print x.data + ' to ' + rospy.get_name()
+
+def main():
+ rospy.init_node('test')
+ sub = rospy.Subscriber('in', std_msgs.msg.String, callback)
+ pub = rospy.Publisher('out', std_msgs.msg.String)
+ if 'b' in rospy.get_name():
+ rospy.sleep(1)
+ while not rospy.is_shutdown():
+ pub.publish('from ' + rospy.get_name())
+ rospy.sleep(2)
+
+if __name__ == '__main__':
+ main()

0 comments on commit 57335be

Please sign in to comment.
Something went wrong with that request. Please try again.