chainiksolver_pos_nr_jl_mimic.cpp
Go to the documentation of this file.
00001 // Copyright  (C)  2007  Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00002 // Copyright  (C)  2008  Mikael Mayer
00003 // Copyright  (C)  2008  Julia Jesse
00004 
00005 // Version: 1.0
00006 // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00007 // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00008 // URL: http://www.orocos.org/kdl
00009 
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00023 
00024 // Modified to account for "mimic" joints, i.e. joints whose motion has a
00025 // linear relationship to that of another joint.
00026 // Copyright  (C)  2013  Sachin Chitta, Willow Garage
00027 
00028 #include "moveit/kdl_kinematics_plugin/chainiksolver_pos_nr_jl_mimic.hpp"
00029 #include <ros/console.h>
00030 
00031 namespace KDL
00032 {
00033 
00034 ChainIkSolverPos_NR_JL_Mimic::ChainIkSolverPos_NR_JL_Mimic(const Chain& _chain,
00035                                                            const JntArray& _q_min,
00036                                                            const JntArray& _q_max,
00037                                                            ChainFkSolverPos& _fksolver,
00038                                                            ChainIkSolverVel& _iksolver,
00039                                                            unsigned int _maxiter,
00040                                                            double _eps,
00041                                                            bool _position_ik)
00042   : chain(_chain),
00043     q_min(_q_min),
00044     q_min_mimic(chain.getNrOfJoints()),
00045     q_max(_q_max),
00046     q_max_mimic(chain.getNrOfJoints()),
00047     q_temp(chain.getNrOfJoints()),
00048     fksolver(_fksolver),
00049     iksolver(_iksolver),
00050     delta_q(_chain.getNrOfJoints()),
00051     maxiter(_maxiter),
00052     eps(_eps),
00053     position_ik(_position_ik)
00054 {
00055   mimic_joints.resize(chain.getNrOfJoints());
00056   for(std::size_t i=0; i < mimic_joints.size(); ++i)
00057   { 
00058     mimic_joints[i].reset(i);
00059   }
00060   ROS_DEBUG_NAMED("kdl","Limits");
00061   for(std::size_t i=0; i < q_min.rows(); ++i)
00062   { 
00063     ROS_DEBUG_NAMED("kdl","%ld: Min: %f, Max: %f", long(i), q_min(i), q_max(i));
00064   }
00065   ROS_DEBUG_NAMED("kdl"," ");
00066 }
00067 
00068 bool ChainIkSolverPos_NR_JL_Mimic::setMimicJoints(const std::vector<kdl_kinematics_plugin::JointMimic>& _mimic_joints)
00069 {
00070   if(_mimic_joints.size() != chain.getNrOfJoints())
00071   {
00072     ROS_ERROR_NAMED("kdl","Mimic Joint info should be same size as number of joints in chain: %d", chain.getNrOfJoints());
00073     return false;
00074   }
00075 
00076   for(std::size_t i=0; i < _mimic_joints.size(); ++i)
00077   {
00078     if(_mimic_joints[i].map_index >= chain.getNrOfJoints())
00079     {
00080       ROS_ERROR_NAMED("kdl","Mimic Joint index should be less than number of joints in chain: %d", chain.getNrOfJoints());
00081       return false;
00082     }
00083   }
00084   mimic_joints = _mimic_joints;
00085 
00086   //Note that q_min and q_max will be of size chain.getNrOfJoints() - num_mimic_joints
00087   //  qToqMimic(q_min,q_min_mimic);
00088   //  qToqMimic(q_max,q_max_mimic);
00089 
00090   ROS_DEBUG_NAMED("kdl","Set mimic joints");
00091   return true;
00092 }
00093 
00094 void ChainIkSolverPos_NR_JL_Mimic::qToqMimic(const JntArray& q, JntArray& q_result)
00095 {
00096   for(std::size_t i=0; i < chain.getNrOfJoints(); ++i)
00097   {
00098     q_result(i) = mimic_joints[i].offset + mimic_joints[i].multiplier * q(mimic_joints[i].map_index);
00099   }
00100 }
00101 
00102 void ChainIkSolverPos_NR_JL_Mimic::qMimicToq(const JntArray& q, JntArray& q_result)
00103 {
00104   for(std::size_t i=0; i < chain.getNrOfJoints(); ++i)
00105   {
00106     if(mimic_joints[i].active) //This is not a mimic joint
00107     {
00108       q_result(mimic_joints[i].map_index) = q(i);
00109     }
00110   }
00111 }
00112 
00113 int ChainIkSolverPos_NR_JL_Mimic::CartToJnt(const JntArray& q_init, const Frame& p_in, JntArray& q_out)
00114 {
00115   return CartToJntAdvanced(q_init,p_in,q_out,false);
00116 }
00117 
00118 int ChainIkSolverPos_NR_JL_Mimic::CartToJntAdvanced(const JntArray& q_init, const Frame& p_in, JntArray& q_out, bool lock_redundant_joints)
00119 {
00120   //  Note that q_init and q_out will be of size chain.getNrOfJoints()
00121   //  qToqMimic(q_init,q_temp);
00122 
00123   q_temp = q_init;
00124   ROS_DEBUG_STREAM_NAMED("kdl","Input:");
00125   for(std::size_t i=0; i < q_out.rows(); ++i)
00126     ROS_DEBUG_NAMED("kdl","%d: %f",(int) i,q_out(i));
00127 
00128   unsigned int i;
00129   for(i=0;i<maxiter;++i)
00130   {
00131     fksolver.JntToCart(q_temp,f);
00132     delta_twist = diff(f,p_in);
00133 
00134     if(position_ik)
00135     {
00136       if(fabs(delta_twist(0)) < eps && fabs(delta_twist(1)) < eps && fabs(delta_twist(2)) < eps)
00137         break;
00138     }
00139     else
00140     {
00141       if(Equal(delta_twist,Twist::Zero(),eps))
00142         break;
00143     }
00144 
00145     ROS_DEBUG_STREAM_NAMED("kdl","delta_twist");
00146     for(std::size_t i=0; i < 6; ++i)
00147       ROS_DEBUG_NAMED("kdl","%d: %f",(int) i, delta_twist(i));
00148 
00149     iksolver.CartToJnt(q_temp,delta_twist,delta_q);
00150 
00151     Add(q_temp,delta_q,q_temp);
00152 
00153     ROS_DEBUG_STREAM_NAMED("kdl","delta_q");
00154     for(std::size_t i=0; i < delta_q.rows(); ++i)
00155       ROS_DEBUG_NAMED("kdl","%d: %f",(int) i, delta_q(i));
00156 
00157     ROS_DEBUG_STREAM_NAMED("kdl","q_temp");
00158     for(std::size_t i=0; i < q_temp.rows(); ++i)
00159       ROS_DEBUG_NAMED("kdl","%d: %f",(int) i, q_temp(i));
00160 
00161     for(std::size_t j=0; j<q_min.rows(); ++j)
00162     {
00163       //      if(mimic_joints[j].active)
00164         if(q_temp(j) < q_min(j))
00165           q_temp(j) = q_min(j);
00166     }
00167     for(std::size_t j=0; j<q_max.rows(); ++j)
00168     {
00169       //      if(mimic_joints[j].active)
00170         if(q_temp(j) > q_max(j))
00171           q_temp(j) = q_max(j);
00172     }
00173 
00174     //    q_out = q_temp;
00175     //Make sure limits are applied on the mimic joints to
00176     //    qMimicToq(q_temp,q_out);
00177     //    qToqMimic(q_out,q_temp);
00178 
00179   }
00180 
00181   //  qMimicToq(q_temp, q_out);
00182   q_out = q_temp;
00183   ROS_DEBUG_STREAM_NAMED("kdl","Full Solution:");
00184   for(std::size_t i=0; i < q_temp.rows(); ++i)
00185     ROS_DEBUG_NAMED("kdl","%d: %f",(int) i,q_temp(i));
00186 
00187   ROS_DEBUG_STREAM_NAMED("kdl","Actual Solution:");
00188   for(std::size_t i=0; i < q_out.rows(); ++i)
00189     ROS_DEBUG_NAMED("kdl","%d: %f",(int) i,q_out(i));
00190 
00191   if(i!=maxiter)
00192     return 0;
00193   else
00194     return -3;
00195 }
00196 
00197 ChainIkSolverPos_NR_JL_Mimic::~ChainIkSolverPos_NR_JL_Mimic()
00198 {
00199 }
00200 }


planning
Author(s): Ioan Sucan , Sachin Chitta
autogenerated on Wed Aug 26 2015 12:43:30