-
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.
Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>
- Loading branch information
1 parent
ad5226f
commit e385e5b
Showing
9 changed files
with
228 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<root main_tree_to_execute="main"> | ||
<BehaviorTree ID="main"> | ||
<ReactiveSequence> | ||
<IsPauseTriggered paused="{paused}"/> | ||
<Fallback> | ||
<PausableSequence pause="{paused}"> | ||
<ForEachTask task="{task}" workcell="{workcell}"> | ||
<ExecuteTask task="{task}" workcell="{workcell}"/> | ||
</ForEachTask> | ||
<BidTransporter name="bid_transporter" destination="unloading" result="{transporter}"/> | ||
<TransporterRequest name="transporter_request" transporter="{transporter}" destination="unloading"/> | ||
</PausableSequence> | ||
<PausableSequence pause="{paused}"> | ||
<BidTransporter name="bid_transporter" destination="unloading" result="{transporter}"/> | ||
<TransporterRequest name="transporter_request" transporter="{transporter}" destination="unloading"/> | ||
<AlwaysFailure/> | ||
</PausableSequence> | ||
</Fallback> | ||
</ReactiveSequence> | ||
</BehaviorTree> | ||
</root> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (C) 2023 Johnson & Johnson | ||
* | ||
* 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 "execute_task.hpp" | ||
|
||
namespace nexus::system_orchestrator { | ||
|
||
BT::NodeStatus ExecuteTask::onStart() | ||
{ | ||
auto task = this->getInput<Task>("task"); | ||
if (!task) | ||
{ | ||
RCLCPP_ERROR( | ||
this->_ctx->node.get_logger(), "%s: [task] port is required", | ||
this->name().c_str()); | ||
return BT::NodeStatus::FAILURE; | ||
} | ||
|
||
auto workcell = this->getInput<std::string>("workcell"); | ||
if (!workcell) | ||
{ | ||
RCLCPP_ERROR( | ||
this->_ctx->node.get_logger(), "%s: [workcell] port is required", | ||
this->name().c_str()); | ||
return BT::NodeStatus::FAILURE; | ||
} | ||
|
||
// Remap the BT filename to load if one is provided. | ||
std::string bt_name = task->type; | ||
auto it = _ctx->task_remaps->find(task->type); | ||
if (it != _ctx->task_remaps->end()) | ||
{ | ||
RCLCPP_DEBUG( | ||
_ctx->node.get_logger(), | ||
"[ExecuteTask] Loading remapped BT [%s] for original task type [%s]", | ||
it->second.c_str(), | ||
task->type.c_str() | ||
); | ||
bt_name = it->second; | ||
} | ||
std::filesystem::path task_bt_path(this->_bt_path / (bt_name + ".xml")); | ||
if (!std::filesystem::is_regular_file(task_bt_path)) | ||
{ | ||
RCLCPP_ERROR( | ||
this->_ctx->node.get_logger(), "%s: no behavior tree to execute task type [%s]", | ||
this->name().c_str(), task->type.c_str()); | ||
return BT::NodeStatus::FAILURE; | ||
} | ||
|
||
this->_bt = std::make_unique<BT::Tree>(this->_bt_factory->createTreeFromFile( | ||
task_bt_path)); | ||
this->_bt->rootBlackboard()->set("task", *task); | ||
this->_bt->rootBlackboard()->set("workcell", *workcell); | ||
|
||
return BT::NodeStatus::RUNNING; | ||
} | ||
|
||
BT::NodeStatus ExecuteTask::onRunning() | ||
{ | ||
return this->_bt->tickRoot(); | ||
} | ||
|
||
void ExecuteTask::onHalted() | ||
{ | ||
this->_bt->haltTree(); | ||
} | ||
|
||
} |
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,75 @@ | ||
/* | ||
* Copyright (C) 2023 Johnson & Johnson | ||
* | ||
* 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_SYSTEM_ORCHESTRATOR__EXECUTE_TASK_HPP | ||
#define NEXUS_SYSTEM_ORCHESTRATOR__EXECUTE_TASK_HPP | ||
|
||
#include "context.hpp" | ||
|
||
#include <nexus_orchestrator_msgs/msg/workcell_task.hpp> | ||
|
||
#include <behaviortree_cpp_v3/action_node.h> | ||
#include <behaviortree_cpp_v3/bt_factory.h> | ||
|
||
#include <filesystem> | ||
|
||
namespace nexus::system_orchestrator { | ||
|
||
/** | ||
* Searches for a behavior tree to execute a task and run it as a sub tree. | ||
* | ||
* The behavior tree used is based on the task type after remapping and the | ||
* filename of the behavior tree without extension. | ||
* | ||
* Input Ports: | ||
* task |nexus_orchestrator_msgs::msg::WorkcellTask| The task to execute. | ||
* workcell |std::string| Workcell to execute on. | ||
*/ | ||
class ExecuteTask : public BT::StatefulActionNode | ||
{ | ||
public: using Task = nexus_orchestrator_msgs::msg::WorkcellTask; | ||
|
||
public: static BT::PortsList providedPorts() | ||
{ | ||
return { BT::InputPort<Task>("task"), | ||
BT::InputPort<std::string>("workcell") }; | ||
} | ||
|
||
public: ExecuteTask(const std::string& name, | ||
const BT::NodeConfiguration& config, | ||
std::shared_ptr<Context> ctx, | ||
std::filesystem::path bt_path, | ||
std::shared_ptr<BT::BehaviorTreeFactory> bt_factory) | ||
: BT::StatefulActionNode(name, config), _ctx(std::move(ctx)), _bt_path(std::move( | ||
bt_path)), | ||
_bt_factory(std::move(bt_factory)) {} | ||
|
||
protected: BT::NodeStatus onStart() override; | ||
|
||
protected: BT::NodeStatus onRunning() override; | ||
|
||
protected: void onHalted() override; | ||
|
||
private: std::shared_ptr<Context> _ctx; | ||
private: std::filesystem::path _bt_path; | ||
private: std::shared_ptr<BT::BehaviorTreeFactory> _bt_factory; | ||
private: std::unique_ptr<BT::Tree> _bt; | ||
}; | ||
|
||
} | ||
|
||
#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
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