Go to the documentation of this file.00001 #include "robodyn_controllers/MobileTreeIk.h"
00002
00003 const int MobileTreeIk::BASE = -1;
00004
00005 MobileTreeIk::MobileTreeIk()
00006 : KdlTreeTr(), mobileJoints(6)
00007 {
00008 }
00009
00010 MobileTreeIk::~MobileTreeIk()
00011 {
00012 }
00013
00014 void MobileTreeIk::setBases(const std::vector<std::string>& basesIn)
00015 {
00016 if (basesIn.size() < 1)
00017 {
00018 std::stringstream err;
00019 err << "MobileTreeIk setBases must get at least one base" << std::endl;
00020 NasaCommonLogging::Logger::log("gov.nasa.controllers.MobileTreeIk", log4cpp::Priority::ERROR, err.str());
00021 throw std::runtime_error(err.str());
00022 return;
00023 }
00024
00025 for (unsigned int i = 0; i < basesIn.size(); ++i)
00026 {
00027 if (!hasSegment(basesIn[i]))
00028 {
00029 std::stringstream err;
00030 err << "MobileTreeIk setBases couldn't find segment " << basesIn[i];
00031 NasaCommonLogging::Logger::log("gov.nasa.controllers.MobileTreeIk", log4cpp::Priority::ERROR, err.str());
00032 throw std::runtime_error(err.str());
00033 return;
00034 }
00035 }
00036
00037 bases = basesIn;
00038 }
00039
00040 void MobileTreeIk::resetMobileJoints()
00041 {
00042 mobileJoints = KDL::JntArray(6);
00043 }
00044
00045 void MobileTreeIk::getJointPositions(const KDL::JntArray& jointsIn, const std::vector<std::string>& nodeNames,
00046 const std::vector<KDL::Frame>& nodeFrames, KDL::JntArray& jointsOut,
00047 const std::vector<KdlTreeIk::NodePriority>& nodePriorities)
00048 {
00049
00050 KDL::JntArray allJoints(jointsIn.rows() + mobileJoints.rows());
00051 allJoints.data.segment(mobileJoints.rows(), jointsIn.rows()) = jointsIn.data;
00052
00053 for (unsigned int i = 0; i < mobileJoints.rows(); ++i)
00054 {
00055 allJoints.data[i] = mobileJoints(i);
00056 }
00057
00058 std::vector<KDL::Frame> frames = nodeFrames;
00059
00060
00061 for (unsigned int i = 0; i < bases.size(); ++i)
00062 {
00063 KDL::Frame baseFrame;
00064 fkPosSolverPtr->getPose(allJoints, bases[i], baseFrame);
00065 frames.insert(frames.begin(), baseFrame);
00066 }
00067
00068 std::vector<KdlTreeIk::NodePriority> priorities = nodePriorities;
00069
00070 std::vector<std::string> nodes = nodeNames;
00071
00072 for (unsigned int i = 0; i < bases.size(); ++i)
00073 {
00074 nodes.insert(nodes.begin(), bases[i]);
00075 KDL::Twist priority;
00076
00077 for (unsigned int j = 0; j < mobileJoints.rows(); ++j)
00078 {
00079 priority[j] = MobileTreeIk::BASE;
00080 }
00081
00082 priorities.insert(priorities.begin(), priority);
00083 }
00084
00085 KDL::JntArray allJoints_out(allJoints.rows());
00086 KdlTreeTr::getJointPositions(allJoints, nodes, frames, allJoints_out, priorities);
00087
00088
00089 for (unsigned int i = 0; i < jointsOut.rows(); ++i)
00090 {
00091 jointsOut(i) = allJoints_out(i + mobileJoints.rows());
00092 }
00093
00094 for (unsigned int i = 0; i < mobileJoints.rows(); ++i)
00095 {
00096 mobileJoints(i) = allJoints_out(i);
00097 }
00098 }
00099
00100 void MobileTreeIk::initialize()
00101 {
00102 robotModelBase = KdlTreeTr::getBaseName();
00103 bases.push_back(robotModelBase);
00104
00105 KDL::Tree newTree("world_base");
00106 KDL::Chain baseChain;
00107
00108
00109 resetMobileJoints();
00110 baseChain.addSegment(KDL::Segment("baseX", KDL::Joint("baseX", KDL::Joint::TransX)));
00111 baseChain.addSegment(KDL::Segment("baseY", KDL::Joint("baseY", KDL::Joint::TransY)));
00112 baseChain.addSegment(KDL::Segment("baseZ", KDL::Joint("baseZ", KDL::Joint::TransZ)));
00113 baseChain.addSegment(KDL::Segment("baseRoll", KDL::Joint("baseRoll", KDL::Joint::RotX)));
00114 baseChain.addSegment(KDL::Segment("basePitch", KDL::Joint("basePitch", KDL::Joint::RotY)));
00115 baseChain.addSegment(KDL::Segment("baseYaw", KDL::Joint("baseYaw", KDL::Joint::RotZ)));
00116 baseChain.addSegment(KDL::Segment(robotModelBase));
00117
00118 newTree.addChain(baseChain, "world_base");
00119 newTree.addTree(tree, robotModelBase);
00120 tree = newTree;
00121
00122 KdlTreeTr::initialize();
00123 }
00124
00125 void MobileTreeIk::getJointNames(std::vector<std::string>& jointNames) const
00126 {
00127 jointNames.resize(tree.getNrOfJoints() - mobileJoints.rows());
00128 const KDL::SegmentMap& segments = tree.getSegments();
00129
00130 for (KDL::SegmentMap::const_iterator segIt = segments.begin(); segIt != segments.end(); ++segIt)
00131 {
00132 if (segIt->second.segment.getJoint().getType() != KDL::Joint::None)
00133 {
00134
00135
00136 if (segIt->second.q_nr > mobileJoints.rows() - 1)
00137 {
00138 jointNames.at(segIt->second.q_nr - mobileJoints.rows()) = segIt->second.segment.getJoint().getName();
00139 }
00140 }
00141 }
00142 }
00143
00144 void MobileTreeIk::getFrames (const KDL::JntArray& joints_in, std::map<std::string, KDL::Frame>& frameMap)
00145 {
00146 KDL::JntArray mobile_joints_in(joints_in.rows() + mobileJoints.rows());
00147
00148 for (unsigned int i = 0; i < joints_in.rows(); ++i)
00149 {
00150 mobile_joints_in(i + mobileJoints.rows()) = joints_in(i);
00151 }
00152
00153 for (unsigned int i = 0; i < mobileJoints.rows(); ++i)
00154 {
00155 mobile_joints_in(i) = mobileJoints(i);
00156 }
00157
00158 KdlTreeIk::getFrames(mobile_joints_in, frameMap);
00159 }
00160