I’m hoping to have a config file loaded in via rosparam
that includes the type for a generic callback to subscribe to. Right now this is what I have but can’t figure out how to make it work with the subscriber.
JavaScript
x
14
14
1
import rospy
2
from std_msgs.msg import *
3
4
def generic_cb(data):
5
print(data.data)
6
7
if __name__ == '__main__':
8
rospy.init_node('generic_node')
9
topic_name = rospy.get_param('topic_name')
10
topic_type = rospy.get_param('topic_type')
11
rospy.Subscriber(topic_name, topic_type, generic_cb)
12
13
rospy.spin()
14
Advertisement
Answer
In Python you can use globals
to lookup and return the message type from a string
JavaScript
1
18
18
1
import rospy
2
from std_msgs.msg import *
3
4
def generic_cb(data):
5
print(data.data)
6
7
if __name__ == '__main__':
8
rospy.init_node('generic_node')
9
10
topic_name = rospy.get_param('topic_name')
11
topic_type = rospy.get_param('topic_type')
12
13
ros_msg_type = globals()[topic_type]
14
15
rospy.Subscriber(topic_name, ros_msg_type, generic_cb)
16
17
rospy.spin()
18