ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I've done something similar via:

~~~ import importlib def get_type(msg_type_str): pkg, tt = msg_type_str.split('/') msg_module = importlib.import_module(pkg + ".msg") msg_type = getattr(msg_module, tt) return msg_type

msg_type = get_type('sensor_msgs/LaserScan') msg = msg_type() ~~~

I've done something similar via:

~~~ ``` import importlib def get_type(msg_type_str): pkg, tt = msg_type_str.split('/') msg_module = importlib.import_module(pkg + ".msg") msg_type = getattr(msg_module, tt) return msg_type

msg_type = get_type('sensor_msgs/LaserScan') msg = msg_type() ~~~```

I've done something similar via:

```

import importlib
 def get_type(msg_type_str):
    pkg, tt = msg_type_str.split('/')
    msg_module = importlib.import_module(pkg + ".msg")
    msg_type = getattr(msg_module, tt)
    return msg_type

msg_type

msg_type = get_type('sensor_msgs/LaserScan') msg = msg_type() ```

I've done something similar via:

import importlib

def get_type(msg_type_str):
    pkg, tt = msg_type_str.split('/')
    msg_module = importlib.import_module(pkg + ".msg")
    msg_type = getattr(msg_module, tt)
    return msg_type

msg_type = get_type('sensor_msgs/LaserScan')
msg = msg_type()

Edit:

Since you don't include the definition of self._ros_api, I'm not sure what's going wrong for you, but the following works for me:

import rospy
rospy.init_node('ipython')

def callback(msg):
    print("got msg!", msg)

msg_type = get_type('std_msgs/Float32')               

sub = rospy.Subscriber("sub_test", msg_type, callback)

I tested it by publishing from the command line: rostopic pub /sub_test std_msgs/Float32 "data: 2.0"