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