void printKDLchain(std::string s,const KDL::Chain & kdlChain) { std::cout << s << std::endl; for(int p=0; p < (int)kdlChain.getNrOfSegments(); p++) { std::cout << "Segment " << p << ":" << std::endl; KDL::Segment seg = kdlChain.getSegment(p); KDL::Frame fra = seg.getFrameToTip(); std::cout << seg << std::endl; std::cout << fra << std::endl; } }
int main() { KDLCollada kdlCollada; vector <KDL::Chain> kinematicsModels; const string filename = "puma.dae"; // loading collada kinematics model and converting it to kdl serial chain if (!kdlCollada.load(COLLADA_MODELS_PATH + filename, kinematicsModels)) { cout << "Failed to import " << filename; return 0; } cout << "Imported " << kinematicsModels.size() << " kinematics chains" << endl; for (unsigned int i = 0; i < kinematicsModels.size(); i++) // parsing output kdl serail chain { KDL::Chain chain = kinematicsModels[i]; cout << "Chain " << i << " has " << chain.getNrOfSegments() << " segments" << endl; for (unsigned int u = 0; u < chain.getNrOfSegments(); u++) { KDL::Segment segment = chain.segments[u]; string segmentName = segment.getName(); cout << "Segment " << segmentName << " :" <<endl; KDL::Frame f_tip = segment.getFrameToTip(); KDL::Vector rotAxis = f_tip.M.GetRot(); double rotAngle = f_tip.M.GetRotAngle(rotAxis); KDL::Vector trans = f_tip.p; cout << " frame: rotation " << rotAxis.x() << " " << rotAxis.y() << " " << rotAxis.z() << " " << rotAngle * 180 / M_PI << endl; cout << " frame: translation " << trans.x() << " " << trans.y() << " " << trans.z() << endl; KDL::RigidBodyInertia inertia = segment.getInertia(); KDL::Joint joint = segment.getJoint(); string jointName = joint.getName(); string jointType = joint.getTypeName(); KDL::Vector jointAxis = joint.JointAxis(); KDL::Vector jointOrigin = joint.JointOrigin(); cout << " joint name: " << jointName << endl; cout << " type: " << jointType << endl; cout << " axis: " << jointAxis.x() << " " <<jointAxis.y() << " " << jointAxis.z() << endl; cout << " origin: " << jointOrigin.x() << " " << jointOrigin.y() << " " << jointOrigin.z() << endl; } } return 0; }
static inline std::vector<YAML::Node> get_frame_tip_nodes(const KDL::Segment& seg) { std::vector<YAML::Node> frames; KDL::Frame frame = seg.getFrameToTip() * seg.getJoint().pose(0).Inverse(); // Set xyz if(!KDL::Equal(frame.p, KDL::Vector::Zero())) { YAML::Node xyz_frame; YAML::Node axis_angle_null; axis_angle_null["axis-angle"].push_back(get_vector3(1, 0, 0)); axis_angle_null["axis-angle"].push_back(0); xyz_frame["frame"].push_back(axis_angle_null); xyz_frame["frame"].push_back(get_vector3(frame.p)); frames.push_back(xyz_frame); } // Set rpy if(!KDL::Equal(frame.M, KDL::Rotation::Identity())) { std::vector<double> rpy(3); frame.M.GetRPY(rpy[0],rpy[1],rpy[2]); for (std::vector<double>::size_type i = rpy.size() - 1; i != (std::vector<double>::size_type) -1; i--) { if(rpy[i] != 0) { YAML::Node rpy_frame; YAML::Node axis_angle; KDL::Vector axis; axis[i] = 1; axis_angle["axis-angle"].push_back(get_vector3(axis)); axis_angle["axis-angle"].push_back(rpy[i]); rpy_frame["frame"].push_back(axis_angle); rpy_frame["frame"].push_back(get_vector3(0, 0, 0)); frames.push_back(rpy_frame); } } } return frames; }
bool addBaseTransformation(const KDL::Chain & old_chain, KDL::Chain & new_chain, KDL::Frame H_new_old) { new_chain = KDL::Chain(); for(unsigned int i=0; i<old_chain.getNrOfSegments(); i++) { KDL::Segment segm; segm = old_chain.getSegment(i); //if is not the first segment add normally the segment if( i != 0 ) { new_chain.addSegment(segm); } else { //otherwise modify the segment before adding it KDL::Segment new_segm; KDL::Joint new_joint, old_joint; old_joint = segm.getJoint(); KDL::Joint::JointType new_type; switch(old_joint.getType()) { case KDL::Joint::RotAxis: case KDL::Joint::RotX: case KDL::Joint::RotY: case KDL::Joint::RotZ: new_type = KDL::Joint::RotAxis; break; case KDL::Joint::TransAxis: case KDL::Joint::TransX: case KDL::Joint::TransY: case KDL::Joint::TransZ: new_type = KDL::Joint::TransAxis; break; case KDL::Joint::None: default: new_type = KDL::Joint::None; } //check ! new_joint = KDL::Joint(old_joint.getName(),H_new_old*old_joint.JointOrigin(),H_new_old.M*old_joint.JointAxis(),new_type); new_segm = KDL::Segment(segm.getName(),new_joint,H_new_old*segm.getFrameToTip(),segm.getInertia()); new_chain.addSegment(new_segm); } } return true; }
KDL::Frame getH_new_old(KDL::Segment seg) { KDL::Joint jnt = seg.getJoint(); KDL::Frame frameToTip = seg.getFrameToTip(); return getH_new_old(jnt,frameToTip); }