ros2_control - jazzy
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:
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 data_.fill(std::numeric_limits<double>::quiet_NaN());
46 }
47
49
58 const std::string & interface_force_x, const std::string & interface_force_y,
59 const std::string & interface_force_z, const std::string & interface_torque_x,
60 const std::string & interface_torque_y, const std::string & interface_torque_z)
62 {
63 data_.fill(std::numeric_limits<double>::quiet_NaN());
64 auto check_and_add_interface =
65 [this](const std::string & interface_name, const std::size_t index)
66 {
67 if (!interface_name.empty())
68 {
69 interface_names_.emplace_back(interface_name);
70 existing_axes_[index] = true;
71 }
72 else
73 {
74 existing_axes_[index] = false;
75 }
76 };
77
78 check_and_add_interface(interface_force_x, 0);
79 check_and_add_interface(interface_force_y, 1);
80 check_and_add_interface(interface_force_z, 2);
81 check_and_add_interface(interface_torque_x, 3);
82 check_and_add_interface(interface_torque_y, 4);
83 check_and_add_interface(interface_torque_z, 5);
84 }
85
87
92 std::array<double, 3> get_forces() const
93 {
95 std::array<double, 3> forces;
96 std::copy(data_.begin(), data_.begin() + 3, forces.begin());
97 return forces;
98 }
99
101
106 std::array<double, 3> get_torques() const
107 {
109 std::array<double, 3> torques;
110 std::copy(data_.begin() + 3, data_.end(), torques.begin());
111 return torques;
112 }
113
115
122 bool get_values_as_message(geometry_msgs::msg::Wrench & message) const
123 {
125 message.force.x = data_[0];
126 message.force.y = data_[1];
127 message.force.z = data_[2];
128 message.torque.x = data_[3];
129 message.torque.y = data_[4];
130 message.torque.z = data_[5];
131
132 return true;
133 }
134
135protected:
143 {
144 std::size_t interface_counter{0};
145 for (auto i = 0u; i < data_.size(); ++i)
146 {
147 if (existing_axes_[i])
148 {
149 const auto data = state_interfaces_[interface_counter].get().get_optional();
150 if (data.has_value())
151 {
152 data_[i] = data.value();
153 }
154 ++interface_counter;
155 }
156 }
157 }
158
160 mutable std::array<double, 6> data_;
162 std::array<bool, 6> existing_axes_;
163};
164
165} // namespace semantic_components
166
167#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:162
bool get_values_as_message(geometry_msgs::msg::Wrench &message) const
Return Wrench message with forces and torques.
Definition force_torque_sensor.hpp:122
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:92
std::array< double, 3 > get_torques() const
Return torque.
Definition force_torque_sensor.hpp:106
std::array< double, 6 > data_
Array to store the data of the FT sensors.
Definition force_torque_sensor.hpp:160
void update_data_from_interfaces() const
Update the data from the state interfaces.
Definition force_torque_sensor.hpp:142
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:57
Definition semantic_component_interface.hpp:28