joint_model_group.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2013, Ioan A. Sucan
00005 *  Copyright (c) 2008-2013, Willow Garage, Inc.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of Willow Garage, Inc. nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *********************************************************************/
00035 
00036 /* Author: Ioan Sucan */
00037 
00038 #ifndef MOVEIT_CORE_ROBOT_MODEL_JOINT_MODEL_GROUP_
00039 #define MOVEIT_CORE_ROBOT_MODEL_JOINT_MODEL_GROUP_
00040 
00041 #include <moveit/robot_model/joint_model.h>
00042 #include <moveit/robot_model/link_model.h>
00043 #include <moveit/kinematics_base/kinematics_base.h>
00044 #include <srdfdom/model.h>
00045 #include <boost/function.hpp>
00046 #include <set>
00047 
00048 namespace moveit
00049 {
00050 namespace core
00051 {
00052 
00053 class RobotModel;
00054 class JointModelGroup;
00055 
00057 typedef boost::function<kinematics::KinematicsBasePtr(const JointModelGroup*)> SolverAllocatorFn;
00058 
00060 typedef std::map<const JointModelGroup*, SolverAllocatorFn> SolverAllocatorMapFn;
00061 
00062 
00064 typedef std::map<std::string, JointModelGroup*> JointModelGroupMap;
00065 
00067 typedef std::map<std::string, const JointModelGroup*> JointModelGroupMapConst;
00068 
00069 
00070 typedef std::vector<const JointModel::Bounds*> JointBoundsVector;
00071 
00072 class JointModelGroup
00073 {
00074 public:
00075   
00076   struct KinematicsSolver
00077   {
00078     KinematicsSolver() 
00079       : default_ik_timeout_(0.5)
00080       , default_ik_attempts_(2)
00081     {
00082     }
00083     
00085     operator bool() const
00086     {
00087       return allocator_ && !bijection_.empty() && solver_instance_;
00088     }
00089 
00090     void reset()
00091     {
00092       solver_instance_.reset();
00093       solver_instance_const_.reset();
00094       bijection_.clear();
00095     }
00096     
00098     SolverAllocatorFn allocator_;
00099     
00103     std::vector<unsigned int> bijection_;
00104 
00105     kinematics::KinematicsBaseConstPtr solver_instance_const_;
00106     
00107     kinematics::KinematicsBasePtr solver_instance_;
00108 
00109     double default_ik_timeout_;
00110 
00111     unsigned int default_ik_attempts_;
00112   };
00113   
00115   typedef std::map<const JointModelGroup*, KinematicsSolver> KinematicsSolverMap;
00116   
00117   JointModelGroup(const std::string& name, const srdf::Model::Group &config,
00118                   const std::vector<const JointModel*>& joint_vector, const RobotModel *parent_model);
00119 
00120   ~JointModelGroup();
00121 
00123   const RobotModel& getParentModel() const
00124   {
00125     return *parent_model_;
00126   }
00127 
00129   const std::string& getName() const
00130   {
00131     return name_;
00132   }
00133 
00135   const srdf::Model::Group& getConfig() const
00136   {
00137     return config_;
00138   }
00139   
00141   bool hasJointModel(const std::string &joint) const;
00142 
00144   bool hasLinkModel(const std::string &link) const;
00145 
00147   const JointModel* getJointModel(const std::string &joint) const;
00148 
00150   const LinkModel* getLinkModel(const std::string &link) const;
00151 
00153   const std::vector<const JointModel*>& getJointModels() const
00154   {
00155     return joint_model_vector_;
00156   }
00157 
00159   const std::vector<std::string>& getJointModelNames() const
00160   {
00161     return joint_model_name_vector_;
00162   }
00163 
00165   const std::vector<const JointModel*>& getActiveJointModels() const
00166   {
00167     return active_joint_model_vector_;
00168   }
00169 
00171   const std::vector<std::string>& getActiveJointModelNames() const
00172   {
00173     return active_joint_model_name_vector_;
00174   }
00175 
00177   const std::vector<const JointModel*>& getFixedJointModels() const
00178   {
00179     return fixed_joints_;
00180   }
00181 
00183   const std::vector<const JointModel*>& getMimicJointModels() const
00184   {
00185     return mimic_joints_;
00186   }
00187 
00189   const std::vector<const JointModel*>& getContinuousJointModels() const
00190   {
00191     return continuous_joint_model_vector_;
00192   }
00193 
00196   const std::vector<std::string>& getVariableNames() const
00197   {
00198     return variable_names_;
00199   }
00200 
00208   const std::vector<const JointModel*>& getJointRoots() const
00209   {
00210     return joint_roots_;
00211   }
00212 
00214   const JointModel* getCommonRoot() const
00215   {
00216     return common_root_;
00217   }
00218   
00220   const std::vector<const LinkModel*>& getLinkModels() const
00221   {
00222     return link_model_vector_;
00223   }
00224 
00226   const std::vector<std::string>& getLinkModelNames() const
00227   {
00228     return link_model_name_vector_;
00229   }
00230 
00232   const std::vector<std::string>& getLinkModelNamesWithCollisionGeometry() const
00233   {
00234     return link_model_with_geometry_name_vector_;
00235   }
00236 
00240   const std::vector<const LinkModel*>& getUpdatedLinkModels() const
00241   {
00242     return updated_link_model_vector_;
00243   }
00244 
00246   const std::vector<std::string>& getUpdatedLinkModelNames() const
00247   {
00248     return updated_link_model_name_vector_;
00249   }
00250 
00254   const std::vector<const LinkModel*>& getUpdatedLinkModelsWithGeometry() const
00255   {
00256     return updated_link_model_with_geometry_vector_;
00257   }
00258 
00260   const std::set<const LinkModel*>& getUpdatedLinkModelsWithGeometrySet() const
00261   {
00262     return updated_link_model_with_geometry_set_;
00263   }
00264 
00266   const std::vector<std::string>& getUpdatedLinkModelsWithGeometryNames() const
00267   {
00268     return updated_link_model_with_geometry_name_vector_;
00269   }
00270 
00272   const std::set<std::string>& getUpdatedLinkModelsWithGeometryNamesSet() const
00273   {
00274     return updated_link_model_with_geometry_name_set_;
00275   }
00276 
00280   bool isLinkUpdated(const std::string &name) const
00281   {
00282     return updated_link_model_name_set_.find(name) != updated_link_model_name_set_.end();
00283   }
00284   
00286   const std::vector<int>& getVariableIndexList() const
00287   {
00288     return variable_index_list_;
00289   }
00290 
00292   int getVariableGroupIndex(const std::string &variable) const;  
00293   
00295   const std::vector<std::string>& getDefaultStateNames() const
00296   {
00297     return default_states_names_;
00298   }
00299   
00300   void addDefaultState(const std::string &name, const std::map<std::string, double> &default_state);
00301   
00303   bool getVariableDefaultPositions(const std::string &name, std::map<std::string, double> &values) const;
00304 
00306   void getVariableDefaultPositions(std::map<std::string, double> &values) const;
00307 
00309   void getVariableDefaultPositions(std::vector<double> &values) const
00310   {
00311     values.resize(variable_count_);
00312     getVariableDefaultPositions(&values[0]);
00313   }
00314 
00316   void getVariableDefaultPositions(double *values) const;
00317 
00319   void getVariableRandomPositions(random_numbers::RandomNumberGenerator &rng, double *values) const
00320   {
00321     getVariableRandomPositions(rng, values, active_joint_models_bounds_);
00322   }
00323 
00325   void getVariableRandomPositions(random_numbers::RandomNumberGenerator &rng, std::vector<double> &values) const
00326   {
00327     values.resize(variable_count_);
00328     getVariableRandomPositions(rng, &values[0], active_joint_models_bounds_);
00329   }
00330 
00332   void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, double *values,
00333                                         const double *near, const double distance) const
00334   {
00335     getVariableRandomPositionsNearBy(rng, values, active_joint_models_bounds_, near, distance);
00336   }
00338   void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, std::vector<double> &values,
00339                                         const std::vector<double> &near, double distance) const
00340   {
00341     values.resize(variable_count_);
00342     getVariableRandomPositionsNearBy(rng, &values[0], active_joint_models_bounds_, &near[0], distance);
00343   }  
00344 
00346   void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, std::vector<double> &values,
00347                                         const std::vector<double> &near, const std::map<JointModel::JointType, double> &distance_map) const
00348   {
00349     values.resize(variable_count_);
00350     getVariableRandomPositionsNearBy(rng, &values[0], active_joint_models_bounds_, &near[0], distance_map);
00351   }
00352 
00354   void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, std::vector<double> &values, 
00355                                         const std::vector<double> &near, const std::vector<double> &distances) const
00356   {
00357     values.resize(variable_count_);
00358     getVariableRandomPositionsNearBy(rng, &values[0], active_joint_models_bounds_, &near[0], distances);
00359   }  
00360   
00361   void getVariableRandomPositions(random_numbers::RandomNumberGenerator &rng, double *values, const JointBoundsVector &active_joint_bounds) const;
00362   
00364   void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, double *values, const JointBoundsVector &active_joint_bounds,
00365                                         const double *near, const double distance) const;
00366   
00368   void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, double *values, const JointBoundsVector &active_joint_bounds,
00369                                         const double *near, const std::map<JointModel::JointType, double> &distance_map) const;
00370   
00372   void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, double *values, const JointBoundsVector &active_joint_bounds,
00373                                         const double *near, const std::vector<double> &distances) const;
00374   
00375   bool enforcePositionBounds(double *state) const
00376   {
00377     return enforcePositionBounds(state, active_joint_models_bounds_);
00378   }
00379 
00380   bool enforcePositionBounds(double *state, const JointBoundsVector &active_joint_bounds) const;
00381   bool satisfiesPositionBounds(const double *state, double margin = 0.0) const
00382   {
00383     return satisfiesPositionBounds(state, active_joint_models_bounds_, margin);
00384   }
00385   bool satisfiesPositionBounds(const double *state, const JointBoundsVector &active_joint_bounds, double margin = 0.0) const;
00386 
00387   double getMaximumExtent() const
00388   {
00389     return getMaximumExtent(active_joint_models_bounds_);
00390   }
00391   double getMaximumExtent(const JointBoundsVector &active_joint_bounds) const;
00392   
00393   double distance(const double *state1, const double *state2) const;  
00394   void interpolate(const double *from, const double *to, double t, double *state) const;
00395   
00398   unsigned int getVariableCount() const
00399   {
00400     return variable_count_;
00401   }
00402   
00404   void setSubgroupNames(const std::vector<std::string> &subgroups);
00405 
00407   const std::vector<std::string>& getSubgroupNames() const
00408   {
00409     return subgroup_names_;
00410   }
00411 
00413   void getSubgroups(std::vector<const JointModelGroup*>& sub_groups) const;
00414 
00416   bool isSubgroup(const std::string& group) const
00417   {
00418     return subgroup_names_set_.find(group) != subgroup_names_set_.end();
00419   }  
00420   
00422   bool isChain() const
00423   {
00424     return is_chain_;
00425   }
00426 
00428   bool isSingleDOFJoints() const
00429   {
00430     return is_single_dof_;
00431   }
00432   
00434   bool isEndEffector() const
00435   {
00436     return !end_effector_name_.empty();
00437   }
00438 
00439   bool isContiguousWithinState() const
00440   {
00441     return is_contiguous_index_list_; 
00442   }
00443   
00445   const std::string& getEndEffectorName() const
00446   {
00447     return end_effector_name_;
00448   }
00449 
00451   void setEndEffectorName(const std::string &name);
00452   
00454   void setEndEffectorParent(const std::string &group, const std::string &link);
00455   
00457   void attachEndEffector(const std::string &eef_name);
00458   
00460   const std::pair<std::string, std::string>& getEndEffectorParentGroup() const
00461   {
00462     return end_effector_parent_;
00463   }
00464 
00466   const std::vector<std::string>& getAttachedEndEffectorNames() const
00467   {
00468     return attached_end_effector_names_;
00469   }
00470 
00477   bool getEndEffectorTips(std::vector<const LinkModel*> &tips) const;
00478 
00485   bool getEndEffectorTips(std::vector<std::string> &tips) const;
00486 
00488   const JointBoundsVector& getActiveJointModelsBounds() const
00489   {
00490     return active_joint_models_bounds_;
00491   }
00492 
00493   const std::pair<KinematicsSolver, KinematicsSolverMap>& getGroupKinematics() const
00494   {
00495     return group_kinematics_;
00496   }
00497 
00498   void setSolverAllocators(const SolverAllocatorFn &solver, const SolverAllocatorMapFn &solver_map = SolverAllocatorMapFn())
00499   {
00500     setSolverAllocators(std::make_pair(solver, solver_map));
00501   }
00502 
00503   void setSolverAllocators(const std::pair<SolverAllocatorFn, SolverAllocatorMapFn> &solvers);
00504 
00505   const kinematics::KinematicsBaseConstPtr& getSolverInstance() const
00506   {
00507     return group_kinematics_.first.solver_instance_const_;
00508   }
00509 
00510   const kinematics::KinematicsBasePtr& getSolverInstance()
00511   {
00512     return group_kinematics_.first.solver_instance_;
00513   }
00514 
00515   bool canSetStateFromIK(const std::string &tip) const;
00516 
00517   bool setRedundantJoints(const std::vector<std::string> &joints)
00518   {
00519     if (group_kinematics_.first.solver_instance_)
00520       return group_kinematics_.first.solver_instance_->setRedundantJoints(joints);
00521     return false;
00522   }
00523 
00525   double getDefaultIKTimeout() const
00526   {
00527     return group_kinematics_.first.default_ik_timeout_;
00528   }
00529 
00531   void setDefaultIKTimeout(double ik_timeout);
00532 
00534   unsigned int getDefaultIKAttempts() const
00535   {
00536     return group_kinematics_.first.default_ik_attempts_;
00537   }
00538 
00540   void setDefaultIKAttempts(unsigned int ik_attempts);
00541 
00545   const std::vector<unsigned int>& getKinematicsSolverJointBijection() const
00546   {
00547     return group_kinematics_.first.bijection_;
00548   }
00549 
00551   void printGroupInfo(std::ostream &out = std::cout) const;
00552 
00553 protected:
00554 
00555   bool computeIKIndexBijection(const std::vector<std::string> &ik_jnames, std::vector<unsigned int> &joint_bijection) const;
00556   
00558   void updateMimicJoints(double *values) const;
00559 
00561   const RobotModel                                          *parent_model_;
00562 
00564   std::string                                                name_;
00565 
00567   std::vector<const JointModel*>                             joint_model_vector_;
00568 
00570   std::vector<std::string>                                   joint_model_name_vector_;
00571 
00573   std::vector<const JointModel*>                             active_joint_model_vector_;
00574 
00576   std::vector<std::string>                                   active_joint_model_name_vector_;
00577 
00579   std::vector<const JointModel*>                             fixed_joints_;
00580 
00582   std::vector<const JointModel*>                             mimic_joints_;
00583 
00585   std::vector<const JointModel*>                             continuous_joint_model_vector_;
00586 
00588   std::vector<std::string>                                   variable_names_;
00589 
00591   std::set<std::string>                                      variable_names_set_;
00592 
00594   JointModelMapConst                                         joint_model_map_;
00595   
00597   std::vector<const JointModel*>                             joint_roots_;
00598 
00600   const JointModel                                          *common_root_;
00601   
00605   VariableIndexMap                                           joint_variables_index_map_;
00606 
00608   JointBoundsVector                                          active_joint_models_bounds_;
00609 
00611   std::vector<int>                                           variable_index_list_;
00612     
00614   std::vector<int>                                           active_joint_model_start_index_;
00615   
00619   std::vector<const LinkModel*>                              link_model_vector_;
00620 
00622   LinkModelMapConst                                          link_model_map_;
00623 
00625   std::vector<std::string>                                   link_model_name_vector_;
00626 
00627   std::vector<const LinkModel*>                              link_model_with_geometry_vector_;
00628 
00630   std::vector<std::string>                                   link_model_with_geometry_name_vector_;
00631 
00633   std::vector<const LinkModel*>                              updated_link_model_vector_;
00634 
00636   std::set<const LinkModel*>                                 updated_link_model_set_;
00637 
00639   std::vector<std::string>                                   updated_link_model_name_vector_;
00640 
00642   std::set<std::string>                                      updated_link_model_name_set_;
00643 
00645   std::vector<const LinkModel*>                              updated_link_model_with_geometry_vector_;
00646 
00648   std::set<const LinkModel*>                                 updated_link_model_with_geometry_set_;
00649 
00651   std::vector<std::string>                                   updated_link_model_with_geometry_name_vector_;
00652 
00654   std::set<std::string>                                      updated_link_model_with_geometry_name_set_;
00655 
00657   unsigned int                                               variable_count_;
00658 
00661   bool                                                       is_contiguous_index_list_;
00662   
00664   std::vector<std::string>                                   subgroup_names_;
00665 
00667   std::set<std::string>                                      subgroup_names_set_;
00668   
00670   std::vector<std::string>                                   attached_end_effector_names_;
00671 
00673   std::pair<std::string, std::string>                        end_effector_parent_;
00674 
00676   std::string                                                end_effector_name_;
00677 
00678   bool                                                       is_chain_;
00679 
00680   bool                                                       is_single_dof_;
00681 
00682   struct GroupMimicUpdate
00683   {
00684     GroupMimicUpdate(int s, int d, double f, double o) : src(s), dest(d), factor(f), offset(o)
00685     {
00686     }
00687     int src;
00688     int dest;
00689     double factor;
00690     double offset;
00691   };
00692   
00693   std::vector<GroupMimicUpdate>                              group_mimic_update_;
00694 
00695   std::pair<KinematicsSolver, KinematicsSolverMap>           group_kinematics_;
00696   
00697   srdf::Model::Group                                         config_;
00698   
00700   std::map<std::string, std::map<std::string, double> >      default_states_;
00701 
00703   std::vector<std::string>                                   default_states_names_;
00704   
00705 };
00706 
00707 }
00708 }
00709 
00710 #endif


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Thu Aug 27 2015 13:58:52