interactive_frame_target.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import sys
19 import math
20 import copy
21 from copy import deepcopy
22 
23 import rospy
24 import tf
25 from std_srvs.srv import Empty
26 from cob_srvs.srv import SetString
27 from geometry_msgs.msg import Point, PoseStamped
28 from visualization_msgs.msg import Marker, InteractiveMarker, InteractiveMarkerControl
31 
32 
34  def __init__(self):
37 
38  #get this from the frame_tracker parameters
39  if rospy.has_param('cartesian_controller/chain_tip_link'):
40  self.active_frame = rospy.get_param("cartesian_controller/chain_tip_link")
41  else:
42  rospy.logerr("No chain_tip_link specified. Aborting!")
43  sys.exit()
44  if rospy.has_param('cartesian_controller/tracking_frame'):
45  self.tracking_frame = rospy.get_param("cartesian_controller/tracking_frame")
46  else:
47  rospy.logerr("No tracking_frame specified. Aborting!")
48  sys.exit()
49  if rospy.has_param('cartesian_controller/root_frame'):
50  self.root_frame = rospy.get_param("cartesian_controller/root_frame")
51  else:
52  rospy.logerr("No root_frame specified. Setting to 'base_link'!")
53  self.root_frame = "base_link"
54 
55  if rospy.has_param('cartesian_controller/movable_trans'):
56  self.movable_trans = rospy.get_param("cartesian_controller/movable_trans")
57  else:
58  rospy.logerr("No movable_trans specified. Setting True!")
59  self.movable_trans = True
60  if rospy.has_param('cartesian_controller/movable_rot'):
61  self.movable_rot = rospy.get_param("cartesian_controller/movable_rot")
62  else:
63  rospy.logerr("No movable_rot specified. Setting True!")
64  self.movable_rot = True
65 
66  self.tracking = False
67  print("Waiting for StartTrackingServer...")
68  rospy.wait_for_service('frame_tracker/start_tracking')
69  print("...done!")
70  self.start_tracking_client = rospy.ServiceProxy('frame_tracker/start_tracking', SetString)
71 
72  print("Waiting for StopTrackingServer...")
73  rospy.wait_for_service('frame_tracker/stop_tracking')
74  print("...done!")
75  self.stop_tracking_client = rospy.ServiceProxy('frame_tracker/stop_tracking', Empty)
76 
77  self.target_pose = PoseStamped()
78  self.target_pose.header.stamp = rospy.Time.now()
79  self.target_pose.header.frame_id = self.root_frame
80  self.target_pose.pose.orientation.w = 1.0
81 
82  ##give tf_listener some time to fill cache
83  #try:
84  #rospy.sleep(1.0)
85  #except rospy.ROSInterruptException as e:
86  ##print "ROSInterruptException"
87  #pass
88 
89  transform_available = False
90  while not transform_available:
91  try:
92  (trans,rot) = self.listener.lookupTransform(self.root_frame, self.active_frame, rospy.Time(0))
94  #rospy.logwarn("Waiting for transform...")
95  try:
96  rospy.sleep(0.1)
97  except rospy.ROSInterruptException as e:
98  #print "ROSInterruptException"
99  pass
100  continue
101  transform_available = True
102 
103  self.target_pose.pose.position.x = trans[0]
104  self.target_pose.pose.position.y = trans[1]
105  self.target_pose.pose.position.z = trans[2]
106  self.target_pose.pose.orientation.x = rot[0]
107  self.target_pose.pose.orientation.y = rot[1]
108  self.target_pose.pose.orientation.z = rot[2]
109  self.target_pose.pose.orientation.w = rot[3]
110 
111  self.ia_server = InteractiveMarkerServer("marker_server")
112  self.int_marker = InteractiveMarker()
113  self.int_marker.header.frame_id = self.root_frame
114  self.int_marker.pose = self.target_pose.pose
115  self.int_marker.name = "interactive_target"
116  self.int_marker.description = self.tracking_frame
117  self.int_marker.scale = 0.8
118 
119  # create a grey box marker
120  box_marker = Marker()
121  box_marker.type = Marker.CUBE
122  box_marker.scale.x = 0.1
123  box_marker.scale.y = 0.1
124  box_marker.scale.z = 0.1
125  box_marker.color.r = 0.0
126  box_marker.color.g = 0.5
127  box_marker.color.b = 0.5
128  box_marker.color.a = 1.0
129  control_3d = InteractiveMarkerControl()
130  control_3d.always_visible = True
131  control_3d.name = "move_rotate_3D"
132  control_3d.interaction_mode = InteractiveMarkerControl.MOVE_ROTATE_3D
133  control_3d.markers.append( box_marker )
134  self.int_marker.controls.append(control_3d)
135 
136  control = InteractiveMarkerControl()
137  control.always_visible = True
138  control.orientation.w = 1
139  control.orientation.x = 1
140  control.orientation.y = 0
141  control.orientation.z = 0
142  if(self.movable_trans):
143  control.name = "move_x"
144  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
145  self.int_marker.controls.append(deepcopy(control))
146  control.name = "move_y"
147  control.orientation.x = 0
148  control.orientation.y = 1
149  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
150  self.int_marker.controls.append(deepcopy(control))
151  control.name = "move_z"
152  control.orientation.y = 0
153  control.orientation.z = 1
154  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
155  self.int_marker.controls.append(deepcopy(control))
156  if(self.movable_rot):
157  control.orientation.w = 1
158  control.orientation.x = 1
159  control.orientation.y = 0
160  control.orientation.z = 0
161  control.name = "rotate_x"
162  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
163  self.int_marker.controls.append(deepcopy(control))
164  control.name = "rotate_y"
165  control.orientation.x = 0
166  control.orientation.y = 1
167  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
168  self.int_marker.controls.append(deepcopy(control))
169  control.name = "rotate_z"
170  control.orientation.y = 0
171  control.orientation.z = 1
172  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
173  self.int_marker.controls.append(deepcopy(control))
174 
175  self.ia_server.insert(self.int_marker, self.marker_fb)
176 
177  #create menu
179  self.menu_handler.insert( "StartTracking", callback=self.start_tracking )
180  self.menu_handler.insert( "StopTracking", callback=self.stop_tracking )
181  self.menu_handler.insert( "ResetTracking", callback=self.reset_tracking )
182  self.int_marker_menu = InteractiveMarker()
183  self.int_marker_menu.header.frame_id = self.root_frame
184  self.int_marker_menu.name = "marker_menu"
185  self.int_marker_menu.description = rospy.get_namespace()
186  self.int_marker_menu.scale = 1.0
187  self.int_marker_menu.pose.position.z = 1.2
188  control = InteractiveMarkerControl()
189  control.interaction_mode = InteractiveMarkerControl.MENU
190  control.name = "menu_control"
191  control.description= "InteractiveTargetMenu"
192  self.int_marker_menu.controls.append(copy.deepcopy(control))
193  self.ia_server.insert(self.int_marker_menu, self.menu_fb)
194  self.menu_handler.apply( self.ia_server, self.int_marker_menu.name )
195  self.ia_server.applyChanges()
196 
197  def start_tracking(self, fb):
198  #print "start_tracking pressed"
199  try:
200  res = self.start_tracking_client(data=self.tracking_frame)
201  print(res)
202  self.tracking = True
203  except rospy.ServiceException as e:
204  print("Service call failed: %s"%e)
205  self.tracking = False
206 
207  def stop_tracking(self, fb):
208  #print "stop_tracking pressed"
209  try:
210  res = self.stop_tracking_client()
211  print(res)
212  self.tracking = False
213  except rospy.ServiceException as e:
214  print("Service call failed: %s"%e)
215  self.tracking = False
216 
217  def reset_tracking(self, fb):
218  #print "reset_tracking pressed"
219  self.stop_tracking(fb)
220  self.update_marker()
221 
222  def update_marker(self):
223  transform_available = False
224  while not transform_available:
225  try:
226  (trans,rot) = self.listener.lookupTransform(self.root_frame, self.active_frame, rospy.Time(0))
228  rospy.logwarn("Waiting for transform...")
229  try:
230  rospy.sleep(0.1)
231  except rospy.ROSInterruptException as e:
232  #print "ROSInterruptException"
233  pass
234  continue
235  transform_available = True
236 
237  reset_pose = PoseStamped()
238  reset_pose.header.frame_id = self.root_frame
239  reset_pose.header.stamp = rospy.Time.now()
240  reset_pose.pose.position.x = trans[0]
241  reset_pose.pose.position.y = trans[1]
242  reset_pose.pose.position.z = trans[2]
243  reset_pose.pose.orientation.x = rot[0]
244  reset_pose.pose.orientation.y = rot[1]
245  reset_pose.pose.orientation.z = rot[2]
246  reset_pose.pose.orientation.w = rot[3]
247 
248  self.target_pose = reset_pose
249  self.ia_server.setPose(self.int_marker.name, reset_pose.pose);
250  self.ia_server.applyChanges()
251 
252  def menu_fb(self, fb):
253  pass
254 
255  def marker_fb(self, fb):
256  #p = feedback.pose.position
257  #print feedback.marker_name + " is now at " + str(p.x) + ", " + str(p.y) + ", " + str(p.z)
258  self.target_pose.header = fb.header
259  self.target_pose.pose = fb.pose
260  self.ia_server.applyChanges()
261 
262  def run(self):
263  if not self.tracking:
264  self.update_marker()
265 
266  self.br.sendTransform(
267  (self.target_pose.pose.position.x, self.target_pose.pose.position.y, self.target_pose.pose.position.z),
268  (self.target_pose.pose.orientation.x, self.target_pose.pose.orientation.y, self.target_pose.pose.orientation.z, self.target_pose.pose.orientation.w),
269  rospy.Time.now(), self.tracking_frame, self.target_pose.header.frame_id)
270 
271 
272 if __name__ == "__main__":
273  rospy.init_node("interactive_frame_target")
274 
276 
277  r = rospy.Rate(50)
278  while not rospy.is_shutdown():
279  ilt.run()
280  try:
281  r.sleep()
282  except rospy.ROSInterruptException as e:
283  #print "ROSInterruptException"
284  pass
ia_server
give tf_listener some time to fill cache try: rospy.sleep(1.0) except rospy.ROSInterruptException as ...


cob_frame_tracker
Author(s): Felix Messmer
autogenerated on Thu Apr 8 2021 02:39:38