|
| 1 | +# Copyright (C) 2025 Robotec.AI |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Type |
| 16 | + |
| 17 | +from pydantic import BaseModel, Field |
| 18 | + |
| 19 | +from rai.tools.ros2.base import BaseROS2Tool |
| 20 | +from rai.tools.ros2.detection.pcl import ( |
| 21 | + GrippingPointEstimator, |
| 22 | + PointCloudFilter, |
| 23 | + PointCloudFromSegmentation, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +class GetGrippingPointToolInput(BaseModel): |
| 28 | + object_name: str = Field( |
| 29 | + ..., |
| 30 | + description="The name of the object to get the gripping point of e.g. 'box', 'apple', 'screwdriver'", |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +# TODO(maciejmajek): Configuration system configurable with namespacing |
| 35 | +class GetGrippingPointTool(BaseROS2Tool): |
| 36 | + name: str = "get_gripping_point" |
| 37 | + description: str = "Get gripping points for specified object/objects. Returns 3D coordinates where a robot gripper can grasp the object." |
| 38 | + |
| 39 | + point_cloud_from_segmentation: PointCloudFromSegmentation |
| 40 | + gripping_point_estimator: GrippingPointEstimator |
| 41 | + point_cloud_filter: PointCloudFilter |
| 42 | + |
| 43 | + args_schema: Type[GetGrippingPointToolInput] = GetGrippingPointToolInput |
| 44 | + |
| 45 | + def _run(self, object_name: str) -> str: |
| 46 | + pcl = self.point_cloud_from_segmentation.run(object_name) |
| 47 | + pcl = self.point_cloud_filter.run(pcl) |
| 48 | + gps = self.gripping_point_estimator.run(pcl) |
| 49 | + |
| 50 | + message = "" |
| 51 | + if len(gps) == 0: |
| 52 | + message += f"No gripping point found for the object {object_name}\n" |
| 53 | + elif len(gps) == 1: |
| 54 | + message += f"The gripping point of the object {object_name} is {gps[0]}\n" |
| 55 | + else: |
| 56 | + message += f"Multiple gripping points found for the object {object_name}\n" |
| 57 | + |
| 58 | + for i, gp in enumerate(gps): |
| 59 | + message += ( |
| 60 | + f"The gripping point of the object {i + 1} {object_name} is {gp}\n" |
| 61 | + ) |
| 62 | + |
| 63 | + return message |
0 commit comments