【问题标题】:Get ros message type at runtime运行时获取ros消息类型
【发布时间】:2021-09-22 14:51:23
【问题描述】:

我希望通过rosparam 加载一个配置文件,其中包含要订阅的通用回调类型。现在这就是我所拥有的,但不知道如何让它与订阅者一起工作。

import rospy
from std_msgs.msg import *

def generic_cb(data):
    print(data.data)

if __name__ == '__main__':
    rospy.init_node('generic_node')
    topic_name = rospy.get_param('topic_name')
    topic_type = rospy.get_param('topic_type')
    rospy.Subscriber(topic_name, topic_type, generic_cb)

    rospy.spin()

【问题讨论】:

    标签: python publish-subscribe ros


    【解决方案1】:

    在 Python 中,您可以使用 globals 从字符串中查找并返回消息类型

    import rospy
    from std_msgs.msg import *
    
    def generic_cb(data):
        print(data.data)
    
    if __name__ == '__main__':
        rospy.init_node('generic_node')
    
        topic_name = rospy.get_param('topic_name')
        topic_type = rospy.get_param('topic_type')
        
        ros_msg_type = globals()[topic_type]
    
        rospy.Subscriber(topic_name, ros_msg_type, generic_cb)
    
        rospy.spin()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2010-10-20
      • 1970-01-01
      相关资源
      最近更新 更多