def create_timer(self, timer_period_sec: float, callback: Callable, callback_group: CallbackGroup = None) -> WallTimer: """ Create a new timer. The timer will be started and every ``timer_period_sec`` number of seconds the provided callback function will be called. :param timer_period_sec: The period (s) of the timer. :param callback: A user-defined callback function that is called when the timer expires. :param callback_group: The callback group for the timer. If ``None``, then the nodes default callback group is used. """ timer_period_nsec = int(float(timer_period_sec) * S_TO_NS) if callback_group is None: callback_group = self.default_callback_group timer = WallTimer(callback, callback_group, timer_period_nsec, context=self.context) timer.handle.requires(self.handle) self.__timers.append(timer) callback_group.add_entity(timer) return timer
def create_client(self, srv_type, srv_name: str, *, qos_profile: QoSProfile = qos_profile_services_default, callback_group: CallbackGroup = None) -> Client: """ Create a new service client. :param srv_type: The service type. :param srv_name: The name of the service. :param qos_profile: The quality of service profile to apply the service client. :param callback_group: The callback group for the service client. 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: [client_handle, client_pointer ] = _rclpy.rclpy_create_client(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) client = Client(self.handle, self.context, client_handle, client_pointer, srv_type, srv_name, qos_profile, callback_group) self.clients.append(client) callback_group.add_entity(client) return client
def create_guard_condition( self, callback: Callable, callback_group: CallbackGroup = None) -> GuardCondition: """Create a new guard condition.""" if callback_group is None: callback_group = self.default_callback_group guard = GuardCondition(callback, callback_group, context=self.context) self.guards.append(guard) callback_group.add_entity(guard) return guard
def create_subscription(self, msg_type, topic: str, callback: Callable[[MsgType], None], *, qos_profile: QoSProfile = qos_profile_default, callback_group: CallbackGroup = None, raw: bool = False) -> Subscription: """ Create a new subscription. :param msg_type: The type of ROS messages the subscription will subscribe to. :param topic: The name of the topic the subscription will subscribe to. :param callback: A user-defined callback function that is called when a message is received by the subscription. :param qos_profile: The quality of service profile to apply to the subscription. :param callback_group: The callback group for the subscription. If ``None``, then the nodes default callback group is used. :param raw: If ``True``, then received messages will be stored in raw binary representation. """ if callback_group is None: callback_group = self.default_callback_group # this line imports the typesupport for the message module if not already done check_for_type_support(msg_type) failed = False try: with self.handle as capsule: subscription_capsule = _rclpy.rclpy_create_subscription( capsule, msg_type, topic, qos_profile.get_c_qos_profile()) except ValueError: failed = True if failed: self._validate_topic_or_service_name(topic) subscription_handle = Handle(subscription_capsule) subscription_handle.requires(self.handle) subscription = Subscription(subscription_handle, msg_type, topic, callback, callback_group, qos_profile, raw) self.__subscriptions.append(subscription) callback_group.add_entity(subscription) return subscription
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