robothw_interfaces.h
Go to the documentation of this file.
1 // Copyright (C) 2015, PAL Robotics S.L.
3 // Copyright (C) 2017, Clearpath Robotics
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the names of PAL Robotics S.L. nor the names of its
13 // contributors may be used to endorse or promote products derived from
14 // this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
28 
31 #pragma once
32 
33 
34 #include <algorithm>
35 #include <sstream>
36 
39 
40 namespace controller_interface
41 {
42 
44 namespace internal
45 {
46 
47 template <class T>
48 inline std::string enumerateElements(const T& val,
49  const std::string& delimiter,
50  const std::string& prefix,
51  const std::string& suffix)
52 {
53  std::string ret;
54  if (val.empty()) {return ret;}
55 
56  const std::string sdp = suffix+delimiter+prefix;
57  std::stringstream ss;
58  ss << prefix;
59  std::copy(val.begin(), val.end(), std::ostream_iterator<typename T::value_type>(ss, sdp.c_str()));
60  ret = ss.str();
61  if (!ret.empty()) {ret.erase(ret.size() - delimiter.size() - prefix.size());}
62  return ret;
63 }
64 
65 
66 template <typename T>
67 inline bool hasInterfaces(hardware_interface::RobotHW* robot_hw)
68 {
69  T* hw = robot_hw->get<T>();
70  if (!hw)
71  {
72  const std::string hw_name = hardware_interface::internal::demangledTypeName<T>();
73  ROS_ERROR_STREAM("This controller requires a hardware interface of type '" << hw_name << "', " <<
74  "but is not exposed by the robot. Available interfaces in robot:\n" <<
75  enumerateElements(robot_hw->getNames(), "\n", "- '", "'")); // delimiter, prefix, suffux
76  return false;
77  }
78  return true;
79 }
80 
81 template <typename T1, typename T2, typename... More>
82 inline bool hasInterfaces(hardware_interface::RobotHW* robot_hw)
83 {
84  return hasInterfaces<T1>(robot_hw) && hasInterfaces<T2, More...>(robot_hw);
85 }
86 
87 
88 template <typename T>
89 void clearClaims(hardware_interface::RobotHW* robot_hw)
90 {
91  T* hw = robot_hw->get<T>();
92  if (hw)
93  {
94  hw->clearClaims();
95  }
96 }
97 
98 template <typename T1, typename T2, typename... More>
99 void clearClaims(hardware_interface::RobotHW* robot_hw)
100 {
101  clearClaims<T1>(robot_hw);
102  clearClaims<T2, More...>(robot_hw);
103 }
104 
105 
106 template <typename T>
107 inline void extractInterfaceResources(hardware_interface::RobotHW* robot_hw_in,
108  hardware_interface::RobotHW* robot_hw_out)
109 {
110  T* hw = robot_hw_in->get<T>();
111  if (hw) {robot_hw_out->registerInterface(hw);}
112 }
113 
114 template <typename T1, typename T2, typename... More>
115 inline void extractInterfaceResources(hardware_interface::RobotHW* robot_hw_in,
116  hardware_interface::RobotHW* robot_hw_out)
117 {
118  extractInterfaceResources<T1>(robot_hw_in, robot_hw_out);
119  extractInterfaceResources<T2, More...>(robot_hw_in, robot_hw_out);
120 }
121 
122 
123 template <typename T>
124 inline void populateClaimedResources(hardware_interface::RobotHW* robot_hw,
125  ControllerBase::ClaimedResources& claimed_resources)
126 {
127  T* hw = robot_hw->get<T>();
128  if (hw)
129  {
131  iface_res.hardware_interface = hardware_interface::internal::demangledTypeName<T>();
132  iface_res.resources = hw->getClaims();
133  claimed_resources.push_back(iface_res);
134  }
135 }
136 
137 template <typename T1, typename T2, typename... More>
138 inline void populateClaimedResources(hardware_interface::RobotHW* robot_hw,
139  ControllerBase::ClaimedResources& claimed_resources)
140 {
141  populateClaimedResources<T1>(robot_hw, claimed_resources);
142  populateClaimedResources<T2, More...>(robot_hw, claimed_resources);
143 }
144 
145 } // namespace
148 } // namespace
std::set< std::string > resources
std::vector< hardware_interface::InterfaceResources > ClaimedResources
std::vector< std::string > getNames() const
#define ROS_ERROR_STREAM(args)


controller_interface
Author(s): Wim Meeussen
autogenerated on Mon Feb 28 2022 23:30:15