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 <Eigen/Dense>
35 #include <algorithm>
36 
37 namespace armarx
38 {
39  void RobotNameHelper::writeRobotInfoNode(const RobotInfoNodePtr& n,
40  std::ostream& str,
41  std::size_t indent,
42  char indentChar)
43  {
44  const std::string ind(4 * indent, indentChar);
45  if (!n)
46  {
47  str << ind << "nullptr\n";
48  return;
49  }
50  str << ind << n->name << ", profile = " << n->profile << ", value " << n->value << '\n';
51  for (const auto& c : n->children)
52  {
53  writeRobotInfoNode(c, str, indent + 1);
54  }
55  }
56  const RobotInfoNodePtr& RobotNameHelper::getRobotInfoNodePtr() const
57  {
58  return robotInfo;
59  }
60 
61  const std::string RobotNameHelper::LocationLeft = "Left";
62  const std::string RobotNameHelper::LocationRight = "Right";
63 
64  RobotNameHelper::RobotNameHelper(const RobotInfoNodePtr& robotInfo,
65  const StatechartProfilePtr& profile) :
66  root(Node(robotInfo)), robotInfo(robotInfo)
67  {
69  StatechartProfilePtr p = profile;
70  while (p && !p->isRoot())
71  {
72  profiles.emplace_back(p->getName());
73  p = p->getParent();
74  }
75  profiles.emplace_back(""); // for matching the default/root
76  }
77 
78  std::string RobotNameHelper::select(const std::string& path) const
79  {
81  Node node = root;
82  for (const std::string& p : Split(path, "/"))
83  {
84  node = node.select(p, profiles);
85  }
86  if (!node.isValid())
87  {
88  std::stringstream s;
89  s << "RobotNameHelper::select: path " + path + " not found\nrobotInfo:\n";
90  writeRobotInfoNode(robotInfo, s);
91  throw std::runtime_error(s.str());
92  }
93  return node.value();
94  }
95 
96  RobotNameHelperPtr RobotNameHelper::Create(const RobotInfoNodePtr& robotInfo,
97  const StatechartProfilePtr& profile)
98  {
99  return RobotNameHelperPtr(new RobotNameHelper(robotInfo, profile));
100  }
101 
102  RobotNameHelperPtr RobotNameHelper::Create(const std::string& robotXmlFilename,
103  const StatechartProfilePtr& profile)
104  {
105  RapidXmlReaderPtr reader = RapidXmlReader::FromFile(robotXmlFilename);
106  RapidXmlReaderNode robotNode = reader->getRoot("Robot");
107 
108  return Create(readRobotInfo(robotNode.first_node("RobotInfo")), profile);
109  }
110 
111  RobotInfoNodePtr RobotNameHelper::readRobotInfo(const RapidXmlReaderNode& infoNode)
112  {
113  const std::string name = infoNode.name();
114  const std::string profile = infoNode.attribute_value_or_default("profile", "");
115  const std::string value = infoNode.attribute_value_or_default("value", "");
116 
117  const auto nodes = infoNode.nodes();
118 
119  std::vector<RobotInfoNodePtr> children;
120  children.reserve(nodes.size());
121 
122  std::transform(nodes.begin(),
123  nodes.end(),
124  std::back_inserter(children),
125  [](const auto & childNode)
126  {
127  return readRobotInfo(childNode);
128  });
129 
130  return new RobotInfoNode(name, profile, value, children);
131  }
132 
133  Arm RobotNameHelper::getArm(const std::string& side) const
134  {
135  return Arm(shared_from_this(), side);
136  }
137 
138  RobotArm
139  RobotNameHelper::getRobotArm(const std::string& side, const VirtualRobot::RobotPtr& robot) const
140  {
141  return RobotArm(Arm(shared_from_this(), side), robot);
142  }
143 
144  std::shared_ptr<const RobotNameHelper> Arm::getRobotNameHelper() const
145  {
146  return rnh;
147  }
148 
149  RobotNameHelper::Node::Node(const RobotInfoNodePtr& robotInfo) : robotInfo(robotInfo) {}
150 
152  {
153  checkValid();
154  return robotInfo->value;
155  }
156 
158  const std::vector<std::string>& profiles)
159  {
160  ARMARX_TRACE;
161  if (!isValid())
162  {
163  return Node(nullptr);
164  }
165  std::map<std::string, RobotInfoNodePtr> matches;
166  for (const RobotInfoNodePtr& child : robotInfo->children)
167  {
168  if (child->name == name)
169  {
170  matches[child->profile] = child;
171  }
172  }
173  for (const std::string& p : profiles)
174  {
175  if (matches.count(p))
176  {
177  return matches.at(p);
178  }
179  }
180  return Node(nullptr);
181  }
182 
184  {
185  return robotInfo ? true : false;
186  }
187 
189  {
190  if (!isValid())
191  {
192 
193  std::stringstream s;
194  s << "RobotNameHelper::Node nullptr exception\nrobotInfo:\n";
195  writeRobotInfoNode(robotInfo, s);
196  throw std::runtime_error(s.str());
197  }
198  }
199 
200  std::string Arm::getSide() const
201  {
202  return side;
203  }
204 
205  std::string Arm::getKinematicChain() const
206  {
207  ARMARX_TRACE;
208  return select("KinematicChain");
209  }
210 
211  std::string Arm::getTorsoKinematicChain() const
212  {
213  ARMARX_TRACE;
214  return select("TorsoKinematicChain");
215  }
216 
217  std::string Arm::getTCP() const
218  {
219  ARMARX_TRACE;
220  return select("TCP");
221  }
222 
223  std::string Arm::getForceTorqueSensor() const
224  {
225  ARMARX_TRACE;
226  return select("ForceTorqueSensor");
227  }
228 
229  std::string Arm::getForceTorqueSensorFrame() const
230  {
231  ARMARX_TRACE;
232  return select("ForceTorqueSensorFrame");
233  }
234 
235  std::string Arm::getEndEffector() const
236  {
237  ARMARX_TRACE;
238  return select("EndEffector");
239  }
240 
241  std::string Arm::getMemoryHandName() const
242  {
243  ARMARX_TRACE;
244  return select("MemoryHandName");
245  }
246 
247  std::string Arm::getHandControllerName() const
248  {
249  ARMARX_TRACE;
250  return select("HandControllerName");
251  }
252 
253  std::string Arm::getHandUnitName() const
254  {
255  ARMARX_TRACE;
256  return select("HandUnitName");
257  }
258 
259  std::string Arm::getHandRootNode() const
260  {
261  ARMARX_TRACE;
262  return select("HandRootNode");
263  }
264 
265  std::string Arm::getHandModelPath() const
266  {
267  ARMARX_TRACE;
268  return select("HandModelPath");
269  }
270 
271  std::string Arm::getAbsoluteHandModelPath() const
272  {
274  auto path = getHandModelPath();
275  return ArmarXDataPath::getAbsolutePath(path, path) ? path : "";
276  }
277 
278  std::string Arm::getHandModelPackage() const
279  {
280  ARMARX_TRACE;
281  return select("HandModelPackage");
282  }
283 
284  std::string Arm::getPalmCollisionModel() const
285  {
286  ARMARX_TRACE;
287  return select("PalmCollisionModel");
288  }
289 
290  std::string Arm::getFullHandCollisionModel() const
291  {
292  ARMARX_TRACE;
293  return select("FullHandCollisionModel");
294  }
295 
296  RobotArm
298  {
299  ARMARX_TRACE;
300  return RobotArm(*this, robot);
301  }
302 
303  Arm::Arm(const std::shared_ptr<const RobotNameHelper>& rnh,
304  const std::string& side) :
305  rnh(rnh), side(side)
306  {
307  }
308 
309  std::string Arm::select(const std::string& path) const
310  {
311  ARMARX_TRACE;
312  return rnh->select(side + "Arm/" + path);
313  }
314 
315  std::string RobotArm::getSide() const
316  {
317  ARMARX_TRACE;
318  return arm.getSide();
319  }
320 
321  VirtualRobot::RobotNodeSetPtr RobotArm::getKinematicChain() const
322  {
323  ARMARX_TRACE;
324  ARMARX_CHECK_NOT_NULL(robot);
325  return robot->getRobotNodeSet(arm.getKinematicChain());
326  }
327 
328  VirtualRobot::RobotNodeSetPtr RobotArm::getTorsoKinematicChain() const
329  {
330  ARMARX_TRACE;
331  ARMARX_CHECK_NOT_NULL(robot);
332  return robot->getRobotNodeSet(arm.getTorsoKinematicChain());
333  }
334 
335  VirtualRobot::RobotNodePtr RobotArm::getTCP() const
336  {
337  ARMARX_TRACE;
338  ARMARX_CHECK_NOT_NULL(robot);
339  return robot->getRobotNode(arm.getTCP());
340  }
341 
342  VirtualRobot::RobotNodePtr RobotArm::getPalmCollisionModel() const
343  {
344  ARMARX_TRACE;
345  ARMARX_CHECK_NOT_NULL(robot);
346  return robot->getRobotNode(arm.getPalmCollisionModel());
347  }
348 
349  VirtualRobot::TriMeshModelPtr RobotArm::getFullHandCollisionModel() const
350  {
351  ARMARX_TRACE;
352  std::string abs;
355  return VirtualRobot::TriMeshModel::FromFile(abs);
356  }
357 
358  VirtualRobot::RobotNodePtr RobotArm::getHandRootNode() const
359  {
360  ARMARX_TRACE;
361  ARMARX_CHECK_NOT_NULL(robot);
362  return robot->getRobotNode(arm.getHandRootNode());
363  }
364 
366  {
367  ARMARX_TRACE;
368  const auto tcp = getTCP();
370  const auto hand = getHandRootNode();
371  ARMARX_CHECK_NOT_NULL(hand);
372  return tcp->getPoseInRootFrame().inverse() * hand->getPoseInRootFrame();
373  }
374 
376  {
377  return robot;
378  }
379 
380  const Arm& RobotArm::getArm() const
381  {
382  return arm;
383  }
384 
385  RobotArm::RobotArm(const Arm& arm, const VirtualRobot::RobotPtr& robot) :
386  arm(arm), robot(robot)
387  {
388  ARMARX_CHECK_NOT_NULL(robot);
389  }
390 
391  const std::vector<std::string>& RobotNameHelper::getProfiles() const
392  {
393  return profiles;
394  }
395 } // namespace armarx
armarx::RobotNameHelper::LocationLeft
static const std::string LocationLeft
Definition: RobotNameHelper.h:171
armarx::RapidXmlReaderPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
Definition: RapidXmlReader.h:66
armarx::RapidXmlReader::FromFile
static RapidXmlReaderPtr FromFile(const std::string &path)
Definition: RapidXmlReader.h:497
armarx::RobotNameHelper::Node::select
Node select(const std::string &name, const std::vector< std::string > &profiles)
Definition: RobotNameHelper.cpp:157
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::Arm::getMemoryHandName
std::string getMemoryHandName() const
Definition: RobotNameHelper.cpp:241
armarx::Arm::getFullHandCollisionModel
std::string getFullHandCollisionModel() const
Definition: RobotNameHelper.cpp:290
armarx::Arm::getAbsoluteHandModelPath
std::string getAbsoluteHandModelPath() const
Definition: RobotNameHelper.cpp:271
armarx::Arm::addRobot
RobotArm addRobot(const VirtualRobot::RobotPtr &robot) const
Definition: RobotNameHelper.cpp:297
armarx::RobotArm::RobotArm
RobotArm()=default
armarx::Arm::getPalmCollisionModel
std::string getPalmCollisionModel() const
Definition: RobotNameHelper.cpp:284
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:829
armarx::Arm::getHandRootNode
std::string getHandRootNode() const
Definition: RobotNameHelper.cpp:259
armarx::Arm::getHandModelPackage
std::string getHandModelPackage() const
Definition: RobotNameHelper.cpp:278
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:315
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
trace.h
armarx::Arm::getHandUnitName
std::string getHandUnitName() const
Definition: RobotNameHelper.cpp:253
armarx::StatechartProfilePtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
Definition: StatechartContext.h:52
armarx::RobotNameHelper::Node::Node
Node(const RobotInfoNodePtr &robotInfo)
Definition: RobotNameHelper.cpp:149
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::RobotNameHelper::LocationRight
static const std::string LocationRight
Definition: RobotNameHelper.h:172
armarx::RobotArm::getTorsoKinematicChain
VirtualRobot::RobotNodeSetPtr getTorsoKinematicChain() const
Definition: RobotNameHelper.cpp:328
armarx::RobotNameHelper::getProfiles
const std::vector< std::string > & getProfiles() const
Definition: RobotNameHelper.cpp:391
armarx::RobotArm::getFullHandCollisionModel
VirtualRobot::TriMeshModelPtr getFullHandCollisionModel() const
Definition: RobotNameHelper.cpp:349
armarx::RobotNameHelper::select
std::string select(const std::string &path) const
Definition: RobotNameHelper.cpp:78
armarx::Arm::getTCP
std::string getTCP() const
Definition: RobotNameHelper.cpp:217
StringHelpers.h
armarx::RobotNameHelper::writeRobotInfoNode
static void writeRobotInfoNode(const RobotInfoNodePtr &n, std::ostream &str, std::size_t indent=0, char indentChar=' ')
Definition: RobotNameHelper.cpp:39
armarx::RobotNameHelper::getRobotArm
RobotArm getRobotArm(const std::string &side, const VirtualRobot::RobotPtr &robot) const
Definition: RobotNameHelper.cpp:139
armarx::Arm::getTorsoKinematicChain
std::string getTorsoKinematicChain() const
Definition: RobotNameHelper.cpp:211
armarx::RapidXmlReaderNode::name
std::string name() const
Definition: RapidXmlReader.h:349
armarx::Arm::getForceTorqueSensor
std::string getForceTorqueSensor() const
Definition: RobotNameHelper.cpp:223
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::Arm::getEndEffector
std::string getEndEffector() const
Definition: RobotNameHelper.cpp:235
armarx::RobotArm::getHandRootNode
VirtualRobot::RobotNodePtr getHandRootNode() const
Definition: RobotNameHelper.cpp:358
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::RobotNameHelper::getRobotInfoNodePtr
const RobotInfoNodePtr & getRobotInfoNodePtr() const
Definition: RobotNameHelper.cpp:56
armarx::RobotNameHelperPtr
std::shared_ptr< class RobotNameHelper > RobotNameHelperPtr
Definition: RobotNameHelper.h:34
armarx::RobotArm::getTCP
VirtualRobot::RobotNodePtr getTCP() const
Definition: RobotNameHelper.cpp:335
armarx::RobotArm::getTcp2HandRootTransform
Eigen::Matrix4f getTcp2HandRootTransform() const
Definition: RobotNameHelper.cpp:365
armarx::Arm::getRobotNameHelper
std::shared_ptr< const RobotNameHelper > getRobotNameHelper() const
Definition: RobotNameHelper.cpp:144
armarx::RobotNameHelper::Node::isValid
bool isValid()
Definition: RobotNameHelper.cpp:183
armarx::RobotArm::getPalmCollisionModel
VirtualRobot::RobotNodePtr getPalmCollisionModel() const
Definition: RobotNameHelper.cpp:342
armarx::RobotArm::getKinematicChain
VirtualRobot::RobotNodeSetPtr getKinematicChain() const
Definition: RobotNameHelper.cpp:321
StatechartProfiles.h
armarx::Arm::Arm
Arm()=default
armarx::RobotNameHelper::RobotArm
armarx::RobotArm RobotArm
Definition: RobotNameHelper.h:153
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:68
armarx::RobotNameHelper::Node::value
std::string value()
Definition: RobotNameHelper.cpp:151
armarx::Arm::getKinematicChain
std::string getKinematicChain() const
Definition: RobotNameHelper.cpp:205
armarx::Arm::getHandModelPath
std::string getHandModelPath() const
Definition: RobotNameHelper.cpp:265
armarx::RobotArm
Definition: RobotNameHelper.h:103
armarx::RobotNameHelper::RobotNameHelper
RobotNameHelper(const RobotInfoNodePtr &robotInfo, const StatechartProfilePtr &profile=nullptr)
Definition: RobotNameHelper.cpp:64
armarx::RobotArm::getRobot
const VirtualRobot::RobotPtr & getRobot() const
Definition: RobotNameHelper.cpp:375
ExpressionException.h
armarx::ArmarXDataPath::SearchReadableFile
static bool SearchReadableFile(const std::string &querryFileName, std::string &resultFileName, bool verbose=true)
Definition: ArmarXDataPath.cpp:218
CMakePackageFinder.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
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:152
armarx::Arm::select
std::string select(const std::string &path) const
Definition: RobotNameHelper.cpp:309
armarx::skills::gui::matches
bool matches(std::string skillName, std::vector< std::string > &searches)
Definition: SkillManagerWrapper.cpp:59
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:315
armarx::Arm::getSide
std::string getSide() const
Definition: RobotNameHelper.cpp:200
armarx::RapidXmlReaderNode::nodes
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
Definition: RapidXmlReader.h:158
armarx::RobotNameHelper::Create
static RobotNameHelperPtr Create(const RobotInfoNodePtr &robotInfo, const StatechartProfilePtr &profile)
Definition: RobotNameHelper.cpp:96
armarx::Arm
Definition: RobotNameHelper.h:41
armarx::RobotNameHelper::Node
Definition: RobotNameHelper.h:154
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:111
armarx::RobotArm::getArm
const Arm & getArm() const
Definition: RobotNameHelper.cpp:380
ArmarXDataPath.h
armarx::RapidXmlReaderNode::first_node
RapidXmlReaderNode first_node(const char *name=nullptr) const
Definition: RapidXmlReader.h:140
armarx::Arm::getForceTorqueSensorFrame
std::string getForceTorqueSensorFrame() const
Definition: RobotNameHelper.cpp:229
armarx::RobotNameHelper::Node::checkValid
void checkValid()
Definition: RobotNameHelper.cpp:188
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:28
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:225
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::RobotNameHelper::getArm
Arm getArm(const std::string &side) const
Definition: RobotNameHelper.cpp:133
armarx::Arm::getHandControllerName
std::string getHandControllerName() const
Definition: RobotNameHelper.cpp:247