-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into luca/rmf_transporter
- Loading branch information
Showing
12 changed files
with
236 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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 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,68 @@ | ||
/* | ||
* Copyright (C) 2025 Open Source Robotics Foundation | ||
* | ||
* 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 NEXUS_COMMON__TASK_REMAPPER_HPP | ||
#define NEXUS_COMMON__TASK_REMAPPER_HPP | ||
|
||
#include "nexus_common_export.hpp" | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
#include <optional> | ||
#include <string> | ||
|
||
namespace nexus::common { | ||
|
||
/** | ||
* Provides task remapping capability | ||
*/ | ||
class NEXUS_COMMON_EXPORT TaskRemapper | ||
{ | ||
public: | ||
/* | ||
* Initialize the remapper with the value of a ROS parameter containing a YAML. | ||
* | ||
* The YAML should have a sequence of key:value types where the key is the task to | ||
* remap to, and the value is a list of tasks to remap to the key. | ||
* For example: | ||
* | ||
* pick_and_place: [pick, place] | ||
* a_and_b: [a, b] | ||
* | ||
* Will remap "pick" or "place" to "pick_and_place", "a" and "b" to "a_and_b". | ||
* Note: If the value is specified as an asterisk, "*", any value will be | ||
* remapped to the specified key. | ||
*/ | ||
TaskRemapper(const YAML::Node& remaps); | ||
|
||
/* | ||
* Remaps, if necessary, the input task | ||
* Returns a value if the task was remapped, std::nullopt otherwise | ||
*/ | ||
std::optional<std::string> remap(const std::string& task) const; | ||
|
||
private: | ||
// If present, match every incoming task to the target task | ||
std::optional<std::string> _wildcard_match; | ||
std::unordered_map<std::string, std::string> _task_remaps; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains 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,58 @@ | ||
/* | ||
* Copyright (C) 2025 Open Source Robotics Foundation | ||
* | ||
* 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 "task_remapper.hpp" | ||
|
||
namespace nexus::common { | ||
|
||
TaskRemapper::TaskRemapper(const YAML::Node& remaps) | ||
{ | ||
for (const auto& n : remaps) | ||
{ | ||
const auto task_type = n.first.as<std::string>(); | ||
const auto& mappings = n.second; | ||
for (const auto& m : mappings) | ||
{ | ||
auto mapping = m.as<std::string>(); | ||
if (mapping == "*") | ||
{ | ||
this->_wildcard_match = task_type; | ||
this->_task_remaps.clear(); | ||
return; | ||
} | ||
// TODO(luca) check for duplicates, logging if found | ||
this->_task_remaps.emplace(mapping, task_type); | ||
} | ||
} | ||
} | ||
|
||
std::optional<std::string> TaskRemapper::remap(const std::string& task) const | ||
{ | ||
if (this->_wildcard_match.has_value()) | ||
{ | ||
return this->_wildcard_match.value(); | ||
} | ||
const auto it = this->_task_remaps.find(task); | ||
if (it != this->_task_remaps.end()) | ||
{ | ||
return it->second; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
|
||
} |
This file contains 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 (C) 2025 Open Source Robotics Foundation | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#define CATCH_CONFIG_MAIN | ||
#include <rmf_utils/catch.hpp> | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
#include "task_remapper.hpp" | ||
|
||
namespace nexus::common::test { | ||
|
||
TEST_CASE("task_remapping") { | ||
std::string param = | ||
R"( | ||
pick_and_place: [pick, place] | ||
)"; | ||
const auto yaml = YAML::Load(param); | ||
const auto remapper = TaskRemapper(yaml); | ||
CHECK(remapper.remap("pick") == "pick_and_place"); | ||
CHECK(remapper.remap("place") == "pick_and_place"); | ||
CHECK(remapper.remap("other") == std::nullopt); | ||
} | ||
|
||
TEST_CASE("task_remapping_with_wildcard") { | ||
std::string param = | ||
R"( | ||
pick_and_place: [pick, place] | ||
main : ["*"] | ||
)"; | ||
const auto yaml = YAML::Load(param); | ||
const auto remapper = TaskRemapper(yaml); | ||
CHECK(remapper.remap("pick") == "main"); | ||
CHECK(remapper.remap("place") == "main"); | ||
CHECK(remapper.remap("other") == "main"); | ||
} | ||
|
||
TEST_CASE("task_remapping_with_normal_and_wildcard") { | ||
std::string param = | ||
R"( | ||
pick_and_place: [pick, "*"] | ||
)"; | ||
const auto yaml = YAML::Load(param); | ||
const auto remapper = TaskRemapper(yaml); | ||
CHECK(remapper.remap("pick") == "pick_and_place"); | ||
CHECK(remapper.remap("place") == "pick_and_place"); | ||
} | ||
|
||
} |
This file contains 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 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 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 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 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 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.