Sarafun Behavior Trees package  1
Behavior trees for the SARAFun project
 All Data Structures Namespaces Functions Variables Enumerations Enumerator
sarafun_bt_demo.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 <std_srvs/Empty.h>
9 
10 using namespace sarafun;
11 using namespace BT;
12 
13 TreeRunner *runner;
14 std::string filename;
15 
16 /*
17  Starts behavior tree execution
18 */
19 bool startTreeCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &ans)
20 {
21  runner->startTree(filename);
22 }
23 
24 /*
25  Stops behavior tree execution
26 */
27 bool stopTreeCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &ans)
28 {
29  runner->stopTree();
30 }
31 
32 /*
33  Restarts behavior tree execution
34 */
35 bool restartTreeCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &ans)
36 {
37  runner->stopTree();
38  runner->startTree(filename);
39 }
40 
41 int main(int argc, char **argv) {
42  ros::init(argc, argv, "sarafun_bt_demo");
43  ros::NodeHandle n;
44  int TickPeriod_milliseconds = 0;
45 
46  if (ros::param::has("/sarafun/bt/file")) {
47  ros::param::get("/sarafun/bt/file", filename);
48  } else {
49  filename = std::string("example.json");
50  }
51 
52  if (ros::param::has("/sarafun/bt/tick_period")){
53  ros::param::get("/sarafun/bt/tick_period", TickPeriod_milliseconds);
54  } else {
55  TickPeriod_milliseconds = 1000;
56  }
57 
58  std::string path =
59  ros::package::getPath("sarafun_tree") + "/data/" + filename;
60 
61  ros::ServiceServer bt_start_service = n.advertiseService("/sarafun/start_tree", startTreeCallback);
62  ros::ServiceServer bt_stop_service = n.advertiseService("/sarafun/stop_tree", stopTreeCallback);
63  ros::ServiceServer bt_restart_service = n.advertiseService("/sarafun/restart_tree", restartTreeCallback);
64 
65  runner = new TreeRunner(TickPeriod_milliseconds);
66  ros::spin();
67 
68  return 0;
69 }
bool startTree(std::string tree_description_path)
Definition: TreeRunner.cpp:15