8 from rostopic
import get_topic_class, ROSTopicException
11 Republish a subfield of a topic as another topic. 12 Useful for graph_rviz_plugin. 13 Based on rostopic echo (rostopic/__init__.py) 16 subtopic_repub.py /topic/subtopic/field /my_republished_field [queue_size (default 10)] 19 ./subtopic_repub.py /imu/angular_velocity/x /angular_velocity_x 20 ./subtopic_repub.py /hullbot/sensors/bno055/imu/angular_velocity_covariance[0] /angular_velocity_covariance_0 22 Author: Sammy Pfeiffer <sam.pfeiffer at hullbot.com> 28 Republish new message on subtopic. 30 :param subtopic: topic name, ``str`` 33 published_topics = rospy.get_published_topics()
35 for t_name, t_type
in published_topics:
36 if t_name
in input_subtopic:
39 raise ROSTopicException(
"Didn't find real topic, is it published?")
40 msg_class, _, msg_eval = get_topic_class(input_subtopic, blocking=
True)
41 rospy.loginfo(
"Input topic is of type: {}".format(msg_class._type))
44 'bool': std_msgs.msg.Bool,
46 'float32': std_msgs.msg.Float32,
47 'float64': std_msgs.msg.Float64,
49 'int8': std_msgs.msg.Int8,
50 'int16': std_msgs.msg.Int16,
51 'int32': std_msgs.msg.Int32,
52 'int64': std_msgs.msg.Int64,
54 'uint8': std_msgs.msg.UInt8,
55 'uint16': std_msgs.msg.UInt16,
56 'uint32': std_msgs.msg.UInt32,
57 'uint64': std_msgs.msg.UInt64,
59 'string': std_msgs.msg.String,
60 'char': std_msgs.msg.Char,
61 'time': std_msgs.msg.Time,
65 if len(input_subtopic) > len(real_topic):
66 subtopic = input_subtopic[len(real_topic):]
67 subtopic = subtopic.strip(
'/')
68 user_array_index =
None 70 if '[' in subtopic.split(
'/')[-1]:
72 user_array_index = subtopic.split(
'/')[-1].split(
'[')[1]
73 user_array_index = int(user_array_index.replace(
']',
''))
75 fields = subtopic.split(
'/')
76 submsg_class = msg_class
80 if '[' in input_subtopic:
81 size_array = int(field.split(
82 '[')[1].replace(
']',
''))
83 field = field.replace(
84 '[' + str(size_array) +
']',
'')
86 raise ROSTopicException(
87 "The topic has an array member, not implemented.")
89 index = submsg_class.__slots__.index(field)
92 type_information = submsg_class._slot_types[index]
94 if '[' in type_information:
95 if '[' in input_subtopic:
96 size_array = int(type_information.split(
97 '[')[1].replace(
']',
''))
98 type_information = type_information.replace(
99 '[' + str(size_array) +
']',
'')
101 raise ROSTopicException(
102 "The topic has an array member, not implemented. You may want to add [0] or another element.")
103 if type_information
in basic_types:
104 submsg_class = basic_types[type_information]
106 submsg_class = roslib.message.get_message_class(
109 raise ROSTopicException(
110 "You didn't provide a subtopic, you may want to use 'rostun topictools relay' instead")
112 rospy.loginfo(
"Got submsg of type: {}".format(submsg_class._type))
113 if user_array_index
is not None:
114 rospy.loginfo(
"Got an array index: {}".format(user_array_index))
115 pub = rospy.Publisher(republish_topic, submsg_class,
116 queue_size=queue_size)
118 def republish_callback(msg):
122 submsg = submsg.__getattribute__(submsg.__slots__[i])
123 if user_array_index
is not None:
125 submsg = submsg[user_array_index]
126 except IndexError
as e:
127 rospy.logwarn_throttle(1.0,
128 "Exception on access array at pos {}, reported array size: {}, Exception: {}, raw array: {}".format(
129 user_array_index, len(submsg), e, submsg))
132 sub = rospy.Subscriber(real_topic, msg_class, republish_callback)
139 sys.argv[0] +
"/topic/subtopic/field /my_republished_field [queue_size (default 10)]")
143 if __name__ ==
'__main__':
144 args = rospy.myargv()
148 repub_topic = args[2]
152 rospy.init_node(
'subtopic_repub', anonymous=
True)
153 rospy.loginfo(
"Republishing subtopic: {} into topic: {} (with queue_size: {})".format(
154 subtopic, repub_topic, queue_size))
def rostopic_republish(input_subtopic, republish_topic, queue_size=10)