Exemple #1
0
def echo_util(topic, callback_echo):
    _check_master()
    rospy.init_node('rostopic', anonymous=True, disable_signals=True)
    msg_class, real_topic, msg_eval = get_topic_class(topic, blocking=True)
    if msg_class is None:
        # occurs on ctrl-C
        return
    callback_echo.msg_eval = msg_eval

    # extract type information for submessages
    type_information = None
    if len(topic) > len(real_topic):
        subtopic = topic[len(real_topic):]
        subtopic = subtopic.strip('/')
        if subtopic:
            fields = subtopic.split('/')
            submsg_class = msg_class
            while fields:
                field = fields[0].split('[')[0]
                del fields[0]
                index = submsg_class.__slots__.index(field)
                type_information = submsg_class._slot_types[index]
                if fields:
                    submsg_class = roslib.message.get_message_class(
                        type_information.split('[', 1)[0])
                    if not submsg_class:
                        raise ROSTopicIOException(
                            "Cannot load message class for [%s]. Are your messages built?"
                            % type_information)

    use_sim_time = rospy.get_param('/use_sim_time', False)
    sub = rospy.Subscriber(real_topic, msg_class, callback_echo.callback, {
        'topic': topic,
        'type_information': type_information
    })

    if use_sim_time:
        # #2950: print warning if nothing received for two seconds

        timeout_t = time.time() + 2.
        while time.time() < timeout_t and \
                callback_echo.count == 0 and \
                not rospy.is_shutdown() and \
                not callback_echo.done:
            rospy.rostime.wallsleep(0.1)

        if callback_echo.count == 0 and \
                not rospy.is_shutdown() and \
                not callback_echo.done:
            raise ROSTopicIOException(
                "WARNING: no messages received and simulated time is active.\nIs /clock being published?\n"
            )

    while not rospy.is_shutdown() and not callback_echo.done:
        rospy.rostime.wallsleep(0.1)
Exemple #2
0
def get_rostopic_hz(topic,
                    window_size=-1,
                    filter_expr=None,
                    use_wtime=False,
                    avg=2):
    """
    Periodically print the publishing rate of a topic to console until
    shutdown
    :param topic: topic names, ``list`` of ``str``
    :param window_size: number of messages to average over, -1 for infinite, ``int``
    :param filter_expr: Python filter expression that is called with m, the message instance
    """
    rostopic._check_master()
    if rospy.is_shutdown():
        return
    rt = rostopic.ROSTopicHz(window_size,
                             filter_expr=filter_expr,
                             use_wtime=use_wtime)
    msg_class, real_topic, _ = rostopic.get_topic_class(topic, blocking=False)

    if real_topic is None:
        return 0.0

    # pause hz until topic is published
    # we use a large buffer size as we don't know what sort of messages we're dealing with.
    # may parameterize this in the future
    if filter_expr is not None:
        # have to subscribe with topic_type
        rospy.Subscriber(real_topic,
                         msg_class,
                         rt.callback_hz,
                         callback_args=topic)
    else:
        rospy.Subscriber(real_topic,
                         rospy.AnyMsg,
                         rt.callback_hz,
                         callback_args=topic)

    # if rospy.get_param('use_sim_time', False):
    #     print("WARNING: may be using simulated time",file=sys.stderr)

    rostopic._sleep(1.0)
    hz_avg = 0.0
    for i in range(0, avg):
        hz_cur = rt.get_hz(topic)
        if hz_cur:
            hz_avg = hz_avg + hz_cur[0]
        rostopic._sleep(0.5)
    return hz_avg / avg
def subscribe_call():
    op = request.data.get('op', None)
    service_name = request.data.get('service', None)
    args = request.data.get('args', None)
    if op is None or service_name is None or op != 'call_service':
        return status.HTTP_400_BAD_REQUEST
    else:
        _check_master()
        service_name = rosgraph.names.script_resolve_name('rosservice', service_name)
        if args is None:
            q, response = call_service_util(service_name, [], service_class=None)
        else:
            q, response = call_service_util(service_name, [yaml.load(JSONEncoder().encode(args))], service_class=None)
        print response
    return yaml.load(str(response))
def echo_util(topic, callback_echo):
        _check_master()
        rospy.init_node('rostopic', anonymous=True, disable_signals=True)
        msg_class, real_topic, msg_eval = get_topic_class(topic, blocking=True)
        if msg_class is None:
            # occurs on ctrl-C
            return
        callback_echo.msg_eval = msg_eval

        # extract type information for submessages
        type_information = None
        if len(topic) > len(real_topic):
            subtopic = topic[len(real_topic):]
            subtopic = subtopic.strip('/')
            if subtopic:
                fields = subtopic.split('/')
                submsg_class = msg_class
                while fields:
                    field = fields[0].split('[')[0]
                    del fields[0]
                    index = submsg_class.__slots__.index(field)
                    type_information = submsg_class._slot_types[index]
                    if fields:
                        submsg_class = roslib.message.get_message_class(type_information.split('[', 1)[0])
                        if not submsg_class:
                            raise ROSTopicIOException("Cannot load message class for [%s]. Are your messages built?" % type_information)

        use_sim_time = rospy.get_param('/use_sim_time', False)
        sub = rospy.Subscriber(real_topic, msg_class, callback_echo.callback, {'topic': topic, 'type_information': type_information})

        if use_sim_time:
            # #2950: print warning if nothing received for two seconds

            timeout_t = time.time() + 2.
            while time.time() < timeout_t and \
                    callback_echo.count == 0 and \
                    not rospy.is_shutdown() and \
                    not callback_echo.done:
               rospy.rostime.wallsleep(0.1)

            if callback_echo.count == 0 and \
                    not rospy.is_shutdown() and \
                    not callback_echo.done:
                raise ROSTopicIOException("WARNING: no messages received and simulated time is active.\nIs /clock being published?\n")

        while not rospy.is_shutdown() and not callback_echo.done:
            rospy.rostime.wallsleep(0.1)
def subscribe_call():
    op = request.data.get('op', None)
    service_name = request.data.get('service', None)
    args = request.data.get('args', None)
    if op is None or service_name is None or op != 'call_service':
        return status.HTTP_400_BAD_REQUEST
    else:
        _check_master()
        service_name = rosgraph.names.script_resolve_name(
            'rosservice', service_name)
        if args is None:
            q, response = call_service_util(service_name, [],
                                            service_class=None)
        else:
            q, response = call_service_util(
                service_name, [yaml.load(JSONEncoder().encode(args))],
                service_class=None)
        print response
    return yaml.load(str(response))
def publish():
    op = request.data.get('op', None)
    topic_type = request.data.get('type', None)
    topic_name = request.data.get('topic', None)
    msg = request.data.get('msg', None)
    if op is None or topic_type is None or topic_name is None or msg is None:
        return status.HTTP_400_BAD_REQUEST
    else:
        _check_master()
        topic_name = rosgraph.names.script_resolve_name('rostopic', topic_name)
        try:
            msg_class = roslib.message.get_message_class(topic_type)
        except:
            raise ROSTopicException("invalid topic type: %s" % topic_type)
        if msg_class is None:
            pkg = _resource_name_package(topic_type)
            raise ROSTopicException(
                "invalid message type: %s.\nIf this is a valid message type, perhaps you need to type 'rosmake %s'"
                % (topic_type, pkg))
        #NOT being run in main thread so signals needs to be disabled
        rospy.init_node('rostopic',
                        anonymous=True,
                        disable_rosout=True,
                        disable_rostime=True,
                        disable_signals=True)
        pub = rospy.Publisher(topic_name,
                              msg_class,
                              latch=True,
                              queue_size=100)
        argv_publish(pub, msg_class, [yaml.load(JSONEncoder().encode(msg))],
                     None, True, False)
    return {
        'msg': 'PLEASE WAIT FOR ROBOT TO MOVE',
        'status': 'PUBLISHED',
        'body': request.data
    }, status.HTTP_200_OK
def publish():
    op = request.data.get('op', None)
    topic_type = request.data.get('type', None)
    topic_name = request.data.get('topic', None)
    msg = request.data.get('msg', None)
    if op is None or topic_type is None or topic_name is None or msg is None:
        return status.HTTP_400_BAD_REQUEST
    else:
        _check_master()
        topic_name = rosgraph.names.script_resolve_name('rostopic', topic_name)
        try:
            msg_class = roslib.message.get_message_class(topic_type)
        except:
            raise ROSTopicException("invalid topic type: %s" % topic_type)
        if msg_class is None:
            pkg = _resource_name_package(topic_type)
            raise ROSTopicException(
                "invalid message type: %s.\nIf this is a valid message type, perhaps you need to type 'rosmake %s'" % (
                topic_type, pkg))
        #NOT being run in main thread so signals needs to be disabled
        rospy.init_node('rostopic', anonymous=True, disable_rosout=True, disable_rostime=True, disable_signals=True)
        pub = rospy.Publisher(topic_name, msg_class, latch=True, queue_size=100)
        argv_publish(pub, msg_class, [yaml.load(JSONEncoder().encode(msg))], None, True, False)
    return {'msg': 'PLEASE WAIT FOR ROBOT TO MOVE', 'status': 'PUBLISHED', 'body': request.data}, status.HTTP_200_OK