forward_kinematics.cpp
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   // Moving joints
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   // Fixed joints. These are not needed for the purposes of drawing a marker
00048   // because the fixed joints are read by Builder::Init().
00049   // However, this may be useful code to repurpose elsewhere.
00050   //
00051   // for (std::map<std::string, SegmentPair>::const_iterator seg =
00052   //         segments_fixed_.begin();
00053   //     seg != segments_fixed_.end(); seg++) {
00054   //  geometry_msgs::TransformStamped tf_transform =
00055   //      tf2::kdlToTransform(seg->second.segment.pose(0));
00056   //  tf_transform.header.frame_id = seg->second.root;
00057   //  tf_transform.child_frame_id = seg->second.tip;
00058   //  transforms->push_back(tf_transform);
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       // if (model_.getJoint(child.getJoint().getName()) &&
00074       //    model_.getJoint(child.getJoint().getName())->type ==
00075       //        urdf::Joint::FLOATING) {
00076       //} else {
00077       //   segments_fixed_.insert(make_pair(child.getJoint().getName(), s));
00078       //}
00079     } else {
00080       segments_.insert(make_pair(child.getJoint().getName(), s));
00081     }
00082     AddChildren(children[i]);
00083   }
00084 }
00085 }  // namespace robot_markers


robot_markers
Author(s):
autogenerated on Sat Jun 8 2019 20:39:25