diff --git a/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py b/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py index ae651f3..32f7c6e 100644 --- a/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py +++ b/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py @@ -26,10 +26,12 @@ import std_msgs.msg from std_msgs.msg import Header from rclpy.node import Node from rclpy.duration import Duration +from std_srvs.srv import SetBool from scene_graph_interfaces.msg import Object3DBoundingBox, Object3DBoundingBoxList, ClassifiedRoom, RoomPolygonList from scene_graph_interfaces.msg import FunObject3DBoundingBox, FunObject3DBoundingBoxList from visualization_msgs.msg import Marker, MarkerArray from geometry_msgs.msg import Point32, Point +from scene_graph_interfaces.msg import GraphObject, GraphFunctionalElement from scipy.spatial.transform import Rotation as R from shapely.geometry import Point as ShapelyPoint, Polygon as ShapelyPolygon from sensor_msgs_py import point_cloud2 @@ -301,6 +303,38 @@ class GraphManagementNode(Node): self.pending_yolo = False self.pending_rtdetr = False + # ==================================================================== + # CONTROL VARIABLES (ARM INTERFACE) + # ==================================================================== + + # If True graph can be updated + self.graph_updates_allowed = True + + # call with data=False -> Pause Graph Updates + # call with data=True -> Allow Graph Updates + self.set_update_srv = self.create_service( + SetBool, + '/scene_graph/set_update_state', + self.set_update_state_callback + ) + + self.get_logger().info('Graph Update Control ready.') + + # ==================================================================== + # TARGET OBJECT INTERFACE + # ==================================================================== + + self.target_id = None + self.target_type = None # 'object' or 'functional' + + # Publisher: Wir senden auf zwei Topics, je nachdem was es ist + self.target_obj_pub = self.create_publisher( + GraphObject, '/scene_graph/target/object', 10 + ) + self.target_func_pub = self.create_publisher( + GraphFunctionalElement, '/scene_graph/target/functional', 10 + ) + # ==================================================================== # TIMER FOR PERIODIC UPDATES @@ -411,7 +445,28 @@ class GraphManagementNode(Node): return np.empty((0, 3), dtype=np.float32) return np.asarray(points, dtype=np.float32) + def set_update_state_callback(self, request, response): + """ + Set Scene Graph Update State + request.data == True -> Allow Updates + request.data == False -> Pause Updates + """ + self.graph_updates_allowed = request.data + + status_text = "RESUMED" if self.graph_updates_allowed else "PAUSED" + self.get_logger().info(f"Graph updates set to: {status_text}") + + response.success = True + response.message = f"Graph updates are now {status_text}" + return response + def _try_process_yolo(self): + # Cancel if Graph Updates are paused + if not self.graph_updates_allowed: + self.last_yolo_bboxes = None + self.last_yolo_cloud_np = None + return + # We can only process if we have BOTH a cloud and a bbox message buffered if self.last_yolo_cloud_np is None or self.last_yolo_bboxes is None: return @@ -435,6 +490,12 @@ class GraphManagementNode(Node): self._process_yolo_bboxes(msg) def _try_process_rtdetr(self): + # Cancel if Graph Updates are paused + if not self.graph_updates_allowed: + self.last_yolo_bboxes = None + self.last_yolo_cloud_np = None + return + # We can only process if we have BOTH a cloud and a bbox message buffered if self.last_rtdetr_cloud_np is None or self.last_rtdetr_bboxes is None: return @@ -820,6 +881,18 @@ class GraphManagementNode(Node): self.scene_graph.add_edge(self.current_room_id, new_id) self._merge_nearby_object_nodes(new_id) + # --- TEST MODE START --- + # Wir bestimmen die aktuelle ID (entweder match_id vom Merge oder new_obj.id vom Erstellen) + # current_id = match_id + # if current_id is None and 'new_obj' in locals(): + # current_id = new_obj.id + + # Wenn wir noch kein Target haben, nehmen wir das erste beste Objekt + # if self.target_id is None and current_id is not None: + # self.target_id = current_id + # self.target_type = 'object' + # self.get_logger().info(f"TEST MODE: Auto-selected Target ID {self.target_id}") + # --- TEST MODE END --- finally: # 7) Release graph lock @@ -1128,7 +1201,7 @@ class GraphManagementNode(Node): - def main_loop_callback(self): #TODO rework with functional edges + def main_loop_callback(self): """ Timer callback that runs at 2 Hz for periodic graph maintenance. @@ -1156,6 +1229,8 @@ class GraphManagementNode(Node): self.publish_markers() finally: self.graph_lock = False + + self.publish_target_data() """ # FIXME: add room functionality later @@ -1410,6 +1485,68 @@ class GraphManagementNode(Node): cloud_data = [(float(x), float(y), float(z), rgb_f) for x, y, z in pts] return point_cloud2.create_cloud(header, fields, cloud_data) + def publish_target_data(self): + """ + Publisht kontinuierlich Daten zum gewählten Ziel (falls vorhanden). + """ + # Wenn kein Ziel gesetzt ist, machen wir nichts. + if self.target_id is None: + return + # Falls das Ziel gelöscht wurde (z.B. durch Merge), Reset. + if self.target_id not in self.scene_graph: + # Optional: Warning nur 1x loggen, sonst spammt es + # self.get_logger().warn(f"Target ID {self.target_id} not in graph.") + self.target_id = None + return + node_data = self.scene_graph.nodes[self.target_id]['data'] + centroid = self._get_node_centroid(node_data) + # === FALL 1: NORMALES OBJEKT === + if self.target_type == 'object' and isinstance(node_data, ObjectNode): + msg = GraphObject() + msg.id = node_data.id + msg.class_id = node_data.class_id + msg.graspable = bool(node_data.graspable) + + if centroid is not None: + msg.center_position = Point(x=float(centroid[0]), y=float(centroid[1]), z=float(centroid[2])) + + # Bounding Box direkt übernehmen (Point32[]) + if node_data.bounding_box is not None: + msg.bounding_box = node_data.bounding_box + else: + msg.bounding_box = [] + # PointCloud konvertieren + if node_data.point_cloud is not None: + msg.point_cloud = self.o3d_to_pointcloud2(node_data.point_cloud, frame_id="map") + self.target_obj_pub.publish(msg) + # === FALL 2: FUNKTIONALES ELEMENT === + elif self.target_type == 'functional' and isinstance(node_data, FunctionalElementNode): + msg = GraphFunctionalElement() + msg.id = node_data.id + msg.functional_label = node_data.functional_label + msg.affordances = list(node_data.affordances) if node_data.affordances else [] + + if centroid is not None: + msg.center_position = Point(x=float(centroid[0]), y=float(centroid[1]), z=float(centroid[2])) + + # Parent ID setzen + msg.parent_object_id = node_data.parent_object_id if node_data.parent_object_id is not None else -1 + # Bounding Box: Berechnen, falls nicht vorhanden (Sicherheitsnetz) + if node_data.point_cloud is not None and len(node_data.point_cloud.points) > 0: + try: + min_bound = node_data.point_cloud.get_min_bound() + max_bound = node_data.point_cloud.get_max_bound() + p_min = Point32(x=float(min_bound[0]), y=float(min_bound[1]), z=float(min_bound[2])) + p_max = Point32(x=float(max_bound[0]), y=float(max_bound[1]), z=float(max_bound[2])) + msg.bounding_box = [p_min, p_max] + except Exception: + msg.bounding_box = [] + else: + msg.bounding_box = [] + # PointCloud konvertieren + if node_data.point_cloud is not None: + msg.point_cloud = self.o3d_to_pointcloud2(node_data.point_cloud, frame_id="map") + self.target_func_pub.publish(msg) def publish_markers(self): """ @@ -2941,7 +3078,7 @@ class GraphManagementNode(Node): return False - def export_scene_graph_to_json(self, filename): #TODO rework + def export_scene_graph_to_json(self, filename): #TODO rework?! """ Export the current scene graph to a hierarchical JSON file.