Example #1
0
def get_msg_text(type_, raw=False, rospack=None):
    """
    Get .msg file for type_ as text
    :param type_: message type, ``str``
    :param raw: if True, include comments and whitespace (default False), ``bool``
    :returns: text of .msg file, ``str``
    :raises :exc:`ROSMsgException` If type_ is unknown
    """
    if rospack is None:
        rospack = rospkg.RosPack()
    search_path = {}
    for p in rospack.list():
        search_path[p] = [os.path.join(rospack.get_path(p), 'msg')]

    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_msg_by_type(context, type_, search_path)
        genmsg.load_depends(context, spec, search_path)
    except Exception as e:
        raise ROSMsgException("Unable to load msg [%s]: %s"%(type_, e))
    
    if raw:
        return spec.text
    else:
        return spec_to_str(context, spec)
Example #2
0
def get_msg_text(type_, raw=False, rospack=None):
    """
    Get .msg file for type_ as text
    :param type_: message type, ``str``
    :param raw: if True, include comments and whitespace (default False), ``bool``
    :returns: text of .msg file, ``str``
    :raises :exc:`ROSMsgException` If type_ is unknown
    """
    if rospack is None:
        rospack = rospkg.RosPack()
    search_path = {}
    for p in rospack.list():
        search_path[p] = [os.path.join(rospack.get_path(p), 'msg')]

    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_msg_by_type(context, type_, search_path)
        genmsg.load_depends(context, spec, search_path)
    except Exception as e:
        raise ROSMsgException("Unable to load msg [%s]: %s" % (type_, e))

    if raw:
        return spec.text
    else:
        return spec_to_str(context, spec)
Example #3
0
def get_srv_text(type_, raw=False, rospack=None):
    """
    Get .srv file for type_ as text
    :param type_: service type, ``str``
    :param raw: if True, include comments and whitespace (default False), ``bool``
    :returns: text of .srv file, ``str``
    @raise ROSMsgException: if type_ is unknown
    """
    if rospack is None:
        rospack = rospkg.RosPack()
    srv_search_path = {}
    msg_search_path = {}
    for p in rospack.list():
        path = rospack.get_path(p)
        msg_search_path[p] = [os.path.join(path, 'msg')]
        srv_search_path[p] = [os.path.join(path, 'srv')]
        
    #TODO: cache context somewhere
    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_srv_by_type(context, type_, srv_search_path)
        genmsg.load_depends(context, spec, msg_search_path)
    except Exception as e:
        raise ROSMsgException("Unknown srv type [%s]: %s"%(type_, e))
    
    if raw:
        return spec.text
    else:
        return spec_to_str(context, spec.request)+'---\n'+spec_to_str(context, spec.response)
Example #4
0
def getString(topic):
    try:
        val = rosgraph.Master('/rostopic').getTopicTypes()
    except socket.error:
        raise ROSTopicIOException("Unable to communicate with master!")

    # exact match first, followed by prefix match
    matches = [(t, t_type) for t, t_type in val if t == topic]
    if not matches:
        matches = [(t, t_type) for t, t_type in val if topic.startswith(t+'/')]
        # choose longest match
        matches.sort(key=itemgetter(0), reverse=True)
    if matches:
        msg = matches[0][1]
    rospack = rospkg.RosPack()
    search_path = {}
    for p in rospack.list():
        search_path[p] = [os.path.join(rospack.get_path(p), 'msg')]
    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_msg_by_type(context, msg, search_path)
        genmsg.load_depends(context, spec, search_path)
    except Exception as e:
        raise ROSMsgException("Unable to load msg [%s]: %s"%(msg, e))
    str = spec_to_str(context,spec)
    str = str.replace('\n',' ')
    str = ' '.join(str.split())
    return str
Example #5
0
def get_srv_text(type_, raw=False, rospack=None):
    """
    Get .srv file for type_ as text
    :param type_: service type, ``str``
    :param raw: if True, include comments and whitespace (default False), ``bool``
    :returns: text of .srv file, ``str``
    @raise ROSMsgException: if type_ is unknown
    """
    if rospack is None:
        rospack = rospkg.RosPack()
    srv_search_path = {}
    msg_search_path = {}
    for p in rospack.list():
        path = rospack.get_path(p)
        msg_search_path[p] = [os.path.join(path, 'msg')]
        srv_search_path[p] = [os.path.join(path, 'srv')]

    #TODO: cache context somewhere
    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_srv_by_type(context, type_, srv_search_path)
        genmsg.load_depends(context, spec, msg_search_path)
    except Exception as e:
        raise ROSMsgException("Unknown srv type [%s]: %s" % (type_, e))

    if raw:
        return spec.text
    else:
        return spec_to_str(context, spec.request) + '---\n' + spec_to_str(
            context, spec.response)
def getString(topic):
    import rostopic
    try:
        val = rosgraph.Master('/rostopic').getTopicTypes()
    except socket.error:
        print "Unable to communicate with ROS master. Run roscore before starting the bridge."
    # exact match first, followed by prefix match
    matches = [(t, t_type) for t, t_type in val if t == topic]
    if not matches:
        matches = [(t, t_type) for t, t_type in val if topic.startswith(t+'/')]
        # choose longest match
        if matches:
            matches.sort(key=itemgetter(0), reverse=True)
        else:
            print "A match could not be found for the specified ROS topic. Run rostopic list to check if it does."
    if matches:
        msg = matches[0][1]
    if not msg:
        print "The ROS message corresponding to the specified ROS topic could not be identified."
    rospack = rospkg.RosPack()
    search_path = {}
    for p in rospack.list():
        search_path[p] = [os.path.join(rospack.get_path(p), 'msg')]
    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_msg_by_type(context, msg, search_path)
        genmsg.load_depends(context, spec, search_path)
    except Exception as e:
        raise ROSMsgException("Unable to load msg [%s]: %s"%(msg, e))
    str = spec_to_str(context,spec)
    str = str.replace('\n',' ')
    str = ' '.join(str.split())
    return str
Example #7
0
def get_spec(msg_type, search_path):
    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_msg_by_type(context, msg_type, search_path)
        genmsg.load_depends(context, spec, search_path)
    except Exception as e:
        raise ROSMsgException("Unable to load msg [%s]: %s" % (msg_type, e))
    return spec
Example #8
0
def get_spec(msg_type, search_path):
    context = genmsg.MsgContext.create_default()
    try:
        spec = genmsg.load_msg_by_type(context, msg_type, search_path)
        genmsg.load_depends(context, spec, search_path)
    except Exception as e:
        raise ROSMsgException("Unable to load msg [%s]: %s"%(msg_type, e))
    return spec
Example #9
0
def _compute_md5_text(msg_context, f):
    from genmsg import compute_md5_text, load_depends
    from genmsg.msg_loader import load_msg_from_string

    text = open(f, 'r').read()
    short_name = os.path.basename(f)[:-len('.msg')]
    full_name = "%s/%s"%(TEST_CTX, short_name)
    spec = load_msg_from_string(msg_context, text, full_name)
    search_path = get_search_path()
    load_depends(msg_context, spec, search_path)
    return compute_md5_text(msg_context, spec)
Example #10
0
def _compute_md5_text(msg_context, f):
    from genmsg import compute_md5_text, load_depends
    from genmsg.msg_loader import load_msg_from_string

    text = open(f, 'r').read()
    short_name = os.path.basename(f)[:-len('.msg')]
    full_name = "%s/%s" % (TEST_CTX, short_name)
    spec = load_msg_from_string(msg_context, text, full_name)
    search_path = get_search_path()
    load_depends(msg_context, spec, search_path)
    return compute_md5_text(msg_context, spec)
Example #11
0
def test_compute_full_text():
    from genmsg import MsgContext, compute_full_text, load_msg_by_type, load_depends
    msg_context = MsgContext.create_default()

    search_path = get_search_path()
    
    # regression test against values used for cturtle-electric
    
    spec = load_msg_by_type(msg_context, 'rosgraph_msgs/Log', search_path)
    load_depends(msg_context, spec, search_path)
    val = compute_full_text(msg_context, spec)
    assert val == log_full_text, "[%s][%s]"%(val, log_full_text)

    spec = load_msg_by_type(msg_context, 'geometry_msgs/TwistWithCovarianceStamped', search_path)
    load_depends(msg_context, spec, search_path)
    val = compute_full_text(msg_context, spec)
    assert val == twist_with_covariance_stamped_full_text, "[%s][%s]"%(val, twist_with_covariance_stamped_full_text)
def test_compute_full_text():
    from genmsg import MsgContext, compute_full_text, load_msg_by_type, load_depends
    msg_context = MsgContext.create_default()

    search_path = get_search_path()
    
    # regression test against values used for cturtle-electric
    
    spec = load_msg_by_type(msg_context, 'rosgraph_msgs/Log', search_path)
    load_depends(msg_context, spec, search_path)
    val = compute_full_text(msg_context, spec)
    assert val == log_full_text, "[%s][%s]"%(val, log_full_text)

    spec = load_msg_by_type(msg_context, 'geometry_msgs/TwistWithCovarianceStamped', search_path)
    load_depends(msg_context, spec, search_path)
    val = compute_full_text(msg_context, spec)
    assert val == twist_with_covariance_stamped_full_text, "[%s][%s]"%(val, twist_with_covariance_stamped_full_text)