Robot6Axis.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2002     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 #endif
00028 
00029 #include <Base/Writer.h>
00030 #include <Base/Reader.h>
00031 
00032 #include "kdl_cp/chain.hpp"
00033 #include "kdl_cp/chainfksolver.hpp"
00034 #include "kdl_cp/chainfksolverpos_recursive.hpp"
00035 #include "kdl_cp/frames_io.hpp"
00036 #include "kdl_cp/chainiksolver.hpp"
00037 #include "kdl_cp/chainiksolvervel_pinv.hpp"
00038 #include "kdl_cp/chainjnttojacsolver.hpp"
00039 #include "kdl_cp/chainiksolverpos_nr.hpp"
00040 #include "kdl_cp/chainiksolverpos_nr_jl.hpp"
00041 
00042 #include "Robot6Axis.h"
00043 #include "RobotAlgos.h"
00044 
00045 #ifndef M_PI
00046     #define M_PI    3.14159265358979323846 /* pi */
00047 #endif
00048 
00049 #ifndef M_PI_2
00050     #define M_PI_2  1.57079632679489661923 /* pi/2 */
00051 #endif
00052 
00053 using namespace Robot;
00054 using namespace Base;
00055 using namespace KDL;
00056 
00057 // some default roboter
00058 
00059 AxisDefinition KukaIR500[6] = {
00060 //   a    ,alpha ,d    ,theta ,rotDir ,maxAngle ,minAngle ,AxisVelocity 
00061     {500  ,-90   ,1045 ,0     , -1    ,+185     ,-185     ,156         }, // Axis 1
00062     {1300 ,0     ,0    ,0     , 1     ,+35      ,-155     ,156         }, // Axis 2
00063     {55   ,+90   ,0    ,-90   , 1     ,+154     ,-130     ,156         }, // Axis 3
00064     {0    ,-90   ,-1025,0     , 1     ,+350     ,-350     ,330         }, // Axis 4
00065     {0    ,+90   ,0    ,0     , 1     ,+130     ,-130     ,330         }, // Axis 5
00066     {0    ,+180  ,-300 ,0     , 1     ,+350     ,-350     ,615         }  // Axis 6
00067 };
00068 
00069 
00070 TYPESYSTEM_SOURCE(Robot::Robot6Axis , Base::Persistence);
00071 
00072 Robot6Axis::Robot6Axis()
00073 {
00074     // create joint array for the min and max angel values of each joint
00075     Min = JntArray(6);
00076     Max = JntArray(6);
00077 
00078         // Create joint array
00079     Actuall = JntArray(6);
00080 
00081     // set to default kuka 500
00082     setKinematic(KukaIR500);
00083 }
00084 
00085 Robot6Axis::~Robot6Axis()
00086 {
00087 }
00088 
00089 
00090 void Robot6Axis::setKinematic(const AxisDefinition KinDef[6])
00091 {
00092         Chain temp;
00093 
00094 
00095     for(int i=0 ; i<6 ;i++){
00096         temp.addSegment(Segment(Joint(Joint::RotZ),Frame::DH(KinDef[i].a  ,KinDef[i].alpha * (M_PI/180) ,KinDef[i].d ,KinDef[i].theta * (M_PI/180)      )));
00097         RotDir  [i] = KinDef[i].rotDir;
00098         Max(i) = KinDef[i].maxAngle * (M_PI/180);
00099         Min(i) = KinDef[i].minAngle * (M_PI/180);
00100         Velocity[i] = KinDef[i].velocity;
00101     }
00102 
00103         // for now and testing
00104     Kinematic = temp;
00105 
00106         // get the actuall TCP out of tha axis
00107         calcTcp();
00108 }
00109 
00110 double Robot6Axis::getMaxAngle(int Axis)
00111 {
00112     return Max(Axis) * (180.0/M_PI);
00113 }
00114 
00115 double Robot6Axis::getMinAngle(int Axis)
00116 {
00117     return Min(Axis) * (180.0/M_PI);
00118 }
00119 
00120 void split(std::string const& string, const char delemiter, std::vector<std::string>& destination)
00121 {
00122     std::string::size_type  last_position(0);
00123     std::string::size_type  position(0);
00124          
00125     for (std::string::const_iterator it(string.begin()); it != string.end(); ++it, ++position)
00126     {
00127         if (*it == delemiter )
00128         {
00129             destination.push_back(string.substr(last_position, position - last_position ));
00130             last_position = position + 1;
00131         }
00132     }
00133     destination.push_back(string.substr(last_position, position - last_position ));
00134 }
00135 
00136 void Robot6Axis::readKinematic(const char * FileName)
00137 {
00138     char buf[120];
00139     std::ifstream in(FileName);
00140     if(!in)return;
00141     std::vector<std::string> destination;
00142     AxisDefinition temp[6];
00143 
00144     // over read the header
00145     in.getline(buf,119,'\n');
00146     // read 6 Axis
00147     for( int i = 0; i<6; i++){
00148         in.getline(buf,79,'\n');
00149         destination.clear();
00150         split(std::string(buf),',',destination);
00151         if(destination.size() < 8) continue;
00152         // transfer the values in kinematic structure
00153         temp[i].a        = atof(destination[0].c_str());
00154         temp[i].alpha    = atof(destination[1].c_str());
00155         temp[i].d        = atof(destination[2].c_str());
00156         temp[i].theta    = atof(destination[3].c_str());
00157         temp[i].rotDir   = atof(destination[4].c_str());
00158         temp[i].maxAngle = atof(destination[5].c_str());
00159         temp[i].minAngle = atof(destination[6].c_str());
00160         temp[i].velocity = atof(destination[7].c_str());
00161     }
00162 
00163     setKinematic(temp);
00164 
00165 }
00166 
00167 unsigned int Robot6Axis::getMemSize (void) const
00168 {
00169         return 0;
00170 }
00171 
00172 void Robot6Axis::Save (Writer &writer) const
00173 {
00174     for(unsigned int i=0;i<6;i++){
00175         Base::Placement Tip = toPlacement(Kinematic.getSegment(i).getFrameToTip());
00176             writer.Stream() << writer.ind() << "<Axis "
00177                         << "Px=\""          <<  Tip.getPosition().x  << "\" " 
00178                         << "Py=\""          <<  Tip.getPosition().y  << "\" "
00179                         << "Pz=\""          <<  Tip.getPosition().z  << "\" "
00180                                             << "Q0=\""          <<  Tip.getRotation()[0] << "\" "
00181                         << "Q1=\""          <<  Tip.getRotation()[1] << "\" "
00182                         << "Q2=\""          <<  Tip.getRotation()[2] << "\" "
00183                         << "Q3=\""          <<  Tip.getRotation()[3] << "\" "
00184                         << "rotDir=\""      <<  RotDir[i]                    << "\" "
00185                         << "maxAngle=\""    <<  Max(i)*(180.0/M_PI)  << "\" "
00186                         << "minAngle=\""    <<  Min(i)*(180.0/M_PI)  << "\" "
00187                         << "AxisVelocity=\""<<  Velocity[i]          << "\" "
00188                         << "Pos=\""         <<  Actuall(i)           << "\"/>"
00189                                                               
00190                         << std::endl;
00191     }
00192 
00193 }
00194 
00195 void Robot6Axis::Restore(XMLReader &reader)
00196 {
00197     Chain Temp;
00198     Base::Placement Tip;
00199 
00200     for(unsigned int i=0;i<6;i++){
00201         // read my Element
00202         reader.readElement("Axis");
00203         // get the value of the placement
00204         Tip =    Base::Placement(Base::Vector3d(reader.getAttributeAsFloat("Px"),
00205                                                 reader.getAttributeAsFloat("Py"),
00206                                                 reader.getAttributeAsFloat("Pz")),
00207                                  Base::Rotation(reader.getAttributeAsFloat("Q0"),
00208                                                 reader.getAttributeAsFloat("Q1"),
00209                                                 reader.getAttributeAsFloat("Q2"),
00210                                                 reader.getAttributeAsFloat("Q3")));
00211         Temp.addSegment(Segment(Joint(Joint::RotZ),toFrame(Tip)));
00212 
00213 
00214         if(reader.hasAttribute("rotDir"))
00215             Velocity[i] = reader.getAttributeAsFloat("rotDir");
00216         else
00217             Velocity[i] = 1.0;
00218         // read the axis constraints
00219         Min(i)  = reader.getAttributeAsFloat("maxAngle")* (M_PI/180);
00220         Max(i)  = reader.getAttributeAsFloat("minAngle")* (M_PI/180);
00221         if(reader.hasAttribute("AxisVelocity"))
00222             Velocity[i] = reader.getAttributeAsFloat("AxisVelocity");
00223         else
00224             Velocity[i] = 156.0;
00225         Actuall(i) = reader.getAttributeAsFloat("Pos");
00226     }
00227     Kinematic = Temp;
00228 
00229     calcTcp();
00230 
00231 }
00232 
00233 
00234 
00235 bool Robot6Axis::setTo(const Placement &To)
00236 {
00237         //Creation of the solvers:
00238         ChainFkSolverPos_recursive fksolver1(Kinematic);//Forward position solver
00239         ChainIkSolverVel_pinv iksolver1v(Kinematic);//Inverse velocity solver
00240         ChainIkSolverPos_NR_JL iksolver1(Kinematic,Min,Max,fksolver1,iksolver1v,100,1e-6);//Maximum 100 iterations, stop at accuracy 1e-6
00241          
00242         //Creation of jntarrays:
00243         JntArray result(Kinematic.getNrOfJoints());
00244          
00245         //Set destination frame
00246         Frame F_dest = Frame(KDL::Rotation::Quaternion(To.getRotation()[0],To.getRotation()[1],To.getRotation()[2],To.getRotation()[3]),KDL::Vector(To.getPosition()[0],To.getPosition()[1],To.getPosition()[2]));
00247          
00248         // solve
00249         if(iksolver1.CartToJnt(Actuall,F_dest,result) < 0)
00250                 return false;
00251         else{
00252                 Actuall = result;
00253                 Tcp = F_dest;
00254                 return true;
00255         }
00256 }
00257 
00258 Base::Placement Robot6Axis::getTcp(void)
00259 {
00260         double x,y,z,w;
00261         Tcp.M.GetQuaternion(x,y,z,w);
00262         return Base::Placement(Base::Vector3d(Tcp.p[0],Tcp.p[1],Tcp.p[2]),Base::Rotation(x,y,z,w));
00263 }
00264 
00265 bool Robot6Axis::calcTcp(void)
00266 {
00267     // Create solver based on kinematic chain
00268     ChainFkSolverPos_recursive fksolver = ChainFkSolverPos_recursive(Kinematic);
00269  
00270      // Create the frame that will contain the results
00271     KDL::Frame cartpos;    
00272  
00273     // Calculate forward position kinematics
00274     int kinematics_status;
00275     kinematics_status = fksolver.JntToCart(Actuall,cartpos);
00276     if(kinematics_status>=0){
00277         Tcp = cartpos;
00278                 return true;
00279     }else{
00280         return false;
00281     }
00282 }
00283 
00284 bool Robot6Axis::setAxis(int Axis,double Value)
00285 {
00286         Actuall(Axis) = RotDir[Axis] * Value * (M_PI/180); // degree to radiants
00287 
00288         return calcTcp();
00289 }
00290 
00291 double Robot6Axis::getAxis(int Axis)
00292 {
00293         return RotDir[Axis] * (Actuall(Axis)/(M_PI/180)); // radian to degree
00294 }
00295 

Generated on Wed Nov 23 19:00:36 2011 for FreeCAD by  doxygen 1.6.1