My Project 1.0
Holistic Fusion: A Generic Framework for Multimodal Sensor Fusion
Loading...
Searching...
No Matches
StaticTransformsUrdf.cpp
Go to the documentation of this file.
1/*
2Copyright 2022 by Julian Nubert, Robotic Systems Lab, ETH Zurich.
3All rights reserved.
4This file is released under the "BSD-3-Clause License".
5Please see the LICENSE file that has been included as part of this package.
6 */
7
8// ROS
9#include <ros/ros.h>
10#include <tf/transform_listener.h>
11
12// Workspace
14
15namespace holistic_fusion {
16
17StaticTransformsUrdf::StaticTransformsUrdf(const std::shared_ptr<ros::NodeHandle> privateNodePtr) {
18 std::cout << YELLOW_START << "StaticTransformsUrdf" << GREEN_START << " Initializing..." << COLOR_END << std::endl;
19 privateNode_ = *privateNodePtr;
20}
21
23 std::cout << YELLOW_START << "StaticTransformsUrdf" << GREEN_START << " Setting up for description name '" << urdfDescriptionName_ << "'."
24 << COLOR_END << std::endl;
25
26 // Check whether description contains information
27 std::string urdfDescriptionContent;
28 privateNode_.getParam(std::string("/") + urdfDescriptionName_, urdfDescriptionContent);
29 if (urdfDescriptionContent.empty()) {
30 throw std::runtime_error("StaticTransformsUrdf - Could not load description, description empty.");
31 }
32
33 // load excavator model from URDF
34 double timeStep;
36
37 // Initialize the KDL tree
38 if (!kdl_parser::treeFromUrdfModel(urdfModel_, tree_)) {
39 throw std::runtime_error("Failed to extract kdl tree from robot description");
40 }
41
42 KDL::SegmentMap segments_map = tree_.getSegments();
43 KDL::Chain chain;
44
45 // walk the tree and add segments to segments_
46 segments_.clear();
47 getRootTransformations(tree_.getRootSegment());
48
49 // Summary
50 std::cout << YELLOW_START << "StaticTransformsUrdf" << GREEN_START << " Initialized." << COLOR_END << std::endl;
51}
52
53void StaticTransformsUrdf::getRootTransformations(const KDL::SegmentMap::const_iterator element, std::string rootName) {
54 const std::string& elementName = GetTreeElementSegment(element->second).getName();
55 if (rootName == "") {
56 rootName = elementName;
57 }
58 // Iterate through children
59 std::vector<KDL::SegmentMap::const_iterator> children = GetTreeElementChildren(element->second);
60 for (unsigned int i = 0; i < children.size(); i++) {
61 // Go through children
62 const KDL::SegmentMap::const_iterator child = children[i];
63 // Get kinematic chain from current child to root
64 KDL::Chain chain;
65 tree_.getChain(rootName, child->second.segment.getName(), chain);
66 // Compute trafo to root
67 tf::Transform T_root_element = tf::Transform::getIdentity();
68 for (int i = 0; i < chain.getNrOfSegments(); ++i) {
69 KDL::Frame frameToRoot = chain.getSegment(i).getFrameToTip();
70 double x, y, z, w;
71 frameToRoot.M.GetQuaternion(x, y, z, w);
72 tf::Quaternion q(x, y, z, w);
73 tf::Vector3 t(frameToRoot.p[0], frameToRoot.p[1], frameToRoot.p[2]);
74 T_root_element = T_root_element * tf::Transform(q, t);
75 }
76 // Write into buffer
77 ElementToRoot elementToRoot(T_root_element, rootName, child->second.segment.getName());
78 // Insert into segments
79 segments_.insert(std::make_pair(child->second.segment.getName(), elementToRoot));
80 // Call recursively
81 getRootTransformations(child, rootName);
82 }
83}
84
85tf::Transform StaticTransformsUrdf::kdlToTransform(const KDL::Frame& k) {
86 tf::Transform tf_T;
87 tf_T.setOrigin(tf::Vector3(k.p.x(), k.p.y(), k.p.z()));
88 double qx, qy, qz, qw;
89 k.M.GetQuaternion(qx, qy, qz, qw);
90 tf_T.setRotation(tf::Quaternion(qx, qy, qz, qw));
91
92 return tf_T;
93}
94
95} // namespace holistic_fusion
#define COLOR_END
#define GREEN_START
#define YELLOW_START
std::map< std::string, ElementToRoot > segments_
A map of dynamic segment names to SegmentPair structures.
tf::Transform kdlToTransform(const KDL::Frame &k)
void getRootTransformations(const KDL::SegmentMap::const_iterator element, std::string rootName="")
StaticTransformsUrdf(const std::shared_ptr< ros::NodeHandle > privateNodePtr)