Exemplo n.º 1
0
def main(args):
    if not args.csv:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb(truncate_length, args.no_arr, args.no_str)
    else:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb_csv(truncate_length, args.no_arr, args.no_str)
    qos_profile = qos_profile_from_short_keys(args.qos_profile,
                                              reliability=args.qos_reliability,
                                              durability=args.qos_durability,
                                              depth=args.qos_depth,
                                              history=args.qos_history)
    with NodeStrategy(args) as node:
        if args.message_type is None:
            message_type = get_msg_class(node,
                                         args.topic_name,
                                         include_hidden_topics=True)
        else:
            try:
                message_type = get_message(args.message_type)
            except (AttributeError, ModuleNotFoundError, ValueError):
                raise RuntimeError('The passed message type is invalid')

        if message_type is None:
            raise RuntimeError(
                'Could not determine the type for the passed topic')

        subscriber(node, args.topic_name, message_type, callback, qos_profile,
                   args.lost_messages, args.raw)
Exemplo n.º 2
0
def main(args):
    if not args.csv:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb(truncate_length, args.no_arr, args.no_str)
    else:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb_csv(truncate_length, args.no_arr, args.no_str)
    qos_profile = qos_profile_from_short_keys(args.qos_profile,
                                              reliability=args.qos_reliability,
                                              durability=args.qos_durability,
                                              depth=args.qos_depth,
                                              history=args.qos_history)
    with NodeStrategy(args) as node:
        if args.message_type is None:
            message_type = get_msg_class(node,
                                         args.topic_name,
                                         include_hidden_topics=True)
        else:
            message_type = get_message(args.message_type)

        if message_type is None:
            raise RuntimeError(
                'Could not determine the type for the passed topic')

        future = None
        if args.once:
            future = Future()
            callback = subscriber_cb_once_decorator(callback, future)

        subscriber(node, args.topic_name, message_type, callback, qos_profile,
                   args.lost_messages, future, args.timeout)
Exemplo n.º 3
0
 def __init__(self):
     super().__init__('listener')
     qos_profile = qos_profile_from_short_keys('system_default',
                                               durability='transient_local',
                                               reliability='reliable')
     self.sub = self.create_subscription(String, 'chatter', self.callback,
                                         qos_profile)
Exemplo n.º 4
0
def main(args):
    qos_profile = qos_profile_from_short_keys(args.qos_profile,
                                              reliability=args.qos_reliability,
                                              durability=args.qos_durability)
    with DirectNode(args) as node:
        return publisher(node.node, args.message_type, args.topic_name,
                         args.values, 1. / args.rate, args.print, args.once,
                         qos_profile)
Exemplo n.º 5
0
    def choose_qos(self, node, args):

        if (args.qos_profile is not None or
                args.qos_reliability is not None or
                args.qos_durability is not None or
                args.qos_depth is not None or
                args.qos_history is not None):

            if args.qos_profile is None:
                args.qos_profile = default_profile_str
            return qos_profile_from_short_keys(args.qos_profile,
                                               reliability=args.qos_reliability,
                                               durability=args.qos_durability,
                                               depth=args.qos_depth,
                                               history=args.qos_history)

        qos_profile = QoSPresetProfiles.get_from_short_key(default_profile_str)
        reliability_reliable_endpoints_count = 0
        durability_transient_local_endpoints_count = 0

        pubs_info = node.get_publishers_info_by_topic(args.topic_name)
        publishers_count = len(pubs_info)
        if publishers_count == 0:
            return qos_profile

        for info in pubs_info:
            if (info.qos_profile.reliability == QoSReliabilityPolicy.RELIABLE):
                reliability_reliable_endpoints_count += 1
            if (info.qos_profile.durability == QoSDurabilityPolicy.TRANSIENT_LOCAL):
                durability_transient_local_endpoints_count += 1

        # If all endpoints are reliable, ask for reliable
        if reliability_reliable_endpoints_count == publishers_count:
            qos_profile.reliability = QoSReliabilityPolicy.RELIABLE
        else:
            if reliability_reliable_endpoints_count > 0:
                print(
                    'Some, but not all, publishers are offering '
                    'QoSReliabilityPolicy.RELIABLE. Falling back to '
                    'QoSReliabilityPolicy.BEST_EFFORT as it will connect '
                    'to all publishers'
                )
            qos_profile.reliability = QoSReliabilityPolicy.BEST_EFFORT

        # If all endpoints are transient_local, ask for transient_local
        if durability_transient_local_endpoints_count == publishers_count:
            qos_profile.durability = QoSDurabilityPolicy.TRANSIENT_LOCAL
        else:
            if durability_transient_local_endpoints_count > 0:
                print(
                    'Some, but not all, publishers are offering '
                    'QoSDurabilityPolicy.TRANSIENT_LOCAL. Falling back to '
                    'QoSDurabilityPolicy.VOLATILE as it will connect '
                    'to all publishers'
                )
            qos_profile.durability = QoSDurabilityPolicy.VOLATILE

        return qos_profile
Exemplo n.º 6
0
def test_profile_conversion():
    profile = qos_profile_from_short_keys('sensor_data',
                                          reliability='reliable',
                                          durability='transient_local',
                                          depth=10,
                                          history='keep_last')
    assert profile.durability == rclpy.qos.QoSDurabilityPolicy.TRANSIENT_LOCAL
    assert profile.reliability == rclpy.qos.QoSReliabilityPolicy.RELIABLE
    assert profile.depth == 10
    assert profile.history == rclpy.qos.QoSHistoryPolicy.KEEP_LAST
Exemplo n.º 7
0
def main(args):
    if not args.csv:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb(truncate_length, args.no_arr, args.no_str)
    else:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb_csv(truncate_length, args.no_arr, args.no_str)
    qos_profile = qos_profile_from_short_keys(
        args.qos_profile, reliability=args.qos_reliability, durability=args.qos_durability)
    with DirectNode(args) as node:
        subscriber(
            node.node, args.topic_name, args.message_type, callback, qos_profile)
Exemplo n.º 8
0
def main(args):
    if not args.csv:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb(truncate_length, args.no_arr, args.no_str)
    else:
        truncate_length = args.truncate_length if not args.full_length else None
        callback = subscriber_cb_csv(truncate_length, args.no_arr, args.no_str)
    qos_profile = qos_profile_from_short_keys(
        args.qos_profile, reliability=args.qos_reliability, durability=args.qos_durability)
    with NodeStrategy(args) as node:
        if args.message_type is None:
            message_type = get_msg_class(node, args.topic_name, include_hidden_topics=True)
        else:
            message_type = get_message(args.message_type)
        subscriber(
            node, args.topic_name, message_type, callback, qos_profile)
Exemplo n.º 9
0
def main(args):
    qos_profile = qos_profile_from_short_keys(
        args.qos_profile, reliability=args.qos_reliability, durability=args.qos_durability,
        depth=args.qos_depth, history=args.qos_history)
    times = args.times
    if args.once:
        times = 1
    with DirectNode(args, node_name=args.node_name) as node:
        return publisher(
            node.node,
            args.message_type,
            args.topic_name,
            args.values,
            1. / args.rate,
            args.print,
            times,
            qos_profile,
            args.keep_alive)
Exemplo n.º 10
0
    def execute(self):
        """Publish a message to a topic."""
        msg = ('Publishing message to topic {} '
               'for a duration of {} with a rate of {}')
        self.node.get_logger().info(
            msg.format(self.topic, self.duration, self.rate))
        once = self.duration == 1 and self.rate == 1
        qos_profile = \
            qos_profile_from_short_keys(self.qos_profile_str,
                                        reliability=self.qos_reliability_str,
                                        durability=self.qos_durability_str)
        msg_module = get_message(self.msg_type)
        pub = self.node.create_publisher(msg_module, self.topic, qos_profile)

        msg = msg_module()
        if self.use_substitution:
            # deep copy so we don't lose the variables for future executions
            parameters_copy = copy.deepcopy(self.parameters)
            substitude_values(parameters_copy, self.parameter_values)
            set_message_fields(msg, parameters_copy)
        else:
            set_message_fields(msg, self.parameters)

        if not once:
            clock = self.node.get_clock()
            sched_time = clock.now()
            end_time = sched_time + Duration(nanoseconds=self.duration * 10**9)

            while clock.now() < end_time:
                while clock.now() < sched_time:
                    time.sleep((sched_time - clock.now()).nanoseconds / 10**9)
                if clock.now() > end_time:
                    break
                pub.publish(msg)
                sched_time = sched_time + Duration(
                    nanoseconds=(1. / self.rate) * 10**9)
        else:
            pub.publish(msg)

        self.node.destroy_publisher(pub)

        return True
Exemplo n.º 11
0
    def main(self, *, args):
        # Select print function
        self.print_func = _print_yaml
        if args.csv:
            self.print_func = _print_csv

        # Validate field selection
        self.field = args.field
        if self.field is not None:
            self.field = list(filter(None, self.field.split('.')))
            if not self.field:
                raise RuntimeError(f"Invalid field value '{args.field}'")

        self.truncate_length = args.truncate_length if not args.full_length else None
        self.no_arr = args.no_arr
        self.no_str = args.no_str

        qos_profile = qos_profile_from_short_keys(
            args.qos_profile,
            reliability=args.qos_reliability,
            durability=args.qos_durability,
            depth=args.qos_depth,
            history=args.qos_history)

        with NodeStrategy(args) as node:
            if args.message_type is None:
                message_type = get_msg_class(node,
                                             args.topic_name,
                                             include_hidden_topics=True)
            else:
                try:
                    message_type = get_message(args.message_type)
                except (AttributeError, ModuleNotFoundError, ValueError):
                    raise RuntimeError('The passed message type is invalid')

            if message_type is None:
                raise RuntimeError(
                    'Could not determine the type for the passed topic')

            self.subscribe_and_spin(node, args.topic_name, message_type,
                                    qos_profile, args.lost_messages, args.raw)
Exemplo n.º 12
0
def test_profile_conversion():
    profile = qos_profile_from_short_keys(
        'sensor_data', reliability='reliable', durability='transient_local')
    assert profile.durability == rclpy.qos.QoSDurabilityPolicy.TRANSIENT_LOCAL
    assert profile.reliability == rclpy.qos.QoSReliabilityPolicy.RELIABLE