Skip to content

Commit 4812857

Browse files
committed
add store and get kb info
1 parent 1ce447f commit 4812857

File tree

10 files changed

+457
-0
lines changed

10 files changed

+457
-0
lines changed

bt_nodes/bt_test/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ find_package(arm REQUIRED)
2424
find_package(tf2_ros REQUIRED)
2525
find_package(hri REQUIRED)
2626
find_package(perception_system_interfaces REQUIRED)
27+
find_package(kb_msgs REQUIRED)
2728
# find_package(backward_ros REQUIRED)
2829

2930
set(CMAKE_CXX_STANDARD 17)
@@ -45,6 +46,7 @@ set(dependencies
4546
arm
4647
geometry_msgs
4748
perception_system_interfaces
49+
kb_msgs
4850
# backward_ros
4951
)
5052

@@ -204,6 +206,11 @@ add_executable(extract_person_description_test src/extract_person_description_te
204206
ament_target_dependencies(extract_person_description_test ${dependecies})
205207
target_link_libraries(extract_person_description_test perception::extract_person_description_bt_node perception::is_detected_bt_node hri::speak_bt_node)
206208

209+
# store guest info test
210+
add_executable(store_guest_info_test src/store_guest_info_test.cpp)
211+
ament_target_dependencies(store_guest_info_test ${dependencies})
212+
target_link_libraries(store_guest_info_test hri::store_guest_info_bt_node hri::get_guest_info_bt_node)
213+
207214
# gpsr test
208215
add_executable(gpsr_test src/gpsr_test.cpp)
209216
ament_target_dependencies(gpsr_test ${dependencies})
@@ -400,6 +407,7 @@ install(TARGETS
400407
extract_person_description_test
401408
music_test
402409
set_blackboard_test
410+
store_guest_info_test
403411

404412
gpsr_test
405413
gpsr_answerquiz_test
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<root main_tree_to_execute="BehaviorTree">
3+
<!-- ////////// -->
4+
<BehaviorTree ID="BehaviorTree">
5+
<Sequence>
6+
<!-- <Action ID="StoreGuestInfo" guest_name="Irene" guest_drink="Cola" guest_description="Long hair and glasses."/> -->
7+
<Action ID="GetGuestInfo" guest_name="{guest_name}" guest_drink="{guest_drink}" guest_description="{guest_description}"/>
8+
</Sequence>
9+
</BehaviorTree>
10+
<!-- ////////// -->
11+
<TreeNodesModel>
12+
<Action ID="StoreGuestInfo">
13+
<input_port name="guest_name"/>
14+
<input_port name="guest_drink"/>
15+
<input_port name="guest_description"/>
16+
</Action>
17+
</TreeNodesModel>
18+
</root>

bt_nodes/bt_test/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<depend>tf2_ros</depend>
2626
<depend>hri</depend>
2727
<depend>perception_system_interfaces</depend>
28+
<depend>kb_msgs</depend>
2829

2930
<test_depend>ament_lint_auto</test_depend>
3031
<test_depend>ament_lint_common</test_depend>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "ament_index_cpp/get_package_share_directory.hpp"
2+
#include "behaviortree_cpp_v3/behavior_tree.h"
3+
#include "behaviortree_cpp_v3/bt_factory.h"
4+
#include "behaviortree_cpp_v3/loggers/bt_zmq_publisher.h"
5+
#include "behaviortree_cpp_v3/utils/shared_library.h"
6+
#include "rclcpp/rclcpp.hpp"
7+
#include "rclcpp_cascade_lifecycle/rclcpp_cascade_lifecycle.hpp"
8+
9+
int main(int argc, char * argv[])
10+
{
11+
rclcpp::init(argc, argv);
12+
auto node = std::make_shared<rclcpp_cascade_lifecycle::CascadeLifecycleNode>("store_guest_info_test_node");
13+
BT::BehaviorTreeFactory factory;
14+
BT::SharedLibrary loader;
15+
16+
factory.registerFromPlugin(loader.getOSName("store_guest_info_bt_node"));
17+
factory.registerFromPlugin(loader.getOSName("get_guest_info_bt_node"));
18+
std::string pkgpath = ament_index_cpp::get_package_share_directory("bt_test");
19+
std::string xml_file = pkgpath + "/bt_xml/store_guest_info.xml";
20+
21+
auto blackboard = BT::Blackboard::create();
22+
blackboard->set("node", node);
23+
BT::Tree tree = factory.createTreeFromFile(xml_file, blackboard);
24+
25+
auto publisher_zmq = std::make_shared<BT::PublisherZMQ>(tree, 10, 2666, 2667);
26+
27+
node->trigger_transition(lifecycle_msgs::msg::Transition::TRANSITION_CONFIGURE);
28+
node->trigger_transition(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE);
29+
30+
rclcpp::Rate rate(10);
31+
32+
BT::NodeStatus status = BT::NodeStatus::RUNNING;
33+
bool finish = false;
34+
while (!finish && rclcpp::ok()) {
35+
rclcpp::spin_some(node->get_node_base_interface());
36+
37+
status = tree.rootNode()->executeTick();
38+
finish = (status == BT::NodeStatus::SUCCESS) || (status == BT::NodeStatus::FAILURE);
39+
40+
rate.sleep();
41+
}
42+
std::cout << "Store Guest Info Finished with status: " << status << std::endl;
43+
rclcpp::shutdown();
44+
return 0;
45+
}

bt_nodes/hri/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ find_package(sensor_msgs REQUIRED)
2222
find_package(gpsr_msgs REQUIRED)
2323
find_package(audio_common_msgs REQUIRED)
2424
find_package(perception_system_interfaces REQUIRED)
25+
find_package(kb_msgs REQUIRED)
2526
set(CMAKE_CXX_STANDARD 17)
2627

2728
set(dependencies
@@ -40,6 +41,7 @@ set(dependencies
4041
std_srvs
4142
audio_common_msgs
4243
perception_system_interfaces
44+
kb_msgs
4345
)
4446

4547
include_directories(include)
@@ -77,6 +79,12 @@ list(APPEND plugin_libs start_music_bt_node)
7779
add_library(stop_music_bt_node SHARED src/hri/stop_music.cpp)
7880
list(APPEND plugin_libs stop_music_bt_node)
7981

82+
add_library(store_guest_info_bt_node SHARED src/hri/dialog/store_guest_info.cpp)
83+
list(APPEND plugin_libs store_guest_info_bt_node)
84+
85+
add_library(get_guest_info_bt_node SHARED src/hri/dialog/get_guest_info.cpp)
86+
list(APPEND plugin_libs get_guest_info_bt_node)
87+
8088
foreach(bt_plugin ${plugin_libs})
8189
ament_target_dependencies(${bt_plugin} ${dependencies})
8290
target_compile_definitions(${bt_plugin} PRIVATE BT_PLUGIN_EXPORT)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 andGO2OBJECT
13+
// limitations under the License.
14+
15+
#ifndef HRI__GEST_GUEST_INFO_HPP_
16+
#define HRI__GEST_GUEST_INFO_HPP_
17+
18+
#include <chrono>
19+
#include <functional>
20+
21+
#include "behaviortree_cpp_v3/behavior_tree.h"
22+
#include "behaviortree_cpp_v3/bt_factory.h"
23+
#include "std_msgs/msg/string.hpp"
24+
#include "kb_msgs/srv/query.hpp"
25+
#include "rclcpp/rclcpp.hpp"
26+
#include "rclcpp_cascade_lifecycle/rclcpp_cascade_lifecycle.hpp"
27+
#include "hri/bt_service_node.hpp"
28+
29+
namespace dialog
30+
{
31+
32+
class GetGuestInfo : public hri::BtServiceNode<
33+
kb_msgs::srv::Query,
34+
rclcpp_cascade_lifecycle::CascadeLifecycleNode>
35+
{
36+
public:
37+
explicit GetGuestInfo(const std::string & xml_tag_name, const std::string & srv_name,
38+
const BT::NodeConfiguration & conf);
39+
40+
void on_tick() override;
41+
void on_result() override;
42+
43+
static BT::PortsList providedPorts()
44+
{
45+
return BT::PortsList(
46+
{BT::OutputPort<std::string>("guest_name"),
47+
BT::OutputPort<std::string>("guest_drink"),
48+
BT::OutputPort<std::string>("guest_description"),
49+
});
50+
}
51+
52+
private:
53+
std::shared_ptr<rclcpp_cascade_lifecycle::CascadeLifecycleNode> node_;
54+
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr kb_publisher_;
55+
56+
std::string guest_name_, guest_drink_, guest_description_, guest_id_;
57+
};
58+
59+
} // namespace dialog
60+
61+
#endif // HRI__GEST_GUEST_INFO_HPP_
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 andGO2OBJECT
13+
// limitations under the License.
14+
15+
#ifndef HRI__STORE_GUEST_INFO_HPP_
16+
#define HRI__STORE_GUEST_INFO_HPP_
17+
18+
#include <chrono>
19+
#include <functional>
20+
21+
#include "behaviortree_cpp_v3/behavior_tree.h"
22+
#include "behaviortree_cpp_v3/bt_factory.h"
23+
#include "std_msgs/msg/string.hpp"
24+
#include "kb_msgs/srv/query.hpp"
25+
#include "rclcpp/rclcpp.hpp"
26+
#include "rclcpp_cascade_lifecycle/rclcpp_cascade_lifecycle.hpp"
27+
#include "hri/bt_service_node.hpp"
28+
29+
namespace dialog
30+
{
31+
32+
class StoreGuestInfo : public hri::BtServiceNode<
33+
kb_msgs::srv::Query,
34+
rclcpp_cascade_lifecycle::CascadeLifecycleNode>
35+
{
36+
public:
37+
explicit StoreGuestInfo(const std::string & xml_tag_name, const std::string & srv_name,
38+
const BT::NodeConfiguration & conf);
39+
40+
void on_tick() override;
41+
void on_result() override;
42+
43+
static BT::PortsList providedPorts()
44+
{
45+
return BT::PortsList(
46+
{BT::InputPort<std::string>("guest_name"),
47+
BT::InputPort<std::string>("guest_drink"),
48+
BT::InputPort<std::string>("guest_description"),
49+
});
50+
}
51+
52+
private:
53+
std::shared_ptr<rclcpp_cascade_lifecycle::CascadeLifecycleNode> node_;
54+
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr kb_publisher_;
55+
56+
std::string guest_name_, guest_drink_, guest_description_;
57+
};
58+
59+
} // namespace dialog
60+
61+
#endif // HRI__STORE_GUEST_INFO_HPP_

bt_nodes/hri/package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<depend>shape_msgs</depend>
2020
<depend>std_msgs</depend>
2121
<depend>gpsr_msgs</depend>
22+
<depend>perception_system_interfaces</depend>
23+
<depend>kb_msgs</depend>
2224

2325
<test_depend>ament_lint_auto</test_depend>
2426
<test_depend>ament_lint_common</test_depend>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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_guest_info.hpp"
16+
#include <regex>
17+
18+
using std::placeholders::_1;
19+
using namespace std::chrono_literals;
20+
21+
22+
namespace dialog
23+
{
24+
25+
GetGuestInfo::GetGuestInfo(
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+
this->kb_publisher_= node_->create_publisher<std_msgs::msg::String>("/kb/remove_fact", 10);
34+
}
35+
36+
void GetGuestInfo::on_tick()
37+
{
38+
RCLCPP_DEBUG(node_->get_logger(), "[GetGuestInfo] ticked");
39+
rclcpp::spin_some(node_->get_node_base_interface());
40+
41+
// Patterns
42+
request_->patterns.push_back("robot1 oro:attends ?guest");
43+
request_->patterns.push_back("?guest oro:hasName ?name");
44+
request_->patterns.push_back("?guest oro:hasFavoriteDrink ?drink");
45+
request_->patterns.push_back("?guest oro:description ?desc");
46+
47+
// Vars
48+
request_->vars.push_back("?guest");
49+
request_->vars.push_back("?name");
50+
request_->vars.push_back("?drink");
51+
request_->vars.push_back("?desc");
52+
53+
54+
setStatus(BT::NodeStatus::SUCCESS);
55+
}
56+
57+
void GetGuestInfo::on_result()
58+
{
59+
RCLCPP_DEBUG(node_->get_logger(), "[GetGuestInfo] result received");
60+
61+
if (!result_.error_msg.empty()) {
62+
RCLCPP_ERROR(node_->get_logger(), "[GetGuestInfo] error");
63+
setStatus(BT::NodeStatus::FAILURE);
64+
}
65+
66+
std::regex guest_regex("\"guest\"\\s*:\\s*\"([^\"]*)\"");
67+
std::regex name_regex("\"name\"\\s*:\\s*\"([^\"]*)\"");
68+
std::regex drink_regex("\"drink\"\\s*:\\s*\"([^\"]*)\"");
69+
std::regex desc_regex("\"desc\"\\s*:\\s*\"([^\"]*)\"");
70+
71+
std::smatch match;
72+
73+
if (std::regex_search(result_.json, match, guest_regex)) {
74+
guest_id_ = match[1];
75+
}
76+
77+
if (std::regex_search(result_.json, match, name_regex)) {
78+
guest_name_ = match[1];
79+
}
80+
81+
if (std::regex_search(result_.json, match, drink_regex)) {
82+
guest_drink_ = match[1];
83+
}
84+
85+
if (std::regex_search(result_.json, match, desc_regex)) {
86+
guest_description_ = match[1];
87+
}
88+
89+
setOutput("guest_name", guest_name_);
90+
setOutput("guest_drink", guest_drink_);
91+
setOutput("guest_description", guest_description_);
92+
93+
RCLCPP_INFO(
94+
node_->get_logger(), "[GetGuestInfo] Guest info retrieved: Name: %s, Drink: %s, Description: %s",
95+
guest_name_.c_str(), guest_drink_.c_str(), guest_description_.c_str());
96+
97+
std_msgs::msg::String fact_msg;
98+
99+
// Delete attending fact
100+
fact_msg.data = "robot1 oro:attends " + guest_id_;
101+
kb_publisher_->publish(fact_msg);
102+
103+
setStatus(BT::NodeStatus::SUCCESS);
104+
}
105+
106+
107+
108+
109+
110+
} // namespace hri
111+
112+
#include "behaviortree_cpp_v3/bt_factory.h"
113+
BT_REGISTER_NODES(factory)
114+
{
115+
BT::NodeBuilder builder = [](const std::string & name, const BT::NodeConfiguration & config) {
116+
return std::make_unique<dialog::GetGuestInfo>(
117+
name, "/kb/query", config);
118+
};
119+
120+
factory.registerBuilder<dialog::GetGuestInfo>("GetGuestInfo", builder);
121+
}
122+

0 commit comments

Comments
 (0)