Created
November 22, 2018 10:41
-
-
Save pgorczak/619966badf8c11f4086353283b08b77e to your computer and use it in GitHub Desktop.
How to resolve a ROS message down to a dictionary of base types—mostly extracted from https://github.com/ros/genmsg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
import os | |
import json | |
import genmsg | |
import rosmsg | |
import rospkg | |
def load_specs(context, search_path, msg): | |
spec = genmsg.load_msg_by_type(context, msg, search_path) | |
genmsg.load_depends(context, spec, search_path) | |
def _get_info(context, msg): | |
base_type = genmsg.msgs.bare_msg_type(msg) | |
if base_type in genmsg.msgs.BUILTIN_TYPES: | |
return base_type | |
else: | |
spec = context.get_registered(base_type) | |
return OrderedDict((k, _get_info(context, t)) for k, t in | |
zip(spec.names, spec.types)) | |
def get_info(context, search_path, msg): | |
load_specs(context, search_path, msg) | |
return _get_info(context, msg) | |
rospack = rospkg.RosPack() | |
search_path = {} | |
for p in rospack.list(): | |
package_paths = rosmsg._get_package_paths(p, rospack) | |
search_path[p] = [os.path.join(d, 'msg') for d in package_paths] | |
context = genmsg.MsgContext.create_default() | |
info = get_info( | |
context, search_path, 'geometry_msgs/PoseWithCovarianceStamped') | |
print(json.dumps(info, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment