def init( *, args: Optional[List[str]] = None, context: Context = None, domain_id: Optional[int] = None, signal_handler_options: Optional[SignalHandlerOptions] = None, ) -> None: """ Initialize ROS communications for a given context. :param args: List of command line arguments. :param context: The context to initialize. If ``None``, then the default context is used (see :func:`.get_default_context`). :param domain_id: ROS domain id. :param signal_handler_options: Indicate which signal handlers to install. If `None`, SIGINT and SIGTERM will be installed when initializing the default context. """ context = get_default_context() if context is None else context if signal_handler_options is None: if context is None or context is get_default_context(): signal_handler_options = SignalHandlerOptions.ALL else: signal_handler_options = SignalHandlerOptions.NO install_signal_handlers(signal_handler_options) return context.init(args, domain_id=domain_id)
def __init__(self, *, context: Context = None) -> None: super().__init__() self._context = get_default_context() if context is None else context self._nodes: Set[Node] = set() self._nodes_lock = RLock() # Tasks to be executed (oldest first) 3-tuple Task, Entity, Node self._tasks: List[Tuple[Task, Optional[WaitableEntityType], Optional[Node]]] = [] self._tasks_lock = Lock() # This is triggered when wait_for_ready_callbacks should rebuild the wait list self._guard = GuardCondition(callback=None, callback_group=None, context=self._context) # True if shutdown has been called self._is_shutdown = False self._work_tracker = _WorkTracker() # Protect against shutdown() being called in parallel in two threads self._shutdown_lock = Lock() # State for wait_for_ready_callbacks to reuse generator self._cb_iter = None self._last_args = None self._last_kwargs = None # Executor cannot use ROS clock because that requires a node self._clock = Clock(clock_type=ClockType.STEADY_TIME) self._sigint_gc = SignalHandlerGuardCondition(context) self._context.on_shutdown(self.wake)
def __init__( self, node_name, *, context=None, cli_args=None, namespace=None, use_global_arguments=True, start_parameter_services=True, initial_parameters=None ): self._handle = None self._context = get_default_context() if context is None else context self._parameters = {} self.publishers = [] self.subscriptions = [] self.clients = [] self.services = [] self.timers = [] self.guards = [] self.waitables = [] self._default_callback_group = MutuallyExclusiveCallbackGroup() self._parameters_callback = None namespace = namespace or '' if not self._context.ok(): raise NotInitializedException('cannot create node') try: self._handle = _rclpy.rclpy_create_node( node_name, namespace, self._context.handle, cli_args, use_global_arguments) except ValueError: # these will raise more specific errors if the name or namespace is bad validate_node_name(node_name) # emulate what rcl_node_init() does to accept '' and relative namespaces if not namespace: namespace = '/' if not namespace.startswith('/'): namespace = '/' + namespace validate_namespace(namespace) # Should not get to this point raise RuntimeError('rclpy_create_node failed for unknown reason') self._logger = get_logger(_rclpy.rclpy_get_node_logger_name(self.handle)) # Clock that has support for ROS time. # TODO(dhood): use sim time if parameter has been set on the node. self._clock = ROSClock() self._time_source = TimeSource(node=self) self._time_source.attach_clock(self._clock) self.__executor_weakref = None self._parameter_event_publisher = self.create_publisher( ParameterEvent, 'parameter_events', qos_profile=qos_profile_parameter_events) node_parameters = _rclpy.rclpy_get_node_parameters(Parameter, self.handle) # Combine parameters from params files with those from the node constructor and # use the set_parameters_atomically API so a parameter event is published. if initial_parameters is not None: node_parameters.update({p.name: p for p in initial_parameters}) self.set_parameters_atomically(node_parameters.values()) if start_parameter_services: self._parameter_service = ParameterService(self)
def __init__(self, callback, callback_group, context=None): self._context = get_default_context() if context is None else context self.__handle = Handle( _rclpy.rclpy_create_guard_condition(self._context.handle)) self.callback = callback self.callback_group = callback_group # True when the callback is ready to fire but has not been "taken" by an executor self._executor_event = False # True when the executor sees this has been triggered but has not yet been handled self._executor_triggered = False
def init(*, args: Optional[List[str]] = None, context: Context = None) -> None: """ Initialize ROS communications for a given context. :param args: List of command line arguments. :param context: The context to initialize. If ``None``, then the default context is used (see :func:`.get_default_context`). """ context = get_default_context() if context is None else context return context.init(args)
def init(*, args: List[str] = None, context: Context = None) -> None: """ Initialize ROS communications for a given context. :param args: List of command line arguments. :param context: The context to initialize. If ``None``, then the default context is used (see :func:`.get_default_context`). """ context = get_default_context() if context is None else context # imported locally to avoid loading extensions on module import from rclpy.impl.implementation_singleton import rclpy_implementation return rclpy_implementation.rclpy_init(args if args is not None else sys.argv, context.handle)
def get_global_executor() -> 'Executor': global __executor if __executor is None: # imported locally to avoid loading extensions on module import from rclpy.executors import SingleThreadedExecutor __executor = SingleThreadedExecutor() context = get_default_context() def reset_executor(): global __executor __executor.shutdown() __executor = None context.on_shutdown(reset_executor) return __executor
def __init__(self, callback, callback_group, timer_period_ns, clock, *, context=None): self._context = get_default_context() if context is None else context self._clock = clock with self._clock.handle, self._context.handle: self.__timer = _rclpy.Timer(self._clock.handle, self._context.handle, timer_period_ns) self.timer_period_ns = timer_period_ns self.callback = callback self.callback_group = callback_group # True when the callback is ready to fire but has not been "taken" by an executor self._executor_event = False
def __init__(self, callback, callback_group, timer_period_ns, *, context=None): self._context = get_default_context() if context is None else context # TODO(sloretz) Allow passing clocks in via timer constructor self._clock = Clock(clock_type=ClockType.STEADY_TIME) [self.timer_handle, self.timer_pointer ] = _rclpy.rclpy_create_timer(self._clock._clock_handle, self._context.handle, timer_period_ns) self.timer_period_ns = timer_period_ns self.callback = callback self.callback_group = callback_group # True when the callback is ready to fire but has not been "taken" by an executor self._executor_event = False
def __init__(self, *, context=None): super().__init__() self._context = get_default_context() if context is None else context self._nodes = set() self._nodes_lock = RLock() # Tasks to be executed (oldest first) 3-tuple Task, Entity, Node self._tasks = [] self._tasks_lock = Lock() # This is triggered when wait_for_ready_callbacks should rebuild the wait list gc, gc_handle = _rclpy.rclpy_create_guard_condition(self._context.handle) self._guard_condition = gc self._guard_condition_handle = gc_handle # True if shutdown has been called self._is_shutdown = False self._work_tracker = _WorkTracker() # State for wait_for_ready_callbacks to reuse generator self._cb_iter = None self._last_args = None self._last_kwargs = None
def shutdown(*, context: Context = None, uninstall_handlers: Optional[bool] = None) -> None: """ Shutdown a previously initialized context. This will also shutdown the global executor. :param context: The context to invalidate. If ``None``, then the default context is used (see :func:`.get_default_context`). :param uninstall_handlers: If `None`, signal handlers will be uninstalled when shutting down the default context. If `True`, signal handlers will be uninstalled. If not, signal handlers won't be uninstalled. """ _shutdown(context=context) if (uninstall_handlers or (uninstall_handlers is None and (context is None or context is get_default_context()))): uninstall_signal_handlers()
def __init__(self, *, context: Context = None) -> None: super().__init__() self._context = get_default_context() if context is None else context self._nodes: Set[Node] = set() self._nodes_lock = RLock() # Tasks to be executed (oldest first) 3-tuple Task, Entity, Node self._tasks: List[Tuple[Task, Optional[WaitableEntityType], Optional[Node]]] = [] self._tasks_lock = Lock() # This is triggered when wait_for_ready_callbacks should rebuild the wait list gc, gc_handle = _rclpy.rclpy_create_guard_condition( self._context.handle) self._guard_condition = gc self._guard_condition_handle = gc_handle # True if shutdown has been called self._is_shutdown = False self._work_tracker = _WorkTracker() # Protect against shutdown() being called in parallel in two threads self._shutdown_lock = Lock() # State for wait_for_ready_callbacks to reuse generator self._cb_iter = None self._last_args = None self._last_kwargs = None
def init(*, args=None, context=None): context = get_default_context() if context is None else context # imported locally to avoid loading extensions on module import from rclpy.impl.implementation_singleton import rclpy_implementation return rclpy_implementation.rclpy_init( args if args is not None else sys.argv, context.handle)
def __init__( self, node_name: str, *, context: Context = None, cli_args: List[str] = None, namespace: str = None, use_global_arguments: bool = True, start_parameter_services: bool = True, initial_parameters: List[Parameter] = None, allow_undeclared_parameters: bool = False, automatically_declare_initial_parameters: bool = True) -> None: """ Constructor. :param node_name: A name to give to this node. Validated by :func:`validate_node_name`. :param context: The context to be associated with, or ``None`` for the default global context. :param cli_args: A list of strings of command line args to be used only by this node. :param namespace: The namespace to which relative topic and service names will be prefixed. Validated by :func:`validate_namespace`. :param use_global_arguments: ``False`` if the node should ignore process-wide command line args. :param start_parameter_services: ``False`` if the node should not create parameter services. :param initial_parameters: A list of parameters to be set during node creation. :param allow_undeclared_parameters: True if undeclared parameters are allowed. This flag affects the behavior of parameter-related operations. :param automatically_declare_initial_parameters: True if initial parameters have to be declared upon node creation, false otherwise. """ self.__handle = None self._context = get_default_context() if context is None else context self._parameters: dict = {} self.__publishers: List[Publisher] = [] self.__subscriptions: List[Subscription] = [] self.__clients: List[Client] = [] self.__services: List[Service] = [] self.__timers: List[WallTimer] = [] self.__guards: List[GuardCondition] = [] self.__waitables: List[Waitable] = [] self._default_callback_group = MutuallyExclusiveCallbackGroup() self._parameters_callback = None self._allow_undeclared_parameters = allow_undeclared_parameters self._initial_parameters = {} self._descriptors = {} namespace = namespace or '' if not self._context.ok(): raise NotInitializedException('cannot create node') try: self.__handle = Handle( _rclpy.rclpy_create_node(node_name, namespace, self._context.handle, cli_args, use_global_arguments)) except ValueError: # these will raise more specific errors if the name or namespace is bad validate_node_name(node_name) # emulate what rcl_node_init() does to accept '' and relative namespaces if not namespace: namespace = '/' if not namespace.startswith('/'): namespace = '/' + namespace validate_namespace(namespace) # Should not get to this point raise RuntimeError('rclpy_create_node failed for unknown reason') with self.handle as capsule: self._logger = get_logger( _rclpy.rclpy_get_node_logger_name(capsule)) # Clock that has support for ROS time. self._clock = ROSClock() self._time_source = TimeSource(node=self) self._time_source.attach_clock(self._clock) self.__executor_weakref = None self._parameter_event_publisher = self.create_publisher( ParameterEvent, 'parameter_events', qos_profile=qos_profile_parameter_events) with self.handle as capsule: self._initial_parameters = _rclpy.rclpy_get_node_parameters( Parameter, capsule) # Combine parameters from params files with those from the node constructor and # use the set_parameters_atomically API so a parameter event is published. if initial_parameters is not None: self._initial_parameters.update( {p.name: p for p in initial_parameters}) if automatically_declare_initial_parameters: self._parameters.update(self._initial_parameters) self._descriptors.update( {p: ParameterDescriptor() for p in self._parameters}) if start_parameter_services: self._parameter_service = ParameterService(self)
def __init__(self, context=None): if context is None: context = get_default_context() self.guard_handle, _ = _rclpy.rclpy_create_guard_condition(context.handle) _signals.rclpy_register_sigint_guard_condition(self.guard_handle)
def default_context(): rclpy.init() yield get_default_context() rclpy.shutdown()