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 <string>
20
21#include "semantic_components/semantic_component_interface.hpp"
22#include "sensor_msgs/msg/magnetic_field.hpp"
23
24namespace semantic_components
25{
26class MagneticFieldSensor : public SemanticComponentInterface<sensor_msgs::msg::MagneticField>
27{
28public:
29 explicit MagneticFieldSensor(const std::string & name)
31 name, {name + "/" + "magnetic_field.x", name + "/" + "magnetic_field.y",
32 name + "/" + "magnetic_field.z"})
33 {
34 }
35
42 bool get_values_as_message(sensor_msgs::msg::MagneticField & message)
43 {
44 update_data_from_interfaces();
45 message.magnetic_field.x = data_[0];
46 message.magnetic_field.y = data_[1];
47 message.magnetic_field.z = data_[2];
48 return true;
49 }
50
51private:
58 void update_data_from_interfaces()
59 {
60 for (auto i = 0u; i < data_.size(); ++i)
61 {
62 const auto data = state_interfaces_[i].get().get_optional();
63 if (data.has_value())
64 {
65 data_[i] = data.value();
66 }
67 }
68 }
69
73 std::array<double, 3> data_{{0.0, 0.0, 0.0}};
74};
75
76} // namespace semantic_components
77
78#endif // SEMANTIC_COMPONENTS__MAGNETIC_FIELD_SENSOR_HPP_
Definition magnetic_field_sensor.hpp:27
bool get_values_as_message(sensor_msgs::msg::MagneticField &message)
Returns values as sensor_msgs::msg::MagneticField.
Definition magnetic_field_sensor.hpp:42
Definition semantic_component_interface.hpp:29