kinematic_options_map.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2014, SRI International
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Acorn Pooley */
00036 
00037 #include <moveit/robot_interaction/kinematic_options_map.h>
00038 #include <ros/console.h>
00039 #include <algorithm>
00040 
00041 // These strings have no content.  They are compared by address.
00042 const std::string robot_interaction::KinematicOptionsMap::DEFAULT = "";
00043 const std::string robot_interaction::KinematicOptionsMap::ALL = "";
00044 
00045 robot_interaction::KinematicOptionsMap::KinematicOptionsMap()
00046 {}
00047 
00048 // Returns a copy of the KinematicOptions so that the caller does not need to
00049 // worry about locking.
00050 robot_interaction::KinematicOptions
00051 robot_interaction::KinematicOptionsMap::getOptions(
00052       const std::string& key) const
00053 {
00054   boost::mutex::scoped_lock lock(lock_);
00055 
00056   if (&key == &DEFAULT)
00057     return defaults_;
00058 
00059   M_options::const_iterator it = options_.find(key);
00060   if (it == options_.end())
00061     return defaults_;
00062   return it->second;
00063 }
00064 
00065 void robot_interaction::KinematicOptionsMap::setOptions(
00066       const std::string& key,
00067       const KinematicOptions& options_delta,
00068       KinematicOptions::OptionBitmask fields)
00069 {
00070   boost::mutex::scoped_lock lock(lock_);
00071 
00072   if (&key == &ALL)
00073   {
00074     if (fields == KinematicOptions::ALL)
00075     {
00076       // setting ALL fields for ALL keys
00077       // so just clear all key-specific fields and set the defaults.
00078       defaults_ = options_delta;
00079       options_.clear();
00080       return;
00081     }
00082 
00083     defaults_.setOptions(options_delta, fields);
00084     for (M_options::iterator it = options_.begin() ;
00085          it != options_.end() ;
00086          ++it)
00087     {
00088       it->second.setOptions(options_delta, fields);
00089     }
00090     return;
00091   }
00092 
00093   if (&key == &DEFAULT)
00094   {
00095     defaults_.setOptions(options_delta, fields);
00096     return;
00097   }
00098 
00099   M_options::iterator it = options_.find(key);
00100   KinematicOptions* opts;
00101   if (it == options_.end())
00102   {
00103     // create new entry for key and initialize to defaults_
00104     opts = &options_[key];
00105     *opts = defaults_;
00106   }
00107   else
00108   {
00109     opts = &it->second;
00110   }
00111 
00112   opts->setOptions(options_delta, fields);
00113 }
00114 
00115 // merge other into this.  All options in other take precedence over this.
00116 void robot_interaction::KinematicOptionsMap::merge(
00117       const KinematicOptionsMap& other)
00118 {
00119   if (&other == this)
00120     return;
00121 
00122   // need to lock in consistent order to avoid deadlock.
00123   // Lock the one with lower address first.
00124   boost::mutex *m1 = &lock_;
00125   boost::mutex *m2 = &other.lock_;
00126   if (m2 < m1)
00127     std::swap(m1, m2);
00128   boost::mutex::scoped_lock lock1(*m1);
00129   boost::mutex::scoped_lock lock2(*m2);
00130 
00131   defaults_ = other.defaults_;
00132   for (M_options::const_iterator it = other.options_.begin() ;
00133        it != other.options_.end() ;
00134        ++it)
00135   {
00136     options_[it->first] = it->second;
00137   }
00138 }
00139 
00140 // This is intended to be called as a ModifyStateFunction to modify the state
00141 // maintained by a LockedRobotState in place.
00142 bool robot_interaction::KinematicOptionsMap::setStateFromIK(
00143       robot_state::RobotState& state,
00144       const std::string& key,
00145       const std::string& group,
00146       const std::string& tip,
00147       const geometry_msgs::Pose& pose) const
00148 {
00149   // copy options so lock is not needed during IK solve.
00150   KinematicOptions options = getOptions(key);
00151   return options.setStateFromIK(state, group, tip, pose);
00152 }
00153 


robot_interaction
Author(s): Ioan Sucan
autogenerated on Wed Aug 26 2015 12:44:19