ros2_control - rolling
Loading...
Searching...
No Matches
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 <algorithm>
19#include <limits>
20#include <string>
21#include <vector>
22
23#include "geometry_msgs/msg/wrench.hpp"
24#include "hardware_interface/loaned_state_interface.hpp"
25#include "semantic_components/semantic_component_interface.hpp"
26
27namespace semantic_components
28{
29class ForceTorqueSensor : public SemanticComponentInterface<geometry_msgs::msg::Wrench>
30{
31public:
35 explicit ForceTorqueSensor(const std::string & name)
37 name,
38 // If 6D FTS use standard names
39 {{name + "/" + "force.x"},
40 {name + "/" + "force.y"},
41 {name + "/" + "force.z"},
42 {name + "/" + "torque.x"},
43 {name + "/" + "torque.y"},
44 {name + "/" + "torque.z"}}),
45 existing_axes_({{true, true, true, true, true, true}})
46 {
47 data_.fill(std::numeric_limits<double>::quiet_NaN());
48 }
49
61 const std::string & interface_force_x, const std::string & interface_force_y,
62 const std::string & interface_force_z, const std::string & interface_torque_x,
63 const std::string & interface_torque_y, const std::string & interface_torque_z)
65 {
66 data_.fill(std::numeric_limits<double>::quiet_NaN());
67 auto check_and_add_interface =
68 [this](const std::string & interface_name, const std::size_t index)
69 {
70 if (!interface_name.empty())
71 {
72 interface_names_.emplace_back(interface_name);
73 existing_axes_[index] = true;
74 }
75 else
76 {
77 existing_axes_[index] = false;
78 }
79 };
80
81 check_and_add_interface(interface_force_x, 0);
82 check_and_add_interface(interface_force_y, 1);
83 check_and_add_interface(interface_force_z, 2);
84 check_and_add_interface(interface_torque_x, 3);
85 check_and_add_interface(interface_torque_y, 4);
86 check_and_add_interface(interface_torque_z, 5);
87 }
88
96 std::array<double, 3> get_forces() const
97 {
99 std::array<double, 3> forces;
100 std::copy(data_.begin(), data_.begin() + 3, forces.begin());
101 return forces;
102 }
103
111 std::array<double, 3> get_torques() const
112 {
114 std::array<double, 3> torques;
115 std::copy(data_.begin() + 3, data_.end(), torques.begin());
116 return torques;
117 }
118
129 bool get_values_as_message(geometry_msgs::msg::Wrench & message) const
130 {
132 message.force.x = data_[0];
133 message.force.y = data_[1];
134 message.force.z = data_[2];
135 message.torque.x = data_[3];
136 message.torque.y = data_[4];
137 message.torque.z = data_[5];
138
139 return true;
140 }
141
142protected:
150 {
151 std::size_t interface_counter{0};
152 for (auto i = 0u; i < data_.size(); ++i)
153 {
154 if (existing_axes_[i])
155 {
156 const auto data = state_interfaces_[interface_counter].get().get_optional();
157 if (data.has_value())
158 {
159 data_[i] = data.value();
160 }
161 ++interface_counter;
162 }
163 }
164 }
165
169 mutable std::array<double, 6> data_;
173 std::array<bool, 6> existing_axes_;
174};
175
176} // namespace semantic_components
177
178#endif // SEMANTIC_COMPONENTS__FORCE_TORQUE_SENSOR_HPP_
Definition force_torque_sensor.hpp:30
std::array< bool, 6 > existing_axes_
Array with existing axes for sensors with less than 6D axes.
Definition force_torque_sensor.hpp:173
bool get_values_as_message(geometry_msgs::msg::Wrench &message) const
Return Wrench message with forces and torques.
Definition force_torque_sensor.hpp:129
ForceTorqueSensor(const std::string &name)
Constructor for "standard" 6D FTS.
Definition force_torque_sensor.hpp:35
std::array< double, 3 > get_forces() const
Return forces.
Definition force_torque_sensor.hpp:96
std::array< double, 3 > get_torques() const
Return torque.
Definition force_torque_sensor.hpp:111
std::array< double, 6 > data_
Array to store the data of the FT sensors.
Definition force_torque_sensor.hpp:169
void update_data_from_interfaces() const
Update the data from the state interfaces.
Definition force_torque_sensor.hpp:149
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:60
Definition semantic_component_interface.hpp:28