19#include "auto_apms_behavior_tree/executor/executor_node.hpp"
20#include "auto_apms_util/logging.hpp"
21#include "auto_apms_util/string.hpp"
22#include "rclcpp/rclcpp.hpp"
24sig_atomic_t
volatile termination_requested = 0;
28int main(
int argc,
char ** argv)
30 bool print_help =
false;
31 std::string build_request =
"";
33 const std::string arg(argv[1]);
34 print_help =
"-h" == arg ||
"--help" == arg;
38 std::cerr <<
"run_tree: The program accepts: \n\t1.) Optional: Single string specifying the behavior tree "
39 "build request to be passed to the build handler loaded by the underlying tree executor node.\n";
40 std::cerr <<
"Usage: run_tree [<build_request>]\n";
46 rclcpp::init(argc, argv, rclcpp::InitOptions(), rclcpp::SignalHandlerOptions::SigTerm);
47 signal(SIGINT, [](
int ) { termination_requested = 1; });
50 rclcpp::NodeOptions opt;
52 const rclcpp::Logger logger = executor.getNodePtr()->get_logger();
55 std::shared_future<TreeExecutorBase::ExecutionResult> future = executor.startExecution(build_request);
56 RCLCPP_INFO(logger,
"Executing tree '%s'.", executor.getTreeName().c_str());
58 const std::chrono::duration<double> termination_timeout(3);
59 rclcpp::Time termination_start;
60 bool termination_started =
false;
62 rclcpp::spin_until_future_complete(executor.get_node_base_interface(), future, std::chrono::milliseconds(250)) !=
63 rclcpp::FutureReturnCode::SUCCESS) {
64 if (termination_started) {
65 if (executor.getNodePtr()->now() - termination_start > termination_timeout) {
66 RCLCPP_WARN(logger,
"Termination took too long. Aborted.");
69 }
else if (termination_requested) {
70 termination_start = executor.getNodePtr()->now();
72 termination_started =
true;
73 RCLCPP_INFO(logger,
"Terminating tree execution...");
78 if (future.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
79 throw std::logic_error(
"Future object is not ready.");
82 RCLCPP_INFO(logger,
"Finished with status %s.", toStr(future.get()).c_str());
@ TERMINATE
Halt the currently executing tree and terminate the execution routine.
Flexible ROS 2 node implementing a standardized interface for dynamically executing behavior trees.
std::string trimWhitespaces(const std::string &str)
Trim whitespaces from both ends of a string.
Useful tooling for incorporating behavior trees for task development.