-
Notifications
You must be signed in to change notification settings - Fork 47
feat: ros2 connector #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: ros2 connector #379
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
800c098
feat: allow auto message type for ROS2TopicAPI receive method
maciejmajek d9d35ce
feat: multiple feedback callbacks
maciejmajek e199420
feat(ROS2ActionAPI): handle on done callback
maciejmajek ce6bedb
feat: add metadata to BaseMessage
maciejmajek a0e1c30
feat: add helpers file in communication/ros2 tests directory
maciejmajek 06bca64
fix(test): action server
maciejmajek a9f6067
feat: Implement ROS2ARIConnector + tests
maciejmajek b7221d6
chore: update connectors api
maciejmajek bc07dcc
chore: remove unused code
maciejmajek 7395084
fix: add missing constructors for python3.10 compat
maciejmajek b80575a
chore: remove protocol from BaseMessage class
maciejmajek 0603706
style: rename __del__ to shutdown
maciejmajek c60763f
chore: exmplicit comment ros_setup fn
maciejmajek 3274763
chore: use variable instead of dict value
maciejmajek 0142321
test: wait for eventual feedback
maciejmajek 2748757
style: remove _cleanup method
maciejmajek 38df1ed
feat: use uuid for ros2 connector name
maciejmajek 0a466b4
style: code
maciejmajek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# Copyright (C) 2024 Robotec.AI | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import threading | ||
import uuid | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
from rclpy.executors import MultiThreadedExecutor | ||
from rclpy.node import Node | ||
|
||
from rai.communication.ari_connector import ARIConnector, ARIMessage | ||
from rai.communication.ros2.api import ROS2ActionAPI, ROS2ServiceAPI, ROS2TopicAPI | ||
|
||
|
||
class ROS2ARIMessage(ARIMessage): | ||
def __init__(self, payload: Any, metadata: Optional[Dict[str, Any]] = None): | ||
super().__init__(payload, metadata) | ||
|
||
|
||
class ROS2ARIConnector(ARIConnector[ROS2ARIMessage]): | ||
def __init__( | ||
self, node_name: str = f"rai_ros2_ari_connector_{str(uuid.uuid4())[-12:]}" | ||
): | ||
super().__init__() | ||
self._node = Node(node_name) | ||
self._topic_api = ROS2TopicAPI(self._node) | ||
self._service_api = ROS2ServiceAPI(self._node) | ||
self._actions_api = ROS2ActionAPI(self._node) | ||
|
||
self._executor = MultiThreadedExecutor() | ||
self._executor.add_node(self._node) | ||
self._thread = threading.Thread(target=self._executor.spin) | ||
self._thread.start() | ||
|
||
def send_message(self, message: ROS2ARIMessage, target: str): | ||
auto_qos_matching = message.metadata.get("auto_qos_matching", True) | ||
qos_profile = message.metadata.get("qos_profile", None) | ||
msg_type = message.metadata.get("msg_type", None) | ||
|
||
# TODO: allow msg_type to be None, add auto topic type detection | ||
if msg_type is None: | ||
raise ValueError("msg_type is required") | ||
|
||
self._topic_api.publish( | ||
topic=target, | ||
msg_content=message.payload, | ||
msg_type=msg_type, | ||
auto_qos_matching=auto_qos_matching, | ||
qos_profile=qos_profile, | ||
) | ||
|
||
def receive_message( | ||
self, | ||
source: str, | ||
timeout_sec: float = 1.0, | ||
msg_type: Optional[str] = None, | ||
auto_topic_type: bool = True, | ||
) -> ROS2ARIMessage: | ||
msg = self._topic_api.receive( | ||
topic=source, | ||
timeout_sec=timeout_sec, | ||
msg_type=msg_type, | ||
auto_topic_type=auto_topic_type, | ||
) | ||
return ROS2ARIMessage( | ||
payload=msg, metadata={"msg_type": str(type(msg)), "topic": source} | ||
) | ||
|
||
def service_call( | ||
self, message: ROS2ARIMessage, target: str, timeout_sec: float = 1.0 | ||
) -> ROS2ARIMessage: | ||
msg = self._service_api.call_service( | ||
service_name=target, | ||
service_type=message.metadata["msg_type"], | ||
request=message.payload, | ||
timeout_sec=timeout_sec, | ||
) | ||
maciejmajek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ROS2ARIMessage( | ||
payload=msg, metadata={"msg_type": str(type(msg)), "service": target} | ||
) | ||
|
||
def start_action( | ||
self, | ||
action_data: Optional[ROS2ARIMessage], | ||
target: str, | ||
on_feedback: Callable[[Any], None] = lambda _: None, | ||
on_done: Callable[[Any], None] = lambda _: None, | ||
timeout_sec: float = 1.0, | ||
) -> str: | ||
if not isinstance(action_data, ROS2ARIMessage): | ||
raise ValueError("Action data must be of type ROS2ARIMessage") | ||
|
||
accepted, handle = self._actions_api.send_goal( | ||
action_name=target, | ||
action_type=action_data.metadata["msg_type"], | ||
goal=action_data.payload, | ||
maciejmajek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
timeout_sec=timeout_sec, | ||
feedback_callback=on_feedback, | ||
done_callback=on_done, | ||
) | ||
if not accepted: | ||
raise RuntimeError("Action goal was not accepted") | ||
return handle | ||
|
||
maciejmajek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def terminate_action(self, action_handle: str): | ||
self._actions_api.terminate_goal(action_handle) | ||
|
||
def shutdown(self): | ||
self._executor.shutdown() | ||
self._thread.join() | ||
self._actions_api.shutdown() | ||
self._topic_api.shutdown() | ||
self._node.destroy_node() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.