RobotNameHelper.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "RobotNameHelper.h"
25 
32 // #include <ArmarXCore/core/system/ArmarXDataPath.cpp>
33 
34 #include <algorithm>
35 
36 #include <Eigen/Dense>
37 
38 #include <VirtualRobot/Nodes/RobotNode.h>
39 #include <VirtualRobot/Robot.h>
40 #include <VirtualRobot/VirtualRobot.h>
41 #include <VirtualRobot/Visualization/TriMeshModel.h>
42 
43 namespace armarx
44 {
45  void
46  RobotNameHelper::writeRobotInfoNode(const RobotInfoNodePtr& n,
47  std::ostream& str,
48  std::size_t indent,
49  char indentChar)
50  {
51  const std::string ind(4 * indent, indentChar);
52  if (!n)
53  {
54  str << ind << "nullptr\n";
55  return;
56  }
57  str << ind << n->name << ", profile = " << n->profile << ", value " << n->value << '\n';
58  for (const auto& c : n->children)
59  {
60  writeRobotInfoNode(c, str, indent + 1);
61  }
62  }
63 
64  const RobotInfoNodePtr&
66  {
67  return robotInfo;
68  }
69 
70  const std::string RobotNameHelper::LocationLeft = "Left";
71  const std::string RobotNameHelper::LocationRight = "Right";
72 
73  RobotNameHelper::RobotNameHelper(const RobotInfoNodePtr& robotInfo,
74  const StatechartProfilePtr& profile) :
75  root(Node(robotInfo)), robotInfo(robotInfo)
76  {
78  StatechartProfilePtr p = profile;
79  while (p && !p->isRoot())
80  {
81  profiles.emplace_back(p->getName());
82  p = p->getParent();
83  }
84  profiles.emplace_back(""); // for matching the default/root
85  }
86 
87  std::string
88  RobotNameHelper::select(const std::string& path) const
89  {
91  Node node = root;
92  for (const std::string& p : Split(path, "/"))
93  {
94  node = node.select(p, profiles);
95  }
96  if (!node.isValid())
97  {
98  std::stringstream s;
99  s << "RobotNameHelper::select: path " + path + " not found\nrobotInfo:\n";
100  writeRobotInfoNode(robotInfo, s);
101  throw std::runtime_error(s.str());
102  }
103  return node.value();
104  }
105 
107  RobotNameHelper::Create(const RobotInfoNodePtr& robotInfo, const StatechartProfilePtr& profile)
108  {
109  return RobotNameHelperPtr(new RobotNameHelper(robotInfo, profile));
110  }
111 
113  RobotNameHelper::Create(const std::string& robotXmlFilename,
114  const StatechartProfilePtr& profile)
115  {
116  RapidXmlReaderPtr reader = RapidXmlReader::FromFile(robotXmlFilename);
117  RapidXmlReaderNode robotNode = reader->getRoot("Robot");
118 
119  return Create(readRobotInfo(robotNode.first_node("RobotInfo")), profile);
120  }
121 
122  RobotInfoNodePtr
123  RobotNameHelper::readRobotInfo(const RapidXmlReaderNode& infoNode)
124  {
125  const std::string name = infoNode.name();
126  const std::string profile = infoNode.attribute_value_or_default("profile", "");
127  const std::string value = infoNode.attribute_value_or_default("value", "");
128 
129  const auto nodes = infoNode.nodes();
130 
131  std::vector<RobotInfoNodePtr> children;
132  children.reserve(nodes.size());
133 
134  std::transform(nodes.begin(),
135  nodes.end(),
136  std::back_inserter(children),
137  [](const auto& childNode) { return readRobotInfo(childNode); });
138 
139  return new RobotInfoNode(name, profile, value, children);
140  }
141 
142  Arm
143  RobotNameHelper::getArm(const std::string& side) const
144  {
145  return Arm(shared_from_this(), side);
146  }
147 
148  RobotArm
149  RobotNameHelper::getRobotArm(const std::string& side, const VirtualRobot::RobotPtr& robot) const
150  {
151  return RobotArm(Arm(shared_from_this(), side), robot);
152  }
153 
154  std::shared_ptr<const RobotNameHelper>
156  {
157  return rnh;
158  }
159 
160  RobotNameHelper::Node::Node(const RobotInfoNodePtr& robotInfo) : robotInfo(robotInfo)
161  {
162  }
163 
164  std::string
166  {
167  checkValid();
168  return robotInfo->value;
169  }
170 
172  RobotNameHelper::Node::select(const std::string& name, const std::vector<std::string>& profiles)
173  {
174  ARMARX_TRACE;
175  if (!isValid())
176  {
177  return Node(nullptr);
178  }
179  std::map<std::string, RobotInfoNodePtr> matches;
180  for (const RobotInfoNodePtr& child : robotInfo->children)
181  {
182  if (child->name == name)
183  {
184  matches[child->profile] = child;
185  }
186  }
187  for (const std::string& p : profiles)
188  {
189  if (matches.count(p))
190  {
191  return matches.at(p);
192  }
193  }
194  return Node(nullptr);
195  }
196 
197  bool
199  {
200  return robotInfo ? true : false;
201  }
202 
203  void
205  {
206  if (!isValid())
207  {
208 
209  std::stringstream s;
210  s << "RobotNameHelper::Node nullptr exception\nrobotInfo:\n";
211  writeRobotInfoNode(robotInfo, s);
212  throw std::runtime_error(s.str());
213  }
214  }
215 
216  std::string
217  Arm::getSide() const
218  {
219  return side;
220  }
221 
222  std::string
224  {
225  ARMARX_TRACE;
226  return select("KinematicChain");
227  }
228 
229  std::string
231  {
232  ARMARX_TRACE;
233  return select("TorsoKinematicChain");
234  }
235 
236  std::string
237  Arm::getTCP() const
238  {
239  ARMARX_TRACE;
240  return select("TCP");
241  }
242 
243  std::string
245  {
246  ARMARX_TRACE;
247  return select("ForceTorqueSensor");
248  }
249 
250  std::string
252  {
253  ARMARX_TRACE;
254  return select("ForceTorqueSensorFrame");
255  }
256 
257  std::string
259  {
260  ARMARX_TRACE;
261  return select("EndEffector");
262  }
263 
264  std::string
266  {
267  ARMARX_TRACE;
268  return select("MemoryHandName");
269  }
270 
271  std::string
273  {
274  ARMARX_TRACE;
275  return select("HandControllerName");
276  }
277 
278  std::string
280  {
281  ARMARX_TRACE;
282  return select("HandUnitName");
283  }
284 
285  std::string
287  {
288  ARMARX_TRACE;
289  return select("HandRootNode");
290  }
291 
292  std::string
294  {
295  ARMARX_TRACE;
296  return select("HandModelPath");
297  }
298 
299  std::string
301  {
303  auto path = getHandModelPath();
304  return ArmarXDataPath::getAbsolutePath(path, path) ? path : "";
305  }
306 
307  std::string
309  {
310  ARMARX_TRACE;
311  return select("HandModelPackage");
312  }
313 
314  std::string
316  {
317  ARMARX_TRACE;
318  return select("PalmCollisionModel");
319  }
320 
321  std::string
323  {
324  ARMARX_TRACE;
325  return select("FullHandCollisionModel");
326  }
327 
328  RobotArm
330  {
331  ARMARX_TRACE;
332  return RobotArm(*this, robot);
333  }
334 
335  Arm::Arm(const std::shared_ptr<const RobotNameHelper>& rnh, const std::string& side) :
336  rnh(rnh), side(side)
337  {
338  }
339 
340  std::string
341  Arm::select(const std::string& path) const
342  {
343  ARMARX_TRACE;
344  return rnh->select(side + "Arm/" + path);
345  }
346 
347  std::string
349  {
350  ARMARX_TRACE;
351  return arm.getSide();
352  }
353 
354  VirtualRobot::RobotNodeSetPtr
356  {
357  ARMARX_TRACE;
358  ARMARX_CHECK_NOT_NULL(robot);
359  return robot->getRobotNodeSet(arm.getKinematicChain());
360  }
361 
362  VirtualRobot::RobotNodeSetPtr
364  {
365  ARMARX_TRACE;
366  ARMARX_CHECK_NOT_NULL(robot);
367  return robot->getRobotNodeSet(arm.getTorsoKinematicChain());
368  }
369 
370  VirtualRobot::RobotNodePtr
372  {
373  ARMARX_TRACE;
374  ARMARX_CHECK_NOT_NULL(robot);
375  return robot->getRobotNode(arm.getTCP());
376  }
377 
378  VirtualRobot::RobotNodePtr
380  {
381  ARMARX_TRACE;
382  ARMARX_CHECK_NOT_NULL(robot);
383  return robot->getRobotNode(arm.getPalmCollisionModel());
384  }
385 
386  VirtualRobot::TriMeshModelPtr
388  {
389  ARMARX_TRACE;
390  std::string abs;
393  return VirtualRobot::TriMeshModel::FromFile(abs);
394  }
395 
396  VirtualRobot::RobotNodePtr
398  {
399  ARMARX_TRACE;
400  ARMARX_CHECK_NOT_NULL(robot);
401  return robot->getRobotNode(arm.getHandRootNode());
402  }
403 
406  {
407  ARMARX_TRACE;
408  const auto tcp = getTCP();
410  const auto hand = getHandRootNode();
411  ARMARX_CHECK_NOT_NULL(hand);
412  return tcp->getPoseInRootFrame().inverse() * hand->getPoseInRootFrame();
413  }
414 
417  {
418  return robot;
419  }
420 
421  const Arm&
423  {
424  return arm;
425  }
426 
427  RobotArm::RobotArm(const Arm& arm, const VirtualRobot::RobotPtr& robot) : arm(arm), robot(robot)
428  {
429  ARMARX_CHECK_NOT_NULL(robot);
430  }
431 
432  const std::vector<std::string>&
434  {
435  return profiles;
436  }
437 } // namespace armarx
armarx::RobotNameHelper::LocationLeft
static const std::string LocationLeft
Definition: RobotNameHelper.h:170
armarx::RapidXmlReaderPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
Definition: RapidXmlReader.h:67
armarx::RapidXmlReader::FromFile
static RapidXmlReaderPtr FromFile(const std::string &path)
Definition: RapidXmlReader.h:573
armarx::RobotNameHelper::Node::select
Node select(const std::string &name, const std::vector< std::string > &profiles)
Definition: RobotNameHelper.cpp:172
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::Arm::getMemoryHandName
std::string getMemoryHandName() const
Definition: RobotNameHelper.cpp:265
armarx::Arm::getFullHandCollisionModel
std::string getFullHandCollisionModel() const
Definition: RobotNameHelper.cpp:322
armarx::Arm::getAbsoluteHandModelPath
std::string getAbsoluteHandModelPath() const
Definition: RobotNameHelper.cpp:300
armarx::Arm::addRobot
RobotArm addRobot(const VirtualRobot::RobotPtr &robot) const
Definition: RobotNameHelper.cpp:329
armarx::RobotArm::RobotArm
RobotArm()=default
armarx::Arm::getPalmCollisionModel
std::string getPalmCollisionModel() const
Definition: RobotNameHelper.cpp:315
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::ArmarXDataPath::FindPackageAndAddDataPath
static bool FindPackageAndAddDataPath(const std::string &packageName)
Search for the package and add its data path if it was found.
Definition: ArmarXDataPath.cpp:824
armarx::Arm::getHandRootNode
std::string getHandRootNode() const
Definition: RobotNameHelper.cpp:286
armarx::Arm::getHandModelPackage
std::string getHandModelPackage() const
Definition: RobotNameHelper.cpp:308
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::RobotArm::getSide
std::string getSide() const
Definition: RobotNameHelper.cpp:348
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
trace.h
armarx::Arm::getHandUnitName
std::string getHandUnitName() const
Definition: RobotNameHelper.cpp:279
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:51
armarx::RobotNameHelper::Node::Node
Node(const RobotInfoNodePtr &robotInfo)
Definition: RobotNameHelper.cpp:160
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::RobotNameHelper::LocationRight
static const std::string LocationRight
Definition: RobotNameHelper.h:171
armarx::RobotArm::getTorsoKinematicChain
VirtualRobot::RobotNodeSetPtr getTorsoKinematicChain() const
Definition: RobotNameHelper.cpp:363
armarx::RobotNameHelper::getProfiles
const std::vector< std::string > & getProfiles() const
Definition: RobotNameHelper.cpp:433
armarx::RobotArm::getFullHandCollisionModel
VirtualRobot::TriMeshModelPtr getFullHandCollisionModel() const
Definition: RobotNameHelper.cpp:387
armarx::RobotNameHelper::select
std::string select(const std::string &path) const
Definition: RobotNameHelper.cpp:88
armarx::Arm::getTCP
std::string getTCP() const
Definition: RobotNameHelper.cpp:237
StringHelpers.h
armarx::RobotNameHelper::writeRobotInfoNode
static void writeRobotInfoNode(const RobotInfoNodePtr &n, std::ostream &str, std::size_t indent=0, char indentChar=' ')
Definition: RobotNameHelper.cpp:46
armarx::RobotNameHelper::getRobotArm
RobotArm getRobotArm(const std::string &side, const VirtualRobot::RobotPtr &robot) const
Definition: RobotNameHelper.cpp:149
armarx::Arm::getTorsoKinematicChain
std::string getTorsoKinematicChain() const
Definition: RobotNameHelper.cpp:230
armarx::RapidXmlReaderNode::name
std::string name() const
Definition: RapidXmlReader.h:407
armarx::Arm::getForceTorqueSensor
std::string getForceTorqueSensor() const
Definition: RobotNameHelper.cpp:244
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::Arm::getEndEffector
std::string getEndEffector() const
Definition: RobotNameHelper.cpp:258
armarx::RobotArm::getHandRootNode
VirtualRobot::RobotNodePtr getHandRootNode() const
Definition: RobotNameHelper.cpp:397
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:281
armarx::RobotNameHelper::getRobotInfoNodePtr
const RobotInfoNodePtr & getRobotInfoNodePtr() const
Definition: RobotNameHelper.cpp:65
armarx::RobotNameHelperPtr
std::shared_ptr< class RobotNameHelper > RobotNameHelperPtr
Definition: RobotNameHelper.h:32
armarx::RobotArm::getTCP
VirtualRobot::RobotNodePtr getTCP() const
Definition: RobotNameHelper.cpp:371
armarx::RobotArm::getTcp2HandRootTransform
Eigen::Matrix4f getTcp2HandRootTransform() const
Definition: RobotNameHelper.cpp:405
armarx::Arm::getRobotNameHelper
std::shared_ptr< const RobotNameHelper > getRobotNameHelper() const
Definition: RobotNameHelper.cpp:155
armarx::RobotNameHelper::Node::isValid
bool isValid()
Definition: RobotNameHelper.cpp:198
armarx::RobotArm::getPalmCollisionModel
VirtualRobot::RobotNodePtr getPalmCollisionModel() const
Definition: RobotNameHelper.cpp:379
armarx::RobotArm::getKinematicChain
VirtualRobot::RobotNodeSetPtr getKinematicChain() const
Definition: RobotNameHelper.cpp:355
StatechartProfiles.h
armarx::Arm::Arm
Arm()=default
armarx::RobotNameHelper::RobotArm
armarx::RobotArm RobotArm
Definition: RobotNameHelper.h:151
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:69
armarx::RobotNameHelper::Node::value
std::string value()
Definition: RobotNameHelper.cpp:165
armarx::Arm::getKinematicChain
std::string getKinematicChain() const
Definition: RobotNameHelper.cpp:223
armarx::Arm::getHandModelPath
std::string getHandModelPath() const
Definition: RobotNameHelper.cpp:293
armarx::RobotArm
Definition: RobotNameHelper.h:99
armarx::RobotNameHelper::RobotNameHelper
RobotNameHelper(const RobotInfoNodePtr &robotInfo, const StatechartProfilePtr &profile=nullptr)
Definition: RobotNameHelper.cpp:73
armarx::RobotArm::getRobot
const VirtualRobot::RobotPtr & getRobot() const
Definition: RobotNameHelper.cpp:416
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT >>
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:351
ExpressionException.h
armarx::ArmarXDataPath::SearchReadableFile
static bool SearchReadableFile(const std::string &querryFileName, std::string &resultFileName, bool verbose=true)
Definition: ArmarXDataPath.cpp:216
CMakePackageFinder.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::RobotNameHelper::Arm
armarx::Arm Arm
Definition: RobotNameHelper.h:150
armarx::Arm::select
std::string select(const std::string &path) const
Definition: RobotNameHelper.cpp:341
armarx::skills::gui::matches
bool matches(std::string skillName, std::vector< std::string > &searches)
Definition: SkillManagerWrapper.cpp:66
armarx::Arm::getSide
std::string getSide() const
Definition: RobotNameHelper.cpp:217
armarx::RapidXmlReaderNode::nodes
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
Definition: RapidXmlReader.h:181
armarx::RobotNameHelper::Create
static RobotNameHelperPtr Create(const RobotInfoNodePtr &robotInfo, const StatechartProfilePtr &profile)
Definition: RobotNameHelper.cpp:107
armarx::Arm
Definition: RobotNameHelper.h:39
armarx::RobotNameHelper::Node
Definition: RobotNameHelper.h:153
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:109
armarx::RobotArm::getArm
const Arm & getArm() const
Definition: RobotNameHelper.cpp:422
ArmarXDataPath.h
armarx::RapidXmlReaderNode::first_node
RapidXmlReaderNode first_node(const char *name=nullptr) const
Definition: RapidXmlReader.h:156
armarx::Arm::getForceTorqueSensorFrame
std::string getForceTorqueSensorFrame() const
Definition: RobotNameHelper.cpp:251
armarx::RobotNameHelper::Node::checkValid
void checkValid()
Definition: RobotNameHelper.cpp:204
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
RobotNameHelper.h
armarx::RapidXmlReaderNode::attribute_value_or_default
std::string attribute_value_or_default(const char *attrName, const std::string &defaultValue) const
Definition: RapidXmlReader.h:262
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::RobotNameHelper::getArm
Arm getArm(const std::string &side) const
Definition: RobotNameHelper.cpp:143
armarx::Arm::getHandControllerName
std::string getHandControllerName() const
Definition: RobotNameHelper.cpp:272