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::getHandRootNode() const
254  {
255  ARMARX_TRACE;
256  return select("HandRootNode");
257  }
258 
259  std::string Arm::getHandModelPath() const
260  {
261  ARMARX_TRACE;
262  return select("HandModelPath");
263  }
264 
265  std::string Arm::getAbsoluteHandModelPath() const
266  {
268  auto path = getHandModelPath();
269  return ArmarXDataPath::getAbsolutePath(path, path) ? path : "";
270  }
271 
272  std::string Arm::getHandModelPackage() const
273  {
274  ARMARX_TRACE;
275  return select("HandModelPackage");
276  }
277 
278  std::string Arm::getPalmCollisionModel() const
279  {
280  ARMARX_TRACE;
281  return select("PalmCollisionModel");
282  }
283 
284  std::string Arm::getFullHandCollisionModel() const
285  {
286  ARMARX_TRACE;
287  return select("FullHandCollisionModel");
288  }
289 
290  RobotArm
292  {
293  ARMARX_TRACE;
294  return RobotArm(*this, robot);
295  }
296 
297  Arm::Arm(const std::shared_ptr<const RobotNameHelper>& rnh,
298  const std::string& side) :
299  rnh(rnh), side(side)
300  {
301  }
302 
303  std::string Arm::select(const std::string& path) const
304  {
305  ARMARX_TRACE;
306  return rnh->select(side + "Arm/" + path);
307  }
308 
309  std::string RobotArm::getSide() const
310  {
311  ARMARX_TRACE;
312  return arm.getSide();
313  }
314 
315  VirtualRobot::RobotNodeSetPtr RobotArm::getKinematicChain() const
316  {
317  ARMARX_TRACE;
318  ARMARX_CHECK_NOT_NULL(robot);
319  return robot->getRobotNodeSet(arm.getKinematicChain());
320  }
321 
322  VirtualRobot::RobotNodeSetPtr RobotArm::getTorsoKinematicChain() const
323  {
324  ARMARX_TRACE;
325  ARMARX_CHECK_NOT_NULL(robot);
326  return robot->getRobotNodeSet(arm.getTorsoKinematicChain());
327  }
328 
329  VirtualRobot::RobotNodePtr RobotArm::getTCP() const
330  {
331  ARMARX_TRACE;
332  ARMARX_CHECK_NOT_NULL(robot);
333  return robot->getRobotNode(arm.getTCP());
334  }
335 
336  VirtualRobot::RobotNodePtr RobotArm::getPalmCollisionModel() const
337  {
338  ARMARX_TRACE;
339  ARMARX_CHECK_NOT_NULL(robot);
340  return robot->getRobotNode(arm.getPalmCollisionModel());
341  }
342 
343  VirtualRobot::TriMeshModelPtr RobotArm::getFullHandCollisionModel() const
344  {
345  ARMARX_TRACE;
346  std::string abs;
349  return VirtualRobot::TriMeshModel::FromFile(abs);
350  }
351 
352  VirtualRobot::RobotNodePtr RobotArm::getHandRootNode() const
353  {
354  ARMARX_TRACE;
355  ARMARX_CHECK_NOT_NULL(robot);
356  return robot->getRobotNode(arm.getHandRootNode());
357  }
358 
360  {
361  ARMARX_TRACE;
362  const auto tcp = getTCP();
364  const auto hand = getHandRootNode();
365  ARMARX_CHECK_NOT_NULL(hand);
366  return tcp->getPoseInRootFrame().inverse() * hand->getPoseInRootFrame();
367  }
368 
370  {
371  return robot;
372  }
373 
374  const Arm& RobotArm::getArm() const
375  {
376  return arm;
377  }
378 
379  RobotArm::RobotArm(const Arm& arm, const VirtualRobot::RobotPtr& robot) :
380  arm(arm), robot(robot)
381  {
382  ARMARX_CHECK_NOT_NULL(robot);
383  }
384 
385  const std::vector<std::string>& RobotNameHelper::getProfiles() const
386  {
387  return profiles;
388  }
389 } // namespace armarx
armarx::RobotNameHelper::LocationLeft
static const std::string LocationLeft
Definition: RobotNameHelper.h:169
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:284
armarx::Arm::getAbsoluteHandModelPath
std::string getAbsoluteHandModelPath() const
Definition: RobotNameHelper.cpp:265
armarx::Arm::addRobot
RobotArm addRobot(const VirtualRobot::RobotPtr &robot) const
Definition: RobotNameHelper.cpp:291
armarx::RobotArm::RobotArm
RobotArm()=default
armarx::Arm::getPalmCollisionModel
std::string getPalmCollisionModel() const
Definition: RobotNameHelper.cpp:278
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:253
armarx::Arm::getHandModelPackage
std::string getHandModelPackage() const
Definition: RobotNameHelper.cpp:272
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:309
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::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:170
armarx::RobotArm::getTorsoKinematicChain
VirtualRobot::RobotNodeSetPtr getTorsoKinematicChain() const
Definition: RobotNameHelper.cpp:322
armarx::RobotNameHelper::getProfiles
const std::vector< std::string > & getProfiles() const
Definition: RobotNameHelper.cpp:385
armarx::RobotArm::getFullHandCollisionModel
VirtualRobot::TriMeshModelPtr getFullHandCollisionModel() const
Definition: RobotNameHelper.cpp:343
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:352
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:329
armarx::RobotArm::getTcp2HandRootTransform
Eigen::Matrix4f getTcp2HandRootTransform() const
Definition: RobotNameHelper.cpp:359
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:336
armarx::RobotArm::getKinematicChain
VirtualRobot::RobotNodeSetPtr getKinematicChain() const
Definition: RobotNameHelper.cpp:315
StatechartProfiles.h
armarx::Arm::Arm
Arm()=default
armarx::RobotNameHelper::RobotArm
armarx::RobotArm RobotArm
Definition: RobotNameHelper.h:151
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:259
armarx::RobotArm
Definition: RobotNameHelper.h:101
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:369
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:150
armarx::Arm::select
std::string select(const std::string &path) const
Definition: RobotNameHelper.cpp:303
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:152
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:374
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