WUT Velma robot API
gazebo_move_object.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
6 
7 # Copyright (c) 2017, Robot Control and Pattern Recognition Group,
8 # Institute of Control and Computation Engineering
9 # Warsaw University of Technology
10 #
11 # All rights reserved.
12 #
13 # Redistribution and use in source and binary forms, with or without
14 # modification, are permitted provided that the following conditions are met:
15 # * Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 # * Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions and the following disclaimer in the
19 # documentation and/or other materials provided with the distribution.
20 # * Neither the name of the Warsaw University of Technology nor the
21 # names of its contributors may be used to endorse or promote products
22 # derived from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 # DISCLAIMED. IN NO EVENT SHALL <COPYright HOLDER> BE LIABLE FOR ANY
28 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #
35 # Author: Dawid Seredynski
36 #
37 
38 import roslib; roslib.load_manifest('rcprg_gazebo_utils')
39 
40 import sys
41 import rospy
42 import math
43 import copy
44 import tf
45 
46 from std_msgs.msg import ColorRGBA
49 from visualization_msgs.msg import *
50 from geometry_msgs.msg import *
51 from tf.transformations import *
52 import tf_conversions.posemath as pm
53 import PyKDL
55 import actionlib
56 from gazebo_msgs.srv import *
57 
59  def __init__(self, link_name):
60  self.link_name = link_name
61 
62  rospy.wait_for_service('/gazebo/set_link_state', 4.0)
63  self.set_link_state = rospy.ServiceProxy('/gazebo/set_link_state', SetLinkState)
64 
65  rospy.wait_for_service('/gazebo/get_link_state', 4.0)
66  get_link_state = rospy.ServiceProxy('/gazebo/get_link_state', GetLinkState)
67 
68  req = GetLinkStateRequest()
69  req.link_name = self.link_name
70  req.reference_frame = "torso_base"
71  resp = get_link_state(req)
72  if resp.success == False:
73  print "success:", resp.success, ", status:", resp.status_message
74  raise Exception("/gazebo/get_link_state")
75 
76  print "IntMarkers6D init ok"
77 
78  # create an interactive marker server on the topic namespace simple_marker
79  self.server = InteractiveMarkerServer('gazebo_move_object_' + self.link_name.replace(":", "_"))
80 
81  self.T_B_M = PyKDL.Frame(PyKDL.Vector(1,1,1))
82 
83  self.insert6DofGlobalMarker(self.T_B_M)
84 
85  self.server.applyChanges();
86 
87  def setLinkState(self, T_B_M):
88  req = SetLinkStateRequest()
89  req.link_state.link_name = self.link_name
90  req.link_state.pose.position.x = T_B_M.p.x()
91  req.link_state.pose.position.y = T_B_M.p.y()
92  req.link_state.pose.position.z = T_B_M.p.z()
93  qx, qy, qz, qw = T_B_M.M.GetQuaternion()
94  req.link_state.pose.orientation.x = qx
95  req.link_state.pose.orientation.y = qy
96  req.link_state.pose.orientation.z = qz
97  req.link_state.pose.orientation.w = qw
98  req.link_state.twist.linear.x = 0
99  req.link_state.twist.linear.y = 0
100  req.link_state.twist.linear.z = 0
101  req.link_state.twist.angular.x = 0
102  req.link_state.twist.angular.y = 0
103  req.link_state.twist.angular.z = 0
104  req.link_state.reference_frame = "torso_base"
105  resp = self.set_link_state(req)
106  if resp.success == False:
107  print "success:", resp.success, ", status:", resp.status_message
108 
109  def erase6DofMarker(self):
110  self.server.erase(self.link_name+'_position_marker')
111  self.server.applyChanges();
112 
113  def insert6DofGlobalMarker(self, T_B_M):
114  int_position_marker = InteractiveMarker()
115  int_position_marker.header.frame_id = 'torso_base'
116  int_position_marker.name = self.link_name+'_position_marker'
117  int_position_marker.scale = 0.2
118  int_position_marker.pose = pm.toMsg(T_B_M)
119 
120  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'x'));
121  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'y'));
122  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'z'));
123  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'x'));
124  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'y'));
125  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'z'));
126 
127  box = self.createAxisMarkerControl(Point(0.15,0.015,0.015), Point(0.0, 0.0, 0.0) )
128  box.interaction_mode = InteractiveMarkerControl.BUTTON
129  box.name = 'button'
130  int_position_marker.controls.append( box )
131  self.server.insert(int_position_marker, self.processFeedback);
132  self.server.applyChanges();
133 
134  def processFeedback(self, feedback):
135  if ( feedback.marker_name == self.link_name+'_position_marker' ):
136  T_B_M = pm.fromMsg(feedback.pose)
137  self.setLinkState(T_B_M)
138  print "pose:", T_B_M.p.x(), T_B_M.p.y(), T_B_M.p.z()
139 
140  def createAxisMarkerControl(self, scale, position):
141  markerX = Marker()
142  markerX.type = Marker.ARROW
143  markerX.scale = scale
144  markerX.pose.position = position
145  ori = quaternion_about_axis(0, [0, 1 ,0])
146  markerX.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
147  markerX.color = ColorRGBA(1,0,0,1)
148  markerY = Marker()
149  markerY.type = Marker.ARROW
150  markerY.scale = scale
151  markerY.pose.position = position
152  ori = quaternion_about_axis(math.pi/2.0, [0, 0 ,1])
153  markerY.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
154  markerY.color = ColorRGBA(0,1,0,1)
155  markerZ = Marker()
156  markerZ.type = Marker.ARROW
157  markerZ.scale = scale
158  markerZ.pose.position = position
159  ori = quaternion_about_axis(-math.pi/2.0, [0, 1 ,0])
160  markerZ.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
161  markerZ.color = ColorRGBA(0,0,1,1)
162  control = InteractiveMarkerControl()
163  control.always_visible = True;
164  control.markers.append( markerX );
165  control.markers.append( markerY );
166  control.markers.append( markerZ );
167  return control
168 
169  def createInteractiveMarkerControl6DOF(self, mode, axis):
170  control = InteractiveMarkerControl()
171  control.orientation_mode = InteractiveMarkerControl.FIXED
172  if mode == InteractiveMarkerControl.ROTATE_AXIS:
173  control.name = 'rotate_';
174  if mode == InteractiveMarkerControl.MOVE_AXIS:
175  control.name = 'move_';
176  if axis == 'x':
177  control.orientation = Quaternion(1,0,0,1)
178  control.name = control.name+'x';
179  if axis == 'y':
180  control.orientation = Quaternion(0,1,0,1)
181  control.name = control.name+'x';
182  if axis == 'z':
183  control.orientation = Quaternion(0,0,1,1)
184  control.name = control.name+'x';
185  control.interaction_mode = mode
186  return control
187 
189  print "usage: gazebo_move_object model_name::link_name"
190 
191 if __name__ == "__main__":
192 
193  rospy.init_node('gazebo_move_object', anonymous=True)
194 
195  if len(sys.argv) != 2:
196  printUsage()
197  exit(1)
198 
199  int_markers = IntMarkers6D(sys.argv[1])
200 
201  rospy.spin()
202 
def createAxisMarkerControl(self, scale, position)
def createInteractiveMarkerControl6DOF(self, mode, axis)