AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
set_bool.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// http://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
15#include "std_srvs/srv/set_bool.hpp"
16
17#include "auto_apms_behavior_tree_core/node.hpp"
18
19#define INPUT_KEY_DATA "data"
20#define OUTPUT_KEY_MESSAGE "message"
21
23{
24
25class SetBool : public core::RosServiceNode<std_srvs::srv::SetBool>
26{
27public:
28 SetBool(const std::string & instance_name, const Config & config, const Context & context)
29 : RosServiceNode(instance_name, config, context)
30 {
31 }
32
33 static BT::PortsList providedPorts()
34 {
35 return providedBasicPorts({
36 BT::InputPort<bool>(INPUT_KEY_DATA, "Boolean data to set (true, false, 1, 0)."),
37 BT::OutputPort<std::string>(OUTPUT_KEY_MESSAGE, "Service response: informational message."),
38 });
39 }
40
41 bool setRequest(Request::SharedPtr & request) override final
42 {
43 const BT::Expected<bool> expected_data = getInput<bool>(INPUT_KEY_DATA);
44 if (!expected_data) {
45 RCLCPP_ERROR(
46 logger_, "%s - %s", context_.getFullyQualifiedTreeNodeName(this).c_str(), expected_data.error().c_str());
47 return false;
48 }
49 request->data = expected_data.value();
50 return true;
51 }
52
53 BT::NodeStatus onResponseReceived(const Response::SharedPtr & response) override final
54 {
55 setOutput(OUTPUT_KEY_MESSAGE, response->message);
56 return response->success ? BT::NodeStatus::SUCCESS : BT::NodeStatus::FAILURE;
57 }
58};
59
60} // namespace auto_apms_behavior_tree
61
62AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE(auto_apms_behavior_tree::SetBool)
Generic behavior tree node wrapper for a ROS 2 service client.
RosServiceNode(const std::string &instance_name, const Config &config, Context context)
#define AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE(type)
Macro for registering a behavior tree node plugin.
Definition node.hpp:44
Powerful tooling for incorporating behavior trees for task development.
Definition behavior.hpp:32