def main(ascii, color_mode, topic, args=None): if color_mode == "mono": color_support = termgraphics.COLOR_SUPPORT_1 elif color_mode == "c4": color_support = termgraphics.COLOR_SUPPORT_16 elif color_mode == "c24": color_support = termgraphics.COLOR_SUPPORT_24BIT else: color_support = None # TermGraphics class will autodetect rclpy.init(args=args) node = ROS2Show(topic, ascii, color_support) # Drawing loop frame_rate = 15. frame_duration = 1. / frame_rate try: while rclpy.ok(): start_time = time.time() #node.viewer.draw() stop_time = time.time() draw_time = stop_time - start_time delay_time = max(0, frame_duration - draw_time) rclpy.spin_once(node, timeout_sec=delay_time) except KeyboardInterrupt: pass finally: print("cleaning") node.getch.reset() node.destroy_node() if rclpy.ok(): print("shutdown") rclpy.try_shutdown()
def main(args=None): parsed_args = parse_args() rclpy.init(args=args) topic = 'qos_deadline_chatter' deadline = Duration(seconds=parsed_args.deadline / 1000.0) qos_profile = QoSProfile(depth=10, deadline=deadline) subscription_callbacks = SubscriptionEventCallbacks( deadline=lambda event: get_logger('Listener').info(str(event))) listener = Listener(topic, qos_profile, event_callbacks=subscription_callbacks) publisher_callbacks = PublisherEventCallbacks( deadline=lambda event: get_logger('Talker').info(str(event))) talker = Talker(topic, qos_profile, event_callbacks=publisher_callbacks) publish_for_seconds = parsed_args.publish_for / 1000.0 pause_for_seconds = parsed_args.pause_for / 1000.0 pause_timer = talker.create_timer( # noqa: F841 publish_for_seconds, lambda: talker.pause_for(pause_for_seconds)) executor = SingleThreadedExecutor() executor.add_node(listener) executor.add_node(talker) try: executor.spin() except KeyboardInterrupt: pass except ExternalShutdownException: sys.exit(1) finally: rclpy.try_shutdown()
def test_destroy_node_while_spinning(): context = rclpy.context.Context() rclpy.init(context=context) try: executor = rclpy.executors.SingleThreadedExecutor(context=context) node = rclpy.create_node('test_node1', context=context) def spin(): with pytest.raises(rclpy.executors.ExternalShutdownException): rclpy.spin(node, executor) thread = threading.Thread(target=spin, daemon=True) thread.start() try: # Let the spin thread get going time.sleep(0.5) node.destroy_node() # Make sure the spin thread has time to react to the # destruction of the node before stopping the test time.sleep(0.5) context.shutdown() finally: thread.join() finally: rclpy.try_shutdown(context=context)
def main(args=None): rclpy.init(args=args) node = Listener() try: rclpy.spin(node) except KeyboardInterrupt: pass except ExternalShutdownException: sys.exit(1) finally: node.destroy_node() rclpy.try_shutdown()
def main(): rclpy.init(args=None) listener = MessageLostListener() executor = SingleThreadedExecutor() executor.add_node(listener) try: executor.spin() except KeyboardInterrupt: pass except ExternalShutdownException: sys.exit(1) finally: rclpy.try_shutdown()
def main(args=None): rclpy.init(args=args) node = AddTwoIntsServer() try: rclpy.spin(node) except KeyboardInterrupt: pass except ExternalShutdownException: sys.exit(1) finally: # Destroy the node explicitly # (optional - Done automatically when node is garbage collected) rclpy.try_shutdown() node.destroy_node()
def requester(service_type, service_name, values, period): # TODO(wjwwood) this logic should come from a rosidl related package try: package_name, srv_name = service_type.split('/', 2) if not package_name or not srv_name: raise ValueError() except ValueError: raise RuntimeError('The passed service type is invalid') module = importlib.import_module(package_name + '.srv') srv_module = getattr(module, srv_name) values_dictionary = yaml.load(values) rclpy.init() node = rclpy.create_node('requester_%s_%s' % (package_name, srv_name)) cli = node.create_client(srv_module, service_name) request = srv_module.Request() try: set_msg_fields(request, values_dictionary) except SetFieldError as e: return "Failed to populate field '{e.field_name}': {e.exception}" \ .format_map(locals()) if not cli.service_is_ready(): print('waiting for service to become available...') cli.wait_for_service() while True: print('requester: making request: %r\n' % request) last_call = time.time() cli.call(request) cli.wait_for_future() if cli.response is not None: print('response:\n%r\n' % cli.response) if period is None or not rclpy.ok(): break time_until_next_period = (last_call + period) - time.time() if time_until_next_period > 0: time.sleep(time_until_next_period) node.destroy_node() rclpy.try_shutdown( ) # avoid duplicate shutdown from shutdown within cli.wait_for_future()
def test_node(subject_name, object_kind, permission, object_name): qualifier = None try: rclpy.try_shutdown() check_node(subject_name, object_kind, permission, object_name) except RuntimeError as e: message = str(e) expected_error = expected_errors[object_kind][permission] if message.startswith(expected_error): rclpy.try_shutdown() qualifier = 'deny' else: print("subject_name: {}\n object_kind: {}\n permission: {}\n object_name: {}\n".format( subject_name, object_kind, permission, object_name)) raise e else: qualifier = 'allow' return qualifier
def __exit__(self, exc_type, exc_value, traceback): self.node.destroy_node() rclpy.try_shutdown()
def main(args=None): # Argument parsing and usage parser = get_parser() parsed_args = parser.parse_args() # Configuration variables qos_policy_name = parsed_args.incompatible_qos_policy_name qos_profile_publisher = QoSProfile(depth=10) qos_profile_subscription = QoSProfile(depth=10) if qos_policy_name == 'durability': print('Durability incompatibility selected.\n' 'Incompatibility condition: publisher durability kind <' 'subscripition durability kind.\n' 'Setting publisher durability to: VOLATILE\n' 'Setting subscription durability to: TRANSIENT_LOCAL\n') qos_profile_publisher.durability = \ QoSDurabilityPolicy.VOLATILE qos_profile_subscription.durability = \ QoSDurabilityPolicy.TRANSIENT_LOCAL elif qos_policy_name == 'deadline': print( 'Deadline incompatibility selected.\n' 'Incompatibility condition: publisher deadline > subscription deadline.\n' 'Setting publisher durability to: 2 seconds\n' 'Setting subscription durability to: 1 second\n') qos_profile_publisher.deadline = Duration(seconds=2) qos_profile_subscription.deadline = Duration(seconds=1) elif qos_policy_name == 'liveliness_policy': print('Liveliness Policy incompatibility selected.\n' 'Incompatibility condition: publisher liveliness policy <' 'subscripition liveliness policy.\n' 'Setting publisher liveliness policy to: AUTOMATIC\n' 'Setting subscription liveliness policy to: MANUAL_BY_TOPIC\n') qos_profile_publisher.liveliness = \ QoSLivelinessPolicy.AUTOMATIC qos_profile_subscription.liveliness = \ QoSLivelinessPolicy.MANUAL_BY_TOPIC elif qos_policy_name == 'liveliness_lease_duration': print( 'Liveliness lease duration incompatibility selected.\n' 'Incompatibility condition: publisher liveliness lease duration >' 'subscription liveliness lease duration.\n' 'Setting publisher liveliness lease duration to: 2 seconds\n' 'Setting subscription liveliness lease duration to: 1 second\n') qos_profile_publisher.liveliness_lease_duration = Duration(seconds=2) qos_profile_subscription.liveliness_lease_duration = Duration( seconds=1) elif qos_policy_name == 'reliability': print( 'Reliability incompatibility selected.\n' 'Incompatibility condition: publisher reliability < subscripition reliability.\n' 'Setting publisher reliability to: BEST_EFFORT\n' 'Setting subscription reliability to: RELIABLE\n') qos_profile_publisher.reliability = \ QoSReliabilityPolicy.BEST_EFFORT qos_profile_subscription.reliability = \ QoSReliabilityPolicy.RELIABLE else: print('{name} not recognised.'.format(name=qos_policy_name)) parser.print_help() return 0 # Initialization and configuration rclpy.init(args=args) topic = 'incompatible_qos_chatter' num_msgs = 5 publisher_callbacks = PublisherEventCallbacks( incompatible_qos=lambda event: get_logger('Talker').info(str(event))) subscription_callbacks = SubscriptionEventCallbacks( incompatible_qos=lambda event: get_logger('Listener').info(str(event))) try: talker = Talker(topic, qos_profile_publisher, event_callbacks=publisher_callbacks, publish_count=num_msgs) listener = Listener(topic, qos_profile_subscription, event_callbacks=subscription_callbacks) except UnsupportedEventTypeError as exc: print() print(exc, end='\n\n') print('Please try this demo using a different RMW implementation') return 1 executor = SingleThreadedExecutor() executor.add_node(listener) executor.add_node(talker) try: while talker.publish_count < num_msgs: executor.spin_once() except KeyboardInterrupt: pass except ExternalShutdownException: return 1 finally: rclpy.try_shutdown() return 0