moveit2
The MoveIt Motion Planning Framework for ROS 2.
interpreter.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 # Author: Ioan Sucan
34 
35 import rospy
36 from moveit_commander import (
37  RobotCommander,
38  MoveGroupCommander,
39  PlanningSceneInterface,
40  MoveItCommanderException,
41 )
42 from geometry_msgs.msg import Pose, PoseStamped
43 import tf
44 import re
45 import time
46 import os.path
47 
48 
50  FAIL = 1
51  WARN = 2
52  SUCCESS = 3
53  INFO = 4
54  DEBUG = 5
55 
56 
58  """
59  Interpreter for simple commands
60  """
61 
62  DEFAULT_FILENAME = "move_group.cfg"
63  GO_DIRS = {
64  "up": (2, 1),
65  "down": (2, -1),
66  "z": (2, 1),
67  "left": (1, 1),
68  "right": (1, -1),
69  "y": (1, 1),
70  "forward": (0, 1),
71  "backward": (0, -1),
72  "back": (0, -1),
73  "x": (0, 1),
74  }
75 
76  def __init__(self):
77  self._gdict_gdict = {}
78  self._group_name_group_name = ""
79  self._prev_group_name_prev_group_name = ""
80  self._planning_scene_interface_planning_scene_interface = PlanningSceneInterface()
81  self._robot_robot = RobotCommander()
82  self._last_plan_last_plan = None
83  self._db_host_db_host = None
84  self._db_port_db_port = 33829
85  self._trace_trace = False
86 
87  def get_active_group(self):
88  if len(self._group_name_group_name) > 0:
89  return self._gdict_gdict[self._group_name_group_name]
90  else:
91  return None
92 
93  def get_loaded_groups(self):
94  return self._gdict_gdict.keys()
95 
96  def execute(self, cmd):
97  cmd = self.resolve_command_aliasresolve_command_alias(cmd)
98  result = self.execute_generic_commandexecute_generic_command(cmd)
99  if result != None:
100  return result
101  else:
102  if len(self._group_name_group_name) > 0:
103  return self.execute_group_commandexecute_group_command(self._gdict_gdict[self._group_name_group_name], cmd)
104  else:
105  return (
106  MoveGroupInfoLevel.INFO,
107  self.get_helpget_help()
108  + "\n\nNo groups initialized yet. You must call the 'use' or the 'load' command first.\n",
109  )
110 
111  def execute_generic_command(self, cmd):
112  if os.path.isfile("cmd/" + cmd):
113  cmd = "load cmd/" + cmd
114  cmdlow = cmd.lower()
115  if cmdlow.startswith("use"):
116  if cmdlow == "use":
117  return (MoveGroupInfoLevel.INFO, "\n".join(self.get_loaded_groupsget_loaded_groups()))
118  clist = cmd.split()
119  clist[0] = clist[0].lower()
120  if len(clist) == 2:
121  if clist[0] == "use":
122  if clist[1] == "previous":
123  clist[1] = self._prev_group_name_prev_group_name
124  if len(clist[1]) == 0:
125  return (MoveGroupInfoLevel.DEBUG, "OK")
126  if clist[1] in self._gdict_gdict:
127  self._prev_group_name_prev_group_name = self._group_name_group_name
128  self._group_name_group_name = clist[1]
129  return (MoveGroupInfoLevel.DEBUG, "OK")
130  else:
131  try:
132  mg = MoveGroupCommander(clist[1])
133  self._gdict_gdict[clist[1]] = mg
134  self._group_name_group_name = clist[1]
135  return (MoveGroupInfoLevel.DEBUG, "OK")
136  except MoveItCommanderException as e:
137  return (
138  MoveGroupInfoLevel.FAIL,
139  "Initializing " + clist[1] + ": " + str(e),
140  )
141  except:
142  return (
143  MoveGroupInfoLevel.FAIL,
144  "Unable to initialize " + clist[1],
145  )
146  elif cmdlow.startswith("trace"):
147  if cmdlow == "trace":
148  return (
149  MoveGroupInfoLevel.INFO,
150  "trace is on" if self._trace_trace else "trace is off",
151  )
152  clist = cmdlow.split()
153  if clist[0] == "trace" and clist[1] == "on":
154  self._trace_trace = True
155  return (MoveGroupInfoLevel.DEBUG, "OK")
156  if clist[0] == "trace" and clist[1] == "off":
157  self._trace_trace = False
158  return (MoveGroupInfoLevel.DEBUG, "OK")
159  elif cmdlow.startswith("load"):
160  filename = self.DEFAULT_FILENAMEDEFAULT_FILENAME
161  clist = cmd.split()
162  if len(clist) > 2:
163  return (MoveGroupInfoLevel.WARN, "Unable to parse load command")
164  if len(clist) == 2:
165  filename = clist[1]
166  if not os.path.isfile(filename):
167  filename = "cmd/" + filename
168  line_num = 0
169  line_content = ""
170  try:
171  f = open(filename, "r")
172  for line in f:
173  line_num += 1
174  line = line.rstrip()
175  line_content = line
176  if self._trace_trace:
177  print("%s:%d: %s" % (filename, line_num, line_content))
178  comment = line.find("#")
179  if comment != -1:
180  line = line[0:comment].rstrip()
181  if line != "":
182  self.executeexecute(line.rstrip())
183  line_content = ""
184  return (MoveGroupInfoLevel.DEBUG, "OK")
185  except MoveItCommanderException as e:
186  if line_num > 0:
187  return (
188  MoveGroupInfoLevel.WARN,
189  "Error at %s:%d: %s\n%s"
190  % (filename, line_num, line_content, str(e)),
191  )
192  else:
193  return (
194  MoveGroupInfoLevel.WARN,
195  "Processing " + filename + ": " + str(e),
196  )
197  except:
198  if line_num > 0:
199  return (
200  MoveGroupInfoLevel.WARN,
201  "Error at %s:%d: %s" % (filename, line_num, line_content),
202  )
203  else:
204  return (MoveGroupInfoLevel.WARN, "Unable to load " + filename)
205  elif cmdlow.startswith("save"):
206  filename = self.DEFAULT_FILENAMEDEFAULT_FILENAME
207  clist = cmd.split()
208  if len(clist) > 2:
209  return (MoveGroupInfoLevel.WARN, "Unable to parse save command")
210  if len(clist) == 2:
211  filename = clist[1]
212  try:
213  f = open(filename, "w")
214  for gr in self._gdict_gdict.keys():
215  f.write("use " + gr + "\n")
216  known = self._gdict_gdict[gr].get_remembered_joint_values()
217  for v in known.keys():
218  f.write(
219  v + " = [" + " ".join([str(x) for x in known[v]]) + "]\n"
220  )
221  if self._db_host_db_host != None:
222  f.write(
223  "database " + self._db_host_db_host + " " + str(self._db_port_db_port) + "\n"
224  )
225  return (MoveGroupInfoLevel.DEBUG, "OK")
226  except:
227  return (MoveGroupInfoLevel.WARN, "Unable to save " + filename)
228  else:
229  return None
230 
231  def execute_group_command(self, g, cmdorig):
232  cmd = cmdorig.lower()
233 
234  if cmd == "stop":
235  g.stop()
236  return (MoveGroupInfoLevel.DEBUG, "OK")
237 
238  if cmd == "id":
239  return (MoveGroupInfoLevel.INFO, g.get_name())
240 
241  if cmd == "help":
242  return (MoveGroupInfoLevel.INFO, self.get_helpget_help())
243 
244  if cmd == "vars":
245  known = g.get_remembered_joint_values()
246  return (MoveGroupInfoLevel.INFO, "[" + " ".join(known.keys()) + "]")
247 
248  if cmd == "joints":
249  joints = g.get_joints()
250  return (
251  MoveGroupInfoLevel.INFO,
252  "\n"
253  + "\n".join([str(i) + " = " + joints[i] for i in range(len(joints))])
254  + "\n",
255  )
256 
257  if cmd == "show":
258  return self.command_showcommand_show(g)
259 
260  if cmd == "current":
261  return self.command_currentcommand_current(g)
262 
263  if cmd == "ground":
264  pose = PoseStamped()
265  pose.pose.position.x = 0
266  pose.pose.position.y = 0
267  # offset such that the box is 0.1 mm below ground (to prevent collision with the robot itself)
268  pose.pose.position.z = -0.0501
269  pose.pose.orientation.x = 0
270  pose.pose.orientation.y = 0
271  pose.pose.orientation.z = 0
272  pose.pose.orientation.w = 1
273  pose.header.stamp = rospy.get_rostime()
274  pose.header.frame_id = self._robot_robot.get_root_link()
275  self._planning_scene_interface_planning_scene_interface.attach_box(
276  self._robot_robot.get_root_link(), "ground", pose, (3, 3, 0.1)
277  )
278  return (MoveGroupInfoLevel.INFO, "Added ground")
279 
280  if cmd == "eef":
281  if len(g.get_end_effector_link()) > 0:
282  return (MoveGroupInfoLevel.INFO, g.get_end_effector_link())
283  else:
284  return (MoveGroupInfoLevel.INFO, "There is no end effector defined")
285 
286  if cmd == "database":
287  if self._db_host_db_host == None:
288  return (MoveGroupInfoLevel.INFO, "Not connected to a database")
289  else:
290  return (
291  MoveGroupInfoLevel.INFO,
292  "Connected to " + self._db_host_db_host + ":" + str(self._db_port_db_port),
293  )
294  if cmd == "plan":
295  if self._last_plan_last_plan != None:
296  return (MoveGroupInfoLevel.INFO, str(self._last_plan_last_plan))
297  else:
298  return (MoveGroupInfoLevel.INFO, "No previous plan")
299 
300  if cmd == "constrain":
301  g.clear_path_constraints()
302  return (MoveGroupInfoLevel.SUCCESS, "Cleared path constraints")
303 
304  if cmd == "tol" or cmd == "tolerance":
305  return (
306  MoveGroupInfoLevel.INFO,
307  "Joint = %0.6g, Position = %0.6g, Orientation = %0.6g"
308  % g.get_goal_tolerance(),
309  )
310 
311  if cmd == "time":
312  return (MoveGroupInfoLevel.INFO, str(g.get_planning_time()))
313 
314  if cmd == "execute":
315  if self._last_plan_last_plan != None:
316  if g.execute(self._last_plan_last_plan):
317  return (MoveGroupInfoLevel.SUCCESS, "Plan submitted for execution")
318  else:
319  return (
320  MoveGroupInfoLevel.WARN,
321  "Failed to submit plan for execution",
322  )
323  else:
324  return (MoveGroupInfoLevel.WARN, "No motion plan computed")
325 
326  # see if we have assignment between variables
327  assign_match = re.match(r"^(\w+)\s*=\s*(\w+)$", cmd)
328  if assign_match:
329  known = g.get_remembered_joint_values()
330  if assign_match.group(2) in known:
331  g.remember_joint_values(
332  assign_match.group(1), known[assign_match.group(2)]
333  )
334  return (
335  MoveGroupInfoLevel.SUCCESS,
336  assign_match.group(1)
337  + " is now the same as "
338  + assign_match.group(2),
339  )
340  else:
341  return (MoveGroupInfoLevel.WARN, "Unknown command: '" + cmd + "'")
342 
343  # see if we have assignment of matlab-like vector syntax
344  set_match = re.match(r"^(\w+)\s*=\s*\[([\d\.e\-\+\s]+)\]$", cmd)
345  if set_match:
346  try:
347  g.remember_joint_values(
348  set_match.group(1), [float(x) for x in set_match.group(2).split()]
349  )
350  return (
351  MoveGroupInfoLevel.SUCCESS,
352  "Remembered joint values ["
353  + set_match.group(2)
354  + "] under the name "
355  + set_match.group(1),
356  )
357  except:
358  return (
359  MoveGroupInfoLevel.WARN,
360  "Unable to parse joint value [" + set_match.group(2) + "]",
361  )
362 
363  # see if we have assignment of matlab-like element update
364  component_match = re.match(
365  r"^(\w+)\s*\[\s*(\d+)\s*\]\s*=\s*([\d\.e\-\+]+)$", cmd
366  )
367  if component_match:
368  known = g.get_remembered_joint_values()
369  if component_match.group(1) in known:
370  try:
371  val = known[component_match.group(1)]
372  val[int(component_match.group(2))] = float(component_match.group(3))
373  g.remember_joint_values(component_match.group(1), val)
374  return (
375  MoveGroupInfoLevel.SUCCESS,
376  "Updated "
377  + component_match.group(1)
378  + "["
379  + component_match.group(2)
380  + "]",
381  )
382  except:
383  return (
384  MoveGroupInfoLevel.WARN,
385  "Unable to parse index or value in '" + cmd + "'",
386  )
387  else:
388  return (MoveGroupInfoLevel.WARN, "Unknown command: '" + cmd + "'")
389 
390  clist = cmdorig.split()
391  clist[0] = clist[0].lower()
392 
393  # if this is an unknown one-word command, it is probably a variable
394  if len(clist) == 1:
395  known = g.get_remembered_joint_values()
396  if cmd in known:
397  return (
398  MoveGroupInfoLevel.INFO,
399  "[" + " ".join([str(x) for x in known[cmd]]) + "]",
400  )
401  else:
402  return (MoveGroupInfoLevel.WARN, "Unknown command: '" + cmd + "'")
403 
404  # command with one argument
405  if len(clist) == 2:
406  if clist[0] == "go":
407  self._last_plan_last_plan = None
408  if clist[1] == "rand" or clist[1] == "random":
409  vals = g.get_random_joint_values()
410  g.set_joint_value_target(vals)
411  if g.go():
412  return (
413  MoveGroupInfoLevel.SUCCESS,
414  "Moved to random target ["
415  + " ".join([str(x) for x in vals])
416  + "]",
417  )
418  else:
419  return (
420  MoveGroupInfoLevel.FAIL,
421  "Failed while moving to random target ["
422  + " ".join([str(x) for x in vals])
423  + "]",
424  )
425  else:
426  try:
427  g.set_named_target(clist[1])
428  if g.go():
429  return (MoveGroupInfoLevel.SUCCESS, "Moved to " + clist[1])
430  else:
431  return (
432  MoveGroupInfoLevel.FAIL,
433  "Failed while moving to " + clist[1],
434  )
435  except MoveItCommanderException as e:
436  return (
437  MoveGroupInfoLevel.WARN,
438  "Going to " + clist[1] + ": " + str(e),
439  )
440  except:
441  return (MoveGroupInfoLevel.WARN, clist[1] + " is unknown")
442  if clist[0] == "plan":
443  self._last_plan_last_plan = None
444  vals = None
445  if clist[1] == "rand" or clist[1] == "random":
446  vals = g.get_random_joint_values()
447  g.set_joint_value_target(vals)
448  self._last_plan_last_plan = g.plan()[1] # The trajectory msg
449  else:
450  try:
451  g.set_named_target(clist[1])
452  self._last_plan_last_plan = g.plan()[1] # The trajectory msg
453  except MoveItCommanderException as e:
454  return (
455  MoveGroupInfoLevel.WARN,
456  "Planning to " + clist[1] + ": " + str(e),
457  )
458  except:
459  return (MoveGroupInfoLevel.WARN, clist[1] + " is unknown")
460  if self._last_plan_last_plan != None:
461  if (
462  len(self._last_plan_last_plan.joint_trajectory.points) == 0
463  and len(self._last_plan_last_plan.multi_dof_joint_trajectory.points) == 0
464  ):
465  self._last_plan_last_plan = None
466  dest_str = clist[1]
467  if vals != None:
468  dest_str = "[" + " ".join([str(x) for x in vals]) + "]"
469  if self._last_plan_last_plan != None:
470  return (MoveGroupInfoLevel.SUCCESS, "Planned to " + dest_str)
471  else:
472  return (
473  MoveGroupInfoLevel.FAIL,
474  "Failed while planning to " + dest_str,
475  )
476  elif clist[0] == "pick":
477  self._last_plan_last_plan = None
478  if g.pick(clist[1]):
479  return (MoveGroupInfoLevel.SUCCESS, "Picked object " + clist[1])
480  else:
481  return (
482  MoveGroupInfoLevel.FAIL,
483  "Failed while trying to pick object " + clist[1],
484  )
485  elif clist[0] == "place":
486  self._last_plan_last_plan = None
487  if g.place(clist[1]):
488  return (MoveGroupInfoLevel.SUCCESS, "Placed object " + clist[1])
489  else:
490  return (
491  MoveGroupInfoLevel.FAIL,
492  "Failed while trying to place object " + clist[1],
493  )
494  elif clist[0] == "planner":
495  g.set_planner_id(clist[1])
496  return (MoveGroupInfoLevel.SUCCESS, "Planner is now " + clist[1])
497  elif clist[0] == "record" or clist[0] == "rec":
498  g.remember_joint_values(clist[1])
499  return (
500  MoveGroupInfoLevel.SUCCESS,
501  "Remembered current joint values under the name " + clist[1],
502  )
503  elif clist[0] == "rand" or clist[0] == "random":
504  g.remember_joint_values(clist[1], g.get_random_joint_values())
505  return (
506  MoveGroupInfoLevel.SUCCESS,
507  "Remembered random joint values under the name " + clist[1],
508  )
509  elif clist[0] == "del" or clist[0] == "delete":
510  g.forget_joint_values(clist[1])
511  return (
512  MoveGroupInfoLevel.SUCCESS,
513  "Forgot joint values under the name " + clist[1],
514  )
515  elif clist[0] == "show":
516  known = g.get_remembered_joint_values()
517  if clist[1] in known:
518  return (
519  MoveGroupInfoLevel.INFO,
520  "[" + " ".join([str(x) for x in known[clist[1]]]) + "]",
521  )
522  else:
523  return (
524  MoveGroupInfoLevel.WARN,
525  "Joint values for " + clist[1] + " are not known",
526  )
527  elif clist[0] == "tol" or clist[0] == "tolerance":
528  try:
529  g.set_goal_tolerance(float(clist[1]))
530  return (MoveGroupInfoLevel.SUCCESS, "OK")
531  except:
532  return (
533  MoveGroupInfoLevel.WARN,
534  "Unable to parse tolerance value '" + clist[1] + "'",
535  )
536  elif clist[0] == "time":
537  try:
538  g.set_planning_time(float(clist[1]))
539  return (MoveGroupInfoLevel.SUCCESS, "OK")
540  except:
541  return (
542  MoveGroupInfoLevel.WARN,
543  "Unable to parse planning duration value '" + clist[1] + "'",
544  )
545  elif clist[0] == "constrain":
546  try:
547  g.set_path_constraints(clist[1])
548  return (MoveGroupInfoLevel.SUCCESS, "OK")
549  except:
550  if self._db_host_db_host != None:
551  return (
552  MoveGroupInfoLevel.WARN,
553  "Constraint " + clist[1] + " is not known.",
554  )
555  else:
556  return (MoveGroupInfoLevel.WARN, "Not connected to a database.")
557  elif clist[0] == "wait":
558  try:
559  time.sleep(float(clist[1]))
560  return (MoveGroupInfoLevel.SUCCESS, clist[1] + " seconds passed")
561  except:
562  return (
563  MoveGroupInfoLevel.WARN,
564  "Unable to wait '" + clist[1] + "' seconds",
565  )
566  elif clist[0] == "database":
567  try:
568  g.set_constraints_database(clist[1], self._db_port_db_port)
569  self._db_host_db_host = clist[1]
570  return (
571  MoveGroupInfoLevel.SUCCESS,
572  "Connected to " + self._db_host_db_host + ":" + str(self._db_port_db_port),
573  )
574  except:
575  return (
576  MoveGroupInfoLevel.WARN,
577  "Unable to connect to '"
578  + clist[1]
579  + ":"
580  + str(self._db_port_db_port)
581  + "'",
582  )
583  else:
584  return (MoveGroupInfoLevel.WARN, "Unknown command: '" + cmd + "'")
585 
586  if len(clist) == 3:
587  if clist[0] == "go" and clist[1] in self.GO_DIRSGO_DIRS:
588  self._last_plan_last_plan = None
589  try:
590  offset = float(clist[2])
591  (axis, factor) = self.GO_DIRSGO_DIRS[clist[1]]
592  return self.command_go_offsetcommand_go_offset(g, offset, factor, axis, clist[1])
593  except MoveItCommanderException as e:
594  return (
595  MoveGroupInfoLevel.WARN,
596  "Going " + clist[1] + ": " + str(e),
597  )
598  except:
599  return (
600  MoveGroupInfoLevel.WARN,
601  "Unable to parse distance '" + clist[2] + "'",
602  )
603  elif clist[0] == "allow" and (clist[1] == "look" or clist[1] == "looking"):
604  if (
605  clist[2] == "1"
606  or clist[2] == "true"
607  or clist[2] == "T"
608  or clist[2] == "True"
609  ):
610  g.allow_looking(True)
611  else:
612  g.allow_looking(False)
613  return (MoveGroupInfoLevel.DEBUG, "OK")
614  elif clist[0] == "allow" and (
615  clist[1] == "replan" or clist[1] == "replanning"
616  ):
617  if (
618  clist[2] == "1"
619  or clist[2] == "true"
620  or clist[2] == "T"
621  or clist[2] == "True"
622  ):
623  g.allow_replanning(True)
624  else:
625  g.allow_replanning(False)
626  return (MoveGroupInfoLevel.DEBUG, "OK")
627  elif clist[0] == "database":
628  try:
629  g.set_constraints_database(clist[1], int(clist[2]))
630  self._db_host_db_host = clist[1]
631  self._db_port_db_port = int(clist[2])
632  return (
633  MoveGroupInfoLevel.SUCCESS,
634  "Connected to " + self._db_host_db_host + ":" + str(self._db_port_db_port),
635  )
636  except:
637  self._db_host_db_host = None
638  return (
639  MoveGroupInfoLevel.WARN,
640  "Unable to connect to '" + clist[1] + ":" + clist[2] + "'",
641  )
642  if len(clist) == 4:
643  if clist[0] == "rotate":
644  try:
645  g.set_rpy_target([float(x) for x in clist[1:]])
646  if g.go():
647  return (MoveGroupInfoLevel.SUCCESS, "Rotation complete")
648  else:
649  return (
650  MoveGroupInfoLevel.FAIL,
651  "Failed while rotating to " + " ".join(clist[1:]),
652  )
653  except MoveItCommanderException as e:
654  return (MoveGroupInfoLevel.WARN, str(e))
655  except:
656  return (
657  MoveGroupInfoLevel.WARN,
658  "Unable to parse X-Y-Z rotation values '"
659  + " ".join(clist[1:])
660  + "'",
661  )
662  if len(clist) >= 7:
663  if clist[0] == "go":
664  self._last_plan_last_plan = None
665  approx = False
666  if len(clist) > 7:
667  if clist[7] == "approx" or clist[7] == "approximate":
668  approx = True
669  try:
670  p = Pose()
671  p.position.x = float(clist[1])
672  p.position.y = float(clist[2])
673  p.position.z = float(clist[3])
674  q = tf.transformations.quaternion_from_euler(
675  float(clist[4]), float(clist[5]), float(clist[6])
676  )
677  p.orientation.x = q[0]
678  p.orientation.y = q[1]
679  p.orientation.z = q[2]
680  p.orientation.w = q[3]
681  if approx:
682  g.set_joint_value_target(p, True)
683  else:
684  g.set_pose_target(p)
685  if g.go():
686  return (
687  MoveGroupInfoLevel.SUCCESS,
688  "Moved to pose target\n%s\n" % (str(p)),
689  )
690  else:
691  return (
692  MoveGroupInfoLevel.FAIL,
693  "Failed while moving to pose \n%s\n" % (str(p)),
694  )
695  except MoveItCommanderException as e:
696  return (
697  MoveGroupInfoLevel.WARN,
698  "Going to pose target: %s" % (str(e)),
699  )
700  except:
701  return (
702  MoveGroupInfoLevel.WARN,
703  "Unable to parse pose '" + " ".join(clist[1:]) + "'",
704  )
705 
706  return (MoveGroupInfoLevel.WARN, "Unknown command: '" + cmd + "'")
707 
708  def command_show(self, g):
709  known = g.get_remembered_joint_values()
710  res = []
711  for k in known.keys():
712  res.append(k + " = [" + " ".join([str(x) for x in known[k]]) + "]")
713  return (MoveGroupInfoLevel.INFO, "\n".join(res))
714 
715  def command_current(self, g):
716  res = (
717  "joints = ["
718  + ", ".join([str(x) for x in g.get_current_joint_values()])
719  + "]"
720  )
721  if len(g.get_end_effector_link()) > 0:
722  res = (
723  res
724  + "\n\n"
725  + g.get_end_effector_link()
726  + " pose = [\n"
727  + str(g.get_current_pose())
728  + " ]"
729  )
730  res = (
731  res
732  + "\n"
733  + g.get_end_effector_link()
734  + " RPY = "
735  + str(g.get_current_rpy())
736  )
737  return (MoveGroupInfoLevel.INFO, res)
738 
739  def command_go_offset(self, g, offset, factor, dimension_index, direction_name):
740  if g.has_end_effector_link():
741  try:
742  g.shift_pose_target(dimension_index, factor * offset)
743  if g.go():
744  return (
745  MoveGroupInfoLevel.SUCCESS,
746  "Moved " + direction_name + " by " + str(offset) + " m",
747  )
748  else:
749  return (
750  MoveGroupInfoLevel.FAIL,
751  "Failed while moving " + direction_name,
752  )
753  except MoveItCommanderException as e:
754  return (MoveGroupInfoLevel.WARN, str(e))
755  except:
756  return (MoveGroupInfoLevel.WARN, "Unable to process pose update")
757  else:
758  return (
759  MoveGroupInfoLevel.WARN,
760  "No known end effector. Cannot move " + direction_name,
761  )
762 
763  def resolve_command_alias(self, cmdorig):
764  cmd = cmdorig.lower()
765  if cmd == "which":
766  return "id"
767  if cmd == "groups":
768  return "use"
769  return cmdorig
770 
771  def get_help(self):
772  res = []
773  res.append("Known commands:")
774  res.append(" help show this screen")
775  res.append(" allow looking <true|false> enable/disable looking around")
776  res.append(" allow replanning <true|false> enable/disable replanning")
777  res.append(" constrain clear path constraints")
778  res.append(
779  " constrain <name> use the constraint <name> as a path constraint"
780  )
781  res.append(" current show the current state of the active group")
782  res.append(
783  " database display the current database connection (if any)"
784  )
785  res.append(
786  " delete <name> forget the joint values under the name <name>"
787  )
788  res.append(
789  " eef print the name of the end effector attached to the current group"
790  )
791  res.append(" execute execute a previously computed motion plan")
792  res.append(
793  " go <name> plan and execute a motion to the state <name>"
794  )
795  res.append(" go rand plan and execute a motion to a random state")
796  res.append(
797  " go <dir> <dx>| plan and execute a motion in direction up|down|left|right|forward|backward for distance <dx>"
798  )
799  res.append(" ground add a ground plane to the planning scene")
800  res.append(
801  " id|which display the name of the group that is operated on"
802  )
803  res.append(
804  " joints display names of the joints in the active group"
805  )
806  res.append(
807  " load [<file>] load a set of interpreted commands from a file"
808  )
809  res.append(" pick <name> pick up object <name>")
810  res.append(" place <name> place object <name>")
811  res.append(" plan <name> plan a motion to the state <name>")
812  res.append(" plan rand plan a motion to a random state")
813  res.append(" planner <name> use planner <name> to plan next motion")
814  res.append(
815  " record <name> record the current joint values under the name <name>"
816  )
817  res.append(
818  " rotate <x> <y> <z> plan and execute a motion to a specified orientation (about the X,Y,Z axes)"
819  )
820  res.append(
821  " save [<file>] save the currently known variables as a set of commands"
822  )
823  res.append(
824  " show display the names and values of the known states"
825  )
826  res.append(" show <name> display the value of a state")
827  res.append(" stop stop the active group")
828  res.append(" time show the configured allowed planning time")
829  res.append(" time <val> set the allowed planning time")
830  res.append(
831  " tolerance show the tolerance for reaching the goal region"
832  )
833  res.append(
834  " tolerance <val> set the tolerance for reaching the goal region"
835  )
836  res.append(" trace <on|off> enable/disable replanning or looking around")
837  res.append(
838  " use <name> switch to using the group named <name> (and load it if necessary)"
839  )
840  res.append(" use|groups show the group names that are already loaded")
841  res.append(" vars display the names of the known states")
842  res.append(" wait <dt> sleep for <dt> seconds")
843  res.append(" x = y assign the value of y to x")
844  res.append(" x = [v1 v2...] assign a vector of values to x")
845  res.append(" x[idx] = val assign a value to dimension idx of x")
846  return "\n".join(res)
847 
848  def get_keywords(self):
849  known_vars = []
850  known_constr = []
851  if self.get_active_groupget_active_group() != None:
852  known_vars = self.get_active_groupget_active_group().get_remembered_joint_values().keys()
853  known_constr = self.get_active_groupget_active_group().get_known_constraints()
854  groups = self._robot_robot.get_group_names()
855  return {
856  "go": ["up", "down", "left", "right", "backward", "forward", "random"]
857  + list(known_vars),
858  "help": [],
859  "record": known_vars,
860  "show": known_vars,
861  "wait": [],
862  "delete": known_vars,
863  "database": [],
864  "current": [],
865  "use": groups,
866  "load": [],
867  "save": [],
868  "pick": [],
869  "place": [],
870  "plan": known_vars,
871  "allow": ["replanning", "looking"],
872  "constrain": known_constr,
873  "vars": [],
874  "joints": [],
875  "tolerance": [],
876  "time": [],
877  "eef": [],
878  "execute": [],
879  "ground": [],
880  "id": [],
881  }
def command_go_offset(self, g, offset, factor, dimension_index, direction_name)
Definition: interpreter.py:739
void print(PropagationDistanceField &pdf, int numX, int numY, int numZ)