WUT Velma robot API
int_markers_cimp.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
6 
7 import roslib; roslib.load_manifest('velma_common')
8 
9 import rospy
10 import math
11 
12 from std_msgs.msg import ColorRGBA
15 from visualization_msgs.msg import *
16 from geometry_msgs.msg import *
17 from tf.transformations import *
18 import tf_conversions.posemath as pm
19 import PyKDL
20 import actionlib
21 
22 from velma_common import *
23 from rcprg_ros_utils import exitError
24 
25 def makeWrench(lx,ly,lz,rx,ry,rz):
26  return PyKDL.Wrench(PyKDL.Vector(lx,ly,lz), PyKDL.Vector(rx,ry,rz))
27 
29  def __init__(self, prefix, velma_interface):
30  self.prefix = prefix
31  self.velma = velma_interface
32 
33  # create an interactive marker server on the topic namespace simple_marker
34  self.server = InteractiveMarkerServer('int_markers_cimp_' + self.prefix)
35 
37 
38  self.marker_state = "global"
39  self.int_hide_marker = InteractiveMarker()
40  self.int_hide_marker.header.frame_id = 'torso_base'
41  self.int_hide_marker.name = self.prefix+'_arm_hide_marker';
42  self.int_hide_marker.scale = 0.2
43  self.int_hide_marker.pose.orientation = Quaternion(0,0,0,1)
44  if self.prefix == "right":
45  self.int_hide_marker.pose.position = Point(0, -0.5, 0)
46  self.hide = self.createButtoMarkerControl(Point(0.15,0.15,0.15), Point(0.0, 0.0, 0.0), ColorRGBA(0,1,0,1) )
47  else:
48  self.int_hide_marker.pose.position = Point(0, 0.5, 0)
49  self.hide = self.createButtoMarkerControl(Point(0.15,0.15,0.15), Point(0.0, 0.0, 0.0), ColorRGBA(1,0,0,1) )
50  self.hide.interaction_mode = InteractiveMarkerControl.BUTTON
51  self.hide.name = 'button_hide'
52  self.int_hide_marker.controls.append( self.hide )
53  self.server.insert(self.int_hide_marker, self.processFeedbackHide);
54 
55  self.server.applyChanges();
56 
57  def erase6DofMarker(self):
58  self.server.erase(self.prefix+'_arm_position_marker')
59  self.server.applyChanges();
60 
62  T_B_T = self.velma.getTf("B", "T" + self.prefix)
63  int_position_marker = InteractiveMarker()
64  int_position_marker.header.frame_id = 'torso_base'
65  int_position_marker.name = self.prefix+'_arm_position_marker'
66  int_position_marker.scale = 0.2
67  int_position_marker.pose = pm.toMsg(T_B_T)
68 
69  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'x'));
70  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'y'));
71  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'z'));
72  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'x'));
73  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'y'));
74  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'z'));
75 
76  box = self.createAxisMarkerControl(Point(0.15,0.015,0.015), Point(0.0, 0.0, 0.0) )
77  box.interaction_mode = InteractiveMarkerControl.BUTTON
78  box.name = 'button'
79  int_position_marker.controls.append( box )
80  self.server.insert(int_position_marker, self.processFeedback);
81  self.server.applyChanges();
82 
83  def globalState(self):
84  if self.marker_state == "global" or self.marker_state=="local":
85  self.erase6DofMarker()
86 
87  self.marker_state = "global"
89 
90  def hiddenState(self):
91  if self.marker_state == "global" or self.marker_state=="local":
92  self.erase6DofMarker()
93 
94  self.marker_state="hidden"
95 
96  def processFeedbackHide(self, feedback):
97  if ( feedback.event_type == InteractiveMarkerFeedback.BUTTON_CLICK and feedback.control_name == "button_hide" ):
98  if self.marker_state == "global":
99  self.hiddenState()
100  else:
101  self.globalState();
102 
103  def processFeedback(self, feedback):
104  #print "feedback", feedback.marker_name, feedback.control_name, feedback.event_type
105 
106  if ( feedback.marker_name == self.prefix+'_arm_position_marker' ) and ( feedback.event_type == InteractiveMarkerFeedback.BUTTON_CLICK ) and ( feedback.control_name == "button" ):
107  T_B_Td = pm.fromMsg(feedback.pose)
108  self.p = pm.toMsg(T_B_Td)
109  #self.velma.moveCartImp(self.prefix, [T_B_Td], [5.0], None, None, None, None, PyKDL.Wrench(PyKDL.Vector(5,5,5), PyKDL.Vector(5,5,5)), start_time=0.5)
110  dmp = 0.35
111  imp = makeWrench(1500,1500,1500,150,150,150)
112  #print 'damping is {}'.format(dmp)
113  self.velma.moveCartImp(self.prefix, [T_B_Td], [5.0], None, None, [imp], [5.0], PyKDL.Wrench(PyKDL.Vector(5,5,5), PyKDL.Vector(5,5,5)), start_time=0.5, damping=PyKDL.Wrench(PyKDL.Vector(dmp, dmp, dmp),PyKDL.Vector(dmp, dmp, dmp)))
114 
115  def createSphereMarkerControl(self, scale, position, color):
116  marker = Marker()
117  marker.type = Marker.SPHERE
118  marker.scale = scale
119  marker.pose.position = position
120  marker.color = color
121  control = InteractiveMarkerControl()
122  control.always_visible = True;
123  control.markers.append( marker );
124  return control
125 
126  def createBoxMarkerControl(self, scale, position):
127  marker = Marker()
128  marker.type = Marker.CUBE
129  marker.scale = scale
130  marker.pose.position = position
131  marker.color = ColorRGBA(0.5,0.5,0.5,1)
132  control = InteractiveMarkerControl()
133  control.always_visible = True;
134  control.markers.append( marker );
135  return control
136 
137  def createAxisMarkerControl(self, scale, position):
138  markerX = Marker()
139  markerX.type = Marker.ARROW
140  markerX.scale = scale
141  markerX.pose.position = position
142  ori = quaternion_about_axis(0, [0, 1 ,0])
143  markerX.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
144  markerX.color = ColorRGBA(1,0,0,1)
145  markerY = Marker()
146  markerY.type = Marker.ARROW
147  markerY.scale = scale
148  markerY.pose.position = position
149  ori = quaternion_about_axis(math.pi/2.0, [0, 0 ,1])
150  markerY.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
151  markerY.color = ColorRGBA(0,1,0,1)
152  markerZ = Marker()
153  markerZ.type = Marker.ARROW
154  markerZ.scale = scale
155  markerZ.pose.position = position
156  ori = quaternion_about_axis(-math.pi/2.0, [0, 1 ,0])
157  markerZ.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
158  markerZ.color = ColorRGBA(0,0,1,1)
159  control = InteractiveMarkerControl()
160  control.always_visible = True;
161  control.markers.append( markerX );
162  control.markers.append( markerY );
163  control.markers.append( markerZ );
164  return control
165 
166  def createButtoMarkerControl(self, scale, position, color):
167  marker = Marker()
168  marker.type = Marker.SPHERE
169  marker.scale = scale
170  marker.pose.position = position
171  marker.pose.orientation = Quaternion(0,0,0,1)
172  marker.color = color
173  control = InteractiveMarkerControl()
174  control.always_visible = True;
175  control.markers.append( marker );
176  return control
177 
178  def createInteractiveMarkerControl6DOF(self, mode, axis):
179  control = InteractiveMarkerControl()
180  control.orientation_mode = InteractiveMarkerControl.FIXED
181  if mode == InteractiveMarkerControl.ROTATE_AXIS:
182  control.name = 'rotate_' + axis;
183  elif mode == InteractiveMarkerControl.MOVE_AXIS:
184  control.name = 'move_' + axis;
185  else:
186  raise Exception('Wrong axis mode: "{}"'.format(mode))
187  if axis == 'x':
188  control.orientation = Quaternion(1,0,0,1)
189  elif axis == 'y':
190  control.orientation = Quaternion(0,1,0,1)
191  elif axis == 'z':
192  control.orientation = Quaternion(0,0,1,1)
193  else:
194  raise Exception('Wrong axis name: "{}"'.format(axis))
195  control.interaction_mode = mode
196  return control
197 
198 if __name__ == "__main__":
199 
200  rospy.init_node('int_markers_cimp', anonymous=False)
201 
202  rospy.sleep(0.5)
203 
204  velma = VelmaInterface()
205  print "waiting for init..."
206 
207  velma.waitForInit()
208  print "init ok"
209 
210  print "Switch to cart_imp mode (no trajectory)..."
211  if not velma.moveCartImpRightCurrentPos(start_time=0.2):
212  exitError(1)
213  if velma.waitForEffectorRight() != 0:
214  exitError(2)
215 
216  if not velma.moveCartImpLeftCurrentPos(start_time=0.2):
217  exitError(3)
218  if velma.waitForEffectorLeft() != 0:
219  exitError(4)
220 
221  print "Switched to cart_imp mode."
222 
223  int_right = IntMarkersCimp("right", velma)
224  int_left = IntMarkersCimp("left", velma)
225 
226  rospy.spin()
227 
def makeWrench(lx, ly, lz, rx, ry, rz)
def createSphereMarkerControl(self, scale, position, color)
def createButtoMarkerControl(self, scale, position, color)
def createBoxMarkerControl(self, scale, position)
def createAxisMarkerControl(self, scale, position)
def createInteractiveMarkerControl6DOF(self, mode, axis)
def __init__(self, prefix, velma_interface)