kinematic_options_map.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014, SRI International
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Acorn Pooley */
36 
38 #include <ros/console.h>
39 #include <algorithm>
40 
41 // These strings have no content. They are compared by address.
43 const std::string robot_interaction::KinematicOptionsMap::ALL = "";
44 
46 {
47 }
48 
49 // Returns a copy of the KinematicOptions so that the caller does not need to
50 // worry about locking.
52 {
53  boost::mutex::scoped_lock lock(lock_);
54 
55  if (&key == &DEFAULT)
56  return defaults_;
57 
58  M_options::const_iterator it = options_.find(key);
59  if (it == options_.end())
60  return defaults_;
61  return it->second;
62 }
63 
64 void robot_interaction::KinematicOptionsMap::setOptions(const std::string& key, const KinematicOptions& options_delta,
66 {
67  boost::mutex::scoped_lock lock(lock_);
68 
69  if (&key == &ALL)
70  {
71  if (fields == KinematicOptions::ALL)
72  {
73  // setting ALL fields for ALL keys
74  // so just clear all key-specific fields and set the defaults.
75  defaults_ = options_delta;
76  options_.clear();
77  return;
78  }
79 
80  defaults_.setOptions(options_delta, fields);
81  for (M_options::iterator it = options_.begin(); it != options_.end(); ++it)
82  {
83  it->second.setOptions(options_delta, fields);
84  }
85  return;
86  }
87 
88  if (&key == &DEFAULT)
89  {
90  defaults_.setOptions(options_delta, fields);
91  return;
92  }
93 
94  M_options::iterator it = options_.find(key);
95  KinematicOptions* opts;
96  if (it == options_.end())
97  {
98  // create new entry for key and initialize to defaults_
99  opts = &options_[key];
100  *opts = defaults_;
101  }
102  else
103  {
104  opts = &it->second;
105  }
106 
107  opts->setOptions(options_delta, fields);
108 }
109 
110 // merge other into this. All options in other take precedence over this.
112 {
113  if (&other == this)
114  return;
115 
116  // need to lock in consistent order to avoid deadlock.
117  // Lock the one with lower address first.
118  boost::mutex* m1 = &lock_;
119  boost::mutex* m2 = &other.lock_;
120  if (m2 < m1)
121  std::swap(m1, m2);
122  boost::mutex::scoped_lock lock1(*m1);
123  boost::mutex::scoped_lock lock2(*m2);
124 
125  defaults_ = other.defaults_;
126  for (M_options::const_iterator it = other.options_.begin(); it != other.options_.end(); ++it)
127  {
128  options_[it->first] = it->second;
129  }
130 }
131 
132 // This is intended to be called as a ModifyStateFunction to modify the state
133 // maintained by a LockedRobotState in place.
134 bool robot_interaction::KinematicOptionsMap::setStateFromIK(robot_state::RobotState& state, const std::string& key,
135  const std::string& group, const std::string& tip,
136  const geometry_msgs::Pose& pose) const
137 {
138  // copy options so lock is not needed during IK solve.
140  return options.setStateFromIK(state, group, tip, pose);
141 }
bool setStateFromIK(robot_state::RobotState &state, const std::string &key, const std::string &group, const std::string &tip, const geometry_msgs::Pose &pose) const
KinematicOptionsMap()
Constructor - set all options to reasonable default values.
static const std::string DEFAULT
When used as key this means the default value.
KinematicOptions getOptions(const std::string &key) const
void setOptions(const KinematicOptions &source, OptionBitmask fields=ALL)
void merge(const KinematicOptionsMap &other)
void setOptions(const std::string &key, const KinematicOptions &options, KinematicOptions::OptionBitmask fields=KinematicOptions::ALL)
options
static const std::string ALL
When used as key this means set ALL keys (including default)
bool setStateFromIK(robot_state::RobotState &state, const std::string &group, const std::string &tip, const geometry_msgs::Pose &pose) const


robot_interaction
Author(s): Ioan Sucan
autogenerated on Sun Oct 18 2020 13:18:32