def destroy_service(self, service: Service) -> bool: """ Destroy a service server created by the node. :return: ``True`` if successful, ``False`` otherwise. """ if service in self.__services: self.__services.remove(service) try: service.destroy() except InvalidHandle: return False return True return False
def create_service(self, srv_type, srv_name, callback, *, qos_profile=qos_profile_services_default, callback_group=None): if callback_group is None: callback_group = self._default_callback_group check_for_type_support(srv_type) failed = False try: [service_handle, service_pointer ] = _rclpy.rclpy_create_service(self.handle, srv_type, srv_name, qos_profile.get_c_qos_profile()) except ValueError: failed = True if failed: self._validate_topic_or_service_name(srv_name, is_service=True) service = Service(self.handle, service_handle, service_pointer, srv_type, srv_name, callback, callback_group, qos_profile) self.services.append(service) callback_group.add_entity(service) return service
def create_service( self, srv_type, srv_name, callback, qos_profile=qos_profile_services_default): if srv_type.__class__._TYPE_SUPPORT is None: srv_type.__class__.__import_type_support__() if srv_type.__class__._TYPE_SUPPORT is None: raise NoTypeSupportImportedException [service_handle, service_pointer] = _rclpy.rclpy_create_service( self.handle, srv_type, srv_name, qos_profile.get_c_qos_profile()) service = Service( self.handle, service_handle, service_pointer, srv_type, srv_name, callback, qos_profile) self.services.append(service) return service
def create_service(self, srv_type, srv_name: str, callback: Callable[[SrvTypeRequest, SrvTypeResponse], SrvTypeResponse], *, qos_profile: QoSProfile = qos_profile_services_default, callback_group: CallbackGroup = None) -> Service: """ Create a new service server. :param srv_type: The service type. :param srv_name: The name of the service. :param callback: A user-defined callback function that is called when a service request received by the server. :param qos_profile: The quality of service profile to apply the service server. :param callback_group: The callback group for the service server. If ``None``, then the nodes default callback group is used. """ if callback_group is None: callback_group = self.default_callback_group check_for_type_support(srv_type) failed = False try: with self.handle as node_capsule: service_capsule = _rclpy.rclpy_create_service( node_capsule, srv_type, srv_name, qos_profile.get_c_qos_profile()) except ValueError: failed = True if failed: self._validate_topic_or_service_name(srv_name, is_service=True) service_handle = Handle(service_capsule) service_handle.requires(self.handle) service = Service(service_handle, srv_type, srv_name, callback, callback_group, qos_profile) self.__services.append(service) callback_group.add_entity(service) return service
def __init__( self, *, enable_communication_interface: bool = True, callback_group: Optional[CallbackGroup] = None, ): """ Initialize a lifecycle node. :param enable_communication_interface: Creates the lifecycle nodes services and publisher if True. :param callback_group: Callback group that will be used by all the lifecycle node services. """ self._callbacks: Dict[int, Callable[[LifecycleState], TransitionCallbackReturn]] = {} # register all state machine transition callbacks self.__register_callback( lifecycle_msgs.msg.State.TRANSITION_STATE_CONFIGURING, self.on_configure) self.__register_callback( lifecycle_msgs.msg.State.TRANSITION_STATE_CLEANINGUP, self.on_cleanup) self.__register_callback( lifecycle_msgs.msg.State.TRANSITION_STATE_SHUTTINGDOWN, self.on_shutdown) self.__register_callback( lifecycle_msgs.msg.State.TRANSITION_STATE_ACTIVATING, self.on_activate) self.__register_callback( lifecycle_msgs.msg.State.TRANSITION_STATE_DEACTIVATING, self.on_deactivate) self.__register_callback( lifecycle_msgs.msg.State.TRANSITION_STATE_ERRORPROCESSING, self.on_error) if callback_group is None: callback_group = self.default_callback_group self._managed_entities: Set[ManagedEntity] = set() for srv_type in ( lifecycle_msgs.srv.ChangeState, lifecycle_msgs.srv.GetState, lifecycle_msgs.srv.GetAvailableStates, lifecycle_msgs.srv.GetAvailableTransitions, ): # this doesn't only checks, but also imports some stuff we need later check_is_valid_srv_type(srv_type) with self.handle: self._state_machine = _rclpy.LifecycleStateMachine( self.handle, enable_communication_interface) if enable_communication_interface: self._service_change_state = Service( self._state_machine.service_change_state, lifecycle_msgs.srv.ChangeState, self._state_machine.service_change_state.name, self.__on_change_state, callback_group, QoSProfile(**self._state_machine.service_change_state.qos)) self._service_get_state = Service( self._state_machine.service_get_state, lifecycle_msgs.srv.GetState, self._state_machine.service_get_state.name, self.__on_get_state, callback_group, QoSProfile(**self._state_machine.service_get_state.qos)) self._service_get_available_states = Service( self._state_machine.service_get_available_states, lifecycle_msgs.srv.GetAvailableStates, self._state_machine.service_get_available_states.name, self.__on_get_available_states, callback_group, QoSProfile( **self._state_machine.service_get_available_states.qos)) self._service_get_available_transitions = Service( self._state_machine.service_get_available_transitions, lifecycle_msgs.srv.GetAvailableTransitions, self._state_machine.service_get_available_transitions.name, self.__on_get_available_transitions, callback_group, QoSProfile(**self._state_machine. service_get_available_transitions.qos)) self._service_get_transition_graph = Service( self._state_machine.service_get_transition_graph, lifecycle_msgs.srv.GetAvailableTransitions, self._state_machine.service_get_transition_graph.name, self.__on_get_transition_graph, callback_group, QoSProfile( **self._state_machine.service_get_transition_graph.qos)) lifecycle_services = [ self._service_change_state, self._service_get_state, self._service_get_available_states, self._service_get_available_transitions, self._service_get_transition_graph, ] for s in lifecycle_services: callback_group.add_entity(s) # Extend base class list of services, so they are added to the executor when spinning. self._services.extend(lifecycle_services)