AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
set_location_state.cpp
1// Copyright 2024 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
15#include "pyrobosim_msgs/srv/set_location_state.hpp"
16
17#include "auto_apms_behavior_tree_core/node.hpp"
18#include "auto_apms_simulation/util.hpp"
19
20#define INPUT_KEY_LOCATION "location"
21#define INPUT_KEY_OPEN "open"
22#define INPUT_KEY_LOCK "lock"
23
24namespace auto_apms_simulation
25{
26
27class SetLocationState : public auto_apms_behavior_tree::core::RosServiceNode<pyrobosim_msgs::srv::SetLocationState>
28{
29public:
30 using RosServiceNode::RosServiceNode;
31
32 static BT::PortsList providedPorts()
33 {
34 return {
35 BT::InputPort<bool>(INPUT_KEY_LOCK, false, "Lock/Unlock the location."),
36 BT::InputPort<bool>(INPUT_KEY_OPEN, true, "Open/Close the location."),
37 BT::InputPort<std::string>(INPUT_KEY_LOCATION, "Name of the location.")};
38 }
39
40 bool setRequest(Request::SharedPtr & request) override final
41 {
42 const BT::Expected<std::string> expected_location = getInput<std::string>(INPUT_KEY_LOCATION);
43 if (!expected_location || expected_location.value().empty()) {
44 RCLCPP_ERROR(
45 logger_, "%s - You must provide a non-empty location name.",
46 context_.getFullyQualifiedTreeNodeName(this).c_str());
47 RCLCPP_DEBUG_EXPRESSION(
48 logger_, !expected_location, "%s - Error message: %s", context_.getFullyQualifiedTreeNodeName(this).c_str(),
49 expected_location.error().c_str());
50 return false;
51 }
52 request->location_name = expected_location.value();
53 request->open = getInput<bool>(INPUT_KEY_OPEN).value();
54 request->lock = getInput<bool>(INPUT_KEY_LOCK).value();
55 return true;
56 }
57
58 BT::NodeStatus onResponseReceived(const Response::SharedPtr & response) override final
59 {
60 const BT::NodeStatus base_status = RosServiceNode::onResponseReceived(response);
61 if (base_status != BT::NodeStatus::SUCCESS) return base_status;
62 if (response->result.status != ExecutionResultMsg::SUCCESS) {
63 RCLCPP_ERROR(
64 logger_, "%s - %s", context_.getFullyQualifiedTreeNodeName(this).c_str(), toStr(response->result).c_str());
65 return BT::NodeStatus::FAILURE;
66 }
67 return BT::NodeStatus::SUCCESS;
68 }
69};
70
71} // namespace auto_apms_simulation
72
73AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_simulation::SetLocationState)
#define AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(type)
Macro for registering a behavior tree node plugin.
Definition node.hpp:40
const char * toStr(const ActionNodeErrorCode &err)
Convert the action error code to string.