interface_services.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import copy
4 import time
5 
6 import rospy
7 import os
8 
9 from frame_editor.objects import *
10 from frame_editor.commands import *
11 from frame_editor.interface import Interface
12 
15 
16 from frame_editor.srv import *
17 
18 
20 
21  def __init__(self, frame_editor):
22 
23  self.editor = frame_editor
24 
25  rospy.Service("~align_frame", AlignFrame, self.callback_align_frame)
26  rospy.Service("~edit_frame", EditFrame, self.callback_edit_frame)
27  rospy.Service("~get_frame", GetFrame, self.callback_get_frame)
28  rospy.Service("~get_frame_names", GetFrameNames, self.callback_get_frame_names)
29  rospy.Service("~remove_frame", RemoveFrame, self.callback_remove_frame)
30  rospy.Service("~set_frame", SetFrame, self.callback_set_frame)
31  rospy.Service("~set_parent", SetParentFrame, self.callback_set_parent_frame)
32  rospy.Service("~copy_frame", CopyFrame, self.callback_copy_frame)
33 
34  rospy.Service("~load_yaml", LoadYaml, self.callback_load_yaml)
35  rospy.Service("~save_yaml", SaveYaml, self.callback_save_yaml)
36 
37 
38  def callback_align_frame(self, request):
39  rospy.loginfo("> Request to align frame {} with frame {} mode {}".format(request.name, request.source_name, request.mode))
40 
41  response = AlignFrameResponse()
42  response.error_code = 0
43 
44  if request.name == "":
45  rospy.logerr(" Error: No name given")
46  response.error_code = 1
47 
48  elif request.source_name == "":
49  rospy.logerr(" Error: No source name given")
50  response.error_code = 3
51 
52  elif request.name not in self.editor.frames:
53  rospy.logerr(" Error: Frame not found: {}".format(request.name))
54  response.error_code = 2
55 
56  else:
57  frame = self.editor.frames[request.name]
58 
59  m = request.mode
60  mode = []
61  if m & 1: mode.append("x")
62  if m & 2: mode.append("y")
63  if m & 4: mode.append("z")
64  if m & 8: mode.append("a")
65  if m & 16: mode.append("b")
66  if m & 32: mode.append("c")
67 
68  self.editor.command(Command_AlignElement(self.editor, frame, request.source_name, mode))
69 
70  return response
71 
72 
73  def callback_edit_frame(self, request):
74  rospy.loginfo("> Request to edit frame {}".format(request.name))
75 
76  response = EditFrameResponse()
77  response.error_code = 0
78 
79  if request.name == "":
80 
81  self.editor.command(Command_SelectElement(self.editor, None))
82 
83  elif request.name not in self.editor.frames:
84  rospy.logerr(" Error: Frame not found: {}".format(request.name))
85  response.error_code = 2
86 
87  else:
88 
89  self.editor.command(Command_SelectElement(self.editor, self.editor.frames[request.name]))
90 
91  return response
92 
93 
94  def callback_get_frame(self, request):
95  rospy.loginfo("> Request to get frame {}".format(request.name))
96 
97  response = GetFrameResponse()
98  response.error_code = 0
99 
100  if request.name == "":
101  rospy.logerr(" Error: No name given")
102  response.error_code = 1
103 
104  elif request.name not in self.editor.frames:
105  rospy.logerr(" Error: Frame not found: {}".format(request.name))
106  response.error_code = 2
107 
108  else:
109  f = self.editor.frames[request.name]
110  f.print_all()
111  response.name = f.name
112  response.parent = f.parent
113  response.pose = ToPose(f.position, f.orientation)
114 
115  return response
116 
117 
118  def callback_get_frame_names(self, request):
119  rospy.loginfo("> Request to get frame names")
120 
121  response = GetFrameNamesResponse()
122  response.error_code = 0
123 
124  for frame in self.editor.frames.values():
125  response.names.append(frame.name)
126 
127  return response
128 
129 
130  def callback_remove_frame(self, request):
131  rospy.loginfo("> Request to remove frame {}".format(request.name))
132 
133  response = RemoveFrameResponse()
134  response.error_code = 0
135 
136  if request.name == "":
137  rospy.logerr(" Error: No name given")
138  response.error_code = 1
139 
140  elif request.name not in self.editor.frames:
141  rospy.logerr(" Error: Frame not found: {}".format(request.name))
142  response.error_code = 2
143 
144  else:
145  self.editor.command(Command_RemoveElement(self.editor, self.editor.frames[request.name]))
146 
147  return response
148 
149 
150  def callback_set_frame(self, request):
151  rospy.loginfo("> Request to set (or add) frame {} {}".format(request.name, request.parent))
152 
153  response = SetFrameResponse()
154 
155  if request.name == "":
156  rospy.logerr(" Error: No name given")
157  response.error_code = 1
158  return response
159 
160  if request.parent == "":
161  if request.name in self.editor.frames:
162  request.parent = self.editor.frames[request.name].parent
163  else:
164  rospy.logerr("Error: No parent given and frame previously not existing")
165  response.error_code = 2
166  return response
167 
168  f = Frame(request.name,
169  FromPoint(request.pose.position),
170  FromQuaternion(request.pose.orientation),
171  request.parent)
172  self.editor.command(Command_AddElement(self.editor, f))
173 
174  response.error_code = 0
175  return response
176 
177 
178  def callback_set_parent_frame(self, request):
179  rospy.loginfo("> Request to set parent_frame {} {}".format(request.name, request.parent))
180 
181  response = SetParentFrameResponse()
182  response.error_code = 0
183 
184  if request.name == "":
185  rospy.logerr(" Error: No frame_name given")
186  response.error_code = 1
187 
188  elif request.parent == "":
189  rospy.logerr(" Error: No parent_name given")
190  response.error_code = 2
191 
192  else:
193  f = self.editor.frames[request.name]
194  self.editor.command(Command_SetParent(self.editor, f, request.parent, request.keep_absolute))
195 
196  return response
197 
198  def callback_load_yaml(self, request):
199  rospy.loginfo("> Request to load yaml file:'{}'".format(request.filename))
200 
201  response = LoadYamlResponse()
202  try:
203  self.editor.load_file(os.path.expanduser(request.filename))
204  response.success = True
205  response.message = "file loaded"
206  except Exception as e:
207  response.success = False
208  response.message = "Exception: {}".format(str(e))
209 
210  return response
211 
212  def callback_save_yaml(self, request):
213  rospy.loginfo("> Request to save yaml file to:'{}'".format(request.filename))
214 
215  response = SaveYamlResponse()
216  try:
217  self.editor.save_file(os.path.expanduser(request.filename))
218  response.success = True
219  response.message = "file saved"
220  except Exception as e:
221  response.success = False
222  response.message = "Exception: {}".format(str(e))
223 
224  return response
225 
226  def callback_copy_frame(self, request):
227  rospy.loginfo("> Request to copy frame '{}' with new name '{}' and new parent name '{}'".format(request.source_name, request.name, request.parent))
228 
229  response = CopyFrameResponse()
230  response.error_code = 0
231 
232  if request.name == "":
233  rospy.logerr(" Error: No name given")
234  response.error_code = 1
235 
236  elif request.source_name == "":
237  rospy.logerr(" Error: No source name given")
238  response.error_code = 3
239 
240  else:
241  t = time.time()
242 
243  try:
244  # If not existing yet: create frame
245  if request.name not in self.editor.frames:
246  rospy.loginfo(">> add")
247 
248  # No parent specified: use source's parent
249  if request.parent == "":
250  if request.source_name in self.editor.frames:
251  request.parent = self.editor.frames[request.source_name].parent
252  else:
253  rospy.logerr(" Error: No parent name given")
254  response.error_code = 3
255  return response
256 
257  Frame.wait_for_transform(request.source_name, request.parent, rospy.Duration(1.0))
258  self.editor.command(Command_CopyElement(self.editor, request.name, request.source_name, request.parent))
259  Frame.wait_for_transform(request.parent, request.name, rospy.Duration(1.0))
260 
261 
262  else:
263  frame = self.editor.frames[request.name]
264 
265  Frame.wait_for_transform(request.source_name, request.parent, rospy.Duration(1.0))
266  if (request.parent != "") and (frame.parent != request.parent):
267  rospy.loginfo(">> rebase")
268  self.editor.command(Command_RebaseElement(self.editor, frame, request.source_name, request.parent))
269  else:
270  rospy.loginfo(">> align")
271  self.editor.command(Command_AlignElement(self.editor, frame, request.source_name, ['x', 'y', 'z', 'a', 'b', 'c']))
272  Frame.wait_for_transform(frame.parent, frame.name, rospy.Duration(1.0))
273 
274  except Exception as e:
275  rospy.logerr("Error: unhandled exception {}".format(e))
276  response.error_code = 9
277 
278  return response
279 
280 # eof
frame_editor.interface_services.FrameEditor_Services.callback_load_yaml
def callback_load_yaml(self, request)
Definition: interface_services.py:198
frame_editor.interface_services.FrameEditor_Services.callback_get_frame_names
def callback_get_frame_names(self, request)
Definition: interface_services.py:118
frame_editor.commands.Command_CopyElement
Definition: commands.py:157
frame_editor.interface_services.FrameEditor_Services
Definition: interface_services.py:19
frame_editor.interface
Definition: interface.py:1
frame_editor.constructors_geometry.FromPoint
def FromPoint(p)
Definition: constructors_geometry.py:23
frame_editor.commands.Command_SelectElement
Definition: commands.py:15
srv
frame_editor.constructors_geometry.FromQuaternion
def FromQuaternion(o)
Definition: constructors_geometry.py:37
frame_editor.interface_services.FrameEditor_Services.callback_set_parent_frame
def callback_set_parent_frame(self, request)
Definition: interface_services.py:178
frame_editor.interface_services.FrameEditor_Services.__init__
def __init__(self, frame_editor)
Definition: interface_services.py:21
frame_editor.commands.Command_AlignElement
Definition: commands.py:104
frame_editor.interface_services.FrameEditor_Services.callback_edit_frame
def callback_edit_frame(self, request)
Definition: interface_services.py:73
frame_editor.interface_services.FrameEditor_Services.editor
editor
Definition: interface_services.py:23
frame_editor.constructors_std
Definition: constructors_std.py:1
frame_editor.interface_services.FrameEditor_Services.callback_align_frame
def callback_align_frame(self, request)
Definition: interface_services.py:38
frame_editor.commands.Command_SetParent
Definition: commands.py:332
frame_editor.interface_services.FrameEditor_Services.callback_remove_frame
def callback_remove_frame(self, request)
Definition: interface_services.py:130
frame_editor.objects
Definition: objects.py:1
frame_editor.commands
Definition: commands.py:1
frame_editor.commands.Command_RebaseElement
Definition: commands.py:193
frame_editor.constructors_geometry.ToPose
def ToPose(p, o)
Pose ##.
Definition: constructors_geometry.py:9
frame_editor.objects.Frame
Definition: objects.py:24
frame_editor.commands.Command_AddElement
Definition: commands.py:33
frame_editor.interface_services.FrameEditor_Services.callback_save_yaml
def callback_save_yaml(self, request)
Definition: interface_services.py:212
frame_editor.interface_services.FrameEditor_Services.callback_copy_frame
def callback_copy_frame(self, request)
Definition: interface_services.py:226
frame_editor.constructors_geometry
Definition: constructors_geometry.py:1
frame_editor.interface.Interface
Definition: interface.py:5
frame_editor.interface_services.FrameEditor_Services.callback_get_frame
def callback_get_frame(self, request)
Definition: interface_services.py:94
frame_editor.commands.Command_RemoveElement
Definition: commands.py:52
frame_editor.interface_services.FrameEditor_Services.callback_set_frame
def callback_set_frame(self, request)
Definition: interface_services.py:150


frame_editor
Author(s): ipa-lth , ipa-frn
autogenerated on Thu May 15 2025 02:17:25