WUT Velma robot API
pose_int_marker.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_ros_utils')
39 
40 import sys
41 import rospy
42 import math
43 import copy
44 
45 import tf
46 import tf2_ros
47 from std_msgs.msg import ColorRGBA
50 from visualization_msgs.msg import *
51 from geometry_msgs.msg import *
52 from tf.transformations import *
53 import tf_conversions.posemath as pm
54 import PyKDL
55 
57  def __init__(self, frame_id):
58  # create an interactive marker server on the topic namespace simple_marker
59  self.br = tf2_ros.TransformBroadcaster()
60  self.frame_id = frame_id
61  self.tf = Transform()
62  self.tf.translation.x = 1.0
63  self.tf.translation.y = 0.0
64  self.tf.translation.z = 1.5
65  self.tf.rotation.x = 0
66  self.tf.rotation.y = 0
67  self.tf.rotation.z = 0
68  self.tf.rotation.w = 1
69 
70  self.server = InteractiveMarkerServer('pose_int_marker_' + self.frame_id)
72  self.server.applyChanges()
73 
74  def spin(self):
75  while not rospy.is_shutdown():
76  t = TransformStamped()
77  t.header.stamp = rospy.Time.now()
78  t.header.frame_id = "world"
79  t.child_frame_id = self.frame_id
80  t.transform = self.tf
81  self.br.sendTransform(t)
82  try:
83  rospy.sleep(0.1)
84  except:
85  break
86 
88  int_position_marker = InteractiveMarker()
89  int_position_marker.header.frame_id = 'world'
90  int_position_marker.name = 'pose_int_marker_' + self.frame_id
91  int_position_marker.scale = 0.2
92  int_position_marker.pose.position.x = self.tf.translation.x
93  int_position_marker.pose.position.y = self.tf.translation.y
94  int_position_marker.pose.position.z = self.tf.translation.z
95  int_position_marker.pose.orientation = self.tf.rotation
96 
97  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'x'));
98  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'y'));
99  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.ROTATE_AXIS,'z'));
100  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'x'));
101  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'y'));
102  int_position_marker.controls.append(self.createInteractiveMarkerControl6DOF(InteractiveMarkerControl.MOVE_AXIS,'z'));
103 
104  box = self.createAxisMarkerControl(Point(0.15,0.015,0.015), Point(0.0, 0.0, 0.0) )
105  box.interaction_mode = InteractiveMarkerControl.BUTTON
106  box.name = 'button'
107  int_position_marker.controls.append( box )
108  self.server.insert(int_position_marker, self.processFeedback);
109  self.server.applyChanges();
110 
111  def processFeedback(self, feedback):
112  if ( feedback.marker_name == 'pose_int_marker_' + self.frame_id ):
113  self.tf.translation.x = feedback.pose.position.x
114  self.tf.translation.y = feedback.pose.position.y
115  self.tf.translation.z = feedback.pose.position.z
116  self.tf.rotation = feedback.pose.orientation
117  print "PyKDL.Frame(PyKDL.Rotation.Quaternion(", self.tf.rotation.x, ",", self.tf.rotation.y, ",", self.tf.rotation.z, ",", self.tf.rotation.w, "), PyKDL.Vector(", self.tf.translation.x, ",", self.tf.translation.y, ",", self.tf.translation.z, "))"
118 
119  def createAxisMarkerControl(self, scale, position):
120  markerX = Marker()
121  markerX.type = Marker.ARROW
122  markerX.scale = scale
123  markerX.pose.position = position
124  ori = quaternion_about_axis(0, [0, 1 ,0])
125  markerX.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
126  markerX.color = ColorRGBA(1,0,0,1)
127  markerY = Marker()
128  markerY.type = Marker.ARROW
129  markerY.scale = scale
130  markerY.pose.position = position
131  ori = quaternion_about_axis(math.pi/2.0, [0, 0 ,1])
132  markerY.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
133  markerY.color = ColorRGBA(0,1,0,1)
134  markerZ = Marker()
135  markerZ.type = Marker.ARROW
136  markerZ.scale = scale
137  markerZ.pose.position = position
138  ori = quaternion_about_axis(-math.pi/2.0, [0, 1 ,0])
139  markerZ.pose.orientation = Quaternion(ori[0], ori[1], ori[2], ori[3])
140  markerZ.color = ColorRGBA(0,0,1,1)
141  control = InteractiveMarkerControl()
142  control.always_visible = True;
143  control.markers.append( markerX );
144  control.markers.append( markerY );
145  control.markers.append( markerZ );
146  return control
147 
148  def createButtoMarkerControl(self, scale, position, color):
149  marker = Marker()
150  marker.type = Marker.SPHERE
151  marker.scale = scale
152  marker.pose.position = position
153  marker.pose.orientation = Quaternion(0,0,0,1)
154  marker.color = color
155  control = InteractiveMarkerControl()
156  control.always_visible = True;
157  control.markers.append( marker );
158  return control
159 
160  def createInteractiveMarkerControl6DOF(self, mode, axis):
161  control = InteractiveMarkerControl()
162  control.orientation_mode = InteractiveMarkerControl.FIXED
163  if mode == InteractiveMarkerControl.ROTATE_AXIS:
164  control.name = 'rotate_';
165  if mode == InteractiveMarkerControl.MOVE_AXIS:
166  control.name = 'move_';
167  if axis == 'x':
168  control.orientation = Quaternion(1,0,0,1)
169  control.name = control.name+'x';
170  if axis == 'y':
171  control.orientation = Quaternion(0,1,0,1)
172  control.name = control.name+'x';
173  if axis == 'z':
174  control.orientation = Quaternion(0,0,1,1)
175  control.name = control.name+'x';
176  control.interaction_mode = mode
177  return control
178 
179 if __name__ == "__main__":
180 
181  rospy.init_node('pose_int_marker', anonymous=True)
182 
183  rospy.sleep(0.5)
184 
185  try:
186  frame_id = rospy.get_param("~frame_id")
187  except KeyError as e:
188  print "Some ROS parameters are not provided:"
189  print e
190  exit(1)
191 
192  int_marker = PoseIntMarker(frame_id)
193 
194  int_marker.spin()
195 
def createInteractiveMarkerControl6DOF(self, mode, axis)
def createAxisMarkerControl(self, scale, position)
def createButtoMarkerControl(self, scale, position, color)