ros2_control - rolling
force_torque_sensor.hpp
1 // Copyright (c) 2021, Stogl Robotics Consulting UG (haftungsbeschränkt)
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SEMANTIC_COMPONENTS__FORCE_TORQUE_SENSOR_HPP_
16 #define SEMANTIC_COMPONENTS__FORCE_TORQUE_SENSOR_HPP_
17 
18 #include <limits>
19 #include <string>
20 #include <vector>
21 
22 #include "geometry_msgs/msg/wrench.hpp"
23 #include "hardware_interface/loaned_state_interface.hpp"
24 #include "semantic_components/semantic_component_interface.hpp"
25 
26 namespace semantic_components
27 {
28 constexpr std::size_t FORCES_SIZE = 3;
29 class ForceTorqueSensor : public SemanticComponentInterface<geometry_msgs::msg::Wrench>
30 {
31 public:
33  explicit ForceTorqueSensor(const std::string & name)
35  name,
36  // If 6D FTS use standard names
37  {{name + "/" + "force.x"},
38  {name + "/" + "force.y"},
39  {name + "/" + "force.z"},
40  {name + "/" + "torque.x"},
41  {name + "/" + "torque.y"},
42  {name + "/" + "torque.z"}}),
43  existing_axes_({{true, true, true, true, true, true}})
44  {
45  }
46 
48 
57  const std::string & interface_force_x, const std::string & interface_force_y,
58  const std::string & interface_force_z, const std::string & interface_torque_x,
59  const std::string & interface_torque_y, const std::string & interface_torque_z)
61  {
62  auto check_and_add_interface =
63  [this](const std::string & interface_name, const std::size_t index)
64  {
65  if (!interface_name.empty())
66  {
67  interface_names_.emplace_back(interface_name);
68  existing_axes_[index] = true;
69  }
70  else
71  {
72  existing_axes_[index] = false;
73  }
74  };
75 
76  check_and_add_interface(interface_force_x, 0);
77  check_and_add_interface(interface_force_y, 1);
78  check_and_add_interface(interface_force_z, 2);
79  check_and_add_interface(interface_torque_x, 3);
80  check_and_add_interface(interface_torque_y, 4);
81  check_and_add_interface(interface_torque_z, 5);
82  }
83 
85 
90  std::array<double, 3> get_forces() const
91  {
92  std::array<double, 3> forces;
93  forces.fill(std::numeric_limits<double>::quiet_NaN());
94  std::size_t interface_counter{0};
95  for (auto i = 0u; i < forces.size(); ++i)
96  {
97  if (existing_axes_[i])
98  {
99  forces[i] = state_interfaces_[interface_counter].get().get_value();
100  ++interface_counter;
101  }
102  }
103  return forces;
104  }
105 
107 
112  std::array<double, 3> get_torques() const
113  {
114  std::array<double, 3> torques;
115  torques.fill(std::numeric_limits<double>::quiet_NaN());
116 
117  // find out how many force interfaces are being used
118  // torque interfaces will be found from the next index onward
119  auto torque_interface_counter = static_cast<std::size_t>(
120  std::count(existing_axes_.begin(), existing_axes_.begin() + FORCES_SIZE, true));
121 
122  for (auto i = 0u; i < torques.size(); ++i)
123  {
124  if (existing_axes_[i + FORCES_SIZE])
125  {
126  torques[i] = state_interfaces_[torque_interface_counter].get().get_value();
127  ++torque_interface_counter;
128  }
129  }
130  return torques;
131  }
132 
134 
141  bool get_values_as_message(geometry_msgs::msg::Wrench & message) const
142  {
143  const auto [force_x, force_y, force_z] = get_forces();
144  const auto [torque_x, torque_y, torque_z] = get_torques();
145 
146  message.force.x = force_x;
147  message.force.y = force_y;
148  message.force.z = force_z;
149  message.torque.x = torque_x;
150  message.torque.y = torque_y;
151  message.torque.z = torque_z;
152 
153  return true;
154  }
155 
156 protected:
158  std::array<bool, 6> existing_axes_;
159 };
160 
161 } // namespace semantic_components
162 
163 #endif // SEMANTIC_COMPONENTS__FORCE_TORQUE_SENSOR_HPP_
Definition: force_torque_sensor.hpp:30
std::array< bool, 6 > existing_axes_
Vector with existing axes for sensors with less then 6D axes.
Definition: force_torque_sensor.hpp:158
bool get_values_as_message(geometry_msgs::msg::Wrench &message) const
Return Wrench message with forces and torques.
Definition: force_torque_sensor.hpp:141
ForceTorqueSensor(const std::string &name)
Constructor for "standard" 6D FTS.
Definition: force_torque_sensor.hpp:33
std::array< double, 3 > get_forces() const
Return forces.
Definition: force_torque_sensor.hpp:90
std::array< double, 3 > get_torques() const
Return torque.
Definition: force_torque_sensor.hpp:112
ForceTorqueSensor(const std::string &interface_force_x, const std::string &interface_force_y, const std::string &interface_force_z, const std::string &interface_torque_x, const std::string &interface_torque_y, const std::string &interface_torque_z)
Constructor for 6D FTS with custom interface names.
Definition: force_torque_sensor.hpp:56
Definition: semantic_component_interface.hpp:28