tools.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright 2011, Florent Lamiraux, Thomas Moulard, JRL, CNRS/AIST
4 
5 from __future__ import print_function
6 
7 import sys
8 from functools import reduce
9 
10 # Robotviewer is optional
11 hasRobotViewer = True
12 try:
13  import robotviewer
14 except ImportError:
15  hasRobotViewer = False
16 
17 
20 
21 
22 def toList(tupleOfTuple):
23  result = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
24  for i in range(4):
25  for j in range(4):
26  result[i][j] = tupleOfTuple[i][j]
27  return result
28 
29 
30 def toTuple(listOfList):
31  return (
32  (listOfList[0][0], listOfList[0][1], listOfList[0][2], listOfList[0][3]),
33  (listOfList[1][0], listOfList[1][1], listOfList[1][2], listOfList[1][3]),
34  (listOfList[2][0], listOfList[2][1], listOfList[2][2], listOfList[2][3]),
35  (listOfList[3][0], listOfList[3][1], listOfList[3][2], listOfList[3][3]),
36  )
37 
38 
40  """
41  Display nicely a 4x4 matrix (usually homogeneous matrix).
42  """
43  import itertools
44 
45  matrix_tuple = tuple(itertools.chain.from_iterable(matrix))
46 
47  formatStr = ""
48  for i in range(4 * 4):
49  formatStr += "{0[" + str(i) + "]: <10} "
50  if i != 0 and (i + 1) % 4 == 0:
51  formatStr += "\n"
52  print(formatStr.format(matrix_tuple))
53 
54 
56  if len(cfg) != 36:
57  raise "bad configuration size"
58  s = ""
59  s += "Free flyer:\n"
60  s += " translation {0[0]: <+10f} {0[1]: <+10f} {0[2]: <+10f}\n"
61  s += " rotation {0[3]: <+10f} {0[4]: <+10f} {0[5]: <+10f}\n"
62  s += "Left leg:\n"
63  s += " hip {0[6]: <+10f} {0[7]: <+10f} {0[8]: <+10f}\n"
64  s += " knee {0[9]: <+10f}\n"
65  s += " ankle {0[10]: <+10f} {0[11]: <+10f}\n"
66  s += "Right leg:\n"
67  s += " hip {0[12]: <+10f} {0[13]: <+10f} {0[14]: <+10f}\n"
68  s += " knee {0[15]: <+10f}\n"
69  s += " ankle {0[16]: <+10f} {0[17]: <+10f}\n"
70  s += "Chest: {0[18]: <+10f} {0[19]: <+10f}\n"
71  s += "Head: {0[20]: <+10f} {0[21]: <+10f}\n"
72  s += "Left arm:\n"
73  s += " shoulder {0[22]: <+10f} {0[23]: <+10f} {0[24]: <+10f}\n"
74  s += " elbow {0[25]: <+10f}\n"
75  s += " wrist {0[26]: <+10f} {0[27]: <+10f}\n"
76  s += " clench {0[28]: <+10f}\n"
77  s += "Left arm:\n"
78  s += " shoulder {0[29]: <+10f} {0[30]: <+10f} {0[31]: <+10f}\n"
79  s += " elbow {0[32]: <+10f}\n"
80  s += " wrist {0[33]: <+10f} {0[34]: <+10f}\n"
81  s += " clench {0[35]: <+10f}\n"
82  print(s.format(cfg))
83 
84 
86  """Initialize robotviewer is possible."""
87  clt = None
88  if hasRobotViewer:
89  try:
90  clt = robotviewer.client()
91  except Exception:
92  print("failed to connect to robotviewer")
93  return clt
94 
95 
96 def reach(robot, op, tx, ty, tz):
97  sdes = toList(robot.dynamic.signal(op).value)
98  sdes[0][3] += tx
99  sdes[1][3] += ty
100  sdes[2][3] += tz
101  robot.features[op].reference.value = toTuple(sdes)
102  # Select translation only.
103  robot.features[op]._feature.signal("selec").value = "000111"
104  robot.tasks[op].signal("controlGain").value = 1.0
105 
106 
107 def sqrDist(value, expectedValue):
108  """Compute the square of the distance between two configurations."""
109 
110  def inner(acc, ab):
111  a, b = ab
112  return acc + abs(a - b) * abs(a - b)
113 
114  return reduce(inner, zip(value, expectedValue), 0.0)
115 
116 
117 def checkFinalConfiguration(position, finalPosition):
118  if sqrDist(position, finalPosition) >= 1e-3:
119  print("Wrong final position. Failing.")
120  print("Value:")
121  displayHrp2Configuration(position)
122  print("Expected value:")
123  displayHrp2Configuration(finalPosition)
124  print("Difference:")
125 
126  def diff(x, y):
127  return x - y
128 
129  displayHrp2Configuration(map(diff, zip(position, finalPosition)))
130  sys.exit(1)
131 
132 
133 
136 
137 if "argv" in sys.__dict__:
138  from optparse import OptionParser
139  from dynamic_graph.sot.dynamic_pinocchio.solver import Solver
140 
141  # Parse options and enable robotviewer client if wanted.
142  clt = None
143  parser = OptionParser()
144  parser.add_option(
145  "-d",
146  "--display",
147  action="store_true",
148  dest="display",
149  default=False,
150  help="enable display using robotviewer",
151  )
152  parser.add_option(
153  "-r",
154  "--robot",
155  action="store",
156  dest="robot",
157  default="Hrp2Laas",
158  help="Specify which robot model to use",
159  )
160  (options, args) = parser.parse_args()
161 
162  if options.display:
163  if not hasRobotViewer:
164  print("Failed to import robotviewer client library.")
165  clt = initRobotViewer()
166  if not clt:
167  print("Failed to initialize robotviewer client library.")
168 
169  # Initialize the stack of tasks.
170  robots = {"Hrp2Laas": Hrp2Laas, "Hrp2Jrl": Hrp2Jrl} # noqa TODO
171  if options.robot not in robots:
172  raise RuntimeError("invalid robot name (should by Hrp2Laas or Hrp2Jrl)")
173  robot = robots[options.robot]("robot")
174  solver = Solver(robot)
dynamic_pinocchio.tools.toList
def toList(tupleOfTuple)
Helper functions #.
Definition: tools.py:22
dynamic_pinocchio.tools.checkFinalConfiguration
def checkFinalConfiguration(position, finalPosition)
Definition: tools.py:117
dynamic_pinocchio.tools.reach
def reach(robot, op, tx, ty, tz)
Definition: tools.py:96
dynamic_pinocchio.tools.toTuple
def toTuple(listOfList)
Definition: tools.py:30
dynamic_pinocchio.tools.sqrDist
def sqrDist(value, expectedValue)
Definition: tools.py:107
dynamic_pinocchio.tools.displayHomogeneousMatrix
def displayHomogeneousMatrix(matrix)
Definition: tools.py:39
dynamic_pinocchio.tools.displayHrp2Configuration
def displayHrp2Configuration(cfg)
Definition: tools.py:55
dynamic_pinocchio.tools.initRobotViewer
def initRobotViewer()
Definition: tools.py:85


sot-dynamic-pinocchio
Author(s): Olivier Stasse
autogenerated on Fri Jul 28 2023 02:10:01