1414#include  < behaviortree_ros2/tree_execution_server.hpp> 
1515#include  < behaviortree_cpp/loggers/bt_cout_logger.h> 
1616
17+ #include  < std_msgs/msg/float32.hpp> 
18+ 
1719//  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+ 
1925class  MyActionServer  : public  BT ::TreeExecutionServer
2026{
2127public: 
2228  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" 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+   }
2442
2543  void  onTreeCreated (BT::Tree& tree) override 
2644  {
@@ -36,6 +54,7 @@ class MyActionServer : public BT::TreeExecutionServer
3654
3755private: 
3856  std::shared_ptr<BT::StdCoutLogger> logger_cout_;
57+   rclcpp::Subscription<std_msgs::msg::Float32>::SharedPtr sub_;
3958};
4059
4160int  main (int  argc, char * argv[])
0 commit comments