Skip to content

Commit

Permalink
Change API to std::optional
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>
  • Loading branch information
luca-della-vedova committed Jan 10, 2025
1 parent c61743e commit b881d1e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
6 changes: 5 additions & 1 deletion nexus_common/include/nexus_common/task_remapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

#include <rclcpp/rclcpp.hpp>

#include <optional>
#include <string>

namespace nexus::common {

/**
Expand All @@ -37,8 +40,9 @@ class NEXUS_COMMON_EXPORT TaskRemapper

/*
* Remaps, if necessary, the input task
* Returns a value if the task was remapped, std::nullopt otherwise
*/
std::string remap(const std::string& task) const;
std::optional<std::string> remap(const std::string& task) const;

private:
// If present, match every incoming task to the target task
Expand Down
4 changes: 2 additions & 2 deletions nexus_common/src/task_remapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TaskRemapper::TaskRemapper(const std::string& param)
}
}

std::string TaskRemapper::remap(const std::string& task) const
std::optional<std::string> TaskRemapper::remap(const std::string& task) const
{
if (this->_wildcard_match.has_value())
{
Expand All @@ -54,7 +54,7 @@ std::string TaskRemapper::remap(const std::string& task) const
{
return it->second;
}
return task;
return std::nullopt;
}


Expand Down
6 changes: 3 additions & 3 deletions nexus_system_orchestrator/src/execute_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ BT::NodeStatus ExecuteTask::onStart()
// Remap the BT filename to load if one is provided.
std::string bt_name = task->type;
const auto new_task = _ctx->task_remapper->remap(task->type);
if (new_task != task->type)
if (new_task.has_value())
{
RCLCPP_DEBUG(
_ctx->node.get_logger(),
"[ExecuteTask] Loading remapped BT [%s] for original task type [%s]",
new_task.c_str(),
new_task.value().c_str(),
task->type.c_str()
);
bt_name = new_task;
bt_name = new_task.value();
}
std::filesystem::path task_bt_path(this->_bt_path / (bt_name + ".xml"));
if (!std::filesystem::is_regular_file(task_bt_path))
Expand Down
8 changes: 5 additions & 3 deletions nexus_workcell_orchestrator/src/workcell_orchestrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,17 +979,19 @@ BT::Tree WorkcellOrchestrator::_create_bt(const std::shared_ptr<Context>& ctx)
// To keep things simple, the task type is used as the key for the behavior tree to use.
this->_ctx_mgr->set_active_context(ctx);
const auto new_task = this->_task_remapper->remap(ctx->task.type);
if (new_task != ctx->task.type)
auto bt_name = ctx->task.type;
if (new_task.has_value())
{
RCLCPP_DEBUG(
this->get_logger(),
"Loading remapped BT [%s] for original task type [%s]",
new_task.c_str(),
new_task.value().c_str(),
ctx->task.type.c_str()
);
bt_name = new_task.value();
}
return this->_bt_factory->createTreeFromFile(this->_bt_store.get_bt(
new_task));
bt_name));
}

void WorkcellOrchestrator::_handle_command_success(
Expand Down

0 comments on commit b881d1e

Please sign in to comment.