From 4b4c98083a604394fbd416b792d99ea32261dc22 Mon Sep 17 00:00:00 2001 From: Guillaume Sarthou Date: Fri, 12 Jan 2024 19:49:32 +0100 Subject: [PATCH] [ontopy] fix ros2 compatibility --- CMakeLists.txt | 7 ++++ ontopy/__init__.py | 1 + ontopy/ontologenius/ConversionClient.py | 8 +++- ontopy/ontologenius/FeederPublisher.py | 11 +++++- ontopy/ontologenius/clients/ClientBase.py | 18 ++++++--- ontopy/ontologenius/clients/SparqlClient.py | 9 ++++- .../clientsIndex/ClientBaseIndex.py | 19 ++++++---- .../clientsIndex/SparqlIndexClient.py | 9 ++++- ontopy/ontologenius/compat/ros.py | 38 ++++++++++++------- 9 files changed, 85 insertions(+), 35 deletions(-) create mode 100644 ontopy/__init__.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 816f2dc3..4f995fee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,13 @@ find_package(ament_cmake REQUIRED) find_package(rosidl_default_generators REQUIRED) find_package(rclcpp REQUIRED) find_package(TinyXML2 REQUIRED) +find_package(ament_cmake_python REQUIRED) + +#install(PROGRAMS +# ontopy/${PROJECT_NAME} +# DESTINATION lib) + +ament_python_install_package(ontopy) endif() ################################################ diff --git a/ontopy/__init__.py b/ontopy/__init__.py new file mode 100644 index 00000000..0f33b917 --- /dev/null +++ b/ontopy/__init__.py @@ -0,0 +1 @@ +from .ontologenius import * \ No newline at end of file diff --git a/ontopy/ontologenius/ConversionClient.py b/ontopy/ontologenius/ConversionClient.py index 04c35b3f..8ee65f5d 100644 --- a/ontopy/ontologenius/ConversionClient.py +++ b/ontopy/ontologenius/ConversionClient.py @@ -1,7 +1,11 @@ from .compat.ros import Ontoros, OntoService +import os from ontologenius.srv import OntologeniusConversion -from ontologenius.srv import OntologeniusConversionRequest +if os.environ["ROS_VERSION"] == "1": + from ontologenius.srv import OntologeniusConversionRequest +else: + from ontologenius.srv._ontologenius_conversion import OntologeniusConversion_Request as OntologeniusConversionRequest class ConversionClient: @@ -112,5 +116,5 @@ def _id2Index(self, id, source): return None def _call(self, source, values_str, values_int): - request = OntologeniusConversionRequest(source, values_str, values_int) + request = OntologeniusConversionRequest(source = source, values_str = values_str, values_int = values_int) return self._client.call(request, self._verbose) \ No newline at end of file diff --git a/ontopy/ontologenius/FeederPublisher.py b/ontopy/ontologenius/FeederPublisher.py index 31f7e09e..b2f8fcbe 100644 --- a/ontopy/ontologenius/FeederPublisher.py +++ b/ontopy/ontologenius/FeederPublisher.py @@ -1,6 +1,12 @@ from .compat.ros import Ontoros + +import os + from std_msgs.msg import String -from ontologenius.msg import StampedString +if os.environ["ROS_VERSION"] == "1": + from ontologenius.msg import StampedString +else: + from ontologenius.msg import OntologeniusStampedString as StampedString import time import random @@ -240,7 +246,8 @@ def _publish(self, data): self._pub.publish(data) def _publish_stamped(self, data, stamp): - self._stamped_pub.publish(data, stamp) + msg = StampedString(data = data, stamp = stamp) + self._stamped_pub.publish(msg) def commitCallback(self, data): if data.data == 'end': diff --git a/ontopy/ontologenius/clients/ClientBase.py b/ontopy/ontologenius/clients/ClientBase.py index b968ebe3..66b8b312 100644 --- a/ontopy/ontologenius/clients/ClientBase.py +++ b/ontopy/ontologenius/clients/ClientBase.py @@ -1,6 +1,12 @@ -from ..compat.ros import Ontoros, OntoService +from ..compat.ros import Ontoros +import os + +from ontologenius.srv import OntologeniusService +if os.environ["ROS_VERSION"] == "1": + from ontologenius.srv import OntologeniusServiceRequest +else: + from ontologenius.srv._ontologenius_service import OntologeniusService_Request as OntologeniusServiceRequest -from ontologenius.srv import OntologeniusService, OntologeniusServiceRequest class ClientBase: """The ClientBase class provides an abstraction for any ROS services. @@ -37,7 +43,7 @@ def call(self, action, param): If the service call fails, the function returns None """ ClientBase._cpt += 1 - request = OntologeniusServiceRequest(action, param) + request = OntologeniusServiceRequest(action = action, param = param) response = self._client.call(request, ClientBase._verbose) if(response is None): self.error_code = -1 @@ -52,7 +58,7 @@ def callStr(self, action, param): If the service call fails, the function returns None """ ClientBase._cpt += 1 - request = OntologeniusServiceRequest(action, param) + request = OntologeniusServiceRequest(action = action, param = param) response = self._client.call(request, ClientBase._verbose) if(response is None): self.error_code = -1 @@ -70,7 +76,7 @@ def callNR(self, action, param): If the service call fails, the function returns False """ ClientBase._cpt += 1 - request = OntologeniusServiceRequest(action, param) + request = OntologeniusServiceRequest(action = action, param = param) response = self._client.call(request, ClientBase._verbose) if(response is None): self.error_code = -1 @@ -86,7 +92,7 @@ def callBool(self, action, param): service is different from SUCCESS. """ ClientBase._cpt += 1 - request = OntologeniusServiceRequest(action, param) + request = OntologeniusServiceRequest(action = action, param = param) response = self._client.call(request, ClientBase._verbose) if(response is None): self.error_code = -1 diff --git a/ontopy/ontologenius/clients/SparqlClient.py b/ontopy/ontologenius/clients/SparqlClient.py index eda1be25..a2886663 100644 --- a/ontopy/ontologenius/clients/SparqlClient.py +++ b/ontopy/ontologenius/clients/SparqlClient.py @@ -1,6 +1,11 @@ from ..compat.ros import Ontoros, OntoService +import os -from ontologenius.srv import OntologeniusSparqlService, OntologeniusSparqlServiceRequest +from ontologenius.srv import OntologeniusSparqlService +if os.environ["ROS_VERSION"] == "1": + from ontologenius.srv import OntologeniusSparqlServiceRequest +else: + from ontologenius.srv._ontologenius_sparql_service import OntologeniusSparqlService_Request as OntologeniusSparqlServiceRequest class SparqlClient: """The SparqlClient class provides a ROS service to explore ontologenius with SPARQL-like queries. @@ -17,7 +22,7 @@ def __init__(self, name): self._client = Ontoros.createService('ontologenius/' + self._name, OntologeniusSparqlService) def call(self, query): - request = OntologeniusSparqlServiceRequest(query) + request = OntologeniusSparqlServiceRequest(query = query) response = self._client.call(request) if(response is None): return None diff --git a/ontopy/ontologenius/clientsIndex/ClientBaseIndex.py b/ontopy/ontologenius/clientsIndex/ClientBaseIndex.py index b07f0de0..aab59c76 100644 --- a/ontopy/ontologenius/clientsIndex/ClientBaseIndex.py +++ b/ontopy/ontologenius/clientsIndex/ClientBaseIndex.py @@ -1,6 +1,11 @@ from ..compat.ros import Ontoros, OntoService +import os -from ontologenius.srv import OntologeniusIndexService, OntologeniusIndexServiceRequest +from ontologenius.srv import OntologeniusIndexService +if os.environ["ROS_VERSION"] == "1": + from ontologenius.srv import OntologeniusIndexServiceRequest +else: + from ontologenius.srv._ontologenius_index_service import OntologeniusIndexService_Request as OntologeniusIndexServiceRequest class ClientBaseIndex: """The ClientBaseIndex class provides an abstraction for any ROS services. @@ -37,7 +42,7 @@ def call(self, action, param): If the service call fails, the function returns None """ ClientBaseIndex._cpt += 1 - request = OntologeniusIndexServiceRequest(action, param) + request = OntologeniusIndexServiceRequest(action = action, param = param) response = self._client.call(request, ClientBaseIndex._verbose) if(response is None): self.error_code = -1 @@ -52,7 +57,7 @@ def callIndexes(self, action, param): If the service call fails, the function returns None """ ClientBaseIndex._cpt += 1 - request = OntologeniusIndexServiceRequest(action, param) + request = OntologeniusIndexServiceRequest(action = action, param = param) response = self._client.call(request, ClientBaseIndex._verbose) if(response is None): self.error_code = -1 @@ -67,7 +72,7 @@ def callStr(self, action, param): If the service call fails, the function returns None """ ClientBaseIndex._cpt += 1 - request = OntologeniusIndexServiceRequest(action, param) + request = OntologeniusIndexServiceRequest(action = action, param = param) response = self._client.call(request, ClientBaseIndex._verbose) if(response is None): self.error_code = -1 @@ -85,7 +90,7 @@ def callIndex(self, action, param): If the service call fails, the function returns None """ ClientBaseIndex._cpt += 1 - request = OntologeniusIndexServiceRequest(action, param) + request = OntologeniusIndexServiceRequest(action = action, param = param) response = self._client.call(request, ClientBaseIndex._verbose) if(response is None): self.error_code = -1 @@ -103,7 +108,7 @@ def callNR(self, action, param): If the service call fails, the function returns False """ ClientBaseIndex._cpt += 1 - request = OntologeniusIndexServiceRequest(action, param) + request = OntologeniusIndexServiceRequest(action = action, param = param) response = self._client.call(request, ClientBaseIndex._verbose) if(response is None): self.error_code = -1 @@ -119,7 +124,7 @@ def callBool(self, action, param): service is different from SUCCESS. """ ClientBaseIndex._cpt += 1 - request = OntologeniusIndexServiceRequest(action, param) + request = OntologeniusIndexServiceRequest(action = action, param = param) response = self._client.call(request, ClientBaseIndex._verbose) if(response is None): self.error_code = -1 diff --git a/ontopy/ontologenius/clientsIndex/SparqlIndexClient.py b/ontopy/ontologenius/clientsIndex/SparqlIndexClient.py index 3d1b0c8c..98cf15d4 100644 --- a/ontopy/ontologenius/clientsIndex/SparqlIndexClient.py +++ b/ontopy/ontologenius/clientsIndex/SparqlIndexClient.py @@ -1,6 +1,11 @@ from ..compat.ros import Ontoros, OntoService +import os -from ontologenius.srv import OntologeniusSparqlIndexService, OntologeniusSparqlIndexServiceRequest +from ontologenius.srv import OntologeniusSparqlIndexService +if os.environ["ROS_VERSION"] == "1": + from ontologenius.srv import OntologeniusSparqlIndexServiceRequest +else: + from ontologenius.srv._ontologenius_sparql_index_service import OntologeniusSparqlIndexService_Request as OntologeniusSparqlIndexServiceRequest class SparqlIndexClient: """The SparqlIndexClient class provides a ROS service to explore ontologenius with SPARQL-like queries based on indexes. @@ -18,7 +23,7 @@ def __init__(self, name): def call(self, query): - request = OntologeniusSparqlIndexServiceRequest(query) + request = OntologeniusSparqlIndexServiceRequest(query = query) response = self._client.call(request) if(response is None): return None diff --git a/ontopy/ontologenius/compat/ros.py b/ontopy/ontologenius/compat/ros.py index aaa4a052..a7590cac 100644 --- a/ontopy/ontologenius/compat/ros.py +++ b/ontopy/ontologenius/compat/ros.py @@ -1,4 +1,5 @@ import os +from ontologenius.msg import OntologeniusTimestamp if os.environ["ROS_VERSION"] == "1": import rospy @@ -50,6 +51,9 @@ def __init__(self, sub_name, sub_type, callback): def getNumPublishers(self): self.sub.get_num_connections() + def unregister(self): + self.sub.unregister() + class Ontoros : def createService(srv_name, srv_type): return OntoService(srv_name, srv_type) @@ -61,7 +65,7 @@ def createSubscriber(sub_name, sub_type, callback): return OntoSubscriber(sub_name, sub_type, callback) def getRosTime(): - return rospy.get_rostime() + return rospy.get_rostime() # TODO def isShutdown(): return rospy.is_shutdown() @@ -114,20 +118,21 @@ def __call__(cls, *args, **kwargs): class OntoService: - def __init__(self, client: Client, node_: Node): + def __init__(self, client: Client, name, node_: Node): self.client: Client = client self.node_: Node = node_ + self.srv_name = name - def call(self, params: SrvTypeRequest) -> SrvTypeResponse: + def call(self, params: SrvTypeRequest, verbose) -> SrvTypeResponse: future = self.client.call_async(request=params) rclpy.spin_until_future_complete(self.node_, future) return future.result() - def wait(self, timeout = -1): # TODO + def wait(self, timeout = -1): if(timeout != -1): - self.client.wait_for_service(self.srv_name, timeout) + self.client.wait_for_service(timeout) else: - self.client.wait_for_service(self.srv_name) + self.client.wait_for_service() class OntoPublisher: @@ -138,7 +143,7 @@ def publish(self, msg): self.pub.publish(msg) def getNumSubscribers(self): - self.pub.get_num_connections() # TODO + self.pub.get_num_connections() class OntoSubscriber: @@ -146,29 +151,34 @@ def __init__(self, sub): self.sub: Subscription = sub def getNumPublishers(self): - self.sub.get_num_connections() # TODO + self.sub.get_num_connections() + + def unregister(self): + pass - class OntoROS(Node, metaclass=SingletonMeta): + class Ontoros(Node, metaclass=SingletonMeta): def __init__(self): super().__init__("OntoRos") @staticmethod - def createService(srv_name, srv_type, connected) -> OntoService: - return OntoService(OntoROS().create_client(srv_name, srv_type), OntoROS()) + def createService(srv_name, srv_type) -> OntoService: + return OntoService(Ontoros().create_client(srv_type, srv_name), srv_name, Ontoros()) @staticmethod def createPublisher(pub_name, pub_type, queue_size: int) -> OntoPublisher: - return OntoPublisher(OntoROS().create_publisher(pub_type, pub_name, qos_profile=queue_size)) + return OntoPublisher(Ontoros().create_publisher(pub_type, pub_name, qos_profile=queue_size)) @staticmethod def createSubscriber(sub_name, sub_type, callback, queue_size: int = 10) -> OntoSubscriber: - return OntoSubscriber(OntoROS().create_subscription(sub_type, sub_name, callback, qos_profile=queue_size)) + return OntoSubscriber(Ontoros().create_subscription(sub_type, sub_name, callback, qos_profile=queue_size)) @staticmethod def getRosTime() -> Time: - return OntoROS().get_clock().now() + t_msg = Ontoros().get_clock().now().to_msg() + stamp_msg = OntologeniusTimestamp(seconds = t_msg._sec, nanoseconds = t_msg._nanosec) + return stamp_msg @staticmethod def isShutdown() -> bool: