Sarafun Behavior Trees package  1
Behavior trees for the SARAFun project
 All Data Structures Namespaces Functions Variables Enumerations Enumerator
sarafun_bt.cpp
1 #include <ros/ros.h>
2 #include <sarafun_tree/demo_bt_nodes/ApproachObjectsAction.h>
3 #include <sarafun_tree/demo_bt_nodes/GrabObjectAction.h>
4 #include <sarafun_tree/demo_bt_nodes/FoldingAssemblyAction.h>
5 #include <sarafun_tree/demo_bt_nodes/InsertionWithDeformationAction.h>
6 #include <sarafun_tree/demo_bt_nodes/PlaceAction.h>
7 #include <sarafun_tree/TreeRunner.h>
8 #include <sarafun_tree/LoadTree.h>
9 #include <std_srvs/Empty.h>
10 
11 using namespace sarafun;
12 using namespace BT;
13 
14 TreeRunner *runner = 0;
15 bool running = false;
16 int TickPeriod_milliseconds = 0;
17 
18 /*
19  Initializes the tree runner
20 */
21 bool initializeTree(std::string tree_description_path)
22 {
23  if (!running)
24  {
25  if (runner == 0)
26  {
27  runner = new TreeRunner(TickPeriod_milliseconds);
28  }
29 
30  if(runner->startTree(tree_description_path))
31  {
32  running = true;
33  ROS_INFO("Tree started successfully!");
34  return true;
35  }
36  else
37  {
38  running = false;
39  ROS_ERROR("Tree failed to start!");
40  return false;
41  }
42  }
43 
44  ROS_ERROR("Tree already initialized");
45  return false;
46 }
47 
48 /*
49  Stops and unloads the tree runner
50 */
51 bool unloadTree()
52 {
53  if (running)
54  {
55  runner->stopTree();
56  running = false;
57  return true;
58  }
59 
60  return false;
61 }
62 
63 /*
64  Starts behavior tree execution
65 */
66 bool startTreeCallback(sarafun_tree::LoadTree::Request &req, sarafun_tree::LoadTree::Response &ans)
67 {
68  ans.success = initializeTree(req.file_path);
69  return true;
70 }
71 
72 /*
73  Stops behavior tree execution
74 */
75 bool stopTreeCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &ans)
76 {
77  if (!unloadTree())
78  {
79  ROS_ERROR("Called the service to stop tree without having started it");
80  return false;
81  }
82 
83  ROS_INFO("Tree stopped successfully");
84  return true;
85 }
86 
87 /*
88  Restarts behavior tree execution
89 */
90 bool restartTreeCallback(sarafun_tree::LoadTree::Request &req, sarafun_tree::LoadTree::Response &ans)
91 {
92  if (!unloadTree())
93  {
94  ROS_ERROR("Called the service to stop tree without having started it");
95  ans.success = false;
96  return false;
97  }
98  else
99  {
100  ans.success = initializeTree(req.file_path);
101  return true;
102  }
103 }
104 
105 int main(int argc, char **argv) {
106  ros::init(argc, argv, "sarafun_bt_demo");
107  ros::NodeHandle n;
108 
109  if (ros::param::has("/sarafun/bt/tick_period")){
110  ros::param::get("/sarafun/bt/tick_period", TickPeriod_milliseconds);
111  } else {
112  TickPeriod_milliseconds = 1000;
113  }
114 
115  ros::ServiceServer bt_start_service = n.advertiseService("/sarafun/start_tree", startTreeCallback);
116  ros::ServiceServer bt_stop_service = n.advertiseService("/sarafun/stop_tree", stopTreeCallback);
117  ros::ServiceServer bt_restart_service = n.advertiseService("/sarafun/restart_tree", restartTreeCallback);
118 
119  ROS_INFO("Started the sarafun tree node!");
120  ros::spin();
121  return 0;
122 }
bool startTree(std::string tree_description_path)
Definition: TreeRunner.cpp:15