14
14
#include < behaviortree_ros2/tree_execution_server.hpp>
15
15
#include < behaviortree_cpp/loggers/bt_cout_logger.h>
16
16
17
+ #include < std_msgs/msg/float32.hpp>
18
+
17
19
// Example that shows how to customize TreeExecutionServer.
18
- // Here, we simply add an extra logger
20
+ //
21
+ // Here, we:
22
+ // - add an extra logger, BT::StdCoutLogger
23
+ // - add a subscriber that will continuously update a variable in the global blackboard
24
+
19
25
class MyActionServer : public BT ::TreeExecutionServer
20
26
{
21
27
public:
22
28
MyActionServer (const rclcpp::NodeOptions& options) : TreeExecutionServer(options)
23
- {}
29
+ {
30
+ // here we assume that the battery voltage is published as a std_msgs::msg::Float32
31
+ auto node = std::dynamic_pointer_cast<rclcpp::Node>(nodeBaseInterface ());
32
+ sub_ = node->create_subscription <std_msgs::msg::Float32 >(
33
+ " battery_level" , 10 , [this ](const std_msgs::msg::Float32 ::SharedPtr msg) {
34
+ // Update the global blackboard
35
+ globalBlackboard ()->set (" battery_level" , msg->data );
36
+ });
37
+ // Note that the callback above and the execution of the tree accessing the
38
+ // global blackboard happen in two different threads.
39
+ // The former runs in the MultiThreadedExecutor, while the latter in the thread created
40
+ // by TreeExecutionServer. But this is OK because the blackboard is thread-safe.
41
+ }
24
42
25
43
void onTreeCreated (BT::Tree& tree) override
26
44
{
@@ -36,6 +54,7 @@ class MyActionServer : public BT::TreeExecutionServer
36
54
37
55
private:
38
56
std::shared_ptr<BT::StdCoutLogger> logger_cout_;
57
+ rclcpp::Subscription<std_msgs::msg::Float32 >::SharedPtr sub_;
39
58
};
40
59
41
60
int main (int argc, char * argv[])
0 commit comments