moveit2
The MoveIt Motion Planning Framework for ROS 2.
moveit_commander_cmdline.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 
5 import roslib
6 import rospy
7 
8 try:
9  import readline
10 except ImportError:
11  import pyreadline as readline # for Windows
12 import sys
13 import os
14 import signal
15 
16 import argparse
17 from moveit_commander import (
18  MoveGroupCommandInterpreter,
19  MoveGroupInfoLevel,
20  roscpp_initialize,
21  roscpp_shutdown,
22 )
23 
24 # python3 has renamed raw_input to input: https://www.python.org/dev/peps/pep-3111
25 # Here, we use the new input(). Hence, for python2, we redirect raw_input to input
26 try:
27  import __builtin__ # This is named builtin in python3
28 
29  input = getattr(__builtin__, "raw_input")
30 except (ImportError, AttributeError):
31  pass
32 
33 
34 class bcolors:
35  HEADER = "\033[95m"
36  OKBLUE = "\033[94m"
37  OKGREEN = "\033[92m"
38  WARNING = "\033[93m"
39  FAIL = "\033[91m"
40  ENDC = "\033[0m"
41 
42 
43 class SimpleCompleter(object):
44  def __init__(self, options):
45  self.optionsoptions = options
46 
47  def set_options(self, options):
48  self.optionsoptions = options
49 
50  def complete(self, text, state):
51  response = None
52  cmds = readline.get_line_buffer().split()
53  prefix = ""
54  if len(cmds) > 0:
55  prefix = cmds[0]
56  if not prefix in self.optionsoptions:
57  prefix = ""
58 
59  if state == 0:
60  # This is the first time for this text, so build a match list.
61  if text:
62  if len(prefix) == 0:
63  self.matchesmatches = sorted(
64  [s for s in self.optionsoptions.keys() if s and s.startswith(text)]
65  )
66  else:
67  self.matchesmatches = sorted(
68  [s for s in self.optionsoptions[prefix] if s and s.startswith(text)]
69  )
70  else:
71  if len(prefix) == 0:
72  self.matchesmatches = sorted(self.optionsoptions.keys())
73  else:
74  self.matchesmatches = self.optionsoptions[prefix]
75 
76  # Return the state'th item from the match list,
77  # if we have that many.
78  try:
79  response = self.matchesmatches[state] + " "
80  except IndexError:
81  response = None
82  return response
83 
84 
85 def print_message(level, msg):
86  if level == MoveGroupInfoLevel.FAIL:
87  print(bcolors.FAIL + msg + bcolors.ENDC)
88  elif level == MoveGroupInfoLevel.WARN:
89  print(bcolors.WARNING + msg + bcolors.ENDC)
90  elif level == MoveGroupInfoLevel.SUCCESS:
91  print(bcolors.OKGREEN + msg + bcolors.ENDC)
92  elif level == MoveGroupInfoLevel.DEBUG:
93  print(bcolors.OKBLUE + msg + bcolors.ENDC)
94  else:
95  print(msg)
96 
97 
98 def get_context_keywords(interpreter):
99  kw = interpreter.get_keywords()
100  kw["quit"] = []
101  return kw
102 
103 
104 def run_interactive(group_name):
106  if len(group_name) > 0:
107  c.execute("use " + group_name)
108  completer = SimpleCompleter(get_context_keywords(c))
109  readline.set_completer(completer.complete)
110 
111  print()
112  print(
113  bcolors.HEADER
114  + "Waiting for commands. Type 'help' to get a list of known commands."
115  + bcolors.ENDC
116  )
117  print()
118  readline.parse_and_bind("tab: complete")
119 
120  while not rospy.is_shutdown():
121  cmd = ""
122  try:
123  name = ""
124  ag = c.get_active_group()
125  if ag != None:
126  name = ag.get_name()
127  cmd = input(bcolors.OKBLUE + name + "> " + bcolors.ENDC)
128  except:
129  break
130  cmdorig = cmd.strip()
131  if cmdorig == "":
132  continue
133  cmd = cmdorig.lower()
134 
135  if cmd == "q" or cmd == "quit" or cmd == "exit":
136  break
137  if cmd == "host":
139  MoveGroupInfoLevel.INFO,
140  "Master is '" + os.environ["ROS_MASTER_URI"] + "'",
141  )
142  continue
143 
144  (level, msg) = c.execute(cmdorig)
145  print_message(level, msg)
146  # update the set of keywords
147  completer.set_options(get_context_keywords(c))
148 
149 
150 def run_service(group_name):
152  if len(group_name) > 0:
153  c.execute("use " + group_name)
154  # add service stuff
155  print("Running ROS service")
156  rospy.spin()
157 
158 
159 def stop_ros(reason):
160  rospy.signal_shutdown(reason)
162 
163 
164 def sigint_handler(signal, frame):
165  stop_ros("Ctrl+C pressed")
166  # this won't actually exit, but trigger an exception to terminate input
167  sys.exit(0)
168 
169 
170 if __name__ == "__main__":
171 
172  signal.signal(signal.SIGINT, sigint_handler)
173 
174  roscpp_initialize(sys.argv)
175 
176  rospy.init_node(
177  "move_group_interface_cmdline", anonymous=True, disable_signals=True
178  )
179 
180  parser = argparse.ArgumentParser(
181  usage="""%(prog)s [options] [<group_name>]""",
182  description="Command Line Interface to MoveIt",
183  )
184  parser.add_argument(
185  "-i",
186  "--interactive",
187  action="store_true",
188  dest="interactive",
189  default=True,
190  help="Run the command processing script in interactive mode (default)",
191  )
192  parser.add_argument(
193  "-s",
194  "--service",
195  action="store_true",
196  dest="service",
197  default=False,
198  help="Run the command processing script as a ROS service",
199  )
200  parser.add_argument(
201  "group_name",
202  type=str,
203  default="",
204  nargs="?",
205  help="Group name to initialize the CLI for.",
206  )
207 
208  opt = parser.parse_args(rospy.myargv()[1:])
209 
210  if opt.service:
211  run_service(opt.group_name)
212  else:
213  run_interactive(opt.group_name)
214 
215  stop_ros("Done")
216 
217  print("Bye bye!")
void print(PropagationDistanceField &pdf, int numX, int numY, int numZ)