sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
run_test_utils.py
Go to the documentation of this file.
1 """
2 2019, LAAS/CNRS
3 @author: Gabriele Buondonno
4 This module contains utilities for running the tests
5 """
6 
7 # flake8: noqa
8 from __future__ import print_function
9 
10 from distutils.util import strtobool
11 from time import sleep
12 
13 import rospy
14 from std_srvs.srv import *
15 
17 
18 try:
19  # Python 2
20  input = raw_input # noqa
21 except NameError:
22  pass
23 
24 runCommandClient = rospy.ServiceProxy("run_command", RunCommand)
25 
26 
27 def runVerboseCommandClient(code, verbosity=1):
28  """
29  Auxiliary function to manage the verbosity of runCommandClient
30  verbosity = 0: simply execute runCommandClient
31  verbosity = 1 (default): if runCommandClient returns something on the standard output or standard error, print it
32  verbosity = 2: print the whole answer returned by runCommandClient
33  """
34  if verbosity > 1:
35  print(code)
36 
37  out = runCommandClient(code)
38 
39  if verbosity > 1:
40  print(out)
41  elif verbosity == 1 and (out.standardoutput or out.standarderror):
42  print("command: " + code)
43  if out.standardoutput:
44  print("standardoutput: " + out.standardoutput)
45  if out.standarderror:
46  print("standarderror: " + out.standarderror)
47  return out
48 
49 
51  """
52  Auxiliary function to quickly extract return values from runCommandClient.
53  This will only work when the result in a plain object data type (such as an in or float) or a string
54  """
55  return eval(runCommandClient(code).result)
56 
57 
58 def launch_script(code, title, description="", verbosity=1, interactive=True):
59  if interactive:
60  input(title + ": " + description)
61  rospy.loginfo(title)
62  rospy.loginfo(code)
63  indent = " "
64  indenting = False
65  for line in code:
66  if indenting:
67  if line == "" or line.startswith(indent):
68  codeblock += "\n" + line
69  continue
70  else:
71  answer = runVerboseCommandClient(str(codeblock), verbosity)
72  rospy.logdebug(answer)
73  indenting = False
74  if line != "" and line[0] != "#":
75  if line.endswith(":"):
76  indenting = True
77  codeblock = line
78  else:
79  answer = runVerboseCommandClient(str(line), verbosity)
80  rospy.logdebug(answer)
81  rospy.loginfo("...done with " + title)
82 
83 
84 def run_test(appli, verbosity=1, interactive=True):
85  try:
86  rospy.loginfo("Waiting for run_command")
87  rospy.wait_for_service("/run_command")
88  rospy.loginfo("...ok")
89 
90  rospy.loginfo("Waiting for start_dynamic_graph")
91  rospy.wait_for_service("/start_dynamic_graph")
92  rospy.loginfo("...ok")
93 
94  runCommandStartDynamicGraph = rospy.ServiceProxy("start_dynamic_graph", Empty)
95 
96  initCode = open(appli, "r").read().split("\n")
97 
98  rospy.loginfo("Stack of Tasks launched")
99 
101  initCode, "initialize SoT", verbosity=verbosity, interactive=interactive
102  )
103  if interactive:
104  input("Wait before starting the dynamic graph")
105  runCommandStartDynamicGraph()
106  print()
107  except rospy.ServiceException as e:
108  rospy.logerr("Service call failed: %s" % e)
109 
110 
112  c = input(text + " [y/N] ")
113  try:
114  return strtobool(c)
115  except Exception:
116  return False
117 
118 
119 def run_ft_calibration(sensor_name, force=False):
120  cb = force
121  if not cb:
122  cb = ask_for_confirmation("Calibrate force sensors?")
123  if cb:
124  print("Calibrating sensors...")
125  runCommandClient(sensor_name + ".calibrateFeetSensor()")
126  sleep(1.0) # TODO: get time/state from F/T sensor
127  print("Sensors are calibrated!")
128  print("You can now put the robot on the ground")
129  else:
130  print("Skipping sensor calibration")
131 
132 
133 def run_ft_wrist_calibration(sensor_name, force=False):
134  cb = False
135  if force:
136  cb = True
137  else:
138  c = input("Calibrate wrist force sensors? [y/N] ")
139  try:
140  cb = strtobool(c)
141  except Exception:
142  cb = False
143  if cb:
144  input("Wait before running the calibration")
145  print("Calibrating sensors...")
146  runCommandClient(sensor_name + ".calibrateWristSensor()")
147  sleep(1.0) # TODO: get time/state from F/T sensor
148  print("Sensors are calibrated!")
149  else:
150  print("Skipping sensor calibration")
151 
152 
153 def get_file_folder(argv, send=True):
154  if len(argv) == 1:
155  test_folder = None
156  sot_talos_balance_folder = False
157  print("No folder data")
158  elif len(argv) == 2:
159  test_folder = argv[1]
160  sot_talos_balance_folder = False
161  print("Using folder " + test_folder)
162  elif len(argv) == 3:
163  if argv[1] != "-0":
164  raise ValueError("Unrecognized option: " + argv[1])
165  test_folder = argv[2]
166  sot_talos_balance_folder = True
167  print("Using folder " + test_folder + " from sot_talos_balance")
168  else:
169  raise ValueError("Bad options")
170 
171  if send:
172  print("Sending folder info...")
173  if test_folder is None:
174  runCommandClient("test_folder = None")
175  else:
176  runCommandClient('test_folder = "' + test_folder + '"')
177  runCommandClient("sot_talos_balance_folder = " + str(sot_talos_balance_folder))
178 
179  return test_folder, sot_talos_balance_folder
def run_ft_calibration(sensor_name, force=False)
def run_ft_wrist_calibration(sensor_name, force=False)
def runVerboseCommandClient(code, verbosity=1)
def run_test(appli, verbosity=1, interactive=True)
def launch_script(code, title, description="", verbosity=1, interactive=True)