00001
00002
00003 import roslib
00004 import rospy
00005 from geometry_msgs.msg import PoseStamped
00006 from moveit_msgs.srv import *
00007 from moveit_msgs.msg import RobotState
00008 from std_msgs.msg import Float64MultiArray
00009 from std_msgs.msg import String
00010 from std_msgs.msg import Float32
00011 from std_msgs.msg import MultiArrayDimension
00012 from sensor_msgs.msg import JointState
00013 from visualization_msgs.msg import (
00014 Marker,
00015 InteractiveMarker,
00016 InteractiveMarkerControl,
00017 InteractiveMarkerFeedback)
00018 from interactive_markers.interactive_marker_server import InteractiveMarkerServer
00019 server = None
00020
00021
00022 def makeInteractiveMarker(name, description):
00023 global fixed_frame
00024 interactive_marker = InteractiveMarker()
00025 interactive_marker.header.frame_id = fixed_frame
00026 interactive_marker.name = name
00027 interactive_marker.description = description
00028 return interactive_marker
00029
00030
00031 def makeInteractiveMarkerControl(interactive_marker, mode):
00032 interactive_marker_control = InteractiveMarkerControl()
00033 interactive_marker_control.always_visible = True
00034 interactive_marker_control.interaction_mode = mode
00035 interactive_marker.controls.append(interactive_marker_control)
00036 return interactive_marker_control
00037
00038 def setOrientation(w, x, y, z, marker):
00039 marker.orientation.w = w
00040 marker.orientation.x = x
00041 marker.orientation.y = y
00042 marker.orientation.z = z
00043
00044 def setColor(red, green, blue, alpha, marker):
00045 marker.color.r = red
00046 marker.color.g = green
00047 marker.color.b = blue
00048 marker.color.a = alpha
00049
00050
00051 def callback(req):
00052 print req
00053 return GetPositionIKResponse
00054
00055 def initial_callback(msg):
00056 global joint_states,joint_names, initial_joint_position, group_name
00057 group_name = msg.data
00058 joint_names = rospy.get_param("/link_group/" + group_name)
00059 initial_joint_position = [0] * len(joint_names)
00060
00061 def im_size_callback(msg):
00062 global interactive_marker
00063 interactive_marker.scale = msg.data
00064 server.clear()
00065 server.insert(interactive_marker, feedback)
00066 server.applyChanges()
00067
00068
00069 def joint_state_callback(msg):
00070 global joint_states
00071 joint_states = msg
00072
00073 def feedback(feedback):
00074 global pub, initial_joint_position, fixed_frame, joint_names, group_name
00075
00076 server.setPose(feedback.marker_name, feedback.pose)
00077 server.applyChanges()
00078
00079 if feedback.event_type == 0:
00080 return
00081
00082 rospy.wait_for_service('compute_ik')
00083 try:
00084 service = rospy.ServiceProxy('compute_ik', GetPositionIK)
00085 request = GetPositionIKRequest()
00086 request.ik_request.group_name = group_name
00087 request.ik_request.timeout = rospy.Duration.from_sec(0.0001)
00088
00089
00090 robot_state = RobotState()
00091 robot_state.joint_state.header.frame_id = fixed_frame
00092 robot_state.joint_state.name = joint_names
00093 robot_state.joint_state.position = initial_joint_position
00094 robot_state.joint_state.velocity = []
00095 request.ik_request.robot_state = robot_state
00096
00097
00098 pose_stamped = PoseStamped()
00099 pose_stamped.header.frame_id = fixed_frame
00100 pose_stamped.pose.position.x = feedback.pose.position.x
00101 pose_stamped.pose.position.y = feedback.pose.position.y
00102 pose_stamped.pose.position.z = feedback.pose.position.z
00103 pose_stamped.pose.orientation.x = feedback.pose.orientation.x
00104 pose_stamped.pose.orientation.y = feedback.pose.orientation.y
00105 pose_stamped.pose.orientation.z = feedback.pose.orientation.z
00106 pose_stamped.pose.orientation.w = feedback.pose.orientation.w
00107
00108 request.ik_request.pose_stamped = pose_stamped
00109 response = service(request)
00110 print response
00111 if len(response.solution.joint_state.position) != 0:
00112 print "success"
00113 msg = Float64MultiArray()
00114 for i,joint_name in enumerate(response.solution.joint_state.name):
00115 for j, name in enumerate(joint_names):
00116 if joint_name == name:
00117 initial_joint_position[j] = response.solution.joint_state.position[i]
00118 dim = MultiArrayDimension()
00119 dim.label = name
00120 msg.layout.dim.append(dim)
00121 msg.data.append(response.solution.joint_state.position[i])
00122 pub.publish(msg)
00123
00124 except rospy.ServiceException, e:
00125 print "Service call failed: %s"%e
00126
00127 if __name__ == '__main__':
00128 rospy.init_node("moveit_interactive_marker")
00129 global pub, fixed_frame, interactive_marker
00130 fixed_frame = rospy.get_param("/fixed_frame")
00131 prefix = rospy.get_param("~prefix")
00132
00133 pub = rospy.Publisher('update_' + prefix + '_joint_position',Float64MultiArray)
00134 rospy.Subscriber('/' + prefix + '/initial_marker', String, initial_callback)
00135 rospy.Subscriber('/' + prefix + '_joint_states', JointState, joint_state_callback)
00136 rospy.Subscriber('/im_size/update', Float32, im_size_callback)
00137
00138 server = InteractiveMarkerServer("/" + prefix + "/marker")
00139
00140 interactive_marker = makeInteractiveMarker(prefix, "")
00141 interactive_marker.scale = 0.3
00142 interactive_marker.pose.position.x = 0
00143 interactive_marker.pose.position.y = 0
00144 interactive_marker.pose.position.z = 1.0
00145
00146 control_slide_x = makeInteractiveMarkerControl(
00147 interactive_marker, InteractiveMarkerControl.MOVE_AXIS)
00148 control_slide_y = makeInteractiveMarkerControl(
00149 interactive_marker, InteractiveMarkerControl.MOVE_AXIS)
00150 control_slide_z = makeInteractiveMarkerControl(
00151 interactive_marker, InteractiveMarkerControl.MOVE_AXIS)
00152 control_rotate_x = makeInteractiveMarkerControl(
00153 interactive_marker, InteractiveMarkerControl.ROTATE_AXIS)
00154 control_rotate_y = makeInteractiveMarkerControl(
00155 interactive_marker, InteractiveMarkerControl.ROTATE_AXIS)
00156 control_rotate_z = makeInteractiveMarkerControl(
00157 interactive_marker, InteractiveMarkerControl.ROTATE_AXIS)
00158 control_sphere = makeInteractiveMarkerControl(
00159 interactive_marker, InteractiveMarkerControl.MOVE_ROTATE_3D)
00160 marker = Marker()
00161 marker.color.r = 0.2
00162 marker.color.g = 0.3
00163 marker.color.b = 0.7
00164 marker.color.a = 0.5
00165
00166 marker.type = Marker.SPHERE
00167 marker.scale.x = 0.2
00168 marker.scale.y = 0.2
00169 marker.scale.z = 0.2
00170 control_sphere.markers.append(marker)
00171
00172 setOrientation(1,1,0,0, control_slide_x)
00173 setOrientation(1,0,1,0, control_slide_y)
00174 setOrientation(1,0,0,1, control_slide_z)
00175 setOrientation(1,1,0,0, control_rotate_x)
00176 setOrientation(1,0,1,0, control_rotate_y)
00177 setOrientation(1,0,0,1, control_rotate_z)
00178 setOrientation(1,1,0,0, control_sphere)
00179
00180
00181 server.insert(interactive_marker, feedback)
00182 server.applyChanges()
00183 rospy.spin()