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