Go to the documentation of this file.00001 #include "robot_markers/forward_kinematics.h"
00002
00003 #include <string>
00004 #include <vector>
00005
00006 #include "kdl/frames_io.hpp"
00007 #include "kdl/tree.hpp"
00008 #include "kdl_parser/kdl_parser.hpp"
00009 #include "ros/ros.h"
00010 #include "ros/time.h"
00011 #include "tf2_kdl/tf2_kdl.h"
00012 #include "urdf/model.h"
00013
00014 namespace robot_markers {
00015 SegmentPair::SegmentPair(const KDL::Segment& p_segment,
00016 const std::string& p_root, const std::string& p_tip)
00017 : segment(p_segment), root(p_root), tip(p_tip) {}
00018
00019 ForwardKinematics::ForwardKinematics(const urdf::Model& model)
00020 : model_(model), tree_(), segments_() {}
00021
00022 void ForwardKinematics::Init() {
00023 kdl_parser::treeFromUrdfModel(model_, tree_);
00024 AddChildren(tree_.getRootSegment());
00025 }
00026
00027 void ForwardKinematics::GetTransforms(
00028 const std::map<std::string, double>& joint_positions,
00029 std::vector<geometry_msgs::TransformStamped>* transforms) const {
00030
00031 for (std::map<std::string, double>::const_iterator jnt =
00032 joint_positions.begin();
00033 jnt != joint_positions.end(); jnt++) {
00034 std::map<std::string, SegmentPair>::const_iterator seg =
00035 segments_.find(jnt->first);
00036 if (seg != segments_.end()) {
00037 geometry_msgs::TransformStamped tf_transform =
00038 tf2::kdlToTransform(seg->second.segment.pose(jnt->second));
00039 tf_transform.header.frame_id = seg->second.root;
00040 tf_transform.child_frame_id = seg->second.tip;
00041 transforms->push_back(tf_transform);
00042 } else {
00043 ROS_ERROR("Robot does not have joint \"%s\"", jnt->first.c_str());
00044 }
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 }
00061
00062 void ForwardKinematics::AddChildren(
00063 const KDL::SegmentMap::const_iterator segment) {
00064 const std::string& root = GetTreeElementSegment(segment->second).getName();
00065
00066 const std::vector<KDL::SegmentMap::const_iterator>& children =
00067 GetTreeElementChildren(segment->second);
00068 for (unsigned int i = 0; i < children.size(); i++) {
00069 const KDL::Segment& child = GetTreeElementSegment(children[i]->second);
00070 SegmentPair s(GetTreeElementSegment(children[i]->second), root,
00071 child.getName());
00072 if (child.getJoint().getType() == KDL::Joint::None) {
00073
00074
00075
00076
00077
00078
00079 } else {
00080 segments_.insert(make_pair(child.getJoint().getName(), s));
00081 }
00082 AddChildren(children[i]);
00083 }
00084 }
00085 }