sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
sot_utils.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-1
2 """
3 2019, LAAS/CNRS
4 @author: Andrea Del Prete, Gabriele Buondonno
5 This module contains utilities for the SOT
6 """
7 from __future__ import print_function
8 
9 import os
10 from time import sleep
11 
12 import numpy as np
13 
14 
15 class Bunch:
16  def __init__(self, **kwds):
17  self.__dict__.update(kwds)
18 
19  def __str__(self, prefix=""):
20  res = ""
21  for key, value in self.__dict__.iteritems():
22  if (
23  isinstance(value, np.ndarray)
24  and len(value.shape) == 2
25  and value.shape[0] > value.shape[1]
26  ):
27  res += prefix + " - " + key + ": " + str(value.T) + "\n"
28  elif isinstance(value, Bunch):
29  res += (
30  prefix + " - " + key + ":\n" + value.__str__(prefix + " ") + "\n"
31  )
32  else:
33  res += prefix + " - " + key + ": " + str(value) + "\n"
34  return res[:-1]
35 
36 
37 def start_sot():
38  os.system("rosservice call /start_dynamic_graph")
39 
40 
41 def stop_sot():
42  os.system("rosservice call /stop_dynamic_graph")
43 
44 
46  value_type = type(sig.value)
47  v = np.array(sig.value)
48  for i in range(40):
49  v = np.array(0.95 * v)
50  sig.value = value_type(v)
51  sleep(1)
52  print("Setting signal to zero")
53  v[:] = 0.0
54  sig.value = value_type(v)
55 
56 
57 def smoothly_set_signal(sig, final_value, duration=5.0, steps=500, prints=10):
58  value_type = type(sig.value)
59  v = np.array(sig.value)
60  vf = np.array(final_value)
61  for i in range(steps + 1):
62  alpha = 1.0 * i / steps
63  sig.value = value_type(np.array(vf * alpha + (1 - alpha) * v))
64  sleep(1.0 * duration / steps)
65  print("Signal set")
66  sig.value = value_type(vf)
67 
68 
69 def monitor_tracking_error(sig, sigRef, dt, time):
70  N = int(time / dt)
71  err = np.zeros((N, 6))
72  for i in range(N):
73  err[i, :] = np.array(sig.value) - np.array(sigRef.value)
74  sleep(dt)
75  for i in range(6):
76  print(
77  "Max tracking error for axis %d: %.2f"
78  % (i, np.max(np.abs(err[:, i])))
79  )
80  print(
81  "Mean square tracking error for axis %d: %.2f"
82  % (i, np.linalg.norm(err[:, i]) / N)
83  )
sot_talos_balance.utils.sot_utils.Bunch.__str__
def __str__(self, prefix="")
Definition: sot_utils.py:19
sot_talos_balance.utils.sot_utils.start_sot
def start_sot()
Definition: sot_utils.py:37
sot_talos_balance.utils.sot_utils.smoothly_set_signal_to_zero
def smoothly_set_signal_to_zero(sig)
Definition: sot_utils.py:45
sot_talos_balance.utils.sot_utils.monitor_tracking_error
def monitor_tracking_error(sig, sigRef, dt, time)
Definition: sot_utils.py:69
sot_talos_balance.utils.sot_utils.Bunch.__init__
def __init__(self, **kwds)
Definition: sot_utils.py:16
sot_talos_balance.utils.sot_utils.Bunch
Definition: sot_utils.py:15
sot_talos_balance.utils.sot_utils.smoothly_set_signal
def smoothly_set_signal(sig, final_value, duration=5.0, steps=500, prints=10)
Definition: sot_utils.py:57
sot_talos_balance.utils.sot_utils.stop_sot
def stop_sot()
Definition: sot_utils.py:41