simple_moveit_interface.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import roslib; roslib.load_manifest('cob_moveit_interface')
00004 import threading
00005 from copy import deepcopy
00006 
00007 import rospy
00008 import tf
00009 from geometry_msgs.msg import PoseStamped
00010 from moveit_commander import MoveGroupCommander, PlanningSceneInterface
00011 
00012 
00013 
00014 
00015 
00016 _transform_listener = None
00017 _transform_listener_creation_lock = threading.Lock()
00018 
00019 _mgc_dict = dict()
00020 _mgc_dict_creation_lock = threading.Lock()
00021 
00022 _psi = None
00023 _psi_creation_lock = threading.Lock()
00024 
00025 def get_transform_listener():
00026     '''
00027     Gets the transform listener for this process.
00028 
00029     This is needed because tf only allows one transform listener per process. Threadsafe, so
00030     that two threads could call this at the same time, at it will do the right thing.
00031     '''
00032     global _transform_listener
00033     with _transform_listener_creation_lock:
00034         if _transform_listener == None:
00035             _transform_listener = tf.TransformListener()
00036         return _transform_listener
00037 
00038 
00039 
00040 def get_move_group_commander(group):
00041     '''
00042     Gets the move_group_commander for this process.
00043 
00044     '''
00045     global _mgc_dict
00046     with _mgc_dict_creation_lock:
00047         if not group in _mgc_dict:
00048             _mgc_group = MoveGroupCommander(group)
00049             _mgc_group.set_planner_id('RRTConnectkConfigDefault')
00050             _mgc_dict[group] = _mgc_group
00051             add_ground()
00052         return _mgc_dict[group]
00053 
00054 
00055 
00056 def get_planning_scene_interface():
00057     '''
00058     Gets the planning_scene_interface for this process.
00059 
00060     '''
00061     global _psi
00062     with _psi_creation_lock:
00063         if _psi == None:
00064             _psi = PlanningSceneInterface()
00065         return _psi
00066 
00067 
00068 def add_ground():
00069     psi = get_planning_scene_interface()
00070     pose = PoseStamped()
00071     pose.pose.position.x = 0
00072     pose.pose.position.y = 0
00073     # offset such that the box is 0.1 mm below ground (to prevent collision with the robot itself)
00074     pose.pose.position.z = -0.0501
00075     pose.pose.orientation.x = 0
00076     pose.pose.orientation.y = 0
00077     pose.pose.orientation.z = 0
00078     pose.pose.orientation.w = 1
00079     pose.header.stamp = rospy.get_rostime()
00080     pose.header.frame_id = "base_link"
00081     psi.attach_box("base_link", "ground", pose, (3, 3, 0.1))
00082 
00083 def clear_objects():
00084     psi = get_planning_scene_interface()
00085     psi.remove_attached_object("arm_7_link")
00086     psi.remove_world_object("")
00087     
00088 def clear_attached_object(object_name):
00089     psi = get_planning_scene_interface()
00090     psi.remove_attached_object(link="arm_7_link", name = object_name)
00091     psi.remove_world_object(object_name)
00092     
00093 
00094 def attach_mesh_to_link(link, name, path):
00095     psi = get_planning_scene_interface()
00096     pose = PoseStamped()
00097     pose.pose.orientation.w = 1
00098     pose.header.stamp = rospy.get_rostime()
00099     pose.header.frame_id = link
00100     psi.attach_mesh(link, name, pose, path)
00101 
00102 def moveit_joint_goal(group, goal, replanning=False):
00103     mgc = get_move_group_commander(group)
00104     mgc.allow_replanning(replanning)
00105     if mgc.go(goal):
00106         print "Done moving"
00107         return 'succeeded'
00108     else:
00109         print "Failed to plan path"
00110         return 'failed'
00111 
00112 def moveit_pose_goal(group, ref_frame, goal, replan=False):
00113     mgc = get_move_group_commander(group)
00114     mgc.allow_replanning(replan)
00115     mgc.set_pose_reference_frame(ref_frame)
00116     ret = mgc.go(goal)
00117     mgc.allow_replanning(False)
00118     if ret:
00119         print "Done moving"
00120         return 'succeeded'
00121     else:
00122         print "Failed to plan path"
00123         return 'failed'
00124     
00125     
00126 
00127 #this is linear movement
00128 def moveit_cart_goals(group, ref_frame, goal_list, avoid_collisions=True):
00129     mgc = get_move_group_commander(group)
00130     
00131     mgc.set_pose_reference_frame(ref_frame)
00132     (traj,frac)  = mgc.compute_cartesian_path(goal_list, 0.01, 4, avoid_collisions)
00133     print traj,frac
00134 
00135     #mgc.execute(traj)
00136     #print "Done moving"
00137     #return 'succeeded'  
00138     
00139     if frac == 1.0:
00140         if mgc.execute(traj):
00141                 print "Done moving"
00142                 return 'succeeded'
00143         else:
00144                 print "Something happened during execution"
00145                 print 'failed'
00146     else:
00147         print "Failed to plan full path!"
00148         return 'failed'
00149 
00150 
00151 def moveit_get_current_pose(group):
00152     mgc = get_move_group_commander(group)
00153 
00154     cp = mgc.get_current_pose()
00155     cp.header.stamp = rospy.Time()
00156     return cp
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 ####################################
00165 #  helpers
00166 ####################################
00167 
00168 def get_goal_from_server(group, parameter_name):
00169 
00170         ns_global_prefix = "/script_server"
00171 
00172         # get joint_names from parameter server
00173         param_string = ns_global_prefix + "/" + group + "/joint_names"
00174         if not rospy.has_param(param_string):
00175                 rospy.logerr("parameter %s does not exist on ROS Parameter Server, aborting...",param_string)
00176                 return None
00177         joint_names = rospy.get_param(param_string)
00178         
00179         # check joint_names parameter
00180         if not type(joint_names) is list: # check list
00181                 rospy.logerr("no valid joint_names for %s: not a list, aborting...",group)
00182                 print "joint_names are:",joint_names
00183                 return None
00184         else:
00185             for i in joint_names:
00186                 if not type(i) is str: # check string
00187                     rospy.logerr("no valid joint_names for %s: not a list of strings, aborting...",group)
00188                     print "joint_names are:",joint_names
00189                     return None
00190                 else:
00191                     rospy.logdebug("accepted joint_names for group %s",group)
00192         
00193         # get joint values from parameter server
00194         if type(parameter_name) is str:
00195             if not rospy.has_param(ns_global_prefix + "/" + group + "/" + parameter_name):
00196                 rospy.logerr("parameter %s does not exist on ROS Parameter Server, aborting...",ns_global_prefix + "/" + group + "/" + parameter_name)
00197                 return None
00198             param = rospy.get_param(ns_global_prefix + "/" + group + "/" + parameter_name)
00199         else:
00200             param = parameter_name
00201 
00202         # check trajectory parameters
00203         if not type(param) is list: # check outer list
00204                 rospy.logerr("no valid parameter for %s: not a list, aborting...",group)
00205                 print "parameter is:",param
00206                 return None
00207 
00208 
00209         #no need for trajectories anymore, since planning (will) guarantee collision-free motion!
00210         point = param[len(param)-1]
00211 
00212         #print point,"type1 = ", type(point)
00213         if type(point) is str:
00214             if not rospy.has_param(ns_global_prefix + "/" + group + "/" + point):
00215                 rospy.logerr("parameter %s does not exist on ROS Parameter Server, aborting...",ns_global_prefix + "/" + group + "/" + point)
00216                 return None
00217             point = rospy.get_param(ns_global_prefix + "/" + group + "/" + point)
00218             point = point[0] # \todo TODO: hack because only first point is used, no support for trajectories inside trajectories
00219             #print point
00220         elif type(point) is list:
00221             rospy.logdebug("point is a list")
00222         else:
00223             rospy.logerr("no valid parameter for %s: not a list of lists or strings, aborting...",group)
00224             print "parameter is:",param
00225             return None
00226 
00227         # here: point should be list of floats/ints
00228         #print point
00229         if not len(point) == len(joint_names): # check dimension
00230             rospy.logerr("no valid parameter for %s: dimension should be %d and is %d, aborting...",group,len(joint_names),len(point))
00231             print "parameter is:",param
00232             return None
00233 
00234         for value in point:
00235             #print value,"type2 = ", type(value)
00236             if not ((type(value) is float) or (type(value) is int)): # check type
00237                 #print type(value)
00238                 rospy.logerr("no valid parameter for %s: not a list of float or int, aborting...",group)
00239                 print "parameter is:",param
00240                 return None
00241             rospy.logdebug("accepted value %f for %s",value,group)
00242         
00243         return point
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 


cob_moveit_interface
Author(s): Felix Messmer
autogenerated on Wed Aug 26 2015 11:01:06