Skip to content

Commit d81b385

Browse files
committed
add get_attended_guest
1 parent 9698e30 commit d81b385

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2024 Intelligent Robotics Lab - Gentlebots
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 "hri/dialog/get_attended_guest.hpp"
16+
#include <regex>
17+
18+
using std::placeholders::_1;
19+
using namespace std::chrono_literals;
20+
21+
22+
namespace dialog
23+
{
24+
25+
GetAttendedGuest::GetAttendedGuest(
26+
const std::string & xml_tag_name, const std::string & srv_name,
27+
const BT::NodeConfiguration & conf)
28+
: hri::BtServiceNode<
29+
kb_msgs::srv::Query,
30+
rclcpp_cascade_lifecycle::CascadeLifecycleNode>(xml_tag_name, srv_name, conf)
31+
{
32+
config().blackboard->get("node", node_);
33+
}
34+
35+
void GetAttendedGuest::on_tick()
36+
{
37+
RCLCPP_DEBUG(node_->get_logger(), "[GetAttendedGuest] ticked");
38+
rclcpp::spin_some(node_->get_node_base_interface());
39+
40+
// Patterns
41+
request_->patterns.push_back("robot1 oro:attends ?guest");
42+
43+
// Vars
44+
request_->vars.push_back("?guest");
45+
46+
setStatus(BT::NodeStatus::SUCCESS);
47+
}
48+
49+
void GetAttendedGuest::on_result()
50+
{
51+
RCLCPP_DEBUG(node_->get_logger(), "[GetAttendedGuest] result received");
52+
53+
if (!result_.error_msg.empty()) {
54+
RCLCPP_ERROR(node_->get_logger(), "[GetAttendedGuest] error");
55+
setStatus(BT::NodeStatus::FAILURE);
56+
}
57+
58+
std::regex guest_regex("\"guest\"\\s*:\\s*\"([^\"]*)\"");
59+
60+
std::smatch match;
61+
62+
if (std::regex_search(result_.json, match, guest_regex)) {
63+
guest_id_ = match[1];
64+
}
65+
66+
setOutput("guest_attended", guest_id_);
67+
68+
RCLCPP_INFO(
69+
node_->get_logger(), "[GetAttendedGuest] Guest attended: %s", guest_id_.c_str());
70+
71+
std_msgs::msg::String fact_msg;
72+
73+
setStatus(BT::NodeStatus::SUCCESS);
74+
}
75+
76+
77+
78+
} // namespace hri
79+
80+
#include "behaviortree_cpp_v3/bt_factory.h"
81+
BT_REGISTER_NODES(factory)
82+
{
83+
BT::NodeBuilder builder = [](const std::string & name, const BT::NodeConfiguration & config) {
84+
return std::make_unique<dialog::GetAttendedGuest>(
85+
name, "/kb/query", config);
86+
};
87+
88+
factory.registerBuilder<dialog::GetAttendedGuest>("GetAttendedGuest", builder);
89+
}
90+

0 commit comments

Comments
 (0)