ros2_control - rolling
Loading...
Searching...
No Matches
filter_traits.hpp
1// Copyright (c) 2025, ros2_control development team
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 CONTROL_TOOLBOX__FILTER_TRAITS_HPP_
16#define CONTROL_TOOLBOX__FILTER_TRAITS_HPP_
17
18#include <fmt/format.h>
19#include <Eigen/Dense>
20#include <algorithm>
21#include <cmath>
22#include <limits>
23#include <stdexcept>
24#include <vector>
25
26#include "geometry_msgs/msg/wrench_stamped.hpp"
27
28namespace control_toolbox
29{
30
31template <typename T>
32struct FilterTraits;
33
34// Wrapper around std::vector<U> to be used as
35// the std::vector<U> StorageType specialization.
36// This is a workaround for the fact that
37// std::vector<U>'s operator* and operator+ cannot be overloaded.
38template <typename U>
39struct FilterVector
40{
41 std::vector<U> data;
42
43 FilterVector() = default;
44
45 explicit FilterVector(const std::vector<U> & vec) : data(vec) {}
46
47 explicit FilterVector(size_t size, const U & initial_value = U{}) : data(size, initial_value) {}
48
49 FilterVector operator*(const U & scalar) const
50 {
51 FilterVector result = *this;
52 for (auto & val : result.data)
53 {
54 val *= scalar;
55 }
56 return result;
57 }
58
59 FilterVector operator+(const FilterVector & other) const
60 {
61 if (data.size() != other.data.size())
62 {
63 throw std::runtime_error(
64 fmt::format(
65 "Vectors must be of the same size for addition ({} vs {}).", data.size(),
66 other.data.size()));
67 }
68 FilterVector result = *this;
69 for (size_t i = 0; i < data.size(); ++i)
70 {
71 result.data[i] += other.data[i];
72 }
73 return result;
74 }
75
76 size_t size() const { return data.size(); }
77};
78
79// Enable scalar * FilterVector
80template <typename U>
81inline FilterVector<U> operator*(const U & scalar, const FilterVector<U> & vec)
82{
83 return vec * scalar;
84}
85
86template <typename T>
88{
89 using StorageType = T;
90
91 static void initialize(StorageType & storage)
92 {
93 storage = T{std::numeric_limits<T>::quiet_NaN()};
94 }
95
96 static bool is_nan(const StorageType & storage) { return std::isnan(storage); }
97
98 static bool is_finite(const StorageType & storage) { return std::isfinite(storage); }
99
100 static bool is_empty(const StorageType & storage)
101 {
102 (void)storage;
103 return false;
104 }
105
106 static void assign(StorageType & storage, const StorageType & data_in) { storage = data_in; }
107
108 static void validate_input(const T & data_in, const StorageType & filtered_value, T & data_out)
109 {
110 (void)data_in;
111 (void)filtered_value;
112 (void)data_out; // Suppress unused warnings
113 }
114
115 static void add_metadata(StorageType & storage, const StorageType & data_in)
116 {
117 (void)storage;
118 (void)data_in;
119 }
120};
121
122template <>
123struct FilterTraits<geometry_msgs::msg::WrenchStamped>
124{
125 using StorageType = Eigen::Matrix<double, 6, 1>;
126 using DataType = geometry_msgs::msg::WrenchStamped;
127
128 static void initialize(StorageType & storage)
129 {
130 // NaN marks the state as uninitialized, so the first update initializes it.
131 // Explicit, because EIGEN_INITIALIZE_MATRICES_BY_NAN is ignored if Eigen was included before
132 storage = StorageType::Constant(std::numeric_limits<double>::quiet_NaN());
133 }
134
135 static bool is_nan(const StorageType & storage) { return storage.hasNaN(); }
136
137 static bool is_finite(const DataType & data)
138 {
139 return std::isfinite(data.wrench.force.x) && std::isfinite(data.wrench.force.y) &&
140 std::isfinite(data.wrench.force.z) && std::isfinite(data.wrench.torque.x) &&
141 std::isfinite(data.wrench.torque.y) && std::isfinite(data.wrench.torque.z);
142 }
143
144 static bool is_empty(const StorageType & storage)
145 {
146 (void)storage;
147 return false;
148 }
149
150 static void assign(DataType & data_in, const StorageType & storage)
151 {
152 data_in.wrench.force.x = storage[0];
153 data_in.wrench.force.y = storage[1];
154 data_in.wrench.force.z = storage[2];
155 data_in.wrench.torque.x = storage[3];
156 data_in.wrench.torque.y = storage[4];
157 data_in.wrench.torque.z = storage[5];
158 }
159
160 static void assign(StorageType & storage, const DataType & data_in)
161 {
162 storage[0] = data_in.wrench.force.x;
163 storage[1] = data_in.wrench.force.y;
164 storage[2] = data_in.wrench.force.z;
165 storage[3] = data_in.wrench.torque.x;
166 storage[4] = data_in.wrench.torque.y;
167 storage[5] = data_in.wrench.torque.z;
168 }
169
170 static void assign(StorageType & storage, const StorageType & data_in) { storage = data_in; }
171
172 static void validate_input(
173 const DataType & data_in, const StorageType & filtered_value, DataType & data_out)
174 {
175 (void)filtered_value; // filtered_value has no header
176
177 // Compare new input's frame_id with previous output's frame_id,
178 // but only if it existed (filter_chains does not initialize output)
179 if (!data_out.header.frame_id.empty() && data_in.header.frame_id != data_out.header.frame_id)
180 {
181 throw std::runtime_error(
182 "Frame ID changed between filter updates! Out: " + data_out.header.frame_id +
183 ", In: " + data_in.header.frame_id);
184 }
185 }
186
187 static void add_metadata(DataType & data_out, const DataType & data_in)
188 {
189 data_out.header = data_in.header;
190 }
191};
192
193template <typename U>
194struct FilterTraits<std::vector<U>>
195{
196 using StorageType = FilterVector<U>;
197 using DataType = std::vector<U>;
198
199 static void initialize(StorageType & storage) { (void)storage; }
200
201 static bool is_finite(const StorageType & storage)
202 {
203 return std::all_of(
204 storage.data.begin(), storage.data.end(), [](U val) { return std::isfinite(val); });
205 }
206
207 static bool is_finite(const DataType & storage)
208 {
209 return std::all_of(storage.begin(), storage.end(), [](U val) { return std::isfinite(val); });
210 }
211
212 static bool is_empty(const StorageType & storage) { return storage.data.empty(); }
213
214 static bool is_nan(const StorageType & storage)
215 {
216 for (const auto & val : storage.data)
217 {
218 if (std::isnan(val))
219 {
220 return true;
221 }
222 }
223
224 return false;
225 }
226
227 static void assign(StorageType & storage, const DataType & data_in) { storage.data = data_in; }
228
229 static void assign(DataType & storage, const StorageType & data_in) { storage = data_in.data; }
230
231 static void assign(StorageType & storage, const StorageType & data_in)
232 {
233 storage.data = data_in.data;
234 }
235
236 static void validate_input(
237 const DataType & data_in, const StorageType & filtered_value, DataType & data_out)
238 {
239 if (data_in.size() != filtered_value.size())
240 {
241 throw std::runtime_error(
242 fmt::format(
243 "Input vector size ({}) does not match internal state size ({}).", data_in.size(),
244 filtered_value.size()));
245 }
246 // Compare new input's size with output's size,
247 // but only if it existed (filter_chains does not initialize output)
248 if (!data_out.empty() && data_out.size() != data_in.size())
249 {
250 throw std::runtime_error(
251 fmt::format(
252 "Input and output vectors must be the same size, {} vs {}.", data_out.size(),
253 data_in.size()));
254 }
255 }
256
257 static void add_metadata(DataType & storage, const DataType & data_in)
258 {
259 (void)storage;
260 (void)data_in;
261 }
262};
263
264} // namespace control_toolbox
265
266#endif // CONTROL_TOOLBOX__FILTER_TRAITS_HPP_
Definition dither.hpp:42
Definition filter_traits.hpp:88
Definition filter_traits.hpp:40