test_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 ## Simple talker demo that publishes std_msg/Strings to the 'chatter' topic
35 
36 PKG = 'test_rospy'
37 NAME = 'test_node'
38 
39 import sys
40 
41 import rospy
42 import test_rosmaster.msg
43 
44 # Copied from test_ros.test_node
45 #_required_publications = 'test_string_out', 'test_primitives_out', 'test_arrays_out', 'test_header_out'
46 #_required_subscriptions = 'test_string_in', 'test_primitives_in', 'test_arrays_in', 'test_header_in', 'probe_topic'
47 
48 ## pass-through callback that republishes message on \a pub.
49 ## @param pub TopicPub: topic to republish incoming messages on
50 ## @return fn: callback fn for TopicSub
51 def chain_callback(pub):
52  def chained_callback(data):
53  # special logic for handling TestHeader
54  if isinstance(data, test_rosmaster.msg.TestHeader):
55  if data.auto == 0:
56  new_data = test_rosmaster.msg.TestHeader()
57  # when auto is 0, must send 1234, 5678 as time
58  new_data.header.stamp = Time(1234, 5678)
59  # frame_id not really important
60  new_data.header.frame_id = 1234
61  else: # force auto-header timestamp
62  new_data = test_rosmaster.msg.TestHeader(None, rospy.caller_id(), data.auto)
63  data = new_data
64  data.caller_id = rospy.caller_id()
65  pub.publish(data)
66 
67 def test_node():
68  # required publications
69  string_out = rospy.Publisher("test_string_out", test_rosmaster.msg.TestString)
70  primitives_out = rospy.Publisher("test_primitives_out", test_rosmaster.msg.TestPrimitives)
71  arrays_out = rospy.Publisher("test_arrays_out", test_rosmaster.msg.TestArrays)
72  header_out = rospy.Publisher("test_header_out", test_rosmaster.msg.TestHeader)
73 
74  #required subs
75  rospy.Subscriber("test_string_in", test_rosmaster.msg.TestString, chain_callback(string_out))
76  rospy.Subscriber("test_primitives_in", test_rosmaster.msg.TestPrimitives, chain_callback(primitives_out))
77  rospy.Subscriber("test_arrays_in", test_rosmaster.msg.TestArrays, chain_callback(arrays_out))
78  rospy.Subscriber("test_header_in", test_rosmaster.msg.TestHeader, chain_callback(header_out))
79 
80  # subscription with no publisher
81  probe_in = rospy.Subscriber("probe_topic", test_rosmaster.msg.TestString)
82 
83  rospy.init_node(NAME)
84  rospy.spin()
85 
86 if __name__ == '__main__':
87  test_node()
88 
89 
def chain_callback(pub)
pass-through callback that republishes message on pub.
Definition: test_node.py:51
def test_node()
Definition: test_node.py:67


test_rospy
Author(s): Ken Conley, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:56