test_interactive_obstacle_node.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 import sys
00019 import math
00020 from copy import deepcopy
00021 
00022 
00023 import rospy
00024 import tf
00025 from geometry_msgs.msg import Pose, PoseStamped
00026 from moveit_msgs.msg import CollisionObject
00027 from shape_msgs.msg import SolidPrimitive
00028 from visualization_msgs.msg import Marker, InteractiveMarker, InteractiveMarkerControl
00029 from interactive_markers.interactive_marker_server import *
00030 
00031 
00032 class InteractiveObstacle:
00033     def __init__(self):
00034 
00035         #Specify a frame_id - transformation to root_frame of obstacle_distance node is handled in according subscriber callback
00036         self.root_frame = rospy.get_param("root_frame")
00037         self.obstacle_frame = rospy.get_namespace() + "interactive_box_frame"
00038 
00039         self.pub = rospy.Publisher("obstacle_distance/registerObstacle", CollisionObject, queue_size=1, latch=True)
00040         self.br = tf.TransformBroadcaster()
00041 
00042         while self.pub.get_num_connections() < 1:
00043             rospy.logwarn("Please create a subscriber to '" + rospy.get_namespace() + "/obstacle_distance/registerObstacle' topic (Type: moveit_msgs/CollisionObject)")
00044             rospy.sleep(1.0)
00045 
00046         rospy.loginfo("Continue ...")
00047 
00048         # Compose CollisionObject (Box)
00049         self.co = CollisionObject()
00050         self.co.id = "Interactive Box"
00051         self.co.header.frame_id = self.obstacle_frame
00052         self.co.operation = CollisionObject.ADD
00053 
00054         primitive = SolidPrimitive()
00055         primitive.type = SolidPrimitive.SPHERE
00056         primitive.dimensions = [0.1] # extent x, y, z
00057         self.co.primitives.append(primitive)
00058 
00059         pose = Pose()
00060         pose.orientation.w = 1.0;
00061         self.co.primitive_poses.append(pose)
00062 
00063         # Compose InteractiveMarker
00064         self.interactive_obstacle_pose = PoseStamped()
00065         self.interactive_obstacle_pose.header.stamp = rospy.Time.now()
00066         self.interactive_obstacle_pose.header.frame_id = self.root_frame
00067 
00068         self.interactive_obstacle_pose.pose.position.x = -0.5
00069         self.interactive_obstacle_pose.pose.position.y =  0.3
00070         self.interactive_obstacle_pose.pose.position.z =  0.8
00071         self.interactive_obstacle_pose.pose.orientation.w = 1.0
00072 
00073         self.ia_server = InteractiveMarkerServer("obstacle_marker_server")
00074         self.int_marker = InteractiveMarker()
00075         self.int_marker.header.frame_id = self.root_frame
00076         self.int_marker.pose = self.interactive_obstacle_pose.pose
00077         self.int_marker.name = "interactive_obstacle_marker"
00078         self.int_marker.scale = 0.5
00079 
00080         # Setup MarkerControls
00081         control_3d = InteractiveMarkerControl()
00082         control_3d.always_visible = True
00083         control_3d.name = "move_rotate_3D"
00084         control_3d.interaction_mode = InteractiveMarkerControl.MOVE_ROTATE_3D
00085         self.int_marker.controls.append(control_3d)
00086 
00087         control = InteractiveMarkerControl()
00088         control.always_visible = True
00089         control.orientation.w = 1
00090         control.orientation.x = 1
00091         control.orientation.y = 0
00092         control.orientation.z = 0
00093         control.name = "move_x"
00094         control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
00095         self.int_marker.controls.append(deepcopy(control))
00096         control.name = "move_y"
00097         control.orientation.x = 0
00098         control.orientation.y = 1
00099         control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
00100         self.int_marker.controls.append(deepcopy(control))
00101         control.name = "move_z"
00102         control.orientation.y = 0
00103         control.orientation.z = 1
00104         control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
00105         self.int_marker.controls.append(deepcopy(control))
00106         control.orientation.w = 1
00107         control.orientation.x = 1
00108         control.orientation.y = 0
00109         control.orientation.z = 0
00110         control.name = "rotate_x"
00111         control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
00112         self.int_marker.controls.append(deepcopy(control))
00113         control.name = "rotate_y"
00114         control.orientation.x = 0
00115         control.orientation.y = 1
00116         control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
00117         self.int_marker.controls.append(deepcopy(control))
00118         control.name = "rotate_z"
00119         control.orientation.y = 0
00120         control.orientation.z = 1
00121         control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
00122         self.int_marker.controls.append(deepcopy(control))
00123 
00124         self.ia_server.insert(self.int_marker, self.marker_fb)
00125         self.ia_server.applyChanges()
00126 
00127         # initial send
00128         self.br.sendTransform(
00129             (self.interactive_obstacle_pose.pose.position.x, self.interactive_obstacle_pose.pose.position.y, self.interactive_obstacle_pose.pose.position.z),
00130             (self.interactive_obstacle_pose.pose.orientation.x, self.interactive_obstacle_pose.pose.orientation.y, self.interactive_obstacle_pose.pose.orientation.z, self.interactive_obstacle_pose.pose.orientation.w),
00131              rospy.Time.now(), self.obstacle_frame, self.interactive_obstacle_pose.header.frame_id)
00132         rospy.sleep(1.0)
00133         self.pub.publish(self.co)
00134         rospy.sleep(1.0)
00135 
00136     def marker_fb(self, fb):
00137         #p = feedback.pose.position
00138         #print feedback.marker_name + " is now at " + str(p.x) + ", " + str(p.y) + ", " + str(p.z)
00139         self.interactive_obstacle_pose.header = fb.header
00140         self.interactive_obstacle_pose.pose = fb.pose
00141         self.ia_server.applyChanges()
00142 
00143     def run(self):
00144         self.br.sendTransform(
00145             (self.interactive_obstacle_pose.pose.position.x, self.interactive_obstacle_pose.pose.position.y, self.interactive_obstacle_pose.pose.position.z),
00146             (self.interactive_obstacle_pose.pose.orientation.x, self.interactive_obstacle_pose.pose.orientation.y, self.interactive_obstacle_pose.pose.orientation.z, self.interactive_obstacle_pose.pose.orientation.w),
00147              rospy.Time.now(), self.obstacle_frame, self.interactive_obstacle_pose.header.frame_id)
00148 
00149         self.co.operation = CollisionObject.MOVE
00150         self.pub.publish(self.co)
00151 
00152 
00153 if __name__ == "__main__":
00154     rospy.init_node("interactive_obstacle_node")
00155 
00156     io = InteractiveObstacle()
00157 
00158     r = rospy.Rate(20)
00159     while not rospy.is_shutdown():
00160         io.run()
00161         try:
00162             r.sleep()
00163         except rospy.ROSInterruptException as e:
00164             #print "ROSInterruptException"
00165             pass


cob_obstacle_distance
Author(s): Marco Bezzon
autogenerated on Thu Jun 6 2019 21:19:14