AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
simple_skill_node.cpp
1// Copyright 2025 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
25
26#include <chrono>
27
28#include "auto_apms_interfaces/action/example_simple_skill.hpp"
29#include "auto_apms_util/action_wrapper.hpp"
30
34
35namespace auto_apms_examples
36{
37
38using SimpleSkillActionType = auto_apms_interfaces::action::ExampleSimpleSkill;
39
40class SimpleSkillServer : public auto_apms_util::ActionWrapper<SimpleSkillActionType>
41{
42public:
43 SimpleSkillServer(const rclcpp::NodeOptions & options) : ActionWrapper("simple_skill", options) {}
44
45 // Callback invoked when a goal arrives
46 bool onGoalRequest(std::shared_ptr<const Goal> /*goal_ptr*/) override final
47 {
48 index_ = 1;
49 start_ = node_ptr_->now();
50 return true;
51 }
52
53 // Callback invoked asynchronously by the internal execution routine
54 Status executeGoal(
55 std::shared_ptr<const Goal> goal_ptr, std::shared_ptr<Feedback> /*feedback_ptr*/,
56 std::shared_ptr<Result> result_ptr) override final
57 {
58 RCLCPP_INFO(node_ptr_->get_logger(), "#%i - %s", index_++, goal_ptr->msg.c_str());
59 if (index_ <= goal_ptr->n_times) {
60 return Status::RUNNING;
61 }
62 result_ptr->time_required = (node_ptr_->now() - start_).to_chrono<std::chrono::duration<double>>().count();
63 return Status::SUCCESS;
64 }
65
66private:
67 uint8_t index_;
68 rclcpp::Time start_;
69};
70
71} // namespace auto_apms_examples
72
73// Register the skill as a ROS 2 component
74#include "rclcpp_components/register_node_macro.hpp"
75RCLCPP_COMPONENTS_REGISTER_NODE(auto_apms_examples::SimpleSkillServer)
76
77
80
81#include "auto_apms_behavior_tree/node.hpp"
82
83namespace auto_apms_examples
84{
85
86class SimpleSkillClient : public auto_apms_behavior_tree::core::RosActionNode<SimpleSkillActionType>
87{
88public:
89 using RosActionNode::RosActionNode;
90
91 // We must define data ports to accept arguments
92 static BT::PortsList providedPorts()
93 {
94 return {BT::InputPort<std::string>("msg"), BT::InputPort<uint8_t>("n_times")};
95 }
96
97 // Callback invoked to specify the action goal
98 bool setGoal(Goal & goal) override final
99 {
100 RCLCPP_INFO(logger_, "--- Set goal ---");
101 goal.msg = getInput<std::string>("msg").value();
102 goal.n_times = getInput<uint8_t>("n_times").value();
103 return true;
104 }
105
106 // Callback invoked when the action is finished
107 BT::NodeStatus onResultReceived(const WrappedResult & result) override final
108 {
109 RCLCPP_INFO(logger_, "--- Result received ---");
110 RCLCPP_INFO(logger_, "Time elapsed: %f", result.result->time_required);
111 return RosActionNode::onResultReceived(result);
112 }
113};
114
115} // namespace auto_apms_examples
116
117// Make the node discoverable for the class loader
118AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_examples::SimpleSkillClient)
ActionWrapper(const std::string &action_name, rclcpp::Node::SharedPtr node_ptr, std::shared_ptr< ActionContextType > action_context_ptr)
#define AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(type)
Macro for registering a behavior tree node plugin.
Definition node.hpp:43