$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 # Revision $Id$ 00035 00036 ## Simple talker demo that publishes std_msg/Strings to the 'chatter' topic 00037 00038 PKG = 'test_rospy' 00039 NAME = 'test_node' 00040 import roslib; roslib.load_manifest(PKG) 00041 00042 import sys 00043 00044 import rospy 00045 import test_ros.msg 00046 00047 # Copied from test_ros.test_node 00048 #_required_publications = 'test_string_out', 'test_primitives_out', 'test_arrays_out', 'test_header_out' 00049 #_required_subscriptions = 'test_string_in', 'test_primitives_in', 'test_arrays_in', 'test_header_in', 'probe_topic' 00050 00051 ## pass-through callback that republishes message on \a pub. 00052 ## @param pub TopicPub: topic to republish incoming messages on 00053 ## @return fn: callback fn for TopicSub 00054 def chain_callback(pub): 00055 def chained_callback(data): 00056 # special logic for handling TestHeader 00057 if isinstance(data, test_ros.msg.TestHeader): 00058 if data.auto == 0: 00059 new_data = test_ros.msg.TestHeader() 00060 # when auto is 0, must send 1234, 5678 as time 00061 new_data.header.stamp = Time(1234, 5678) 00062 # frame_id not really important 00063 new_data.header.frame_id = 1234 00064 else: # force auto-header timestamp 00065 new_data = test_ros.msg.TestHeader(None, rospy.caller_id(), data.auto) 00066 data = new_data 00067 data.caller_id = rospy.caller_id() 00068 pub.publish(data) 00069 00070 def test_node(): 00071 # required publications 00072 string_out = rospy.Publisher("test_string_out", test_ros.msg.TestString) 00073 primitives_out = rospy.Publisher("test_primitives_out", test_ros.msg.TestPrimitives) 00074 arrays_out = rospy.Publisher("test_arrays_out", test_ros.msg.TestArrays) 00075 header_out = rospy.Publisher("test_header_out", test_ros.msg.TestHeader) 00076 00077 #required subs 00078 rospy.Subscriber("test_string_in", test_ros.msg.TestString, chain_callback(string_out)) 00079 rospy.Subscriber("test_primitives_in", test_ros.msg.TestPrimitives, chain_callback(primitives_out)) 00080 rospy.Subscriber("test_arrays_in", test_ros.msg.TestArrays, chain_callback(arrays_out)) 00081 rospy.Subscriber("test_header_in", test_ros.msg.TestHeader, chain_callback(header_out)) 00082 00083 # subscription with no publisher 00084 probe_in = rospy.Subscriber("probe_topic", test_ros.msg.TestString) 00085 00086 rospy.init_node(NAME) 00087 rospy.spin() 00088 00089 if __name__ == '__main__': 00090 test_node() 00091 00092