ros2_control - rolling
Loading...
Searching...
No Matches
magnetic_field_sensor.hpp
1// Copyright 2025 Aarav Gupta
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__MAGNETIC_FIELD_SENSOR_HPP_
16#define SEMANTIC_COMPONENTS__MAGNETIC_FIELD_SENSOR_HPP_
17
18#include <array>
19#include <limits>
20#include <string>
21
22#include "semantic_components/semantic_component_interface.hpp"
23#include "sensor_msgs/msg/magnetic_field.hpp"
24
25namespace semantic_components
26{
27class MagneticFieldSensor : public SemanticComponentInterface<sensor_msgs::msg::MagneticField>
28{
29public:
30 explicit MagneticFieldSensor(const std::string & name)
31 : SemanticComponentInterface(
32 name, {name + "/" + "magnetic_field.x", name + "/" + "magnetic_field.y",
33 name + "/" + "magnetic_field.z"})
34 {
35 data_.fill(std::numeric_limits<double>::quiet_NaN());
36 }
37
44 bool get_values_as_message(sensor_msgs::msg::MagneticField & message)
45 {
46 update_data_from_interfaces();
47 message.magnetic_field.x = data_[0];
48 message.magnetic_field.y = data_[1];
49 message.magnetic_field.z = data_[2];
50 return true;
51 }
52
53private:
59 void update_data_from_interfaces()
60 {
61 for (auto i = 0u; i < data_.size(); ++i)
62 {
63 const auto data = state_interfaces_[i].get().get_optional();
64 if (data.has_value())
65 {
66 data_[i] = data.value();
67 }
68 }
69 }
70
74 std::array<double, 3> data_{};
75};
76
77} // namespace semantic_components
78
79#endif // SEMANTIC_COMPONENTS__MAGNETIC_FIELD_SENSOR_HPP_
bool get_values_as_message(sensor_msgs::msg::MagneticField &message)
Returns values as sensor_msgs::msg::MagneticField.
Definition magnetic_field_sensor.hpp:44