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
43namespace 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
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 {
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
218 {
219 return side;
220 }
221
222 std::string
224 {
226 return select("KinematicChain");
227 }
228
229 std::string
231 {
233 return select("TorsoKinematicChain");
234 }
235
236 std::string
238 {
240 return select("TCP");
241 }
242
243 std::string
245 {
247 return select("ForceTorqueSensor");
248 }
249
250 std::string
252 {
254 return select("ForceTorqueSensorFrame");
255 }
256
257 std::string
259 {
261 return select("EndEffector");
262 }
263
264 std::string
266 {
268 return select("MemoryHandName");
269 }
270
271 std::string
273 {
275 return select("HandControllerName");
276 }
277
278 std::string
280 {
282 return select("HandUnitName");
283 }
284
285 std::string
287 {
289 return select("HandRootNode");
290 }
291
292 std::string
294 {
296 return select("HandModelPath");
297 }
298
299 std::string
306
307 std::string
309 {
311 return select("HandModelPackage");
312 }
313
314 std::string
316 {
318 return select("PalmCollisionModel");
319 }
320
321 std::string
323 {
325 return select("FullHandCollisionModel");
326 }
327
330 {
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 {
344 return rnh->select(side + "Arm/" + path);
345 }
346
347 std::string
349 {
351 return arm.getSide();
352 }
353
354 VirtualRobot::RobotNodeSetPtr
356 {
359 return robot->getRobotNodeSet(arm.getKinematicChain());
360 }
361
362 VirtualRobot::RobotNodeSetPtr
364 {
367 return robot->getRobotNodeSet(arm.getTorsoKinematicChain());
368 }
369
370 VirtualRobot::RobotNodePtr
372 {
375 return robot->getRobotNode(arm.getTCP());
376 }
377
378 VirtualRobot::RobotNodePtr
380 {
383 return robot->getRobotNode(arm.getPalmCollisionModel());
384 }
385
386 VirtualRobot::TriMeshModelPtr
388 {
390 std::string abs;
392 ArmarXDataPath::SearchReadableFile(arm.getFullHandCollisionModel(), abs));
393 return VirtualRobot::TriMeshModel::FromFile(abs);
394 }
395
396 VirtualRobot::RobotNodePtr
398 {
401 return robot->getRobotNode(arm.getHandRootNode());
402 }
403
404 Eigen::Matrix4f
406 {
408 const auto tcp = getTCP();
410 const auto hand = getHandRootNode();
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 {
430 }
431
432 const std::vector<std::string>&
434 {
435 return profiles;
436 }
437} // namespace armarx
constexpr T c
std::string str(const T &t)
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
static bool SearchReadableFile(const std::string &querryFileName, std::string &resultFileName, bool verbose=true)
static bool FindPackageAndAddDataPath(const std::string &packageName)
Search for the package and add its data path if it was found.
RapidXmlReaderNode first_node(const char *name=nullptr) const
std::string name() const
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
std::string attribute_value_or_default(const char *attrName, const std::string &defaultValue) const
static RapidXmlReaderPtr FromFile(const std::string &path)
Node(const RobotInfoNodePtr &robotInfo)
Node select(const std::string &name, const std::vector< std::string > &profiles)
armarx::RobotArm RobotArm
static const std::string LocationLeft
const RobotInfoNodePtr & getRobotInfoNodePtr() const
Arm getArm(const std::string &side) const
static RobotNameHelperPtr Create(const RobotInfoNodePtr &robotInfo, const StatechartProfilePtr &profile)
RobotArm getRobotArm(const std::string &side, const VirtualRobot::RobotPtr &robot) const
static const std::string LocationRight
static void writeRobotInfoNode(const RobotInfoNodePtr &n, std::ostream &str, std::size_t indent=0, char indentChar=' ')
RobotNameHelper(const RobotInfoNodePtr &robotInfo, const StatechartProfilePtr &profile=nullptr)
const std::vector< std::string > & getProfiles() const
std::string select(const std::string &path) const
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#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...
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
std::shared_ptr< class StatechartProfile > StatechartProfilePtr
std::vector< T > abs(const std::vector< T > &v)
std::shared_ptr< class RobotNameHelper > RobotNameHelperPtr
std::string getHandRootNode() const
std::shared_ptr< const RobotNameHelper > getRobotNameHelper() const
std::string getSide() const
Arm()=default
std::string getTCP() const
std::string getAbsoluteHandModelPath() const
std::string getHandModelPath() const
std::string getKinematicChain() const
std::string getFullHandCollisionModel() const
std::string getTorsoKinematicChain() const
std::string getHandUnitName() const
std::string getPalmCollisionModel() const
std::string getEndEffector() const
std::string getForceTorqueSensor() const
std::string getForceTorqueSensorFrame() const
std::string getHandModelPackage() const
std::string getHandControllerName() const
RobotArm addRobot(const VirtualRobot::RobotPtr &robot) const
std::string select(const std::string &path) const
std::string getMemoryHandName() const
Eigen::Matrix4f getTcp2HandRootTransform() const
friend struct Arm
std::string getSide() const
VirtualRobot::RobotNodePtr getPalmCollisionModel() const
VirtualRobot::TriMeshModelPtr getFullHandCollisionModel() const
VirtualRobot::RobotNodeSetPtr getTorsoKinematicChain() const
const VirtualRobot::RobotPtr & getRobot() const
const Arm & getArm() const
VirtualRobot::RobotNodeSetPtr getKinematicChain() const
RobotArm()=default
VirtualRobot::RobotNodePtr getHandRootNode() const
VirtualRobot::RobotNodePtr getTCP() const
#define ARMARX_TRACE
Definition trace.h:77