-
Notifications
You must be signed in to change notification settings - Fork 378
Add magnetic_field_sensor semantic component #2627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
christophfroehlich
merged 9 commits into
ros-controls:master
from
Amronos:add-magnetic_field_sensor
Oct 9, 2025
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b16e811
Add magnetic_field_sensor semantic component
Amronos cab54b5
Apply changes from code review
Amronos 5f2c453
Merge branch 'master' into add-magnetic_field_sensor
Amronos 8d8718a
Apply changes from code review
Amronos c3e1d57
Add test to CMakeLists.txt
Amronos f733df7
Merge branch 'master' into add-magnetic_field_sensor
Amronos 027b9ac
Merge branch 'master' into add-magnetic_field_sensor
Amronos c356023
Update documentation
Amronos 2a915ae
Update doc/release_notes.rst
christophfroehlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
controller_interface/include/semantic_components/magnetic_field_sensor.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright 2025 Aarav Gupta | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef SEMANTIC_COMPONENTS__MAGNETIC_FIELD_SENSOR_HPP_ | ||
| #define SEMANTIC_COMPONENTS__MAGNETIC_FIELD_SENSOR_HPP_ | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "semantic_components/semantic_component_interface.hpp" | ||
| #include "sensor_msgs/msg/magnetic_field.hpp" | ||
|
|
||
| namespace semantic_components | ||
| { | ||
| class MagneticFieldSensor : public SemanticComponentInterface<sensor_msgs::msg::MagneticField> | ||
| { | ||
| public: | ||
| explicit MagneticFieldSensor(const std::string & name) | ||
| : SemanticComponentInterface( | ||
| name, {name + "/" + "magnetic_field.x", name + "/" + "magnetic_field.y", | ||
| name + "/" + "magnetic_field.z"}) | ||
| { | ||
| } | ||
|
|
||
| /// Returns values as sensor_msgs::msg::MagneticField | ||
| /** | ||
| * \return MagneticField message from values | ||
| */ | ||
| bool get_values_as_message(sensor_msgs::msg::MagneticField & message) | ||
| { | ||
| update_data_from_interfaces(); | ||
| message.magnetic_field.x = data_[0]; | ||
| message.magnetic_field.y = data_[1]; | ||
| message.magnetic_field.z = data_[2]; | ||
| return true; | ||
| } | ||
|
|
||
| private: | ||
| /** | ||
| * @brief Update the data array from the state interfaces. | ||
| * @note This method is thread-safe and non-blocking. | ||
| * @note This method might return stale data if the data is not updated. This is to ensure that | ||
| * the data from the sensor is not discontinuous. | ||
| */ | ||
| void update_data_from_interfaces() | ||
| { | ||
| for (auto i = 0u; i < data_.size(); ++i) | ||
| { | ||
| const auto data = state_interfaces_[i].get().get_optional(); | ||
| if (data.has_value()) | ||
| { | ||
| data_[i] = data.value(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Array to store the data of the magnetic field sensor | ||
| std::array<double, 3> data_{{0.0, 0.0, 0.0}}; | ||
| }; | ||
|
|
||
| } // namespace semantic_components | ||
|
|
||
| #endif // SEMANTIC_COMPONENTS__MAGNETIC_FIELD_SENSOR_HPP_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2025 Aarav Gupta | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "test_magnetic_field_sensor.hpp" | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
| #include "sensor_msgs/msg/magnetic_field.hpp" | ||
|
|
||
| void MagneticFieldSensorTest::TearDown() { magnetic_field_sensor_.reset(nullptr); } | ||
|
|
||
| TEST_F(MagneticFieldSensorTest, validate_all) | ||
| { | ||
| // Create the magnetic field sensor | ||
| magnetic_field_sensor_ = std::make_unique<TestableMagneticFieldSensor>(sensor_name_); | ||
|
|
||
| // validate the component name | ||
| ASSERT_EQ(magnetic_field_sensor_->name_, sensor_name_); | ||
|
|
||
| // Validate the space reserved for interface_names_ and state_interfaces_ | ||
| // Note : Using capacity() for state_interfaces_ as no such interfaces are defined yet | ||
| ASSERT_EQ(magnetic_field_sensor_->interface_names_.size(), size_); | ||
| ASSERT_EQ(magnetic_field_sensor_->state_interfaces_.capacity(), size_); | ||
|
|
||
| // Validate the default interface_names_ | ||
| ASSERT_TRUE( | ||
| std::equal( | ||
| magnetic_field_sensor_->interface_names_.begin(), | ||
| magnetic_field_sensor_->interface_names_.end(), full_interface_names_.begin(), | ||
| full_interface_names_.end())); | ||
|
|
||
| // Get the interface names | ||
| std::vector<std::string> interface_names = magnetic_field_sensor_->get_state_interface_names(); | ||
|
|
||
| // Assign values | ||
| auto magnetic_field_x = std::make_shared<hardware_interface::StateInterface>( | ||
| sensor_name_, magnetic_field_interface_names_[0], &magnetic_field_values_[0]); | ||
| auto magnetic_field_y = std::make_shared<hardware_interface::StateInterface>( | ||
| sensor_name_, magnetic_field_interface_names_[1], &magnetic_field_values_[1]); | ||
| auto magnetic_field_z = std::make_shared<hardware_interface::StateInterface>( | ||
| sensor_name_, magnetic_field_interface_names_[2], &magnetic_field_values_[2]); | ||
|
|
||
| // Create local state interface vector | ||
| std::vector<hardware_interface::LoanedStateInterface> temp_state_interfaces; | ||
| temp_state_interfaces.reserve(10); | ||
|
|
||
| // Insert the interfaces in jumbled sequence | ||
| temp_state_interfaces.emplace_back(magnetic_field_y); | ||
| temp_state_interfaces.emplace_back(magnetic_field_z); | ||
| temp_state_interfaces.emplace_back(magnetic_field_x); | ||
|
|
||
| // Now call the function to make them in order like interface_names | ||
| magnetic_field_sensor_->assign_loaned_state_interfaces(temp_state_interfaces); | ||
|
|
||
| // Validate the count of state_interfaces_ | ||
| ASSERT_EQ(magnetic_field_sensor_->state_interfaces_.size(), size_); | ||
|
|
||
| // Validate get_values_as_message() | ||
| sensor_msgs::msg::MagneticField temp_message; | ||
| ASSERT_TRUE(magnetic_field_sensor_->get_values_as_message(temp_message)); | ||
| ASSERT_EQ(temp_message.magnetic_field.x, magnetic_field_values_[0]); | ||
| ASSERT_EQ(temp_message.magnetic_field.y, magnetic_field_values_[1]); | ||
| ASSERT_EQ(temp_message.magnetic_field.z, magnetic_field_values_[2]); | ||
|
|
||
| // release the state_interfaces_ | ||
| magnetic_field_sensor_->release_interfaces(); | ||
|
|
||
| // validate the count of state_interfaces_ | ||
| ASSERT_EQ(magnetic_field_sensor_->state_interfaces_.size(), 0u); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright 2025 Aarav Gupta | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef TEST_MAGNETIC_FIELD_SENSOR_HPP_ | ||
| #define TEST_MAGNETIC_FIELD_SENSOR_HPP_ | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "gmock/gmock.h" | ||
| #include "semantic_components/magnetic_field_sensor.hpp" | ||
|
|
||
| // implementing and friending so we can access member variables | ||
| class TestableMagneticFieldSensor : public semantic_components::MagneticFieldSensor | ||
| { | ||
| FRIEND_TEST(MagneticFieldSensorTest, validate_all); | ||
|
|
||
| public: | ||
| // Use generation of interface names | ||
| explicit TestableMagneticFieldSensor(const std::string & name) : MagneticFieldSensor(name) {} | ||
|
|
||
| virtual ~TestableMagneticFieldSensor() = default; | ||
| }; | ||
|
|
||
| class MagneticFieldSensorTest : public ::testing::Test | ||
| { | ||
| public: | ||
| void SetUp() | ||
| { | ||
| full_interface_names_.reserve(size_); | ||
| for (auto index = 0u; index < size_; ++index) | ||
| { | ||
| full_interface_names_.emplace_back( | ||
| sensor_name_ + "/" + magnetic_field_interface_names_[index]); | ||
| } | ||
| } | ||
|
|
||
| void TearDown(); | ||
|
|
||
| protected: | ||
| const size_t size_ = 3; | ||
| const std::string sensor_name_ = "test_magnetometer"; | ||
| std::array<double, 3> magnetic_field_values_ = {{4.4, 5.5, 6.6}}; | ||
| std::unique_ptr<TestableMagneticFieldSensor> magnetic_field_sensor_; | ||
|
|
||
| std::vector<std::string> full_interface_names_; | ||
| const std::vector<std::string> magnetic_field_interface_names_ = { | ||
| "magnetic_field.x", "magnetic_field.y", "magnetic_field.z"}; | ||
| }; | ||
|
|
||
| #endif // TEST_MAGNETIC_FIELD_SENSOR_HPP_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.