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
36
39 bool get_values_as_message(sensor_msgs::msg::MagneticField & message)
40 {
41 update_data_from_interfaces();
42 message.magnetic_field.x = data_[0];
43 message.magnetic_field.y = data_[1];
44 message.magnetic_field.z = data_[2];
45 return true;
46 }
47
48private:
55 void update_data_from_interfaces()
56 {
57 for (auto i = 0u; i < data_.size(); ++i)
58 {
59 const auto data = state_interfaces_[i].get().get_optional();
60 if (data.has_value())
61 {
62 data_[i] = data.value();
63 }
64 }
65 }
66
67 // Array to store the data of the magnetic field sensor
68 std::array<double, 3> data_{{0.0, 0.0, 0.0}};
69};
70
71} // namespace semantic_components
72
73#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:39
Definition semantic_component_interface.hpp:28